diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/UI/UI_Templates.cpp | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/core/src/UI/UI_Templates.cpp b/core/src/UI/UI_Templates.cpp index 628f3f7..082b72a 100644 --- a/core/src/UI/UI_Templates.cpp +++ b/core/src/UI/UI_Templates.cpp @@ -17,6 +17,9 @@ namespace { class TemplateUI { public: + static std::unique_ptr<TemplateUI> CreateByKind(Template::Kind kind, std::unique_ptr<Template> tmpl); + static std::unique_ptr<TemplateUI> CreateByKind(Template::Kind kind); + virtual ~TemplateUI() = default; virtual void Draw() = 0; }; @@ -81,6 +84,26 @@ private: } }; +std::unique_ptr<TemplateUI> TemplateUI::CreateByKind(Template::Kind kind, std::unique_ptr<Template> tmpl) +{ +#pragma push_macro("UNIQUE_CAST") +#undef UNIQUE_CAST +#define UNIQUE_CAST(TargetType, input) std::unique_ptr<TargetType>(static_cast<TargetType*>(input.release())) + switch (kind) { + case Template::KD_Table: return std::make_unique<TableTemplateUI>(UNIQUE_CAST(TableTemplate, tmpl)); + case Template::InvalidKind: return nullptr; + } +#pragma pop_macro("UNIQUE_CAST") +} + +std::unique_ptr<TemplateUI> TemplateUI::CreateByKind(Template::Kind kind) +{ + switch (kind) { + case Template::KD_Table: return std::make_unique<TableTemplateUI>(std::make_unique<TableTemplate>()); + case Template::InvalidKind: return nullptr; + } +} + struct DrawTemplateList_State { // Internal data @@ -130,7 +153,9 @@ void UI::TemplatesTab() DrawTemplateList(state); if (state.SelectedTemplate) { - // TODO + auto kind = state.SelectedTemplate->Kind; + auto tmpl = state.SelectedTemplate->LoadFromDisk(); + openTemplate = TemplateUI::CreateByKind(kind, std::move(tmpl)); } ImGui::EndPopup(); @@ -207,7 +232,7 @@ void UI::TemplatesTab() if (ImGui::Button(ls->DialogConfirm.Get(), IsInputValid())) { auto& project = *uis.CurrentProject; - auto& info = *project.InsertTemplate( + project.InsertTemplate( newName, TemplateInfo{ .Path = project.GetTemplatePath(newName), @@ -215,18 +240,7 @@ void UI::TemplatesTab() .Kind = newKind, }); - auto tmpl = Template::CreateByKind(newKind); - - std::ofstream ofs(info.Path); - if (ofs) { - tmpl->WriteTo(ofs); - } else { - // Writing to disk here isn't necessary for downstream operations to function successfully; - // if the user makes any changes and don't save, those changes will not persist anyways - // if the user doesn't make any changes, it doesn't matter that the empty template file isn't created yet - } - - openTemplate = std::make_unique<TableTemplateUI>(std::move(tmpl)); + openTemplate = TemplateUI::CreateByKind(newKind); } ImGui::SameLine(); if (ImGui::Button(ls->DialogCancel.Get())) { |