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

#include "Model/GlobalStates.hpp"
#include "Model/Project.hpp"
#include "UI/Localization.hpp"
#include "UI/States.hpp"

#include <IconsFontAwesome.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <imgui_stdlib.h>
#include <filesystem>

namespace fs = std::filesystem;

namespace {
void LoadProjectAt(const std::filesystem::path& path) {
	auto& uis = UIState::GetInstance();
	auto& gs = GlobalStates::GetInstance();

	if (uis.CurrentProject) {
		uis.CloseCurrentProject();
	}
}

void ProjectTab_Normal() {
	// TODO
}

void ProjectTab_NoProject() {
	auto ls = LocaleStrings::Instance.get();
	auto& gs = GlobalStates::GetInstance();
	auto& uis = UIState::GetInstance();

	static std::string projectName;
	static std::string dirName;
	static fs::path dirPath;
	static bool dirNameIsValid = false;
	if (ImGui::Button(ls->NewProject.Get())) {
		auto vs = ImGui::GetMainViewport()->Size; // Viewport Size
		ImGui::SetNextWindowSize({ vs.x * 0.5f, vs.y * 0.5f });
		ImGui::SetNextWindowPos({ vs.x / 2, vs.y / 2 }, ImGuiCond_Always, { 0.5f, 0.5f }); // Center window initially
		ImGui::OpenPopup(ls->TitleNewProject.Get());
	}

	// Make it so that the modal dialog has a close button
	bool newProjectDialogDummyTrue = true;
	if (ImGui::BeginPopupModal(ls->TitleNewProject.Get(), &newProjectDialogDummyTrue)) {
		ImGui::InputTextWithHint("##ProjectName", ls->HintNewProjectName.Get(), &projectName);

		if (ImGui::InputTextWithHint("##ProjectPath", ls->HintNewProjectPath.Get(), &dirName)) {
			// Changed, validate value
			fs::path newPath(dirName);
			if (fs::exists(newPath)) {
				dirNameIsValid = true;
				dirPath = std::move(newPath);
			} else {
				dirNameIsValid = false;
			}
		}
		ImGui::SameLine();
		if (ImGui::Button("...")) {
			// TODO file dialog
		}

		if (projectName.empty()) {
			ImGui::ErrorIcon();

			ImGui::SameLine();
			ImGui::Text(ls->ErrorNewProjectEmptyName.Get());
		}

		if (!dirNameIsValid) {
			ImGui::ErrorIcon();

			ImGui::SameLine();
			ImGui::Text(ls->ErrorNewProjectInvalidPath.Get());
		}

		ImGui::Spacing();

		bool formValid = dirNameIsValid && !projectName.empty();

		if (!formValid) {
			ImGui::PushItemFlag(ImGuiItemFlags_Disabled, false);
			ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.5f * ImGui::GetStyle().Alpha);
		}
		if (ImGui::Button(ls->ActionNewProjectConfirm.Get())) {
			ImGui::CloseCurrentPopup();

			auto project = Project::Create(std::move(projectName), dirPath);
			auto uptr = std::unique_ptr<Project>(new Project(std::move(project)));
			uis.SetCurrentProject(std::move(uptr));

			// Dialog just got closed, reset states
			projectName = "";
			dirName = "";
			dirPath = fs::path{};
			dirNameIsValid = false;
		}
		if (!formValid) {
			ImGui::PopItemFlag();
			ImGui::PopStyleVar();
		}

		ImGui::SameLine();
		if (ImGui::Button(ls->ActionNewProjectCancel.Get())) {
			ImGui::CloseCurrentPopup();
		}

		ImGui::EndPopup();
	}

	if (ImGui::Button(ls->OpenProject.Get())) {
		// TODO
	}

	ImGui::Separator();
	ImGui::Text(ls->RecentProjects.Get());
	ImGui::SameLine();
	if (ImGui::Button(ls->ActionClearRecentProjects.Get())) {
		gs.ClearRecentProjects();
	}

	auto& recentProjects = gs.GetRecentProjects();
	if (recentProjects.empty()) {
		ImGui::Text(ls->MessageNoRecentProjects.Get());
	}
	for (auto it = recentProjects.begin(); it != recentProjects.end(); ++it) {
		auto& [path, recent] = *it;
		ImGui::Text(recent.c_str());

		ImGui::SameLine();
		if (ImGui::Button(ICON_FA_EDIT)) {
			LoadProjectAt(path);
		}
		if (ImGui::IsItemHovered()) {
			ImGui::SetTooltip(ls->TooltipOpenRecentProject.Get());
		}

		ImGui::SameLine();
		if (ImGui::Button(ICON_FA_TRASH)) {
			gs.RemoveRecentProject(std::distance(recentProjects.begin(), it));
		}
		if (ImGui::IsItemHovered()) {
			ImGui::SetTooltip(ls->TooltipDeleteRecentProject.Get());
		}
	}
}
} // namespace

void UI::MainWindow() {
	auto ls = LocaleStrings::Instance.get();
	auto& uis = UIState::GetInstance();

	auto windowSize = ImGui::GetMainViewport()->Size;
	ImGui::SetNextWindowSize({ windowSize.x, windowSize.y });
	ImGui::SetNextWindowPos({ 0, 0 });
	ImGui::Begin("##MainWindow", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
	if (ImGui::BeginTabBar("##MainWindowTabs")) {
		if (ImGui::BeginTabItem(ls->TabSettings.Get())) {
			UI::SettingsTab();
			ImGui::EndTabItem();
		}

		if (ImGui::BeginTabItem(ls->TabProject.Get(), nullptr, ImGuiTabItemFlags_SetSelected)) {
			if (uis.CurrentProject) {
				ProjectTab_Normal();
			} else {
				ProjectTab_NoProject();
			}
			ImGui::EndTabItem();
		}
		if (!uis.CurrentProject) {
			// No project open, simply skip all project specific tabs
			goto endTab;
		}

		if (ImGui::BeginTabItem(ls->TabDatabaseView.Get())) {
			UI::DatabaseViewTab();
			ImGui::EndTabItem();
		}

		if (ImGui::BeginTabItem(ls->TabItems.Get())) {
			UI::ItemsTab();
			ImGui::EndTabItem();
		}

		if (ImGui::BeginTabItem(ls->TabExport.Get())) {
			UI::ExportTab();
			ImGui::EndTabItem();
		}

	endTab:
		ImGui::EndTabBar();
	}
	ImGui::End();
}