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
|
#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_stdlib.h>
#include <portable-file-dialogs.h>
#include <filesystem>
#include <memory>
namespace fs = std::filesystem;
namespace {
void LoadProjectAt(const std::filesystem::path& path) {
auto& uis = UIState::GetInstance();
auto project = Project::Load(path);
uis.SetCurrentProject(std::make_unique<Project>(std::move(project)));
}
void ProjectTab_Normal() {
auto ls = LocaleStrings::Instance.get();
auto& gs = GlobalStates::GetInstance();
auto& uis = UIState::GetInstance();
if (ImGui::Button(ls->CloseActiveProject.Get())) {
uis.CloseCurrentProject();
return;
}
ImGui::SameLine();
if (ImGui::Button(ls->OpenActiveProjectInFileSystem.Get())) {
// TODO
}
ImGui::Text("%s%s", ls->ActiveProjectName.Get(), uis.CurrentProject->GetName().c_str());
ImGui::Text("%s%s", ls->ActiveProjectPath.Get(), uis.CurrentProject->GetPathString().c_str());
}
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;
auto TrySelectPath = [&](fs::path newPath) {
if (fs::exists(newPath)) {
dirNameIsValid = true;
dirName = newPath.string();
dirPath = std::move(newPath);
} else {
dirNameIsValid = false;
}
};
if (ImGui::Button(ls->NewProject.Get())) {
ImGui::SetNextWindowCentered();
ImGui::SetNextWindowSizeRelScreen(0.5f, 0.5f);
ImGui::OpenPopup(ls->NewProjectDialogTitle.Get());
}
// Make it so that the modal dialog has a close button
bool newProjectDialogDummyTrue = true;
if (ImGui::BeginPopupModal(ls->NewProjectDialogTitle.Get(), &newProjectDialogDummyTrue)) {
ImGui::InputTextWithHint("##ProjectName", ls->NewProjectNameHint.Get(), &projectName);
if (ImGui::InputTextWithHint("##ProjectPath", ls->NewProjectPathHint.Get(), &dirName)) {
// Changed, validate value
TrySelectPath(fs::path(dirName));
}
ImGui::SameLine();
if (ImGui::Button("...")) {
auto selection = pfd::select_folder(ls->NewProjectPathDialogTitle.Get()).result();
if (!selection.empty()) {
TrySelectPath(fs::path(selection));
}
}
if (projectName.empty()) {
ImGui::ErrorIcon();
ImGui::SameLine();
ImGui::Text("%s", ls->NewProjectEmptyNameError.Get());
}
if (!dirNameIsValid) {
ImGui::ErrorIcon();
ImGui::SameLine();
ImGui::Text("%s", ls->NewProjectInvalidPathError.Get());
}
ImGui::Spacing();
bool formValid = dirNameIsValid && !projectName.empty();
if (!formValid) ImGui::PushDisabled();
if (ImGui::Button(ls->DialogConfirm.Get())) {
ImGui::CloseCurrentPopup();
auto project = Project::Create(std::move(projectName), dirPath);
uis.SetCurrentProject(std::make_unique<Project>(std::move(project)));
// Dialog just got closed, reset states
projectName = "";
dirName = "";
dirPath = fs::path{};
dirNameIsValid = false;
}
if (!formValid) ImGui::PopDisabled();
ImGui::SameLine();
if (ImGui::Button(ls->DialogCancel.Get())) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
if (ImGui::Button(ls->OpenProject.Get())) {
auto selection = pfd::open_file(ls->OpenProjectDialogTitle.Get()).result();
if (!selection.empty()) {
fs::path path(selection[0]);
LoadProjectAt(path);
}
}
ImGui::Separator();
ImGui::Text("%s", ls->RecentProjects.Get());
ImGui::SameLine();
if (ImGui::Button(ls->ClearRecentProjects.Get())) {
gs.ClearRecentProjects();
}
auto& recentProjects = gs.GetRecentProjects();
if (recentProjects.empty()) {
ImGui::Text("%s", ls->NoRecentProjectsMessage.Get());
}
for (auto it = recentProjects.begin(); it != recentProjects.end(); ++it) {
auto& [path, recent] = *it;
ImGui::Text("%s", recent.c_str());
ImGui::SameLine();
if (ImGui::Button(ICON_FA_EDIT)) {
LoadProjectAt(path);
}
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("%s", ls->OpenRecentProjectTooltip.Get());
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_TRASH)) {
gs.RemoveRecentProject(std::distance(recentProjects.begin(), it));
}
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("%s", ls->DeleteRecentProjectTooltip.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)) {
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();
}
|