yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
widget_measurement.cc
Go to the documentation of this file.
2
3#include "absl/strings/str_format.h"
4#include "imgui/imgui.h"
5#include "imgui/imgui_internal.h"
6
7namespace yaze {
8namespace gui {
9
11 const std::string& type) {
12 if (!enabled_) {
13 return WidgetMetrics{};
14 }
15
16 WidgetMetrics metrics;
17 metrics.widget_id = widget_id;
18 metrics.type = type;
19
20 // Get item rect (bounding box)
21 metrics.rect_min = ImGui::GetItemRectMin();
22 metrics.rect_max = ImGui::GetItemRectMax();
23
24 // Calculate size
25 metrics.size = ImVec2(metrics.rect_max.x - metrics.rect_min.x,
26 metrics.rect_max.y - metrics.rect_min.y);
27
28 // Store position
29 metrics.position = metrics.rect_min;
30
31 // Get content region
32 metrics.content_size = ImGui::GetContentRegionAvail();
33
34 // Get cursor position after item
35 metrics.cursor_pos_x = ImGui::GetCursorPosX();
36
37 // Store in current toolbar if active
38 if (!current_toolbar_id_.empty()) {
39 toolbar_metrics_[current_toolbar_id_].push_back(metrics);
40 }
41
42 // Store in frame metrics
43 frame_metrics_.push_back(metrics);
44
45 return metrics;
46}
47
48void WidgetMeasurement::BeginToolbarMeasurement(const std::string& toolbar_id) {
49 current_toolbar_id_ = toolbar_id;
50 current_toolbar_start_x_ = ImGui::GetCursorPosX();
52
53 // Clear previous measurements for this toolbar
54 toolbar_metrics_[toolbar_id].clear();
55}
56
58 if (current_toolbar_id_.empty()) return;
59
60 // Calculate total width from cursor movement
61 float end_x = ImGui::GetCursorPosX();
63
64 // Store the width
66
67 // Reset state
68 current_toolbar_id_.clear();
71}
72
73float WidgetMeasurement::GetToolbarWidth(const std::string& toolbar_id) const {
74 auto it = toolbar_widths_.find(toolbar_id);
75 if (it != toolbar_widths_.end()) {
76 return it->second;
77 }
78 return 0.0f;
79}
80
81bool WidgetMeasurement::WouldToolbarOverflow(const std::string& toolbar_id,
82 float available_width) const {
83 float toolbar_width = GetToolbarWidth(toolbar_id);
84 return toolbar_width > available_width;
85}
86
87const std::vector<WidgetMetrics>& WidgetMeasurement::GetToolbarMetrics(
88 const std::string& toolbar_id) const {
89 static const std::vector<WidgetMetrics> empty;
90 auto it = toolbar_metrics_.find(toolbar_id);
91 if (it != toolbar_metrics_.end()) {
92 return it->second;
93 }
94 return empty;
95}
96
98 frame_metrics_.clear();
99 // Don't clear toolbar metrics - they persist across frames for debugging
100}
101
103 std::string json = "{\n \"toolbars\": {\n";
104
105 bool first_toolbar = true;
106 for (const auto& [toolbar_id, metrics] : toolbar_metrics_) {
107 if (!first_toolbar) json += ",\n";
108 first_toolbar = false;
109
110 json += absl::StrFormat(" \"%s\": {\n", toolbar_id);
111 json += absl::StrFormat(" \"total_width\": %.1f,\n",
112 GetToolbarWidth(toolbar_id));
113 json += " \"widgets\": [\n";
114
115 bool first_widget = true;
116 for (const auto& metric : metrics) {
117 if (!first_widget) json += ",\n";
118 first_widget = false;
119
120 json += " {\n";
121 json += absl::StrFormat(" \"id\": \"%s\",\n", metric.widget_id);
122 json += absl::StrFormat(" \"type\": \"%s\",\n", metric.type);
123 json += absl::StrFormat(" \"width\": %.1f,\n", metric.size.x);
124 json += absl::StrFormat(" \"height\": %.1f,\n", metric.size.y);
125 json += absl::StrFormat(" \"x\": %.1f,\n", metric.position.x);
126 json += absl::StrFormat(" \"y\": %.1f\n", metric.position.y);
127 json += " }";
128 }
129
130 json += "\n ]\n";
131 json += " }";
132 }
133
134 json += "\n }\n}\n";
135 return json;
136}
137
138} // namespace gui
139} // namespace yaze
140
bool WouldToolbarOverflow(const std::string &toolbar_id, float available_width) const
Check if toolbar would overflow given window width.
float GetToolbarWidth(const std::string &toolbar_id) const
Get total measured width of a toolbar.
std::unordered_map< std::string, std::vector< WidgetMetrics > > toolbar_metrics_
std::vector< WidgetMetrics > frame_metrics_
void EndToolbarMeasurement()
End measuring a toolbar section and store total width.
std::unordered_map< std::string, float > toolbar_widths_
const std::vector< WidgetMetrics > & GetToolbarMetrics(const std::string &toolbar_id) const
Get all measurements for a specific toolbar.
WidgetMetrics MeasureLastItem(const std::string &widget_id, const std::string &type="unknown")
Measure the last rendered ImGui item.
void BeginToolbarMeasurement(const std::string &toolbar_id)
Begin measuring a toolbar section.
std::string ExportMetricsJSON() const
Export measurements for test automation.
void ClearFrame()
Clear all measurements (call once per frame)
Main namespace for the application.
Definition controller.cc:20