From 31950890c939862f79c817053c106bf711c63a64 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Tue, 30 Mar 2021 19:40:11 -0700 Subject: Product items and misc stuff --- core/src/UI/Localization.hpp | 22 ++++++-- core/src/UI/States.cpp | 9 +++- core/src/UI/States.hpp | 1 + core/src/UI/UI.hpp | 8 +++ core/src/UI/UI_Items.cpp | 117 +++++++++++++++++++++++++++++++++++++++++- core/src/UI/UI_MainWindow.cpp | 67 ++++++++++++------------ core/src/UI/UI_Utils.cpp | 21 ++++++++ 7 files changed, 208 insertions(+), 37 deletions(-) (limited to 'core/src/UI') diff --git a/core/src/UI/Localization.hpp b/core/src/UI/Localization.hpp index e604165..d5424ea 100644 --- a/core/src/UI/Localization.hpp +++ b/core/src/UI/Localization.hpp @@ -12,6 +12,9 @@ public: static std::unique_ptr Instance; public: + BasicTranslation DialogConfirm{ "Generic.Dialog.Confirm"sv }; + BasicTranslation DialogCancel{ "Generic.Dialog.Cancel"sv }; + BasicTranslation TabSettings{ "MainWindow.Tab.Settings"sv }; BasicTranslation TabProject{ "MainWindow.Tab.Project"sv }; BasicTranslation TabDatabaseView{ "MainWindow.Tab.DatabaseView"sv }; @@ -19,9 +22,7 @@ public: BasicTranslation TabExport{ "MainWindow.Tab.Exports"sv }; BasicTranslation NewProject{ "Project.New"sv }; - BasicTranslation NewProjectTitle{ "Project.New.DialogTitle"sv }; - BasicTranslation ConfirmNewProject{ "Project.New.Confirm"sv }; - BasicTranslation CancelNewProject{ "Project.New.Cancel"sv }; + BasicTranslation NewProjectDialogTitle{ "Project.New.DialogTitle"sv }; BasicTranslation NewProjectNameHint{ "Project.New.Name"sv }; BasicTranslation NewProjectPathHint{ "Project.New.Path"sv }; BasicTranslation NewProjectPathDialogTitle{ "Project.New.Path.DialogTitle"sv }; @@ -36,4 +37,19 @@ public: BasicTranslation NoRecentProjectsMessage{ "Project.Recents.NonePresent"sv }; BasicTranslation OpenRecentProjectTooltip{ "Project.Recents.Open.Tooltip"sv }; BasicTranslation DeleteRecentProjectTooltip{ "Project.Recents.Delete.Tooltip"sv }; + + BasicTranslation CloseActiveProject{ "ActiveProject.Close"sv }; + BasicTranslation OpenActiveProjectInFileSystem{ "ActiveProject.OpenInFilesystem"sv }; + BasicTranslation ActiveProjectName{ "ActiveProject.Info.Name"sv }; + BasicTranslation ActiveProjectPath{ "ActiveProject.Info.Path"sv }; + + BasicTranslation AddItem{ "ItemEditor.Add"sv }; + BasicTranslation AddItemDialogTitle{ "ItemEditor.Add.DialogTitle"sv }; + BasicTranslation DeleteItem{ "ItemEditor.Delete"sv }; + + BasicTranslation ProductCategoryName{ "Item.Product.CategoryName"sv }; + BasicTranslation ProductNameColumn{ "Item.Product.Column.Name"sv }; + BasicTranslation ProductDescriptionColumn{ "Item.Product.Column.Description"sv }; + BasicTranslation FactoryCategoryName{ "Item.Factory.CategoryName"sv }; + BasicTranslation CustomerCategoryName{ "Item.Customer.CategoryName"sv }; }; diff --git a/core/src/UI/States.cpp b/core/src/UI/States.cpp index 07bbcf7..dc7c37a 100644 --- a/core/src/UI/States.cpp +++ b/core/src/UI/States.cpp @@ -11,6 +11,13 @@ void UIState::Init() { uiStateInstance = std::make_unique(); } +void UIState::Shutdown() { + if (uiStateInstance) { + uiStateInstance->CloseCurrentProject(); + uiStateInstance = nullptr; + } +} + UIState& UIState::GetInstance() { return *uiStateInstance; } @@ -22,7 +29,7 @@ void UIState::SetCurrentProject(std::unique_ptr project) { void UIState::CloseCurrentProject() { if (CurrentProject) { - // TODO save stuff + CurrentProject->WriteToDisk(); CurrentProject = nullptr; } } diff --git a/core/src/UI/States.hpp b/core/src/UI/States.hpp index cbb556f..de0510c 100644 --- a/core/src/UI/States.hpp +++ b/core/src/UI/States.hpp @@ -9,6 +9,7 @@ class UIState { public: static void Init(); + static void Shutdown(); static UIState& GetInstance(); public: diff --git a/core/src/UI/UI.hpp b/core/src/UI/UI.hpp index b0c3aaa..52a2ca9 100644 --- a/core/src/UI/UI.hpp +++ b/core/src/UI/UI.hpp @@ -1,7 +1,15 @@ #pragma once +#include + namespace ImGui { +void SetNextWindowSizeRelScreen(float xPercent, float yPercent, ImGuiCond_ cond = ImGuiCond_None); +void SetNextWindowCentered(ImGuiCond_ cond = ImGuiCond_None); + +void PushDisabled(); +void PopDisabled(); + void ErrorIcon(); void WarningIcon(); diff --git a/core/src/UI/UI_Items.cpp b/core/src/UI/UI_Items.cpp index 371e682..a990a96 100644 --- a/core/src/UI/UI_Items.cpp +++ b/core/src/UI/UI_Items.cpp @@ -1,9 +1,124 @@ #include "UI.hpp" +#include "Model/GlobalStates.hpp" +#include "Model/Project.hpp" #include "UI/Localization.hpp" +#include "UI/States.hpp" +#include #include +#include -void UI::ItemsTab() { +namespace { +/// Specialized for each item type. +template +void AddToItemListDialog(ItemList& list); +/// Specialized for each item type. +template +void ItemListEntries(ItemList& list); + +template <> +void AddToItemListDialog(ItemList& list) { + static std::string productName; + static std::string description; + + auto ls = LocaleStrings::Instance.get(); + auto& uis = UIState::GetInstance(); + ImGui::InputText(ls->ProductNameColumn.Get(), &productName); + ImGui::InputText(ls->ProductDescriptionColumn.Get(), &description); + + if (ImGui::Button(ls->DialogConfirm.Get())) { + auto& product = uis.CurrentProject->Products.Insert(std::move(productName)); + product.SetDescription(std::move(description)); + + productName.clear(); + description.clear(); + + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button(ls->DialogCancel.Get())) { + ImGui::CloseCurrentPopup(); + } +} + +template <> +void AddToItemListDialog(ItemList& list) { // TODO } + +template <> +void AddToItemListDialog(ItemList& list) { + // TODO +} + +template <> +void ItemListEntries(ItemList& list) { + auto ls = LocaleStrings::Instance.get(); + if (ImGui::BeginTable("ItemListEntries", 2)) { + + ImGui::TableSetupColumn(ls->ProductNameColumn.Get()); + ImGui::TableSetupColumn(ls->ProductDescriptionColumn.Get()); + ImGui::TableHeadersRow(); + + for (auto& entry : list) { + ImGui::TableNextRow(); + + ImGui::TableNextColumn(); + ImGui::Text("%s", entry.GetName().c_str()); + ImGui::TableNextColumn(); + ImGui::Text("%.8s", entry.GetDescription().c_str()); + if (ImGui::Button(ICON_FA_EDIT)) { + // TODO + } + } + ImGui::EndTable(); + } +} + +template <> +void ItemListEntries(ItemList& list) { + // TODO +} + +template <> +void ItemListEntries(ItemList& list) { + // TODO +} + +template +void ItemListEditor(ItemList& list) { + auto ls = LocaleStrings::Instance.get(); + + if (ImGui::Button(ls->AddItem.Get())) { + ImGui::SetNextWindowCentered(); + ImGui::OpenPopup(ls->AddItemDialogTitle.Get()); + } + if (ImGui::BeginPopupModal(ls->AddItemDialogTitle.Get())) { + AddToItemListDialog(list); + ImGui::EndPopup(); + } + + ImGui::SameLine(); + if (ImGui::Button(ls->DeleteItem.Get())) { + // TODO + } + + ItemListEntries(list); +} +} // namespace + +void UI::ItemsTab() { + auto ls = LocaleStrings::Instance.get(); + auto& uis = UIState::GetInstance(); + + if (ImGui::CollapsingHeader(ls->ProductCategoryName.Get())) { + ItemListEditor(uis.CurrentProject->Products); + } + if (ImGui::CollapsingHeader(ls->FactoryCategoryName.Get())) { + ItemListEditor(uis.CurrentProject->Factories); + } + if (ImGui::CollapsingHeader(ls->CustomerCategoryName.Get())) { + ItemListEditor(uis.CurrentProject->Customers); + } +} diff --git a/core/src/UI/UI_MainWindow.cpp b/core/src/UI/UI_MainWindow.cpp index de300e2..15c28ff 100644 --- a/core/src/UI/UI_MainWindow.cpp +++ b/core/src/UI/UI_MainWindow.cpp @@ -5,28 +5,38 @@ #include "UI/Localization.hpp" #include "UI/States.hpp" -#include - #include #include -#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(); - auto project = Project::Load(path); - auto uptr = std::unique_ptr(new Project(std::move(project))); - uis.SetCurrentProject(std::move(uptr)); + uis.SetCurrentProject(std::make_unique(std::move(project))); } void ProjectTab_Normal() { - // TODO + 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() { @@ -42,6 +52,7 @@ void ProjectTab_NoProject() { auto TrySelectPath = [&](fs::path newPath) { if (fs::exists(newPath)) { dirNameIsValid = true; + dirName = newPath.string(); dirPath = std::move(newPath); } else { dirNameIsValid = false; @@ -49,15 +60,14 @@ void ProjectTab_NoProject() { }; 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->NewProjectTitle.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->NewProjectTitle.Get(), &newProjectDialogDummyTrue)) { + if (ImGui::BeginPopupModal(ls->NewProjectDialogTitle.Get(), &newProjectDialogDummyTrue)) { ImGui::InputTextWithHint("##ProjectName", ls->NewProjectNameHint.Get(), &projectName); if (ImGui::InputTextWithHint("##ProjectPath", ls->NewProjectPathHint.Get(), &dirName)) { @@ -76,30 +86,26 @@ void ProjectTab_NoProject() { ImGui::ErrorIcon(); ImGui::SameLine(); - ImGui::Text(ls->NewProjectEmptyNameError.Get()); + ImGui::Text("%s", ls->NewProjectEmptyNameError.Get()); } if (!dirNameIsValid) { ImGui::ErrorIcon(); ImGui::SameLine(); - ImGui::Text(ls->NewProjectInvalidPathError.Get()); + ImGui::Text("%s", ls->NewProjectInvalidPathError.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->ConfirmNewProject.Get())) { + if (!formValid) ImGui::PushDisabled(); + if (ImGui::Button(ls->DialogConfirm.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)); + uis.SetCurrentProject(std::make_unique(std::move(project))); // Dialog just got closed, reset states projectName = ""; @@ -107,13 +113,10 @@ void ProjectTab_NoProject() { dirPath = fs::path{}; dirNameIsValid = false; } - if (!formValid) { - ImGui::PopItemFlag(); - ImGui::PopStyleVar(); - } + if (!formValid) ImGui::PopDisabled(); ImGui::SameLine(); - if (ImGui::Button(ls->CancelNewProject.Get())) { + if (ImGui::Button(ls->DialogCancel.Get())) { ImGui::CloseCurrentPopup(); } @@ -129,7 +132,7 @@ void ProjectTab_NoProject() { } ImGui::Separator(); - ImGui::Text(ls->RecentProjects.Get()); + ImGui::Text("%s", ls->RecentProjects.Get()); ImGui::SameLine(); if (ImGui::Button(ls->ClearRecentProjects.Get())) { gs.ClearRecentProjects(); @@ -137,18 +140,18 @@ void ProjectTab_NoProject() { auto& recentProjects = gs.GetRecentProjects(); if (recentProjects.empty()) { - ImGui::Text(ls->NoRecentProjectsMessage.Get()); + ImGui::Text("%s", ls->NoRecentProjectsMessage.Get()); } for (auto it = recentProjects.begin(); it != recentProjects.end(); ++it) { auto& [path, recent] = *it; - ImGui::Text(recent.c_str()); + ImGui::Text("%s", recent.c_str()); ImGui::SameLine(); if (ImGui::Button(ICON_FA_EDIT)) { LoadProjectAt(path); } if (ImGui::IsItemHovered()) { - ImGui::SetTooltip(ls->OpenRecentProjectTooltip.Get()); + ImGui::SetTooltip("%s", ls->OpenRecentProjectTooltip.Get()); } ImGui::SameLine(); @@ -156,7 +159,7 @@ void ProjectTab_NoProject() { gs.RemoveRecentProject(std::distance(recentProjects.begin(), it)); } if (ImGui::IsItemHovered()) { - ImGui::SetTooltip(ls->DeleteRecentProjectTooltip.Get()); + ImGui::SetTooltip("%s", ls->DeleteRecentProjectTooltip.Get()); } } } diff --git a/core/src/UI/UI_Utils.cpp b/core/src/UI/UI_Utils.cpp index 61a62f0..615caae 100644 --- a/core/src/UI/UI_Utils.cpp +++ b/core/src/UI/UI_Utils.cpp @@ -2,6 +2,27 @@ #include #include +#include + +void ImGui::SetNextWindowSizeRelScreen(float xPercent, float yPercent, ImGuiCond_ cond) { + auto vs = ImGui::GetMainViewport()->Size; + ImGui::SetNextWindowSize({ vs.x * xPercent, vs.y * yPercent }, cond); +} + +void ImGui::SetNextWindowCentered(ImGuiCond_ cond) { + auto vs = ImGui::GetMainViewport()->Size; + ImGui::SetNextWindowPos({ vs.x / 2, vs.y / 2 }, cond, { 0.5f, 0.5f }); +} + +void ImGui::PushDisabled() { + ImGui::PushItemFlag(ImGuiItemFlags_Disabled, false); + ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.5f * ImGui::GetStyle().Alpha); +} + +void ImGui::PopDisabled() { + ImGui::PopItemFlag(); + ImGui::PopStyleVar(); +} void ImGui::ErrorIcon() { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 237 / 255.0f, 67 / 255.0f, 55 / 255.0f, 1.0f }); // #ED4337 -- cgit v1.2.3-70-g09d2