diff options
Diffstat (limited to 'core/src/UI/UI_Templates.cpp')
-rw-r--r-- | core/src/UI/UI_Templates.cpp | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/core/src/UI/UI_Templates.cpp b/core/src/UI/UI_Templates.cpp index 309ff21..ebf5f62 100644 --- a/core/src/UI/UI_Templates.cpp +++ b/core/src/UI/UI_Templates.cpp @@ -22,11 +22,12 @@ namespace { class TemplateUI { public: - static std::unique_ptr<TemplateUI> CreateByKind(Template::Kind kind, std::unique_ptr<Template> tmpl); + static std::unique_ptr<TemplateUI> CreateByKind(std::unique_ptr<Template> tmpl); static std::unique_ptr<TemplateUI> CreateByKind(Template::Kind kind); virtual ~TemplateUI() = default; virtual void Display() = 0; + virtual void Close() = 0; }; // Table template styles @@ -133,7 +134,7 @@ public: Resize(6, 5); } - virtual ~TableTemplateUI() override + ~TableTemplateUI() override { // We can't move this to be a destructor of the union // because that way it would run after the destruction of mTable @@ -163,7 +164,7 @@ public: } } - virtual void Display() override + void Display() override { ImGui::Columns(2); if (mFirstDraw) { @@ -182,6 +183,11 @@ public: ImGui::Columns(1); } + void Close() override + { + // TODO + } + void Resize(int width, int height) { mTable->Resize(width, height); @@ -896,16 +902,18 @@ private: } }; -std::unique_ptr<TemplateUI> TemplateUI::CreateByKind(Template::Kind kind, std::unique_ptr<Template> tmpl) +template <class TTarget> +static auto CastTemplateAs(std::unique_ptr<Template>& input) requires std::is_base_of_v<Template, TTarget> { -#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)); + return std::unique_ptr<TTarget>(static_cast<TTarget*>(input.release())); +} + +std::unique_ptr<TemplateUI> TemplateUI::CreateByKind(std::unique_ptr<Template> tmpl) +{ + switch (tmpl->GetKind()) { + case Template::KD_Table: return std::make_unique<TableTemplateUI>(CastTemplateAs<TableTemplate>(tmpl)); case Template::InvalidKind: break; } -#pragma pop_macro("UNIQUE_CAST") return nullptr; } @@ -929,6 +937,7 @@ void UI::TemplatesTab() // Toolbar item: close if (ImGui::Button(ICON_FA_TIMES " " I18N_TEXT("Close", L10N_CLOSE), openTemplate == nullptr)) { + openTemplate->Close(); openTemplate = nullptr; } @@ -940,8 +949,9 @@ void UI::TemplatesTab() if (ImGui::BeginPopupModal(I18N_TEXT("Open asset", L10N_ASSET_OPEN_DIALOG_TITLE), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) { if (ImGui::Button(ICON_FA_FOLDER_OPEN " " I18N_TEXT("Open", L10N_OPEN), state.SelectedAsset == nullptr)) { ImGui::CloseCurrentPopup(); - auto kind = static_cast<Template::Kind>(state.SelectedAsset->Payload); - openTemplate = TemplateUI::CreateByKind(kind); + + auto tmpl = project.Templates.Load(*state.SelectedAsset); + openTemplate = TemplateUI::CreateByKind(std::move(tmpl)); } ImGui::SameLine(); project.Templates.DisplayControls(state); |