aboutsummaryrefslogtreecommitdiff
path: root/core/src/UI/UI_Templates.cpp
blob: 7ebb9e17a5ee622b8e2e8f145975f80bec0ab4cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "UI.hpp"

#include "Model/GlobalStates.hpp"
#include "Model/Project.hpp"
#include "Model/Template/TableTemplate.hpp"
#include "Model/Template/Template.hpp"
#include "UI/Localization.hpp"

#include <imgui.h>
#include <imgui_extra_math.h>
#include <imgui_stdlib.h>
#include <fstream>
#include <iostream>
#include <utility>

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;
};

class TableTemplateUI : public TemplateUI
{
private:
	std::unique_ptr<TableTemplate> mTable;

	TableCell* mSelectedCell = nullptr;

public:
	TableTemplateUI(std::unique_ptr<TableTemplate> table)
		: mTable{ std::move(table) }
	{
	}

	virtual void Draw() override
	{
		ImGui::Columns(2);

		DrawInspector();
		ImGui::NextColumn();

		DrawTable();
		ImGui::NextColumn();

		ImGui::Columns(1);
	}

private:
	void DrawInspector()
	{
		if (ImGui::BeginTabBar("Inspector")) {
			if (ImGui::BeginTabItem("Table")) {
				DrawTableInspector();
				ImGui::EndTabItem();
			}
			if (ImGui::BeginTabItem("Cell")) {
				DrawCellInspector();
				ImGui::EndTabItem();
			}
			ImGui::EndTabBar();
		}
	}

	void DrawTableInspector()
	{
		// TODO
	}

	void DrawCellInspector()
	{
		if (mSelectedCell) {

		} else {
			ImGui::Text("Select a cell to edit");
		}
	}

	void DrawTable()
	{
		constexpr int kCellSpacing = 20;

		int colCount = mTable->GetTableWidth();
		int rowCount = mTable->GetTableHeight();
		float x = 0.0f;
		float y = 0.0f;
		for (int rowIdx = 0; rowIdx < rowCount; ++rowIdx) {
			int rowHeight = mTable->GetRowHeight(rowIdx);

			for (int colIdx = 0; colIdx < colCount; ++colIdx) {
				int colWidth = mTable->GetColumnWidth(colIdx);

				ImRect rect{
					ImVec2(x, y),
					ImVec2(colWidth, rowHeight),
				};

				// TODO

				x += colWidth;
				x += kCellSpacing;
			}

			y += rowHeight;
			y += kCellSpacing;
		}
	}
};

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;
	}
}
} // namespace

void UI::TemplatesTab()
{
	auto ls = LocaleStrings::Instance.get();
	auto& gs = GlobalStates::GetInstance();
	auto& project = *gs.GetCurrentProject();

	bool openedDummy = true;
	static std::unique_ptr<TemplateUI> openTemplate;
	static AssetList::DrawState state;

	// Toolbar item: close
	if (ImGui::Button(ls->Close.Get(), openTemplate == nullptr)) {
		openTemplate = nullptr;
	}

	// Toolbar item: open...
	ImGui::SameLine();
	if (ImGui::Button("Open...")) {
		ImGui::OpenPopup("Open template");
	}
	if (ImGui::BeginPopupModal("Open template", &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		project.Templates.DrawDetails(state);

		if (state.SelectedAsset) {
			auto kind = state.SelectedAsset->Kind;
			auto tmpl = state.SelectedAsset->LoadFromDisk();
			openTemplate = TemplateUI::CreateByKind(kind, std::move(tmpl));
		}

		ImGui::EndPopup();
	}

	// Toolbar item: manage...
	ImGui::SameLine();
	if (ImGui::Button("Manage...")) {
		ImGui::OpenPopup("Manage templates");
	}
	if (ImGui::BeginPopupModal("Manage templates", &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		project.Templates.DrawDetails(state);

		enum class NameSelectionError
		{
			None,
			Duplicated,
			Empty,
		};

		static std::string newName;
		static NameSelectionError newNameError;
		auto ValidateNewName = [&]() -> void {
			if (newName.empty()) {
				newNameError = NameSelectionError::Empty;
			}

			auto& templates = gs.GetCurrentProject()->GetTemplates();
			if (templates.find(newName) != templates.end()) {
				newNameError = NameSelectionError::Duplicated;
			}
		};
		auto ShowNewNameErrors = [&]() -> void {
			switch (newNameError) {
				case NameSelectionError::None: break;
				case NameSelectionError::Duplicated:
					ImGui::ErrorMessage("Duplicate template name");
					break;
				case NameSelectionError::Empty:
					ImGui::ErrorMessage("Template name cannot be empty");
					break;
			}
		};

		static Template::Kind newKind = Template::InvalidKind;
		auto ShowNewKindErrors = [&]() -> void {
			if (newKind == Template::InvalidKind) {
				ImGui::ErrorMessage("Must select a valid template type");
			}
		};

		auto IsInputValid = [&]() -> bool {
			return newNameError == NameSelectionError::None && newKind != Template::InvalidKind;
		};

		if (ImGui::Button(ls->Add.Get())) {
			ImGui::OpenPopup("Create template");
		}
		if (ImGui::BeginPopupModal("Create template")) {
			if (ImGui::InputText("Name", &newName)) {
				ValidateNewName();
			}

			if (ImGui::BeginCombo("Type", Template::FormatKind(newKind))) {
				for (int i = 0; i < Template::KindCount; ++i) {
					auto kind = static_cast<Template::Kind>(i);
					if (ImGui::Selectable(Template::FormatKind(kind), newKind == kind)) {
						newKind = kind;
					}
				}
				ImGui::EndCombo();
			}

			if (ImGui::Button(ls->DialogConfirm.Get(), IsInputValid())) {
				project.InsertTemplate(
					newName,
					TemplateInfo{
						.Path = project.GetTemplatePath(newName),
						.Name = newName, // Don't std::move here because evaluation order of `newName` (as parameter of InsertTemplate()) and this is unspecified
						.Kind = newKind,
					});

				openTemplate = TemplateUI::CreateByKind(newKind);
			}
			ImGui::SameLine();
			if (ImGui::Button(ls->DialogCancel.Get())) {
				ImGui::CloseCurrentPopup();
			}

			ShowNewNameErrors();
			ShowNewKindErrors();

			ImGui::EndPopup();
		}

		ImGui::SameLine();
		if (ImGui::Button(ls->Rename.Get(), state.SelectedTemplate == nullptr)) {
			ImGui::OpenPopup("Rename template");
			newName.clear();
		}
		if (ImGui::BeginPopupModal("Rename template")) {
			if (ImGui::InputText("New name", &newName)) {
				ValidateNewName();
			}

			if (ImGui::Button(ls->DialogConfirm.Get(), IsInputValid())) {
				auto& project = *gs.GetCurrentProject();

				project.Templates.Rename(
					state.SelectedTemplate->Name,
					newName);

				// We mutated the map, the pointer may be invalid now
				state.SelectedTemplate = &project.GetTemplates().at(newName);
			}
			ImGui::SameLine();
			if (ImGui::Button(ls->DialogCancel.Get())) {
				ImGui::CloseCurrentPopup();
			}

			ShowNewNameErrors();

			ImGui::EndPopup();
		}

		ImGui::SameLine();
		if (ImGui::Button(ls->Delete.Get(), state.SelectedTemplate == nullptr)) {
			ImGui::OpenPopup("Delete confirmation");
		}
		if (ImGui::BeginPopupModal("Delete confirmation")) {
			assert(state.SelectedTemplate != nullptr);

			if (ImGui::Button(ls->DialogConfirm.Get())) {
				gs.GetCurrentProject()->RemoveTemplate(state.SelectedTemplate->Name);
			}
			ImGui::SameLine();
			if (ImGui::Button(ls->DialogCancel.Get())) {
				ImGui::CloseCurrentPopup();
			}
			ImGui::EndPopup();
		}

		ImGui::EndPopup();
	}

	if (openTemplate) {
		openTemplate->Draw();
	}
}