yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
widget_state_capture.cc
Go to the documentation of this file.
2
3#include "absl/strings/str_format.h"
4#if defined(YAZE_ENABLE_IMGUI_TEST_ENGINE) && YAZE_ENABLE_IMGUI_TEST_ENGINE
5#include "imgui.h"
6#include "imgui_internal.h"
7#else
8#include "imgui/imgui.h"
9#endif
10#include <string>
11
12#if defined(YAZE_WITH_JSON)
13#include "nlohmann/json.hpp"
14#endif
15
16namespace yaze {
17namespace core {
18
19#if !defined(YAZE_WITH_JSON)
20namespace {
21
22std::string EscapeJsonString(const std::string& value) {
23 std::string escaped;
24 escaped.reserve(value.size() + 2);
25 escaped.push_back('"');
26
27 for (unsigned char c : value) {
28 switch (c) {
29 case '"':
30 escaped.append("\\\"");
31 break;
32 case '\\':
33 escaped.append("\\\\");
34 break;
35 case '\b':
36 escaped.append("\\b");
37 break;
38 case '\f':
39 escaped.append("\\f");
40 break;
41 case '\n':
42 escaped.append("\\n");
43 break;
44 case '\r':
45 escaped.append("\\r");
46 break;
47 case '\t':
48 escaped.append("\\t");
49 break;
50 default:
51 if (c <= 0x1F) {
52 escaped.append(absl::StrFormat("\\\\u%04X", static_cast<int>(c)));
53 } else {
54 escaped.push_back(static_cast<char>(c));
55 }
56 break;
57 }
58 }
59
60 escaped.push_back('"');
61 return escaped;
62}
63
64const char* BoolToJson(bool value) {
65 return value ? "true" : "false";
66}
67
68std::string FormatFloat(float value) {
69 // Match typical JSON formatting without trailing zeros when possible.
70 return absl::StrFormat("%.4f", value);
71}
72
73std::string FormatFloatCompact(float value) {
74 std::string formatted = FormatFloat(value);
75
76 // Trim trailing zeros while keeping at least one decimal place.
77 if (formatted.find('.') != std::string::npos) {
78 while (!formatted.empty() && formatted.back() == '0') {
79 formatted.pop_back();
80 }
81 if (!formatted.empty() && formatted.back() == '.') {
82 formatted.push_back('0');
83 }
84 }
85 return formatted;
86}
87
88} // namespace
89#endif // !defined(YAZE_WITH_JSON)
90
91std::string CaptureWidgetState() {
92 WidgetState state;
93
94#if defined(YAZE_ENABLE_IMGUI_TEST_ENGINE) && YAZE_ENABLE_IMGUI_TEST_ENGINE
95 // Check if ImGui context is available
96 ImGuiContext* ctx = ImGui::GetCurrentContext();
97 if (!ctx) {
98 return R"({"error": "ImGui context not available"})";
99 }
100
101 ImGuiIO& io = ImGui::GetIO();
102
103 // Capture frame information
104 state.frame_count = ImGui::GetFrameCount();
105 state.frame_rate = io.Framerate;
106
107 // Capture focused window
108 ImGuiWindow* current = ImGui::GetCurrentWindow();
109 if (current && !current->Hidden) {
110 state.focused_window = current->Name;
111 }
112
113 // Capture active widget (focused for input)
114 ImGuiID active_id = ImGui::GetActiveID();
115 if (active_id != 0) {
116 state.focused_widget = absl::StrFormat("0x%08X", active_id);
117 }
118
119 // Capture hovered widget
120 ImGuiID hovered_id = ImGui::GetHoveredID();
121 if (hovered_id != 0) {
122 state.hovered_widget = absl::StrFormat("0x%08X", hovered_id);
123 }
124
125 // Traverse visible windows
126 for (ImGuiWindow* window : ctx->Windows) {
127 if (window && window->Active && !window->Hidden) {
128 state.visible_windows.push_back(window->Name);
129 }
130 }
131
132 // Capture open popups
133 for (int i = 0; i < ctx->OpenPopupStack.Size; i++) {
134 ImGuiPopupData& popup = ctx->OpenPopupStack[i];
135 if (popup.Window && !popup.Window->Hidden) {
136 state.open_popups.push_back(popup.Window->Name);
137 }
138 }
139
140 // Capture navigation state
141 state.nav_id = ctx->NavId;
142 state.nav_active = ctx->NavWindow != nullptr;
143
144 // Capture mouse state
145 for (int i = 0; i < 5; i++) {
146 state.mouse_down[i] = io.MouseDown[i];
147 }
148 state.mouse_pos_x = io.MousePos.x;
149 state.mouse_pos_y = io.MousePos.y;
150
151 // Capture keyboard modifiers
152 state.ctrl_pressed = io.KeyCtrl;
153 state.shift_pressed = io.KeyShift;
154 state.alt_pressed = io.KeyAlt;
155
156#else
157 // When UI test engine / ImGui internals aren't available, provide a minimal
158 // payload so downstream systems still receive structured JSON. This keeps
159 // builds that exclude the UI test engine (e.g., Windows release) working.
160 return "{\"warning\": \"Widget state capture unavailable (UI test engine "
161 "disabled)\"}";
162#endif
163
164 return SerializeWidgetStateToJson(state);
165}
166
167std::string SerializeWidgetStateToJson(const WidgetState& state) {
168#if defined(YAZE_WITH_JSON)
169 nlohmann::json j;
170
171 j["frame_count"] = state.frame_count;
172 j["frame_rate"] = state.frame_rate;
173 j["focused_window"] = state.focused_window;
174 j["focused_widget"] = state.focused_widget;
175 j["hovered_widget"] = state.hovered_widget;
176 j["visible_windows"] = state.visible_windows;
177 j["open_popups"] = state.open_popups;
178 j["navigation"] = {{"nav_id", absl::StrFormat("0x%08X", state.nav_id)},
179 {"nav_active", state.nav_active}};
180
181 nlohmann::json mouse_buttons;
182 for (int i = 0; i < 5; ++i) {
183 mouse_buttons.push_back(state.mouse_down[i]);
184 }
185
186 j["input"] = {{"mouse_buttons", mouse_buttons},
187 {"mouse_pos", {state.mouse_pos_x, state.mouse_pos_y}},
188 {"modifiers",
189 {{"ctrl", state.ctrl_pressed},
190 {"shift", state.shift_pressed},
191 {"alt", state.alt_pressed}}}};
192
193 return j.dump(2);
194#else
195 std::string json;
196 json.reserve(512);
197
198 json.append("{\n");
199 json.append(" \"frame_count\": ");
200 json.append(std::to_string(state.frame_count));
201 json.append(",\n");
202
203 json.append(" \"frame_rate\": ");
204 json.append(FormatFloatCompact(state.frame_rate));
205 json.append(",\n");
206
207 json.append(" \"focused_window\": ");
208 json.append(EscapeJsonString(state.focused_window));
209 json.append(",\n");
210
211 json.append(" \"focused_widget\": ");
212 json.append(EscapeJsonString(state.focused_widget));
213 json.append(",\n");
214
215 json.append(" \"hovered_widget\": ");
216 json.append(EscapeJsonString(state.hovered_widget));
217 json.append(",\n");
218
219 json.append(" \"visible_windows\": [");
220 for (size_t i = 0; i < state.visible_windows.size(); ++i) {
221 if (i > 0) {
222 json.append(", ");
223 }
224 json.append(EscapeJsonString(state.visible_windows[i]));
225 }
226 json.append("],\n");
227
228 json.append(" \"open_popups\": [");
229 for (size_t i = 0; i < state.open_popups.size(); ++i) {
230 if (i > 0) {
231 json.append(", ");
232 }
233 json.append(EscapeJsonString(state.open_popups[i]));
234 }
235 json.append("],\n");
236
237 json.append(" \"navigation\": {\n");
238 json.append(" \"nav_id\": ");
239 json.append(EscapeJsonString(absl::StrFormat("0x%08X", state.nav_id)));
240 json.append(",\n");
241 json.append(" \"nav_active\": ");
242 json.append(BoolToJson(state.nav_active));
243 json.append("\n },\n");
244
245 json.append(" \"input\": {\n");
246 json.append(" \"mouse_buttons\": [");
247 for (int i = 0; i < 5; ++i) {
248 if (i > 0) {
249 json.append(", ");
250 }
251 json.append(BoolToJson(state.mouse_down[i]));
252 }
253 json.append("],\n");
254
255 json.append(" \"mouse_pos\": [");
256 json.append(FormatFloatCompact(state.mouse_pos_x));
257 json.append(", ");
258 json.append(FormatFloatCompact(state.mouse_pos_y));
259 json.append("],\n");
260
261 json.append(" \"modifiers\": {\n");
262 json.append(" \"ctrl\": ");
263 json.append(BoolToJson(state.ctrl_pressed));
264 json.append(",\n");
265 json.append(" \"shift\": ");
266 json.append(BoolToJson(state.shift_pressed));
267 json.append(",\n");
268 json.append(" \"alt\": ");
269 json.append(BoolToJson(state.alt_pressed));
270 json.append("\n }\n");
271 json.append(" }\n");
272 json.append("}\n");
273
274 return json;
275#endif // defined(YAZE_WITH_JSON)
276}
277
278} // namespace core
279} // namespace yaze
std::string SerializeWidgetStateToJson(const WidgetState &state)
std::string CaptureWidgetState()
std::vector< std::string > visible_windows
std::vector< std::string > open_popups