yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
widget_id_registry.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <chrono>
5#include <cstdio>
6#include <ctime>
7#include <fstream>
8#include <sstream>
9
10#include "absl/strings/ascii.h"
11#include "absl/strings/str_cat.h"
12#include "absl/strings/str_format.h"
13#include "absl/strings/str_join.h"
14#include "absl/strings/str_split.h"
15#include "absl/strings/strip.h"
16#include "absl/time/clock.h"
17#include "imgui/imgui_internal.h" // For ImGuiContext internals
18
19namespace yaze {
20namespace gui {
21
22// Thread-local storage for ID stack
23thread_local std::vector<std::string> WidgetIdScope::id_stack_;
24
25WidgetIdScope::WidgetIdScope(const std::string& name) : name_(name) {
26 // Only push ID if we're in an active ImGui frame with a valid window
27 // This prevents crashes during editor initialization before ImGui begins its frame
28 ImGuiContext* ctx = ImGui::GetCurrentContext();
29 if (ctx && ctx->CurrentWindow && !ctx->Windows.empty()) {
30 ImGui::PushID(name.c_str());
31 id_stack_.push_back(name);
32 }
33}
34
36 // Only pop if we successfully pushed
37 if (!id_stack_.empty() && id_stack_.back() == name_) {
38 ImGui::PopID();
39 id_stack_.pop_back();
40 }
41}
42
43std::string WidgetIdScope::GetFullPath() const {
44 return absl::StrJoin(id_stack_, "/");
45}
46
47std::string WidgetIdScope::GetWidgetPath(const std::string& widget_type,
48 const std::string& widget_name) const {
49 std::string path = GetFullPath();
50 if (!path.empty()) {
51 path += "/";
52 }
53 return absl::StrCat(path, widget_type, ":", widget_name);
54}
55
56// WidgetIdRegistry implementation
57
59 static WidgetIdRegistry instance;
60 return instance;
61}
62
64 ImGuiContext* ctx = ImGui::GetCurrentContext();
65 if (ctx) {
66 current_frame_ = ctx->FrameCount;
67 } else if (current_frame_ >= 0) {
69 } else {
71 }
72 frame_time_ = absl::Now();
73 for (auto& [_, info] : widgets_) {
74 info.seen_in_current_frame = false;
75 }
76}
77
79 for (auto& [_, info] : widgets_) {
80 if (!info.seen_in_current_frame) {
81 info.visible = false;
82 info.enabled = false;
83 info.bounds.valid = false;
84 info.stale_frame_count += 1;
85 } else {
86 info.seen_in_current_frame = false;
87 info.stale_frame_count = 0;
88 }
89 }
91}
92
93namespace {
94
95std::string ExtractWindowFromPath(absl::string_view path) {
96 size_t slash = path.find('/');
97 if (slash == absl::string_view::npos) {
98 return std::string(path);
99 }
100 return std::string(path.substr(0, slash));
101}
102
103std::string ExtractLabelFromPath(absl::string_view path) {
104 size_t colon = path.rfind(':');
105 if (colon == absl::string_view::npos) {
106 size_t slash = path.rfind('/');
107 if (slash == absl::string_view::npos) {
108 return std::string(path);
109 }
110 return std::string(path.substr(slash + 1));
111 }
112 return std::string(path.substr(colon + 1));
113}
114
115std::string FormatTimestampUTC(const absl::Time& timestamp) {
116 if (timestamp == absl::Time()) {
117 return "";
118 }
119
120 std::chrono::system_clock::time_point chrono_time =
121 absl::ToChronoTime(timestamp);
122 std::time_t time_value = std::chrono::system_clock::to_time_t(chrono_time);
123
124 std::tm tm_buffer;
125#if defined(_WIN32)
126 if (gmtime_s(&tm_buffer, &time_value) != 0) {
127 return "";
128 }
129#else
130 if (gmtime_r(&time_value, &tm_buffer) == nullptr) {
131 return "";
132 }
133#endif
134
135 char buffer[32];
136 if (std::snprintf(buffer, sizeof(buffer), "%04d-%02d-%02dT%02d:%02d:%02dZ",
137 tm_buffer.tm_year + 1900, tm_buffer.tm_mon + 1,
138 tm_buffer.tm_mday, tm_buffer.tm_hour, tm_buffer.tm_min,
139 tm_buffer.tm_sec) <= 0) {
140 return "";
141 }
142
143 return std::string(buffer);
144}
145
148 bounds.min_x = rect.Min.x;
149 bounds.min_y = rect.Min.y;
150 bounds.max_x = rect.Max.x;
151 bounds.max_y = rect.Max.y;
152 bounds.valid = true;
153 return bounds;
154}
155
156} // namespace
157
158void WidgetIdRegistry::RegisterWidget(const std::string& full_path,
159 const std::string& type,
160 ImGuiID imgui_id,
161 const std::string& description,
162 const WidgetMetadata& metadata) {
163 WidgetInfo& info = widgets_[full_path];
164 info.full_path = full_path;
165 info.type = type;
166 info.imgui_id = imgui_id;
167 info.description = description;
168
169 if (metadata.label.has_value()) {
170 info.label = NormalizeLabel(*metadata.label);
171 } else {
172 info.label = NormalizeLabel(ExtractLabelFromPath(full_path));
173 }
174 if (info.label.empty()) {
175 info.label = ExtractLabelFromPath(full_path);
176 }
177
178 if (metadata.window_name.has_value()) {
179 info.window_name = NormalizeLabel(*metadata.window_name);
180 } else {
181 info.window_name = NormalizePathSegment(ExtractWindowFromPath(full_path));
182 }
183 if (info.window_name.empty()) {
184 info.window_name = ExtractWindowFromPath(full_path);
185 }
186
187 ImGuiContext* ctx = ImGui::GetCurrentContext();
188 absl::Time observed_at = absl::Now();
189
190 if (ctx) {
191 const ImGuiLastItemData& last = ctx->LastItemData;
192 if (metadata.visible.has_value()) {
193 info.visible = *metadata.visible;
194 } else {
195 info.visible = (last.StatusFlags & ImGuiItemStatusFlags_Visible) != 0;
196 }
197
198 if (metadata.enabled.has_value()) {
199 info.enabled = *metadata.enabled;
200 } else {
201 info.enabled = (last.ItemFlags & ImGuiItemFlags_Disabled) == 0;
202 }
203
204 if (metadata.bounds.has_value()) {
205 info.bounds = *metadata.bounds;
206 } else {
207 info.bounds = BoundsFromImGui(last.Rect);
208 }
209
210 info.last_seen_frame = ctx->FrameCount;
211 } else {
212 info.visible = metadata.visible.value_or(true);
213 info.enabled = metadata.enabled.value_or(true);
214 if (metadata.bounds.has_value()) {
215 info.bounds = *metadata.bounds;
216 } else {
217 info.bounds.valid = false;
218 }
219 if (current_frame_ >= 0) {
221 }
222 }
223
224 info.last_seen_time = observed_at;
225 info.seen_in_current_frame = true;
226 info.stale_frame_count = 0;
227}
228
229std::vector<std::string> WidgetIdRegistry::FindWidgets(
230 const std::string& pattern) const {
231 std::vector<std::string> matches;
232
233 // Simple glob-style pattern matching
234 // Supports: "*" (any), "?" (single char), exact matches
235 for (const auto& [path, info] : widgets_) {
236 bool match = false;
237
238 if (pattern == "*") {
239 match = true;
240 } else if (pattern.find('*') != std::string::npos) {
241 // Wildcard pattern - convert to simple substring match for now
242 std::string search = pattern;
243 search.erase(std::remove(search.begin(), search.end(), '*'), search.end());
244 if (!search.empty() && path.find(search) != std::string::npos) {
245 match = true;
246 }
247 } else {
248 // Exact match
249 if (path == pattern) {
250 match = true;
251 }
252 }
253
254 if (match) {
255 matches.push_back(path);
256 }
257 }
258
259 // Sort for consistent ordering
260 std::sort(matches.begin(), matches.end());
261 return matches;
262}
263
264ImGuiID WidgetIdRegistry::GetWidgetId(const std::string& full_path) const {
265 auto it = widgets_.find(full_path);
266 if (it != widgets_.end()) {
267 return it->second.imgui_id;
268 }
269 return 0;
270}
271
273 const std::string& full_path) const {
274 auto it = widgets_.find(full_path);
275 if (it != widgets_.end()) {
276 return &it->second;
277 }
278 return nullptr;
279}
280
282 widgets_.clear();
283 current_frame_ = -1;
284}
285
286std::string WidgetIdRegistry::ExportCatalog(const std::string& format) const {
287 std::ostringstream ss;
288
289 if (format == "json") {
290 ss << "{\n";
291 ss << " \"widgets\": [\n";
292
293 bool first = true;
294 for (const auto& [path, info] : widgets_) {
295 if (!first) ss << ",\n";
296 first = false;
297
298 ss << " {\n";
299 ss << absl::StrFormat(" \"path\": \"%s\",\n", path);
300 ss << absl::StrFormat(" \"type\": \"%s\",\n", info.type);
301 ss << absl::StrFormat(" \"imgui_id\": %u,\n", info.imgui_id);
302 ss << absl::StrFormat(" \"label\": \"%s\",\n", info.label);
303 ss << absl::StrFormat(" \"window\": \"%s\",\n", info.window_name);
304 ss << absl::StrFormat(" \"visible\": %s,\n",
305 info.visible ? "true" : "false");
306 ss << absl::StrFormat(" \"enabled\": %s,\n",
307 info.enabled ? "true" : "false");
308 if (info.bounds.valid) {
309 ss << absl::StrFormat(
310 " \"bounds\": {\"min\": [%0.1f, %0.1f], \"max\": [%0.1f, %0.1f]},\n",
311 info.bounds.min_x, info.bounds.min_y, info.bounds.max_x,
312 info.bounds.max_y);
313 } else {
314 ss << " \"bounds\": null,\n";
315 }
316 ss << absl::StrFormat(" \"last_seen_frame\": %d,\n",
317 info.last_seen_frame);
318 std::string iso_timestamp = FormatTimestampUTC(info.last_seen_time);
319 ss << absl::StrFormat(" \"last_seen_at\": \"%s\",\n",
320 iso_timestamp);
321 ss << absl::StrFormat(" \"stale\": %s",
322 info.stale_frame_count > 0 ? "true" : "false");
323 if (!info.description.empty()) {
324 ss << ",\n";
325 ss << absl::StrFormat(" \"description\": \"%s\"\n",
326 info.description);
327 } else {
328 ss << "\n";
329 }
330 ss << " }";
331 }
332
333 ss << "\n ]\n";
334 ss << "}\n";
335 } else {
336 // YAML format (default)
337 ss << "widgets:\n";
338
339 for (const auto& [path, info] : widgets_) {
340 ss << absl::StrFormat(" - path: \"%s\"\n", path);
341 ss << absl::StrFormat(" type: %s\n", info.type);
342 ss << absl::StrFormat(" imgui_id: %u\n", info.imgui_id);
343 ss << absl::StrFormat(" label: \"%s\"\n", info.label);
344 ss << absl::StrFormat(" window: \"%s\"\n", info.window_name);
345 ss << absl::StrFormat(" visible: %s\n", info.visible ? "true" : "false");
346 ss << absl::StrFormat(" enabled: %s\n", info.enabled ? "true" : "false");
347 if (info.bounds.valid) {
348 ss << " bounds:\n";
349 ss << absl::StrFormat(" min: [%0.1f, %0.1f]\n", info.bounds.min_x,
350 info.bounds.min_y);
351 ss << absl::StrFormat(" max: [%0.1f, %0.1f]\n", info.bounds.max_x,
352 info.bounds.max_y);
353 }
354 ss << absl::StrFormat(" last_seen_frame: %d\n", info.last_seen_frame);
355 std::string iso_timestamp = FormatTimestampUTC(info.last_seen_time);
356 ss << absl::StrFormat(" last_seen_at: %s\n", iso_timestamp);
357 ss << absl::StrFormat(" stale: %s\n",
358 info.stale_frame_count > 0 ? "true" : "false");
359
360 // Parse hierarchical context from path
361 std::vector<std::string> segments = absl::StrSplit(path, '/');
362 if (!segments.empty()) {
363 ss << " context:\n";
364 if (segments.size() > 0) {
365 ss << absl::StrFormat(" editor: %s\n", segments[0]);
366 }
367 if (segments.size() > 1) {
368 ss << absl::StrFormat(" tab: %s\n", segments[1]);
369 }
370 if (segments.size() > 2) {
371 ss << absl::StrFormat(" section: %s\n", segments[2]);
372 }
373 }
374
375 if (!info.description.empty()) {
376 ss << absl::StrFormat(" description: %s\n", info.description);
377 }
378
379 // Add suggested actions based on widget type
380 ss << " actions: [";
381 if (info.type == "button") {
382 ss << "click";
383 } else if (info.type == "input") {
384 ss << "type, clear";
385 } else if (info.type == "canvas") {
386 ss << "click, drag, scroll";
387 } else if (info.type == "checkbox") {
388 ss << "toggle";
389 } else if (info.type == "slider") {
390 ss << "drag, set";
391 } else {
392 ss << "interact";
393 }
394 ss << "]\n";
395 }
396 }
397
398 return ss.str();
399}
400
401void WidgetIdRegistry::ExportCatalogToFile(const std::string& output_file,
402 const std::string& format) const {
403 std::string content = ExportCatalog(format);
404 std::ofstream file(output_file);
405 if (file.is_open()) {
406 file << content;
407 file.close();
408 }
409}
410
411std::string WidgetIdRegistry::NormalizeLabel(absl::string_view label) {
412 size_t pos = label.find("##");
413 if (pos != absl::string_view::npos) {
414 label = label.substr(0, pos);
415 }
416 std::string sanitized = std::string(absl::StripAsciiWhitespace(label));
417 return sanitized;
418}
419
420std::string WidgetIdRegistry::NormalizePathSegment(absl::string_view segment) {
421 return NormalizeLabel(segment);
422}
423
425 auto it = widgets_.begin();
426 while (it != widgets_.end()) {
427 if (ShouldPrune(it->second)) {
428 it = widgets_.erase(it);
429 } else {
430 ++it;
431 }
432 }
433}
434
436 if (info.last_seen_frame < 0 || stale_frame_limit_ <= 0) {
437 return false;
438 }
439 if (current_frame_ < 0) {
440 return false;
441 }
443}
444
445} // namespace gui
446} // namespace yaze
Centralized registry for discoverable GUI widgets.
std::unordered_map< std::string, WidgetInfo > widgets_
void ExportCatalogToFile(const std::string &output_file, const std::string &format="yaml") const
static std::string NormalizePathSegment(absl::string_view segment)
std::string ExportCatalog(const std::string &format="yaml") const
bool ShouldPrune(const WidgetInfo &info) const
const WidgetInfo * GetWidgetInfo(const std::string &full_path) const
ImGuiID GetWidgetId(const std::string &full_path) const
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()
std::vector< std::string > FindWidgets(const std::string &pattern) const
WidgetIdScope(const std::string &name)
std::string GetFullPath() const
static thread_local std::vector< std::string > id_stack_
std::string GetWidgetPath(const std::string &widget_type, const std::string &widget_name) const
std::string ExtractWindowFromPath(absl::string_view path)
std::string ExtractLabelFromPath(absl::string_view path)
std::string FormatTimestampUTC(const absl::Time &timestamp)
WidgetIdRegistry::WidgetBounds BoundsFromImGui(const ImRect &rect)
Main namespace for the application.