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