diff options
Diffstat (limited to 'core/src/UI/UI_MainWindow.cpp')
-rw-r--r-- | core/src/UI/UI_MainWindow.cpp | 102 |
1 files changed, 62 insertions, 40 deletions
diff --git a/core/src/UI/UI_MainWindow.cpp b/core/src/UI/UI_MainWindow.cpp index 15c28ff..661c535 100644 --- a/core/src/UI/UI_MainWindow.cpp +++ b/core/src/UI/UI_MainWindow.cpp @@ -15,10 +15,16 @@ namespace fs = std::filesystem; namespace { -void LoadProjectAt(const std::filesystem::path& path) { +bool LoadProjectAt(const std::filesystem::path& path) { auto& uis = UIState::GetInstance(); - auto project = Project::Load(path); - uis.SetCurrentProject(std::make_unique<Project>(std::move(project))); + try { + auto project = Project::Load(path); + uis.SetCurrentProject(std::make_unique<Project>(std::move(project))); + + return true; + } catch (const std::exception& e) { + return false; + } } void ProjectTab_Normal() { @@ -44,6 +50,8 @@ void ProjectTab_NoProject() { auto& gs = GlobalStates::GetInstance(); auto& uis = UIState::GetInstance(); + bool openedDummy = true; + bool openErrorDialog = false; static std::string projectName; static std::string dirName; static fs::path dirPath; @@ -61,13 +69,11 @@ void ProjectTab_NoProject() { 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)) { + if (ImGui::BeginPopupModal(ls->NewProjectDialogTitle.Get(), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::InputTextWithHint("##ProjectName", ls->NewProjectNameHint.Get(), &projectName); if (ImGui::InputTextWithHint("##ProjectPath", ls->NewProjectPathHint.Get(), &dirName)) { @@ -83,25 +89,15 @@ void ProjectTab_NoProject() { } if (projectName.empty()) { - ImGui::ErrorIcon(); - - ImGui::SameLine(); - ImGui::Text("%s", ls->NewProjectEmptyNameError.Get()); + ImGui::ErrorMessage("%s", ls->NewProjectEmptyNameError.Get()); } - if (!dirNameIsValid) { - ImGui::ErrorIcon(); - - ImGui::SameLine(); - ImGui::Text("%s", ls->NewProjectInvalidPathError.Get()); + ImGui::ErrorMessage("%s", ls->NewProjectInvalidPathError.Get()); } ImGui::Spacing(); - bool formValid = dirNameIsValid && !projectName.empty(); - - if (!formValid) ImGui::PushDisabled(); - if (ImGui::Button(ls->DialogConfirm.Get())) { + if (ImGui::Button(ls->DialogConfirm.Get(), !dirNameIsValid || projectName.empty())) { ImGui::CloseCurrentPopup(); auto project = Project::Create(std::move(projectName), dirPath); @@ -113,7 +109,6 @@ void ProjectTab_NoProject() { dirPath = fs::path{}; dirNameIsValid = false; } - if (!formValid) ImGui::PopDisabled(); ImGui::SameLine(); if (ImGui::Button(ls->DialogCancel.Get())) { @@ -123,45 +118,72 @@ void ProjectTab_NoProject() { ImGui::EndPopup(); } + ImGui::SameLine(); 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); + openErrorDialog = !LoadProjectAt(path); } } + // TODO cleanup UI + // Recent projects + 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()) { + auto& rp = gs.GetRecentProjects(); + // End of vector is the most recently used, so that appending has less overhead + size_t toRemoveIdx = rp.size(); + + if (rp.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()); + } else { + for (auto it = rp.rbegin(); it != rp.rend(); ++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()); - } + size_t idx = std::distance(it, rp.rend()) - 1; + ImGui::PushID(idx); - ImGui::SameLine(); - if (ImGui::Button(ICON_FA_TRASH)) { - gs.RemoveRecentProject(std::distance(recentProjects.begin(), it)); - } - if (ImGui::IsItemHovered()) { - ImGui::SetTooltip("%s", ls->DeleteRecentProjectTooltip.Get()); + ImGui::SameLine(); + if (ImGui::Button(ICON_FA_FOLDER_OPEN)) { + openErrorDialog = !LoadProjectAt(path / "cplt_project.json"); + } + if (ImGui::IsItemHovered()) { + ImGui::SetTooltip("%s", ls->OpenRecentProjectTooltip.Get()); + } + + ImGui::SameLine(); + if (ImGui::Button(ICON_FA_TRASH)) { + toRemoveIdx = idx; + } + if (ImGui::IsItemHovered()) { + ImGui::SetTooltip("%s", ls->DeleteRecentProjectTooltip.Get()); + } + + ImGui::PopID(); } } + + if (toRemoveIdx != rp.size()) { + gs.RemoveRecentProject(toRemoveIdx); + } + + if (openErrorDialog) { + ImGui::SetNextWindowCentered(); + ImGui::OpenPopup(ls->Error.Get()); + } + if (ImGui::BeginPopupModal(ls->Error.Get(), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::ErrorMessage("%s", ls->InvalidProjectFormat.Get()); + ImGui::EndPopup(); + } } } // namespace |