From 3bc5295eaebb26f0faea9098301369aee829dcd8 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 13 Dec 2023 11:48:21 +0100 Subject: [PATCH] impr: Allow tutorials to work correctly with localized strings --- .../include/hex/api/tutorial_manager.hpp | 20 +---- lib/libimhex/source/api/tutorial_manager.cpp | 82 +++++++++++-------- 2 files changed, 50 insertions(+), 52 deletions(-) diff --git a/lib/libimhex/include/hex/api/tutorial_manager.hpp b/lib/libimhex/include/hex/api/tutorial_manager.hpp index cf5c18c4d..212750777 100644 --- a/lib/libimhex/include/hex/api/tutorial_manager.hpp +++ b/lib/libimhex/include/hex/api/tutorial_manager.hpp @@ -2,6 +2,8 @@ #include +#include + #include #include #include @@ -11,20 +13,6 @@ namespace hex { class TutorialManager { - private: - class IDStack { - public: - IDStack(); - - void add(const void *pointer); - void add(const std::string &string); - void add(int value); - - ImGuiID get(); - private: - ImVector idStack; - }; - public: enum class Position : u8 { None = 0, @@ -49,14 +37,14 @@ namespace hex { * @param ids ID of the element to highlight * @return Current step */ - Step& addHighlight(const std::string &unlocalizedText, std::initializer_list> &&ids); + Step& addHighlight(const std::string &unlocalizedText, std::initializer_list> &&ids); /** * @brief Adds a highlighting to a specific element * @param ids ID of the element to highlight * @return Current step */ - Step& addHighlight(std::initializer_list> &&ids); + Step& addHighlight(std::initializer_list> &&ids); /** * @brief Sets the text that will be displayed in the tutorial message box diff --git a/lib/libimhex/source/api/tutorial_manager.cpp b/lib/libimhex/source/api/tutorial_manager.cpp index 380cb35e6..83e224b2a 100644 --- a/lib/libimhex/source/api/tutorial_manager.cpp +++ b/lib/libimhex/source/api/tutorial_manager.cpp @@ -4,6 +4,7 @@ #include #include +#include #include @@ -18,6 +19,40 @@ namespace hex { std::vector> s_highlightDisplays; + class IDStack { + public: + IDStack() { + idStack.push_back(0); + } + + void add(const std::string &string) { + const ImGuiID seed = idStack.back(); + const ImGuiID id = ImHashStr(string.c_str(), string.length(), seed); + + idStack.push_back(id); + } + + void add(const void *pointer) { + const ImGuiID seed = idStack.back(); + const ImGuiID id = ImHashData(&pointer, sizeof(pointer), seed); + + idStack.push_back(id); + } + + void add(int value) { + const ImGuiID seed = idStack.back(); + const ImGuiID id = ImHashData(&value, sizeof(value), seed); + + idStack.push_back(id); + } + + ImGuiID get() { + return idStack.back(); + } + private: + ImVector idStack; + }; + } TutorialManager::Tutorial& TutorialManager::createTutorial(const std::string& unlocalizedName, const std::string& unlocalizedDescription) { @@ -158,13 +193,9 @@ namespace hex { s_highlightDisplays.clear(); } - TutorialManager::IDStack::IDStack() { - idStack.push_back(0); - } - TutorialManager::Tutorial::Step& TutorialManager::Tutorial::addStep() { auto &newStep = this->m_steps.emplace_back(this); - this->m_currentStep = this->m_steps.begin(); + this->m_currentStep = this->m_steps.end(); this->m_latestStep = this->m_currentStep; return newStep; @@ -193,19 +224,24 @@ namespace hex { void TutorialManager::Tutorial::Step::advance(i32 steps) const { m_parent->m_currentStep->removeHighlights(); - std::advance(s_currentTutorial->second.m_currentStep, steps); + std::advance(m_parent->m_currentStep, steps); if (m_parent->m_currentStep != m_parent->m_steps.end()) m_parent->m_currentStep->addHighlights(); } - TutorialManager::Tutorial::Step& TutorialManager::Tutorial::Step::addHighlight(const std::string& unlocalizedText, std::initializer_list>&& ids) { + TutorialManager::Tutorial::Step& TutorialManager::Tutorial::Step::addHighlight(const std::string& unlocalizedText, std::initializer_list>&& ids) { IDStack idStack; for (const auto &id : ids) { - std::visit([&idStack](const auto &id) { - idStack.add(id); + std::visit(wolv::util::overloaded { + [&idStack](const Lang &id) { + idStack.add(id.get()); + }, + [&idStack](const auto &id) { + idStack.add(id); + } }, id); } @@ -217,7 +253,7 @@ namespace hex { return *this; } - TutorialManager::Tutorial::Step& TutorialManager::Tutorial::Step::addHighlight(std::initializer_list>&& ids) { + TutorialManager::Tutorial::Step& TutorialManager::Tutorial::Step::addHighlight(std::initializer_list>&& ids) { return this->addHighlight("", std::move(ids)); } @@ -267,32 +303,6 @@ namespace hex { } } - - void TutorialManager::IDStack::add(const std::string &string) { - const ImGuiID seed = idStack.back(); - const ImGuiID id = ImHashStr(string.c_str(), string.length(), seed); - - idStack.push_back(id); - } - - void TutorialManager::IDStack::add(const void *pointer) { - const ImGuiID seed = idStack.back(); - const ImGuiID id = ImHashData(&pointer, sizeof(pointer), seed); - - idStack.push_back(id); - } - - void TutorialManager::IDStack::add(int value) { - const ImGuiID seed = idStack.back(); - const ImGuiID id = ImHashData(&value, sizeof(value), seed); - - idStack.push_back(id); - } - - ImGuiID TutorialManager::IDStack::get() { - return idStack.back(); - } - } void ImGuiTestEngineHook_ItemAdd(ImGuiContext*, ImGuiID id, const ImRect& bb, const ImGuiLastItemData*) {