From 2d7e772909571676dd4266337d43086bd2927e93 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 29 Mar 2021 00:06:34 -0700 Subject: UI big structure --- core/src/Entrypoint/main.cpp | 20 ++++++++++---- core/src/Model/Stock.cpp | 1 + core/src/Model/Stock.hpp | 1 + core/src/Model/fwd.hpp | 1 + core/src/UI/Export.cpp | 1 - core/src/UI/Export.hpp | 1 - core/src/UI/Localization.cpp | 3 ++ core/src/UI/Localization.hpp | 24 ++++++++++++++++ core/src/UI/UI.hpp | 12 ++++++++ core/src/UI/UI_DatabaseView.cpp | 9 ++++++ core/src/UI/UI_Export.cpp | 9 ++++++ core/src/UI/UI_Items.cpp | 9 ++++++ core/src/UI/UI_MainWindow.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ core/src/UI/UI_Settings.cpp | 9 ++++++ core/src/UI/fwd.hpp | 4 +++ core/src/Utils/I18n.cpp | 53 +++++++++++++++++++---------------- core/src/Utils/I18n.hpp | 4 +-- core/src/cplt_fwd.hpp | 5 ++++ 18 files changed, 195 insertions(+), 32 deletions(-) create mode 100644 core/src/Model/Stock.cpp create mode 100644 core/src/Model/Stock.hpp create mode 100644 core/src/Model/fwd.hpp delete mode 100644 core/src/UI/Export.cpp delete mode 100644 core/src/UI/Export.hpp create mode 100644 core/src/UI/Localization.cpp create mode 100644 core/src/UI/Localization.hpp create mode 100644 core/src/UI/UI.hpp create mode 100644 core/src/UI/UI_DatabaseView.cpp create mode 100644 core/src/UI/UI_Export.cpp create mode 100644 core/src/UI/UI_Items.cpp create mode 100644 core/src/UI/UI_MainWindow.cpp create mode 100644 core/src/UI/UI_Settings.cpp create mode 100644 core/src/UI/fwd.hpp create mode 100644 core/src/cplt_fwd.hpp (limited to 'core/src') diff --git a/core/src/Entrypoint/main.cpp b/core/src/Entrypoint/main.cpp index a8c988a..9f57a5e 100644 --- a/core/src/Entrypoint/main.cpp +++ b/core/src/Entrypoint/main.cpp @@ -5,6 +5,10 @@ #include "Entrypoint/OpenGL2.hpp" #include "Entrypoint/OpenGL3.hpp" #include "Entrypoint/Vulkan.hpp" +#include "UI/Localization.hpp" +#include "UI/UI.hpp" +#include "Utils/I18n.hpp" +#include "Utils/Sigslot.hpp" #include #include @@ -123,19 +127,25 @@ int main(int argc, char* argv[]) { auto backendOption = parser.get("--rendering-backend"); auto backend = CreateBackend(backendOption); - auto& io = ImGui::GetIO(); + ImGui::StyleColorsLight(); + // Includes latin alphabet, although for some reason smaller than if rendered using 18 point NotoSans regular - io.Fonts->AddFontFromFileTTF("fonts/NotoSansSC-Regular.otf", 18, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon()); + ImGui::GetIO().Fonts->AddFontFromFileTTF("fonts/NotoSansSC-Regular.otf", 18, nullptr, ImGui::GetIO().Fonts->GetGlyphRangesChineseSimplifiedCommon()); - ImWchar kIconRanges[] = { ICON_MIN_FA, ICON_MAX_FA }; + ImWchar iconRanges[] = { ICON_MIN_FA, ICON_MAX_FA }; ImFontConfig config; config.MergeMode = true; - io.Fonts->AddFontFromFileTTF("fonts/FontAwesome5-Solid.otf", 14, &config, kIconRanges); + ImGui::GetIO().Fonts->AddFontFromFileTTF("fonts/FontAwesome5-Solid.otf", 14, &config, iconRanges); + + I18n::reloadSignal.Connect([]() { LocaleStrings::Instance = std::make_unique(); }); + // Do i18n intialization after linking reload signals, so that when SetLanguage() is called, the locale strings will be initialized (without us writing the code another time outside the slot) + I18n::Init(); + I18n::SetLanguage("zh_CN"); auto window = backend->GetWindow(); while (!glfwWindowShouldClose(window)) { backend->BeginFrame(); - ImGui::ShowDemoWindow(); + UI::MainWindow(); backend->EndFrame(); } diff --git a/core/src/Model/Stock.cpp b/core/src/Model/Stock.cpp new file mode 100644 index 0000000..a56de03 --- /dev/null +++ b/core/src/Model/Stock.cpp @@ -0,0 +1 @@ +#include "Stock.hpp" diff --git a/core/src/Model/Stock.hpp b/core/src/Model/Stock.hpp new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/core/src/Model/Stock.hpp @@ -0,0 +1 @@ +#pragma once diff --git a/core/src/Model/fwd.hpp b/core/src/Model/fwd.hpp new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/core/src/Model/fwd.hpp @@ -0,0 +1 @@ +#pragma once diff --git a/core/src/UI/Export.cpp b/core/src/UI/Export.cpp deleted file mode 100644 index d6b26dc..0000000 --- a/core/src/UI/Export.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Export.hpp" diff --git a/core/src/UI/Export.hpp b/core/src/UI/Export.hpp deleted file mode 100644 index 6f70f09..0000000 --- a/core/src/UI/Export.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/core/src/UI/Localization.cpp b/core/src/UI/Localization.cpp new file mode 100644 index 0000000..220df6f --- /dev/null +++ b/core/src/UI/Localization.cpp @@ -0,0 +1,3 @@ +#include "Localization.hpp" + +std::unique_ptr LocaleStrings::Instance{}; diff --git a/core/src/UI/Localization.hpp b/core/src/UI/Localization.hpp new file mode 100644 index 0000000..a2ac09b --- /dev/null +++ b/core/src/UI/Localization.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "Utils/I18n.hpp" + +#include +#include + +using namespace std::literals::string_view_literals; + +class LocaleStrings { +public: + static std::unique_ptr Instance; + +public: + BasicTranslation MenuBarFile{ "MenuBar.File"sv }; + BasicTranslation MenuBarNewWindow{ "MenuBar.File.NewWindow"sv }; + BasicTranslation MenuBarNewProject{ "MenuBar.File.NewProject"sv }; + BasicTranslation MenuBarOpenProject{ "MenuBar.File.OpenProject"sv }; + + BasicTranslation TabSettings{ "MainWindow.Tab.Settings"sv }; + BasicTranslation TabDatabaseView{ "MainWindow.Tab.DatabaseView"sv }; + BasicTranslation TabItems{ "MainWindow.Tab.Items"sv }; + BasicTranslation TabExport{ "MainWindow.Tab.Exports"sv }; +}; diff --git a/core/src/UI/UI.hpp b/core/src/UI/UI.hpp new file mode 100644 index 0000000..08f5771 --- /dev/null +++ b/core/src/UI/UI.hpp @@ -0,0 +1,12 @@ +#pragma once + +namespace UI { + +void MainWindow(); + +void SettingsTab(); +void DatabaseViewTab(); +void ItemsTab(); +void ExportTab(); + +} // namespace UI diff --git a/core/src/UI/UI_DatabaseView.cpp b/core/src/UI/UI_DatabaseView.cpp new file mode 100644 index 0000000..234aeaa --- /dev/null +++ b/core/src/UI/UI_DatabaseView.cpp @@ -0,0 +1,9 @@ +#include "UI.hpp" + +#include "UI/Localization.hpp" + +#include + +void UI::DatabaseViewTab() { + // TODO +} diff --git a/core/src/UI/UI_Export.cpp b/core/src/UI/UI_Export.cpp new file mode 100644 index 0000000..06b49f5 --- /dev/null +++ b/core/src/UI/UI_Export.cpp @@ -0,0 +1,9 @@ +#include "UI.hpp" + +#include "UI/Localization.hpp" + +#include + +void UI::ExportTab() { + // TODO +} diff --git a/core/src/UI/UI_Items.cpp b/core/src/UI/UI_Items.cpp new file mode 100644 index 0000000..371e682 --- /dev/null +++ b/core/src/UI/UI_Items.cpp @@ -0,0 +1,9 @@ +#include "UI.hpp" + +#include "UI/Localization.hpp" + +#include + +void UI::ItemsTab() { + // TODO +} diff --git a/core/src/UI/UI_MainWindow.cpp b/core/src/UI/UI_MainWindow.cpp new file mode 100644 index 0000000..8b1e71d --- /dev/null +++ b/core/src/UI/UI_MainWindow.cpp @@ -0,0 +1,61 @@ +#include "UI.hpp" + +#include "UI/Localization.hpp" + +#include + +void UI::MainWindow() { + auto ls = LocaleStrings::Instance.get(); + + float menuBarHeight; + ImGui::BeginMainMenuBar(); + { + menuBarHeight = ImGui::GetWindowHeight(); + + if (ImGui::BeginMenu(ls->MenuBarFile.Get())) { + if (ImGui::MenuItem(ls->MenuBarNewWindow.Get())) { + // TODO + } + if (ImGui::MenuItem(ls->MenuBarNewProject.Get())) { + // TODO + } + + ImGui::Separator(); + if (ImGui::MenuItem(ls->MenuBarOpenProject.Get())) { + // TODO + } + + ImGui::EndMenu(); + } + } + ImGui::EndMainMenuBar(); + + auto windowSize = ImGui::GetMainViewport()->Size; + ImGui::SetNextWindowSize({ windowSize.x, windowSize.y - menuBarHeight }); + ImGui::SetNextWindowPos({ 0, menuBarHeight }); + 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->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(); + } + + ImGui::EndTabBar(); + } + ImGui::End(); +} diff --git a/core/src/UI/UI_Settings.cpp b/core/src/UI/UI_Settings.cpp new file mode 100644 index 0000000..3dbb0d2 --- /dev/null +++ b/core/src/UI/UI_Settings.cpp @@ -0,0 +1,9 @@ +#include "UI/UI.hpp" + +#include "UI/Localization.hpp" + +#include + +void UI::SettingsTab() { + // TODO +} diff --git a/core/src/UI/fwd.hpp b/core/src/UI/fwd.hpp new file mode 100644 index 0000000..714a443 --- /dev/null +++ b/core/src/UI/fwd.hpp @@ -0,0 +1,4 @@ +#pragma once + +// Localization.hpp +class LocaleStrings; diff --git a/core/src/Utils/I18n.cpp b/core/src/Utils/I18n.cpp index 645cfc5..f5a6a49 100644 --- a/core/src/Utils/I18n.cpp +++ b/core/src/Utils/I18n.cpp @@ -28,15 +28,14 @@ public: } public: - tsl::array_map localeInfos; - tsl::array_map currentEntries; - LanguageInfo* currentLanguage; + tsl::array_map LocaleInfos; + tsl::array_map CurrentEntries; + LanguageInfo* CurrentLanguage = nullptr; }; -std::string findLocalizedName(const fs::path& localeFile) { +std::string FindLocalizedName(const fs::path& localeFile) { std::ifstream ifs{ localeFile }; if (!ifs) { - // TODO log error throw std::runtime_error("Failed to open locale file."); } @@ -45,7 +44,6 @@ std::string findLocalizedName(const fs::path& localeFile) { if (auto& name = root["$localized_name"]; name.isString()) { return std::string(name.asCString()); } else { - // TODO log error throw std::runtime_error("Failed to find $localized_name in language file."); } } @@ -57,8 +55,7 @@ void I18n::Init() { auto dir = fs::current_path() / "locale"; if (!fs::exists(dir)) { - // TODO log error - return; + throw std::runtime_error("Failed to find locale directory."); } for (auto& elm : fs::directory_iterator{ dir }) { @@ -67,16 +64,14 @@ void I18n::Init() { auto& path = elm.path(); auto codeName = path.stem().string(); - state.localeInfos.emplace( + state.LocaleInfos.emplace( codeName, LanguageInfo{ .codeName = codeName, - .localeName = findLocalizedName(path), + .localeName = FindLocalizedName(path), .file = path, }); } - - SetLanguage("en_US"); } void I18n::Shutdown() { @@ -92,25 +87,35 @@ void I18n::ReloadLocales() { std::string_view I18n::GetLanguage() { auto& state = I18nState::Get(); - return state.currentLanguage->localeName; + return state.CurrentLanguage->localeName; } bool I18n::SetLanguage(std::string_view lang) { auto& state = I18nState::Get(); - if (!state.currentLanguage) return false; - if (state.currentLanguage->codeName == lang) return false; + if (state.CurrentLanguage && + state.CurrentLanguage->codeName == lang) + { + return false; + } - if (auto iter = state.localeInfos.find(lang); iter != state.localeInfos.end()) { - state.currentLanguage = &iter.value(); - state.currentEntries.clear(); + if (auto iter = state.LocaleInfos.find(lang); iter != state.LocaleInfos.end()) { + state.CurrentLanguage = &iter.value(); + state.CurrentEntries.clear(); - auto& file = state.currentLanguage->file; + auto& file = state.CurrentLanguage->file; std::ifstream ifs{ file }; Json::Value root; ifs >> root; for (auto name : root.getMemberNames()) { + if (name == "$localized_name") { + continue; + } + auto& value = root[name]; + if (value.isString()) { + state.CurrentEntries.insert(name, value.asCString()); + } } } ReloadLocales(); @@ -119,8 +124,8 @@ bool I18n::SetLanguage(std::string_view lang) { std::optional I18n::Lookup(std::string_view key) { auto& state = I18nState::Get(); - auto iter = state.currentEntries.find(key); - if (iter != state.currentEntries.end()) { + auto iter = state.CurrentEntries.find(key); + if (iter != state.CurrentEntries.end()) { return iter.value(); } else { return std::nullopt; @@ -140,10 +145,12 @@ std::string_view I18n::LookupUnwrap(std::string_view key) { } BasicTranslation::BasicTranslation(std::string_view key) - : mContent{ I18n::LookupUnwrap(key) } { + // Assuming the string is null terminated, which it is here (because we store interally using std::string) + // TODO properly use std::string_view when imgui supports it + : mContent{ I18n::LookupUnwrap(key).data() } { } -std::string_view BasicTranslation::Get() const { +const char* BasicTranslation::Get() const { return mContent; } diff --git a/core/src/Utils/I18n.hpp b/core/src/Utils/I18n.hpp index 6b72d29..b9386be 100644 --- a/core/src/Utils/I18n.hpp +++ b/core/src/Utils/I18n.hpp @@ -41,11 +41,11 @@ struct FloatArgument { class BasicTranslation { private: - std::string_view mContent; + const char* mContent; public: BasicTranslation(std::string_view key); - std::string_view Get() const; + const char* Get() const; }; class FormattedTranslation { diff --git a/core/src/cplt_fwd.hpp b/core/src/cplt_fwd.hpp new file mode 100644 index 0000000..e05eb4f --- /dev/null +++ b/core/src/cplt_fwd.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include "Model/fwd.hpp" +#include "UI/fwd.hpp" +#include "Utils/fwd.hpp" -- cgit v1.2.3-70-g09d2