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())
59 return;
60
61 // Calculate total width from cursor movement
62 float end_x = ImGui::GetCursorPosX();
64
65 // Store the width
67
68 // Reset state
69 current_toolbar_id_.clear();
72}
73
74float WidgetMeasurement::GetToolbarWidth(const std::string& toolbar_id) const {
75 auto it = toolbar_widths_.find(toolbar_id);
76 if (it != toolbar_widths_.end()) {
77 return it->second;
78 }
79 return 0.0f;
80}
81
82bool WidgetMeasurement::WouldToolbarOverflow(const std::string& toolbar_id,
83 float available_width) const {
84 float toolbar_width = GetToolbarWidth(toolbar_id);
85 return toolbar_width > available_width;
86}
87
88const std::vector<WidgetMetrics>& WidgetMeasurement::GetToolbarMetrics(
89 const std::string& toolbar_id) const {
90 static const std::vector<WidgetMetrics> empty;
91 auto it = toolbar_metrics_.find(toolbar_id);
92 if (it != toolbar_metrics_.end()) {
93 return it->second;
94 }
95 return empty;
96}
97
99 frame_metrics_.clear();
100 // Don't clear toolbar metrics - they persist across frames for debugging
101}
102
104 std::string json = "{\n \"toolbars\": {\n";
105
106 bool first_toolbar = true;
107 for (const auto& [toolbar_id, metrics] : toolbar_metrics_) {
108 if (!first_toolbar)
109 json += ",\n";
110 first_toolbar = false;
111
112 json += absl::StrFormat(" \"%s\": {\n", toolbar_id);
113 json += absl::StrFormat(" \"total_width\": %.1f,\n",
114 GetToolbarWidth(toolbar_id));
115 json += " \"widgets\": [\n";
116
117 bool first_widget = true;
118 for (const auto& metric : metrics) {
119 if (!first_widget)
120 json += ",\n";
121 first_widget = false;
122
123 json += " {\n";
124 json += absl::StrFormat(" \"id\": \"%s\",\n", metric.widget_id);
125 json += absl::StrFormat(" \"type\": \"%s\",\n", metric.type);
126 json += absl::StrFormat(" \"width\": %.1f,\n", metric.size.x);
127 json += absl::StrFormat(" \"height\": %.1f,\n", metric.size.y);
128 json += absl::StrFormat(" \"x\": %.1f,\n", metric.position.x);
129 json += absl::StrFormat(" \"y\": %.1f\n", metric.position.y);
130 json += " }";
131 }
132
133 json += "\n ]\n";
134 json += " }";
135 }
136
137 json += "\n }\n}\n";
138 return json;
139}
140
141} // namespace gui
142} // namespace yaze
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)