yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
layout_helpers.cc
Go to the documentation of this file.
2
3#include <vector>
4
5#include "absl/strings/str_format.h"
9#include "imgui/imgui.h"
10#include "imgui/imgui_internal.h"
11
12namespace yaze {
13namespace gui {
14
15// Core sizing functions
17 const auto& theme = GetTheme();
18 return GetBaseFontSize() * theme.widget_height_multiplier *
19 theme.compact_factor;
20}
21
23 const auto& theme = GetTheme();
24 return GetBaseFontSize() * 0.5f * theme.spacing_multiplier *
25 theme.compact_factor;
26}
27
29 const auto& theme = GetTheme();
30 return GetBaseFontSize() * theme.toolbar_height_multiplier *
31 theme.compact_factor;
32}
33
35 const auto& theme = GetTheme();
36 return GetBaseFontSize() * 0.5f * theme.panel_padding_multiplier *
37 theme.compact_factor;
38}
39
41 const auto& theme = GetTheme();
42 return GetBaseFontSize() * 8.0f * theme.input_width_multiplier *
43 theme.compact_factor;
44}
45
47 const auto& theme = GetTheme();
48 return GetBaseFontSize() * 0.3f * theme.button_padding_multiplier *
49 theme.compact_factor;
50}
51
53 const auto& theme = GetTheme();
54 return GetBaseFontSize() * theme.table_row_height_multiplier *
55 theme.compact_factor;
56}
57
59 const auto& theme = GetTheme();
60 return GetBaseFontSize() * theme.canvas_toolbar_multiplier *
61 theme.compact_factor;
62}
63
64// Layout utilities
65void LayoutHelpers::BeginPaddedPanel(const char* label, float padding) {
66 if (padding < 0.0f) {
67 padding = GetPanelPadding();
68 }
69 ImGui::BeginChild(label, ImVec2(0, 0), true);
70 ImGui::Dummy(ImVec2(padding, padding));
71 ImGui::SameLine();
72 ImGui::BeginGroup();
73 ImGui::Dummy(ImVec2(0, padding));
74}
75
77 ImGui::Dummy(ImVec2(0, GetPanelPadding()));
78 ImGui::EndGroup();
79 ImGui::SameLine();
80 ImGui::Dummy(ImVec2(GetPanelPadding(), 0));
81 ImGui::EndChild();
82}
83
84bool LayoutHelpers::BeginTableWithTheming(const char* str_id, int columns,
85 ImGuiTableFlags flags,
86 const ImVec2& outer_size,
87 float inner_width) {
88 const auto& theme = GetTheme();
89
90 // Apply theme colors to table
91 ImGui::PushStyleColor(ImGuiCol_TableHeaderBg,
92 ConvertColorToImVec4(theme.table_header_bg));
93 ImGui::PushStyleColor(ImGuiCol_TableBorderStrong,
94 ConvertColorToImVec4(theme.table_border_strong));
95 ImGui::PushStyleColor(ImGuiCol_TableBorderLight,
96 ConvertColorToImVec4(theme.table_border_light));
97 ImGui::PushStyleColor(ImGuiCol_TableRowBg,
98 ConvertColorToImVec4(theme.table_row_bg));
99 ImGui::PushStyleColor(ImGuiCol_TableRowBgAlt,
100 ConvertColorToImVec4(theme.table_row_bg_alt));
101
102 // Set row height if not overridden by caller
103 if (!(flags & ImGuiTableFlags_NoHostExtendY)) {
104 ImGui::PushStyleVar(
105 ImGuiStyleVar_CellPadding,
106 ImVec2(ImGui::GetStyle().CellPadding.x, GetTableRowHeight() * 0.25f));
107 }
108
109 return ImGui::BeginTable(str_id, columns, flags, outer_size, inner_width);
110}
111
113 ImGui::EndTable();
114 // Pop style colors (5 colors pushed in BeginTableWithTheming)
115 ImGui::PopStyleColor(5);
116 // Pop style var if it was pushed (CellPadding)
117 ImGui::PopStyleVar(1);
118}
119
120void LayoutHelpers::BeginCanvasPanel(const char* label, ImVec2* canvas_size) {
121 const auto& theme = GetTheme();
122
123 // Apply theme to canvas container
124 ImGui::PushStyleColor(ImGuiCol_ChildBg,
125 ConvertColorToImVec4(theme.editor_background));
126 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
127
128 if (canvas_size) {
129 ImGui::BeginChild(label, *canvas_size, true);
130 } else {
131 ImGui::BeginChild(label, ImVec2(0, 0), true);
132 }
133}
134
136 ImGui::EndChild();
137 ImGui::PopStyleVar(1);
138 ImGui::PopStyleColor(1);
139}
140
141// Input field helpers
142bool LayoutHelpers::AutoSizedInputField(const char* label, char* buf,
143 size_t buf_size,
144 ImGuiInputTextFlags flags) {
145 ImGui::SetNextItemWidth(GetStandardInputWidth());
146 return ImGui::InputText(label, buf, buf_size, flags);
147}
148
149bool LayoutHelpers::AutoSizedInputInt(const char* label, int* v, int step,
150 int step_fast,
151 ImGuiInputTextFlags flags) {
152 ImGui::SetNextItemWidth(GetStandardInputWidth());
153 return ImGui::InputInt(label, v, step, step_fast, flags);
154}
155
156bool LayoutHelpers::AutoSizedInputFloat(const char* label, float* v, float step,
157 float step_fast, const char* format,
158 ImGuiInputTextFlags flags) {
159 ImGui::SetNextItemWidth(GetStandardInputWidth());
160 return ImGui::InputFloat(label, v, step, step_fast, format, flags);
161}
162
163// Input preset functions for common patterns
164bool LayoutHelpers::InputHexRow(const char* label, uint8_t* data) {
165 const auto& theme = GetTheme();
166 float input_width = GetStandardInputWidth() * 0.5f; // Hex inputs are smaller
167
168 ImGui::AlignTextToFramePadding();
169 ImGui::Text("%s", label);
170 ImGui::SameLine();
171
172 // Use theme-aware input width for hex byte (2 chars + controls)
173 ImGui::SetNextItemWidth(input_width);
174
175 char buf[8];
176 snprintf(buf, sizeof(buf), "%02X", *data);
177
178 bool changed = ImGui::InputText(
179 ("##" + std::string(label)).c_str(), buf, sizeof(buf),
180 ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_AutoSelectAll);
181
182 if (changed) {
183 unsigned int temp;
184 if (sscanf(buf, "%X", &temp) == 1) {
185 *data = static_cast<uint8_t>(temp & 0xFF);
186 }
187 }
188
189 return changed;
190}
191
192bool LayoutHelpers::InputHexRow(const char* label, uint16_t* data) {
193 const auto& theme = GetTheme();
194 float input_width =
195 GetStandardInputWidth() * 0.6f; // Hex word slightly wider
196
197 ImGui::AlignTextToFramePadding();
198 ImGui::Text("%s", label);
199 ImGui::SameLine();
200
201 // Use theme-aware input width for hex word (4 chars + controls)
202 ImGui::SetNextItemWidth(input_width);
203
204 char buf[8];
205 snprintf(buf, sizeof(buf), "%04X", *data);
206
207 bool changed = ImGui::InputText(
208 ("##" + std::string(label)).c_str(), buf, sizeof(buf),
209 ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_AutoSelectAll);
210
211 if (changed) {
212 unsigned int temp;
213 if (sscanf(buf, "%X", &temp) == 1) {
214 *data = static_cast<uint16_t>(temp & 0xFFFF);
215 }
216 }
217
218 return changed;
219}
220
221void LayoutHelpers::BeginPropertyGrid(const char* label) {
222 const auto& theme = GetTheme();
223
224 // Create a 2-column table for property editing
225 if (ImGui::BeginTable(label, 2,
226 ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) {
227 // Setup columns: label column (30%) and value column (70%)
228 ImGui::TableSetupColumn("Property", ImGuiTableColumnFlags_WidthFixed,
229 GetStandardInputWidth() * 1.5f);
230 ImGui::TableSetupColumn("Value", ImGuiTableColumnFlags_WidthStretch);
231 }
232}
233
235 ImGui::EndTable();
236}
237
238bool LayoutHelpers::InputToolbarField(const char* label, char* buf,
239 size_t buf_size) {
240 // Compact input field for toolbars
241 float compact_width =
243 ImGui::SetNextItemWidth(compact_width);
244
245 return ImGui::InputText(label, buf, buf_size,
246 ImGuiInputTextFlags_AutoSelectAll);
247}
248
249// Toolbar helpers
250void LayoutHelpers::BeginToolbar(const char* label) {
251 const auto& theme = GetTheme();
252 ImGui::PushStyleColor(ImGuiCol_ChildBg,
253 ConvertColorToImVec4(theme.menu_bar_bg));
254 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding,
256 ImGui::BeginChild(
257 label, ImVec2(0, GetToolbarHeight()), true,
258 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
259}
260
262 ImGui::EndChild();
263 ImGui::PopStyleVar(1);
264 ImGui::PopStyleColor(1);
265}
266
268 ImGui::SameLine();
269 ImGui::Dummy(ImVec2(GetStandardSpacing(), 0));
270 ImGui::SameLine();
271 ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
272 ImGui::SameLine();
273 ImGui::Dummy(ImVec2(GetStandardSpacing(), 0));
274 ImGui::SameLine();
275}
276
277bool LayoutHelpers::ToolbarButton(const char* label, const ImVec2& size) {
278 const auto& theme = GetTheme();
279 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding,
281 bool result = ImGui::Button(label, size);
282 ImGui::PopStyleVar(1);
283 return result;
284}
285
286// Common layout patterns
287void LayoutHelpers::PropertyRow(const char* label,
288 std::function<void()> widget_callback) {
289 ImGui::TableNextRow();
290 ImGui::TableSetColumnIndex(0);
291 ImGui::AlignTextToFramePadding();
292 ImGui::Text("%s", label);
293 ImGui::TableSetColumnIndex(1);
294 ImGui::SetNextItemWidth(-1);
295 widget_callback();
296}
297
298void LayoutHelpers::SectionHeader(const char* label) {
299 const auto& theme = GetTheme();
300 ImGui::PushStyleColor(ImGuiCol_Text, ConvertColorToImVec4(theme.accent));
301 ImGui::SeparatorText(label);
302 ImGui::PopStyleColor(1);
303}
304
305void LayoutHelpers::HelpMarker(const char* desc) {
306 ImGui::TextDisabled("(?)");
307 if (ImGui::BeginItemTooltip()) {
308 ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
309 ImGui::TextUnformatted(desc);
310 ImGui::PopTextWrapPos();
311 ImGui::EndTooltip();
312 }
313}
314
315} // namespace gui
316} // namespace yaze
static void SectionHeader(const char *label)
static float GetPanelPadding()
static void BeginToolbar(const char *label)
static float GetCanvasToolbarHeight()
static void EndTableWithTheming()
static void HelpMarker(const char *desc)
static float GetTableRowHeight()
static const Theme & GetTheme()
static float GetStandardInputWidth()
static void BeginPropertyGrid(const char *label)
static bool AutoSizedInputFloat(const char *label, float *v, float step=0.0f, float step_fast=0.0f, const char *format="%.3f", ImGuiInputTextFlags flags=0)
static bool AutoSizedInputInt(const char *label, int *v, int step=1, int step_fast=100, ImGuiInputTextFlags flags=0)
static bool InputToolbarField(const char *label, char *buf, size_t buf_size)
static void BeginCanvasPanel(const char *label, ImVec2 *canvas_size=nullptr)
static void PropertyRow(const char *label, std::function< void()> widget_callback)
static float GetBaseFontSize()
static float GetButtonPadding()
static float GetToolbarHeight()
static void BeginPaddedPanel(const char *label, float padding=-1.0f)
static bool ToolbarButton(const char *label, const ImVec2 &size=ImVec2(0, 0))
static bool InputHexRow(const char *label, uint8_t *data)
static float GetStandardWidgetHeight()
static float GetStandardSpacing()
static bool BeginTableWithTheming(const char *str_id, int columns, ImGuiTableFlags flags=0, const ImVec2 &outer_size=ImVec2(0, 0), float inner_width=0.0f)
static bool AutoSizedInputField(const char *label, char *buf, size_t buf_size, ImGuiInputTextFlags flags=0)
ImVec4 ConvertColorToImVec4(const Color &color)
Definition color.h:23