aboutsummaryrefslogtreecommitdiff
path: root/core/src/UI/UI_Templates.cpp
blob: 7d0251a99a7b084f13ffb8b06fd1ac54c8528760 (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
#include "UI.hpp"

#include "Model/GlobalStates.hpp"
#include "Model/Project.hpp"
#include "Model/Template/TableTemplate.hpp"
#include "Model/Template/Template.hpp"
#include "Utils/I18n.hpp"

#include <IconsFontAwesome.h>
#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;

	Vec2i mSelectionTL;
	Vec2i mSelectionBR;

	bool mDirty = false;
	bool mFirstDraw = true;

public:
	TableTemplateUI(std::unique_ptr<TableTemplate> table)
		: mTable{ std::move(table) }
	{
		// TODO debug code
		mTable->Resize(6, 2);
	}

	virtual void Draw() override
	{
		ImGui::Columns(2);
		if (mFirstDraw) {
			mFirstDraw = false;
			ImGui::SetColumnWidth(0, ImGui::GetWindowWidth() * 0.15f);
		}

		DrawInspector();
		ImGui::NextColumn();

		DrawTable();
		ImGui::NextColumn();

		ImGui::Columns(1);
	}

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

	void DrawCellInspector()
	{
		if (IsSelected()) {
			if (mSelectionTL == mSelectionBR) {
				auto& selectCell = mTable->GetCell(mSelectionTL);
				// TODO
			} else {
				// TODO
			}
		} else {
			ImGui::Text("Select a cell to edit");
		}
	}

	void DrawTableInspector()
	{
		// TODO
	}

	void DrawTable()
	{
		constexpr int kCellSpacing = 20;

		ImGui::BeginChild("TableTemplate");

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

				int i = rowIdx * colCount + colIdx;
				ImGuiID id = ImGui::GetCurrentWindow()->GetID(i);

				auto p = ImGui::GetCursorPos();
				ImGui::GetWindowDrawList()->AddRectFilled(
					ImVec2(p.x + x, p.y + y),
					ImVec2(p.x + x + colWidth, p.y + y + rowHeight),
					RgbaColor(170, 204, 244).AsImU32());

				ImRect rect{
					ImVec2(x, y),
					ImVec2(colWidth, rowHeight),
				};
				if (ImGui::ButtonBehavior(rect, id, nullptr, nullptr)) {
					if (ImGui::GetIO().KeyShift && IsSelected()) {
						// Select range
						mSelectionBR = { colIdx, rowIdx };
					} else {
						// Select a single cell
						SelectCell({ colIdx, rowIdx });
					}
				}

				x += colWidth;
				x += kCellSpacing;
			}

			y += rowHeight;
			y += kCellSpacing;
		}

		ImGui::EndChild();
	}

	bool IsSelected() const
	{
		return mSelectionTL.x != -1;
	}

	void ClearSelection()
	{
		mSelectionTL = { -1, -1 };
		mSelectionBR = { -1, -1 };
	}

	void SelectCell(Vec2i cell)
	{
		mSelectionTL = cell;
		mSelectionBR = cell;
	}
};

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& project = *GlobalStates::GetInstance().GetCurrentProject();

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

	// Toolbar item: close
	if (ImGui::Button(ICON_FA_TIMES " " I18N_TEXT("Close", L10N_CLOSE), openTemplate == nullptr)) {
		openTemplate = nullptr;
	}

	// Toolbar item: open...
	ImGui::SameLine();
	if (ImGui::Button(I18N_TEXT("Open asset...", L10N_ASSET_OPEN))) {
		ImGui::OpenPopup(I18N_TEXT("Open asset", L10N_ASSET_OPEN_DIALOG_TITLE));
	}
	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);
		}
		ImGui::SameLine();
		project.Templates.DisplayControls(state);
		project.Templates.DisplayDetailsList(state);

		ImGui::EndPopup();
	}

	// Toolbar item: manage...
	ImGui::SameLine();
	if (ImGui::Button(I18N_TEXT("Manage assets...", L10N_ASSET_MANAGE))) {
		ImGui::OpenPopup(I18N_TEXT("Manage assets", L10N_ASSET_MANAGE_DIALOG_TITLE));
	}
	if (ImGui::BeginPopupModal(I18N_TEXT("Manage assets", L10N_ASSET_MANAGE_DIALOG_TITLE), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		project.Templates.DisplayControls(state);
		project.Templates.DisplayDetailsList(state);
		ImGui::EndPopup();
	}

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