yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
widget_auto_register.cc
Go to the documentation of this file.
2
3#include <vector>
4
5#include "imgui/imgui_internal.h"
6#include "absl/strings/str_join.h"
7#include "absl/strings/string_view.h"
8
9namespace yaze {
10namespace gui {
11
12// Thread-local storage for the current auto-registration scope
13thread_local std::vector<std::string> g_auto_scope_stack_;
14
15AutoWidgetScope::AutoWidgetScope(const std::string& name)
16 : scope_(name), name_(name) {
17 g_auto_scope_stack_.push_back(name);
18}
19
21 if (!g_auto_scope_stack_.empty()) {
22 g_auto_scope_stack_.pop_back();
23 }
24}
25
26void AutoRegisterLastItem(const std::string& widget_type,
27 const std::string& explicit_label,
28 const std::string& description) {
29 ImGuiContext* ctx = ImGui::GetCurrentContext();
30 if (!ctx || !ctx->CurrentWindow) {
31 return;
32 }
33
34 // Get the last item's ID
35 ImGuiID imgui_id = ctx->LastItemData.ID;
36 if (imgui_id == 0) {
37 return; // No valid item to register
38 }
39
40 // Extract label
41 std::string label = explicit_label;
42 if (label.empty()) {
43 label = absl::StrCat(widget_type, "_", imgui_id);
44 }
45
46 // Build full hierarchical path
47 std::string full_path;
48 if (!g_auto_scope_stack_.empty()) {
49 full_path = absl::StrJoin(g_auto_scope_stack_, "/");
50 full_path += "/";
51 }
52
53 // Add widget type and normalized label
54 std::string normalized_label = WidgetIdRegistry::NormalizeLabel(label);
55 full_path += absl::StrCat(widget_type, ":", normalized_label);
56
57 // Capture metadata from ImGui's last item
59 metadata.label = label;
60
61 // Get window name
62 if (ctx->CurrentWindow) {
63 metadata.window_name = std::string(ctx->CurrentWindow->Name);
64 }
65
66 // Capture visibility and enabled state
67 const ImGuiLastItemData& last = ctx->LastItemData;
68 metadata.visible = (last.StatusFlags & ImGuiItemStatusFlags_Visible) != 0;
69 metadata.enabled = (last.ItemFlags & ImGuiItemFlags_Disabled) == 0;
70
71 // Capture bounding rectangle
73 bounds.min_x = last.Rect.Min.x;
74 bounds.min_y = last.Rect.Min.y;
75 bounds.max_x = last.Rect.Max.x;
76 bounds.max_y = last.Rect.Max.y;
77 bounds.valid = true;
78 metadata.bounds = bounds;
79
80 // Register with the global registry
82 full_path, widget_type, imgui_id, description, metadata);
83}
84
85} // namespace gui
86} // namespace yaze
87
AutoWidgetScope(const std::string &name)
static std::string NormalizeLabel(absl::string_view label)
void RegisterWidget(const std::string &full_path, const std::string &type, ImGuiID imgui_id, const std::string &description="", const WidgetMetadata &metadata=WidgetMetadata())
static WidgetIdRegistry & Instance()
thread_local std::vector< std::string > g_auto_scope_stack_
void AutoRegisterLastItem(const std::string &widget_type, const std::string &explicit_label, const std::string &description)
Automatically register the last ImGui item.
Main namespace for the application.
Automatic widget registration helpers for ImGui Test Engine integration.