From 2a8daede944e4bd67f0c486b879fdbd290db4d37 Mon Sep 17 00:00:00 2001 From: NotNite Date: Sun, 19 May 2024 20:43:36 -0400 Subject: [PATCH] Add search to object inspection --- Bouny/ui_manager.cpp | 27 +++++++++++++++++++-------- Bouny/utils.cpp | 41 ++++++++++++++++++++++++++++++++++++----- Bouny/utils.h | 3 +++ 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/Bouny/ui_manager.cpp b/Bouny/ui_manager.cpp index eafac26..b969993 100644 --- a/Bouny/ui_manager.cpp +++ b/Bouny/ui_manager.cpp @@ -106,6 +106,20 @@ void ui_manager::draw() CRoom* room = nullptr; g_module_interface->GetCurrentRoomData(room); + CInstance* global = nullptr; + g_module_interface->GetGlobalInstance(&global); + if (global != nullptr) + { + if (ImGui::Begin("Global")) + { + auto value = RValue(global); + std::map results; + utils::enumerate_members(value, results, true); + static std::string search_query; + utils::draw_search(results, search_query); + } + ImGui::End(); + } if (room != nullptr) { @@ -214,14 +228,11 @@ void ui_manager::draw() if (element->m_Type == 2) { auto instance = static_cast(element); - g_module_interface->EnumInstanceMembers(instance->m_Instance, - [](const char* name, RValue* value) -> bool - { - ImGui::Text( - "%s: %s", name, - utils::rvalue_to_string(value).c_str()); - return false; - }); + auto value = RValue(instance->m_Instance); + std::map results; + utils::enumerate_members(value, results); + static std::string search_query; + utils::draw_search(results, search_query); } } diff --git a/Bouny/utils.cpp b/Bouny/utils.cpp index f65f0cf..ff3d809 100644 --- a/Bouny/utils.cpp +++ b/Bouny/utils.cpp @@ -1,9 +1,8 @@ -#include "utils.h" - +#include "globals.h" +#include "utils.h" +#include #include -#include "globals.h" - std::map names = { {VALUE_REAL, "real"}, {VALUE_STRING, "string"}, @@ -42,7 +41,7 @@ void utils::log_hook(std::string name, YYTK::RValue& return_value, int num_args, std::string args_str = std::accumulate(args, args + num_args, std::string{}, [](std::string acc, YYTK::RValue* arg) { - auto arg_str = rvalue_to_string(arg); + auto arg_str = utils::rvalue_to_string(arg); auto type_str = names.contains(arg->m_Kind) ? names[arg->m_Kind] : "unknown"; @@ -69,3 +68,35 @@ int utils::hook_and_log(std::string name) log_hook(name, return_value, num_args, args); }); } + +void utils::draw_search(std::map& search_results, std::string& search_query) +{ + ImGui::InputText("Search", &search_query); + ImGui::Separator(); + if (ImGui::BeginChild("##search_results", ImGui::GetContentRegionAvail())) + { + for (auto& [key, value] : search_results) + { + auto key_lower = key; + std::ranges::transform(key_lower, key_lower.begin(), ::tolower); + auto search_query_lower = search_query; + std::ranges::transform(search_query_lower, search_query_lower.begin(), ::tolower); + if (search_query_lower.empty() || key_lower.contains(search_query_lower)) + { + ImGui::Text("%s: %s", key.c_str(), value.c_str()); + } + } + } + ImGui::EndChild(); +} + +void utils::enumerate_members(YYTK::RValue rvalue, std::map& results, bool ignore_functions) +{ + g_module_interface->EnumInstanceMembers(rvalue, [&results, ignore_functions](const char* name, YYTK::RValue* value) + { + auto str = utils::rvalue_to_string(value); + if (ignore_functions && str == "function") return false; + results[std::string(name)] = str; + return false; + }); +} diff --git a/Bouny/utils.h b/Bouny/utils.h index 7e8b5e0..26e07f6 100644 --- a/Bouny/utils.h +++ b/Bouny/utils.h @@ -7,4 +7,7 @@ class utils static std::string rvalue_to_string(YYTK::RValue* rvalue); static void log_hook(std::string name, YYTK::RValue& return_value, int num_args, YYTK::RValue** args); static int hook_and_log(std::string name); + + static void draw_search(std::map& search_results, std::string& search_query); + static void enumerate_members(YYTK::RValue rvalue, std::map& results, bool ignore_functions = false); };