Cogs.Core
LoadRvmCommand.cpp
1#include "imgui.h"
2
3#include "LoadRvmCommand.h"
4#include "Editor.h"
5#include "Context.h"
6#include "EditorState.h"
7#include "Resources/ModelManager.h"
8#include "Components/Core/ModelComponent.h"
9#include "Commands/EditorCommand.h"
10#include "Foundation/Platform/IO.h"
11#include "Foundation/Logging/Logger.h"
12
13namespace
14{
15 const Cogs::Logging::Log logger = Cogs::Logging::getLogger("LoadRvmCommand");
16}
17
18Cogs::Core::LoadRvmCommand::LoadRvmCommand(EditorState* state, const StringView& path, bool openInRoot, bool synchronous) :
19 ModalEditorCommand(state), pathStr(path), openInRoot(openInRoot), synchronous(synchronous)
20{
21 fileName = Cogs::IO::fileName(pathStr);
22
23 // Initialize rvmparser options, existing variables take precedence over stated defaulted values
24 createOption<bool>(rvmMdb2, "rvm.mdb2", true);
25 createOption<bool>(rvmMdb2GuessHierarchy, "rvm.mdb2.guessHierarchy", true);
26 createOption<bool>(rvmInheritAttributes, "rvm.inheritAttributes", false);
27 createOption<float>(rvmTessellateTolerance, "rvm.tessellate.tolerance", 0.01f);
28 createOption<float>(rvmTessellateFeatureAngle, "rvm.tessellate.featureAngle", 0.7f);
29 createOption<float>(rvmTessellateCullDiameter, "rvm.tessellate.cullDiameter", -1.f);
30 createOption<bool>(rvmParseColorize, "rvm.parse.colorize", true);
31 createOption<bool>(rvmParseIgnoreAttributeIndentation, "rvm.parse.ignoreAttributeIndentation", true);
32 createOption<bool>(rvmParseIgnoreAttributeHierarchy, "rvm.parse.ignoreAttributeHierarchy", false);
33 createOption<bool>(rvmParseAllowReparent, "rvm.parse.allowReparent", true);
34 createOption<bool>(rvmVueExperimental, "rvm.vue.experimental", false);
35}
36
38{
39 modal = true;
40}
41
43{
44 assert(modal);
45
46 ImGui::OpenPopup("Rvmparser file options");
47 if (ImGui::BeginPopupModal("Rvmparser file options", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
48
49 ImGui::Text("File: %s", fileName.c_str());
50 ImGui::Separator();
51 ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.4f);
52
53 ImGui::Checkbox(rvmMdb2.key.data(), &rvmMdb2.value);
54 ImGui::Checkbox(rvmMdb2GuessHierarchy.key.data(), &rvmMdb2GuessHierarchy.value);
55 ImGui::Checkbox(rvmInheritAttributes.key.data(), &rvmInheritAttributes.value);
56
57 ImGui::InputFloat(rvmTessellateTolerance.key.data(), &rvmTessellateTolerance.value);
58 ImGui::InputFloat(rvmTessellateFeatureAngle.key.data(), &rvmTessellateFeatureAngle.value);
59 ImGui::InputFloat(rvmTessellateCullDiameter.key.data(), &rvmTessellateCullDiameter.value);
60
61 ImGui::Checkbox(rvmParseColorize.key.data(), &rvmParseColorize.value);
62 ImGui::Checkbox(rvmParseIgnoreAttributeIndentation.key.data(), &rvmParseIgnoreAttributeIndentation.value);
63 ImGui::Checkbox(rvmParseIgnoreAttributeHierarchy.key.data(), &rvmParseIgnoreAttributeHierarchy.value);
64 ImGui::Checkbox(rvmParseAllowReparent.key.data(), &rvmParseAllowReparent.value);
65
66 ImGui::Checkbox(rvmVueExperimental.key.data(), &rvmVueExperimental.value);
67
68 ImGui::Separator();
69 ImGui::PopItemWidth();
70
71 ImGui::SetItemDefaultFocus();
72 if (ImGui::Button("OK", ImVec2(120, 0))) {
73 loadFile();
74 modal = false;
75 ImGui::CloseCurrentPopup();
76 }
77 ImGui::SameLine();
78 if (ImGui::Button("Cancel", ImVec2(120, 0))) {
79 modal = false;
80 ImGui::CloseCurrentPopup();
81 }
82 ImGui::EndPopup();
83 }
84}
85
87{
88 return modal;
89}
90
91void Cogs::Core::LoadRvmCommand::loadFile()
92{
93 std::string name = Cogs::IO::stem(pathStr);
94 std::unique_ptr<CreateEntityCommand> newCommand = std::make_unique<CreateEntityCommand>(state, "ModelEntity", name, openInRoot);
95 EditorCommand* createCommand = state->editor->doApply(std::move(newCommand));
96
97 createCommand->permanentUndo = true;
98 Cogs::ComponentModel::Entity* selected = state->getSelected();
99 if (!selected) {
100 LOG_ERROR(logger, "Could not create Entity ModelEntity");
101 return;
102 }
103
104 ModelComponent* modelComponent = selected->getComponent<ModelComponent>();
105
106 ModelLoadInfo* loadInfo = context->modelManager->createLoadInfo();
107
108 loadInfo->resourceId = NoResourceId;
109 loadInfo->resourcePath = pathStr;
111 loadInfo->loadFlags = static_cast<ResourceLoadFlags>(flags);
112 loadInfo->modelFlags = flags;
113 if (context->variables->get("resources.models.autoReload", false)) {
114 loadInfo->loadFlags |= ResourceLoadFlags::AutoReload;
115 }
116
117 loadInfo->properties = std::make_unique<PropertyStore>();
118 for (RvmparserOptionBase* option : allOptions) {
119 option->addPropertyTo(loadInfo->properties.get());
120 }
121
122 // Ownership of loadInfo will be passed to modelManager
123 modelComponent->model = context->modelManager->loadResource(loadInfo);
124 modelComponent->waitForSubresources = false;
125 modelComponent->setChanged();
126}
127
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
Definition: Entity.h:35
Log implementation class.
Definition: LogManager.h:139
ModelLoadFlags
Model loading flags. May be combined with resource loading flags.
@ ForceSynchronous
Force loading the resource synchronously.
ResourceLoadFlags
Flags for describing how to load a resource.
Definition: ResourceFlags.h:16
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Base class for Cogs Editor commands.
Definition: EditorCommand.h:19
bool permanentUndo
True if cannot Redo after Undo.
Definition: EditorCommand.h:64
void showGui() override
Display custom ImGUI.
bool continueModal() override
Shall return true while the GUI should still be shown. False when.
void beginModal() override
Called when the command is initiated.