aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-05-30 23:09:38 -0700
committerrtk0c <[email protected]>2021-05-30 23:09:38 -0700
commit20c5cca8c46d0d2af7addeeec97cb9811a1d4647 (patch)
treeb7ef33bb75be7ecb7805f5381a284cf0fb119dec /core
parentc90f78df080a9891930ee346b0ad87498ba5b697 (diff)
Un-hardcoded use of TableTemplateUI in template management dialogs
Diffstat (limited to 'core')
-rw-r--r--core/src/UI/UI_Templates.cpp42
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())) {