summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-03-29 00:06:34 -0700
committerrtk0c <[email protected]>2021-03-29 00:06:34 -0700
commit2d7e772909571676dd4266337d43086bd2927e93 (patch)
tree80ec2c39336b5b7fd1cd50d5a715343531f6a743 /core
parent53bff541e292c5d6cae73881a37bf8f7e4a5fd0a (diff)
UI big structure
Diffstat (limited to 'core')
-rw-r--r--core/CMakeLists.txt42
-rw-r--r--core/locale/en_US.json3
-rw-r--r--core/locale/zh_CN.json11
-rw-r--r--core/src/Entrypoint/main.cpp20
-rw-r--r--core/src/Model/Stock.cpp1
-rw-r--r--core/src/Model/Stock.hpp (renamed from core/src/UI/Export.hpp)0
-rw-r--r--core/src/Model/fwd.hpp1
-rw-r--r--core/src/UI/Export.cpp1
-rw-r--r--core/src/UI/Localization.cpp3
-rw-r--r--core/src/UI/Localization.hpp24
-rw-r--r--core/src/UI/UI.hpp12
-rw-r--r--core/src/UI/UI_DatabaseView.cpp9
-rw-r--r--core/src/UI/UI_Export.cpp9
-rw-r--r--core/src/UI/UI_Items.cpp9
-rw-r--r--core/src/UI/UI_MainWindow.cpp61
-rw-r--r--core/src/UI/UI_Settings.cpp9
-rw-r--r--core/src/UI/fwd.hpp4
-rw-r--r--core/src/Utils/I18n.cpp53
-rw-r--r--core/src/Utils/I18n.hpp4
-rw-r--r--core/src/cplt_fwd.hpp5
20 files changed, 221 insertions, 60 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index 37fccd3..e39512d 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -14,9 +14,9 @@ elseif(APPLE)
option(BUILD_CORE_WITH_METAL_BACKEND ON)
endif()
-function(add_source_group GROUP_NAME GROUP_SOURCES)
- set(${GROUP_NAME} ${GROUP_SOURCES})
- set_source_files_properties(${GROUP_SOURCES}
+function(add_source_group GROUP_NAME)
+ set(${GROUP_NAME} ${ARGN} PARENT_SCOPE)
+ set_source_files_properties(${ARGN}
PROPERTIES
UNITY_GROUP "${GROUP_NAME}"
)
@@ -24,54 +24,37 @@ endfunction()
set(ENTRYPOINT_MODULE_SOURCES
src/Entrypoint/main.cpp
- src/Entrypoint/Common.hpp
src/Entrypoint/Common.cpp
- src/Entrypoint/OpenGL2.hpp
src/Entrypoint/OpenGL2.cpp
- src/Entrypoint/OpenGL3.hpp
src/Entrypoint/OpenGL3.cpp
- src/Entrypoint/Vulkan.hpp
src/Entrypoint/Vulkan.cpp
- src/Entrypoint/DirectX11.hpp
src/Entrypoint/DirectX11.cpp
- src/Entrypoint/DirectX12.hpp
src/Entrypoint/DirectX12.cpp
- src/Entrypoint/Metal.hpp
src/Entrypoint/Metal.mm
)
-# macro(option_with_message OPTION_NAME DEFAULT_VALUE MESSAGE)
-# option(${OPTION_NAME} ${DEFAULT_VALUE})
-# if(${OPTION_NAME})
-# message(${MESSAGE})
-# endif()
-# endmacro()
-# option_with_message(BUILD_CORE_WITH_OPENGL2_BACKEND ON "Building with OpenGL2 backend")
-# option_with_message(BUILD_CORE_WITH_OPENGL3_BACKEND ON "Building with OpenGL3 backend")
-# option_with_message(BUILD_CORE_WITH_VULKAN_BACKEND ON "Building with Vulkan backend")
-# option_with_message(BUILD_CORE_WITH_DX11_BACKEND ON "Building with DirectX11 backend")
-# option_with_message(BUILD_CORE_WITH_DX12_BACKEND ON "Building with DirectX12 backend")
-# option_with_message(BUILD_CORE_WITH_METAL_BACKEND ON "Building with Metal backend")
+add_source_group(MODEL_MODULE_SOURCES
+ src/Model/Stock.cpp
+)
add_source_group(UI_MODULE_SOURCES
- src/UI/Export.hpp
- src/UI/Export.cpp
+ src/UI/Localization.cpp
+ src/UI/UI_DatabaseView.cpp
+ src/UI/UI_Export.cpp
+ src/UI/UI_Items.cpp
+ src/UI/UI_MainWindow.cpp
+ src/UI/UI_Settings.cpp
)
add_source_group(UTILS_MODULE_SOURCES
- src/Utils/Enum.hpp
- src/Utils/I18n.hpp
src/Utils/I18n.cpp
- src/Utils/Sigslot.hpp
src/Utils/Sigslot.cpp
- src/Utils/String.hpp
src/Utils/String.cpp
)
# These files are compiled individually, hence no UNITY_GROUP property
# This is because the files here may contain non-c++ languages
set(UTILS_DIALOG_MODULE_SOURCES
- src/Utils/Dialog/Dialog.hpp
src/Utils/Dialog/Dialog.cpp
)
if(APPLE)
@@ -96,6 +79,7 @@ function(add_executable_variant TARGET_NAME)
add_executable(${TARGET_NAME}
${ENTRYPOINT_MODULE_SOURCES}
+ ${MODEL_MODULE_SOURCES}
${UI_MODULE_SOURCES}
${UTILS_MODULE_SOURCES}
${UTILS_DIALOG_MODULE_SOURCES}
diff --git a/core/locale/en_US.json b/core/locale/en_US.json
new file mode 100644
index 0000000..b6c9c38
--- /dev/null
+++ b/core/locale/en_US.json
@@ -0,0 +1,3 @@
+{
+ "$localized_name": "English - United States"
+} \ No newline at end of file
diff --git a/core/locale/zh_CN.json b/core/locale/zh_CN.json
new file mode 100644
index 0000000..e986041
--- /dev/null
+++ b/core/locale/zh_CN.json
@@ -0,0 +1,11 @@
+{
+ "$localized_name": "中文 - 中国",
+ "MenuBar.File": "文件",
+ "MenuBar.File.NewWindow": "新建窗口",
+ "MenuBar.File.NewProject": "新建项目...",
+ "MenuBar.File.OpenProject": "打开项目...",
+ "MainWindow.Tab.Settings": "\uf013 设置",
+ "MainWindow.Tab.DatabaseView": "\uf1c0 数据",
+ "MainWindow.Tab.Items": "\uf466 物品",
+ "MainWindow.Tab.Exports": "\uf56e 导出"
+} \ No newline at end of file
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 <glad/glad.h>
#include <GLFW/glfw3.h>
@@ -123,19 +127,25 @@ int main(int argc, char* argv[]) {
auto backendOption = parser.get<std::string>("--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<LocaleStrings>(); });
+ // 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/UI/Export.hpp b/core/src/Model/Stock.hpp
index 6f70f09..6f70f09 100644
--- a/core/src/UI/Export.hpp
+++ b/core/src/Model/Stock.hpp
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/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> 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 <memory>
+#include <string_view>
+
+using namespace std::literals::string_view_literals;
+
+class LocaleStrings {
+public:
+ static std::unique_ptr<LocaleStrings> 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 <imgui.h>
+
+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 <imgui.h>
+
+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 <imgui.h>
+
+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 <imgui.h>
+
+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 <imgui.h>
+
+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<char, LanguageInfo> localeInfos;
- tsl::array_map<char, std::string> currentEntries;
- LanguageInfo* currentLanguage;
+ tsl::array_map<char, LanguageInfo> LocaleInfos;
+ tsl::array_map<char, std::string> 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<std::string_view> 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"