From 70cc233165b5efa3a3888af34f7afce095fe6947 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 29 Mar 2021 17:55:02 -0700 Subject: More work on project tab --- core/src/UI/UI_MainWindow.cpp | 129 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 123 insertions(+), 6 deletions(-) (limited to 'core/src/UI/UI_MainWindow.cpp') diff --git a/core/src/UI/UI_MainWindow.cpp b/core/src/UI/UI_MainWindow.cpp index 0c8e7b9..9b20550 100644 --- a/core/src/UI/UI_MainWindow.cpp +++ b/core/src/UI/UI_MainWindow.cpp @@ -1,34 +1,151 @@ #include "UI.hpp" +#include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "UI/Localization.hpp" #include "UI/States.hpp" +#include #include +#include +#include +#include + +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())) { - // TODO + 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(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->Recents.Get()); + ImGui::Text(ls->RecentProjects.Get()); ImGui::SameLine(); - if (ImGui::Button(ls->ClearRecents.Get())) { - // TODO + if (ImGui::Button(ls->ActionClearRecentProjects.Get())) { + gs.ClearRecentProjects(); } - // TODO + 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 @@ -46,7 +163,7 @@ void UI::MainWindow() { ImGui::EndTabItem(); } - if (ImGui::BeginTabItem(ls->TabProject.Get())) { + if (ImGui::BeginTabItem(ls->TabProject.Get(), nullptr, ImGuiTabItemFlags_SetSelected)) { if (uis.CurrentProject) { ProjectTab_Normal(); } else { -- cgit v1.2.3-70-g09d2