1
0
mirror of synced 2024-11-28 09:30:51 +01:00

impr: Allow tutorials to work correctly with localized strings

This commit is contained in:
WerWolv 2023-12-13 11:48:21 +01:00
parent 5bcfe37b4e
commit 3bc5295eae
2 changed files with 50 additions and 52 deletions

View File

@ -2,6 +2,8 @@
#include <hex.hpp>
#include <hex/api/localization_manager.hpp>
#include <string>
#include <list>
#include <variant>
@ -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<ImGuiID> 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<std::variant<std::string, int>> &&ids);
Step& addHighlight(const std::string &unlocalizedText, std::initializer_list<std::variant<Lang, std::string, int>> &&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<std::variant<std::string, int>> &&ids);
Step& addHighlight(std::initializer_list<std::variant<Lang, std::string, int>> &&ids);
/**
* @brief Sets the text that will be displayed in the tutorial message box

View File

@ -4,6 +4,7 @@
#include <imgui_internal.h>
#include <hex/helpers/utils.hpp>
#include <wolv/utils/core.hpp>
#include <map>
@ -18,6 +19,40 @@ namespace hex {
std::vector<std::pair<ImRect, std::string>> 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<ImGuiID> 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<std::variant<std::string, int>>&& ids) {
TutorialManager::Tutorial::Step& TutorialManager::Tutorial::Step::addHighlight(const std::string& unlocalizedText, std::initializer_list<std::variant<Lang, std::string, int>>&& 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<std::variant<std::string, int>>&& ids) {
TutorialManager::Tutorial::Step& TutorialManager::Tutorial::Step::addHighlight(std::initializer_list<std::variant<Lang, std::string, int>>&& 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*) {