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// imgui_internal.h ships with ImGui, not the test engine, and
5// widget_id_registry.cc in this library includes it in every build. This
6// library never gets YAZE_ENABLE_IMGUI_TEST_ENGINE, so gating the capture on
7// that macro made it return a stub in every build.
8#include <string>
9#include "imgui/imgui.h"
10#include "imgui/imgui_internal.h"
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 // Check if ImGui context is available
95 ImGuiContext* ctx = ImGui::GetCurrentContext();
96 if (!ctx) {
97 return R"({"error": "ImGui context not available"})";
98 }
99
100 ImGuiIO& io = ImGui::GetIO();
101
102 // Capture frame information
103 state.frame_count = ImGui::GetFrameCount();
104 state.frame_rate = io.Framerate;
105
106 // Capture focused window. NavWindow is the window ImGui treats as focused
107 // (IsWindowFocused() compares against it). CurrentWindow is only the window
108 // being submitted when the capture runs, and is null between frames.
109 // NavWindow is null when no window has focus; focused_window stays "".
110 if (ctx->NavWindow != nullptr) {
111 state.focused_window = ctx->NavWindow->Name;
112 }
113
114 // Capture active widget (focused for input)
115 ImGuiID active_id = ImGui::GetActiveID();
116 if (active_id != 0) {
117 state.focused_widget = absl::StrFormat("0x%08X", active_id);
118 }
119
120 // Capture hovered widget
121 ImGuiID hovered_id = ImGui::GetHoveredID();
122 if (hovered_id != 0) {
123 state.hovered_widget = absl::StrFormat("0x%08X", hovered_id);
124 }
125
126 // Traverse visible windows
127 for (ImGuiWindow* window : ctx->Windows) {
128 if (window && window->Active && !window->Hidden) {
129 state.visible_windows.push_back(window->Name);
130 }
131 }
132
133 // Capture open popups
134 for (int i = 0; i < ctx->OpenPopupStack.Size; i++) {
135 ImGuiPopupData& popup = ctx->OpenPopupStack[i];
136 if (popup.Window && !popup.Window->Hidden) {
137 state.open_popups.push_back(popup.Window->Name);
138 }
139 }
140
141 // Capture navigation state
142 state.nav_id = ctx->NavId;
143 state.nav_active = ctx->NavWindow != nullptr;
144
145 // Capture mouse state
146 for (int i = 0; i < 5; i++) {
147 state.mouse_down[i] = io.MouseDown[i];
148 }
149 state.mouse_pos_x = io.MousePos.x;
150 state.mouse_pos_y = io.MousePos.y;
151
152 // Capture keyboard modifiers
153 state.ctrl_pressed = io.KeyCtrl;
154 state.shift_pressed = io.KeyShift;
155 state.alt_pressed = io.KeyAlt;
156
157 return SerializeWidgetStateToJson(state);
158}
159
160std::string SerializeWidgetStateToJson(const WidgetState& state) {
161#if defined(YAZE_WITH_JSON)
162 nlohmann::json j;
163
164 j["frame_count"] = state.frame_count;
165 j["frame_rate"] = state.frame_rate;
166 j["focused_window"] = state.focused_window;
167 j["focused_widget"] = state.focused_widget;
168 j["hovered_widget"] = state.hovered_widget;
169 j["visible_windows"] = state.visible_windows;
170 j["open_popups"] = state.open_popups;
171 j["navigation"] = {{"nav_id", absl::StrFormat("0x%08X", state.nav_id)},
172 {"nav_active", state.nav_active}};
173
174 nlohmann::json mouse_buttons;
175 for (int i = 0; i < 5; ++i) {
176 mouse_buttons.push_back(state.mouse_down[i]);
177 }
178
179 j["input"] = {{"mouse_buttons", mouse_buttons},
180 {"mouse_pos", {state.mouse_pos_x, state.mouse_pos_y}},
181 {"modifiers",
182 {{"ctrl", state.ctrl_pressed},
183 {"shift", state.shift_pressed},
184 {"alt", state.alt_pressed}}}};
185
186 return j.dump(2);
187#else
188 std::string json;
189 json.reserve(512);
190
191 json.append("{\n");
192 json.append(" \"frame_count\": ");
193 json.append(std::to_string(state.frame_count));
194 json.append(",\n");
195
196 json.append(" \"frame_rate\": ");
197 json.append(FormatFloatCompact(state.frame_rate));
198 json.append(",\n");
199
200 json.append(" \"focused_window\": ");
201 json.append(EscapeJsonString(state.focused_window));
202 json.append(",\n");
203
204 json.append(" \"focused_widget\": ");
205 json.append(EscapeJsonString(state.focused_widget));
206 json.append(",\n");
207
208 json.append(" \"hovered_widget\": ");
209 json.append(EscapeJsonString(state.hovered_widget));
210 json.append(",\n");
211
212 json.append(" \"visible_windows\": [");
213 for (size_t i = 0; i < state.visible_windows.size(); ++i) {
214 if (i > 0) {
215 json.append(", ");
216 }
217 json.append(EscapeJsonString(state.visible_windows[i]));
218 }
219 json.append("],\n");
220
221 json.append(" \"open_popups\": [");
222 for (size_t i = 0; i < state.open_popups.size(); ++i) {
223 if (i > 0) {
224 json.append(", ");
225 }
226 json.append(EscapeJsonString(state.open_popups[i]));
227 }
228 json.append("],\n");
229
230 json.append(" \"navigation\": {\n");
231 json.append(" \"nav_id\": ");
232 json.append(EscapeJsonString(absl::StrFormat("0x%08X", state.nav_id)));
233 json.append(",\n");
234 json.append(" \"nav_active\": ");
235 json.append(BoolToJson(state.nav_active));
236 json.append("\n },\n");
237
238 json.append(" \"input\": {\n");
239 json.append(" \"mouse_buttons\": [");
240 for (int i = 0; i < 5; ++i) {
241 if (i > 0) {
242 json.append(", ");
243 }
244 json.append(BoolToJson(state.mouse_down[i]));
245 }
246 json.append("],\n");
247
248 json.append(" \"mouse_pos\": [");
249 json.append(FormatFloatCompact(state.mouse_pos_x));
250 json.append(", ");
251 json.append(FormatFloatCompact(state.mouse_pos_y));
252 json.append("],\n");
253
254 json.append(" \"modifiers\": {\n");
255 json.append(" \"ctrl\": ");
256 json.append(BoolToJson(state.ctrl_pressed));
257 json.append(",\n");
258 json.append(" \"shift\": ");
259 json.append(BoolToJson(state.shift_pressed));
260 json.append(",\n");
261 json.append(" \"alt\": ");
262 json.append(BoolToJson(state.alt_pressed));
263 json.append("\n }\n");
264 json.append(" }\n");
265 json.append("}\n");
266
267 return json;
268#endif // defined(YAZE_WITH_JSON)
269}
270
271} // namespace core
272} // namespace yaze
nlohmann::json json
std::string SerializeWidgetStateToJson(const WidgetState &state)
std::string CaptureWidgetState()
std::vector< std::string > visible_windows
std::vector< std::string > open_popups