yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
ui_helpers.cc
Go to the documentation of this file.
2
3#include "absl/strings/str_format.h"
7#include "imgui/imgui.h"
8#include "imgui/imgui_internal.h"
9
10namespace yaze {
11namespace gui {
12
13// ============================================================================
14// Theme and Semantic Colors
15// ============================================================================
16
17ImVec4 GetThemeColor(ImGuiCol idx) {
18 return ImGui::GetStyle().Colors[idx];
19}
20
22 const auto& theme = ThemeManager::Get().GetCurrentTheme();
23 return ConvertColorToImVec4(theme.success);
24}
25
27 const auto& theme = ThemeManager::Get().GetCurrentTheme();
28 return ConvertColorToImVec4(theme.warning);
29}
30
31ImVec4 GetErrorColor() {
32 const auto& theme = ThemeManager::Get().GetCurrentTheme();
33 return ConvertColorToImVec4(theme.error);
34}
35
36ImVec4 GetInfoColor() {
37 const auto& theme = ThemeManager::Get().GetCurrentTheme();
38 return ConvertColorToImVec4(theme.info);
39}
40
42 const auto& theme = ThemeManager::Get().GetCurrentTheme();
43 return ConvertColorToImVec4(theme.accent);
44}
45
46// Entity/Map marker colors (vibrant with good visibility)
48 // Bright yellow with strong visibility
49 return ImVec4(1.0f, 0.9f, 0.0f, 0.85f); // Yellow-gold, high visibility
50}
51
52ImVec4 GetExitColor() {
53 // Bright cyan-white for contrast
54 return ImVec4(0.9f, 1.0f, 1.0f, 0.85f); // Cyan-white, high visibility
55}
56
57ImVec4 GetItemColor() {
58 // Vibrant red for items
59 return ImVec4(1.0f, 0.2f, 0.2f, 0.85f); // Bright red, high visibility
60}
61
63 // Bright magenta for sprites
64 return ImVec4(1.0f, 0.3f, 1.0f, 0.85f); // Bright magenta, high visibility
65}
66
68 const auto& theme = ThemeManager::Get().GetCurrentTheme();
69 return ConvertColorToImVec4(theme.accent);
70}
71
73 return ImVec4(1.0f, 0.5f, 0.0f, 1.0f); // Orange for locked items
74}
75
76// Status colors
78 return GetWarningColor(); // Yellow from theme
79}
80
82 return GetSuccessColor(); // Green from theme
83}
84
86 const auto& theme = ThemeManager::Get().GetCurrentTheme();
87 return ConvertColorToImVec4(theme.accent);
88}
89
90// ============================================================================
91// Layout Helpers
92// ============================================================================
93
94void BeginField(const char* label, float label_width) {
95 ImGui::BeginGroup();
96 if (label_width > 0.0f) {
97 ImGui::Text("%s:", label);
98 ImGui::SameLine(label_width);
99 } else {
100 ImGui::TextUnformatted(label);
101 ImGui::SameLine();
102 }
103 ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x * 0.6f);
104}
105
106void EndField() {
107 ImGui::PopItemWidth();
108 ImGui::EndGroup();
109}
110
111bool BeginPropertyTable(const char* id, int columns, ImGuiTableFlags extra_flags) {
112 ImGuiTableFlags flags = ImGuiTableFlags_Borders |
113 ImGuiTableFlags_SizingFixedFit |
114 extra_flags;
115 if (ImGui::BeginTable(id, columns, flags)) {
116 ImGui::TableSetupColumn("Property", ImGuiTableColumnFlags_WidthFixed, 150);
117 ImGui::TableSetupColumn("Value", ImGuiTableColumnFlags_WidthStretch);
118 return true;
119 }
120 return false;
121}
122
124 ImGui::EndTable();
125}
126
127void PropertyRow(const char* label, const char* value) {
128 ImGui::TableNextColumn();
129 ImGui::Text("%s", label);
130 ImGui::TableNextColumn();
131 ImGui::TextWrapped("%s", value);
132}
133
134void PropertyRow(const char* label, int value) {
135 ImGui::TableNextColumn();
136 ImGui::Text("%s", label);
137 ImGui::TableNextColumn();
138 ImGui::Text("%d", value);
139}
140
141void PropertyRowHex(const char* label, uint8_t value) {
142 ImGui::TableNextColumn();
143 ImGui::Text("%s", label);
144 ImGui::TableNextColumn();
145 ImGui::Text("0x%02X", value);
146}
147
148void PropertyRowHex(const char* label, uint16_t value) {
149 ImGui::TableNextColumn();
150 ImGui::Text("%s", label);
151 ImGui::TableNextColumn();
152 ImGui::Text("0x%04X", value);
153}
154
155void SectionHeader(const char* icon, const char* label, const ImVec4& color) {
156 ImGui::TextColored(color, "%s %s", icon, label);
157 ImGui::Separator();
158}
159
160// ============================================================================
161// Common Widget Patterns
162// ============================================================================
163
164bool IconButton(const char* icon, const char* label, const ImVec2& size) {
165 std::string button_text = std::string(icon) + " " + std::string(label);
166 return ImGui::Button(button_text.c_str(), size);
167}
168
169bool ColoredButton(const char* label, ButtonType type, const ImVec2& size) {
170 ImVec4 color;
171 switch (type) {
172 case ButtonType::Success: color = GetSuccessColor(); break;
173 case ButtonType::Warning: color = GetWarningColor(); break;
174 case ButtonType::Error: color = GetErrorColor(); break;
175 case ButtonType::Info: color = GetInfoColor(); break;
176 default: color = GetThemeColor(ImGuiCol_Button); break;
177 }
178
179 ImGui::PushStyleColor(ImGuiCol_Button, color);
180 ImGui::PushStyleColor(ImGuiCol_ButtonHovered,
181 ImVec4(color.x * 1.2f, color.y * 1.2f, color.z * 1.2f, color.w));
182 ImGui::PushStyleColor(ImGuiCol_ButtonActive,
183 ImVec4(color.x * 0.8f, color.y * 0.8f, color.z * 0.8f, color.w));
184
185 bool result = ImGui::Button(label, size);
186
187 ImGui::PopStyleColor(3);
188 return result;
189}
190
191bool ToggleIconButton(const char* icon_on, const char* icon_off,
192 bool* state, const char* tooltip) {
193 const char* icon = *state ? icon_on : icon_off;
194 ImVec4 color = *state ? GetSuccessColor() : GetThemeColor(ImGuiCol_Button);
195
196 ImGui::PushStyleColor(ImGuiCol_Button, color);
197 bool result = ImGui::SmallButton(icon);
198 ImGui::PopStyleColor();
199
200 if (result) *state = !*state;
201
202 if (tooltip && ImGui::IsItemHovered()) {
203 ImGui::SetTooltip("%s", tooltip);
204 }
205
206 return result;
207}
208
209void HelpMarker(const char* desc) {
210 ImGui::TextDisabled(ICON_MD_HELP_OUTLINE);
211 if (ImGui::IsItemHovered()) {
212 ImGui::BeginTooltip();
213 ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
214 ImGui::TextUnformatted(desc);
215 ImGui::PopTextWrapPos();
216 ImGui::EndTooltip();
217 }
218}
219
220void SeparatorText(const char* label) {
221 ImGui::SeparatorText(label);
222}
223
224void StatusBadge(const char* text, ButtonType type) {
225 ImVec4 color;
226 switch (type) {
227 case ButtonType::Success: color = GetSuccessColor(); break;
228 case ButtonType::Warning: color = GetWarningColor(); break;
229 case ButtonType::Error: color = GetErrorColor(); break;
230 case ButtonType::Info: color = GetInfoColor(); break;
231 default: color = GetThemeColor(ImGuiCol_Text); break;
232 }
233
234 ImGui::PushStyleColor(ImGuiCol_Button, color);
235 ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10.0f);
236 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(8, 3));
237 ImGui::SmallButton(text);
238 ImGui::PopStyleVar(2);
239 ImGui::PopStyleColor();
240}
241
242// ============================================================================
243// Editor-Specific Patterns
244// ============================================================================
245
246void BeginToolset(const char* id) {
247 ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(2, 2));
248 ImGui::BeginTable(id, 32, ImGuiTableFlags_SizingFixedFit);
249}
250
252 ImGui::EndTable();
253 ImGui::PopStyleVar();
254}
255
256void ToolsetButton(const char* icon, bool selected, const char* tooltip,
257 std::function<void()> on_click) {
258 ImGui::TableNextColumn();
259
260 if (selected) {
261 ImGui::PushStyleColor(ImGuiCol_Button, GetAccentColor());
262 }
263
264 if (ImGui::Button(icon)) {
265 if (on_click) on_click();
266 }
267
268 if (selected) {
269 ImGui::PopStyleColor();
270 }
271
272 if (tooltip && ImGui::IsItemHovered()) {
273 ImGui::SetTooltip("%s", tooltip);
274 }
275}
276
277void BeginCanvasContainer(const char* id, bool scrollable) {
278 ImGuiWindowFlags flags = scrollable ?
279 ImGuiWindowFlags_AlwaysVerticalScrollbar :
280 ImGuiWindowFlags_None;
281 ImGui::BeginChild(id, ImVec2(0, 0), true, flags);
282}
283
285 ImGui::EndChild();
286}
287
288bool EditorTabItem(const char* icon, const char* label, bool* p_open) {
289 char tab_label[256];
290 snprintf(tab_label, sizeof(tab_label), "%s %s", icon, label);
291 return ImGui::BeginTabItem(tab_label, p_open);
292}
293
294bool ConfirmationDialog(const char* id, const char* title, const char* message,
295 const char* confirm_text, const char* cancel_text) {
296 bool confirmed = false;
297
298 if (ImGui::BeginPopupModal(id, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
299 ImGui::Text("%s", title);
300 ImGui::Separator();
301 ImGui::TextWrapped("%s", message);
302 ImGui::Separator();
303
304 if (ColoredButton(confirm_text, ButtonType::Warning, ImVec2(120, 0))) {
305 confirmed = true;
306 ImGui::CloseCurrentPopup();
307 }
308
309 ImGui::SameLine();
310 if (ImGui::Button(cancel_text, ImVec2(120, 0))) {
311 ImGui::CloseCurrentPopup();
312 }
313
314 ImGui::EndPopup();
315 }
316
317 return confirmed;
318}
319
320// ============================================================================
321// Visual Indicators
322// ============================================================================
323
324void StatusIndicator(const char* label, bool active, const char* tooltip) {
325 ImVec4 color = active ? GetSuccessColor() : GetThemeColor(ImGuiCol_TextDisabled);
326
327 ImDrawList* draw_list = ImGui::GetWindowDrawList();
328 ImVec2 pos = ImGui::GetCursorScreenPos();
329 float radius = 5.0f;
330
331 pos.x += radius + 3;
332 pos.y += ImGui::GetTextLineHeight() * 0.5f;
333
334 draw_list->AddCircleFilled(pos, radius, ImGui::GetColorU32(color));
335
336 ImGui::SetCursorPosX(ImGui::GetCursorPosX() + radius * 2 + 8);
337 ImGui::Text("%s", label);
338
339 if (tooltip && ImGui::IsItemHovered()) {
340 ImGui::SetTooltip("%s", tooltip);
341 }
342}
343
344void RomVersionBadge(const char* version, bool is_vanilla) {
345 ImVec4 color = is_vanilla ? GetWarningColor() : GetSuccessColor();
346 const char* icon = is_vanilla ? ICON_MD_INFO : ICON_MD_CHECK_CIRCLE;
347
348 ImGui::PushStyleColor(ImGuiCol_Text, color);
349 ImGui::Text("%s %s", icon, version);
350 ImGui::PopStyleColor();
351}
352
353void LockIndicator(bool locked, const char* label) {
354 if (locked) {
355 ImGui::TextColored(GetLockedColor(), ICON_MD_LOCK " %s (Locked)", label);
356 } else {
357 ImGui::Text(ICON_MD_LOCK_OPEN " %s", label);
358 }
359}
360
361// ============================================================================
362// Spacing and Alignment
363// ============================================================================
364
365void VerticalSpacing(float pixels) {
366 ImGui::Dummy(ImVec2(0, pixels));
367}
368
369void HorizontalSpacing(float pixels) {
370 ImGui::Dummy(ImVec2(pixels, 0));
371 ImGui::SameLine();
372}
373
374void CenterText(const char* text) {
375 float text_width = ImGui::CalcTextSize(text).x;
376 ImGui::SetCursorPosX((ImGui::GetContentRegionAvail().x - text_width) * 0.5f);
377 ImGui::Text("%s", text);
378}
379
380void RightAlign(float width) {
381 ImGui::SetCursorPosX(ImGui::GetCursorPosX() +
382 ImGui::GetContentRegionAvail().x - width);
383}
384
385// ============================================================================
386// Animation Helpers
387// ============================================================================
388
389float GetPulseAlpha(float speed) {
390 return 0.5f + 0.5f * sinf(static_cast<float>(ImGui::GetTime()) * speed * 2.0f);
391}
392
393float GetFadeIn(float duration) {
394 static double start_time = 0.0;
395 double current_time = ImGui::GetTime();
396
397 if (start_time == 0.0) {
398 start_time = current_time;
399 }
400
401 float elapsed = static_cast<float>(current_time - start_time);
402 float alpha = ImClamp(elapsed / duration, 0.0f, 1.0f);
403
404 // Reset after complete
405 if (alpha >= 1.0f) {
406 start_time = 0.0;
407 }
408
409 return alpha;
410}
411
412void PushPulseEffect(float speed) {
413 ImGui::PushStyleVar(ImGuiStyleVar_Alpha,
414 ImGui::GetStyle().Alpha * GetPulseAlpha(speed));
415}
416
418 ImGui::PopStyleVar();
419}
420
421void LoadingSpinner(const char* label, float radius) {
422 ImDrawList* draw_list = ImGui::GetWindowDrawList();
423 ImVec2 pos = ImGui::GetCursorScreenPos();
424 pos.x += radius + 4;
425 pos.y += radius + 4;
426
427 const float rotation = static_cast<float>(ImGui::GetTime()) * 3.0f;
428 const int segments = 16;
429 const float thickness = 3.0f;
430
431 const float start_angle = rotation;
432 const float end_angle = rotation + IM_PI * 1.5f;
433
434 draw_list->PathArcTo(pos, radius, start_angle, end_angle, segments);
435 draw_list->PathStroke(ImGui::GetColorU32(GetAccentColor()), 0, thickness);
436
437 if (label) {
438 ImGui::SetCursorPosX(ImGui::GetCursorPosX() + radius * 2 + 8);
439 ImGui::Text("%s", label);
440 } else {
441 ImGui::Dummy(ImVec2(radius * 2 + 8, radius * 2 + 8));
442 }
443}
444
445// ============================================================================
446// Responsive Layout Helpers
447// ============================================================================
448
449float GetResponsiveWidth(float min_width, float max_width, float ratio) {
450 float available = ImGui::GetContentRegionAvail().x;
451 float target = available * ratio;
452
453 if (target < min_width) return min_width;
454 if (target > max_width) return max_width;
455 return target;
456}
457
458void SetupResponsiveColumns(int count, float min_col_width) {
459 float available = ImGui::GetContentRegionAvail().x;
460 float col_width = available / count;
461
462 if (col_width < min_col_width) {
463 col_width = min_col_width;
464 }
465
466 for (int i = 0; i < count; ++i) {
467 ImGui::TableSetupColumn("##col", ImGuiTableColumnFlags_WidthFixed, col_width);
468 }
469}
470
471static int g_two_col_table_active = 0;
472
473void BeginTwoColumns(const char* id, float split_ratio) {
474 ImGuiTableFlags flags = ImGuiTableFlags_Resizable |
475 ImGuiTableFlags_BordersInnerV |
476 ImGuiTableFlags_SizingStretchProp;
477
478 if (ImGui::BeginTable(id, 2, flags)) {
479 float available = ImGui::GetContentRegionAvail().x;
480 ImGui::TableSetupColumn("##left", ImGuiTableColumnFlags_None,
481 available * split_ratio);
482 ImGui::TableSetupColumn("##right", ImGuiTableColumnFlags_None,
483 available * (1.0f - split_ratio));
484 ImGui::TableNextRow();
485 ImGui::TableNextColumn();
486 g_two_col_table_active++;
487 }
488}
489
491 ImGui::TableNextColumn();
492}
493
495 ImGui::EndTable();
496 g_two_col_table_active--;
497}
498
499// ============================================================================
500// Input Helpers
501// ============================================================================
502
503bool LabeledInputHex(const char* label, uint8_t* value) {
504 BeginField(label);
505 ImGui::PushItemWidth(60);
506 bool changed = ImGui::InputScalar("##hex", ImGuiDataType_U8, value, nullptr,
507 nullptr, "%02X",
508 ImGuiInputTextFlags_CharsHexadecimal);
509 ImGui::PopItemWidth();
510 EndField();
511 return changed;
512}
513
514bool LabeledInputHex(const char* label, uint16_t* value) {
515 BeginField(label);
516 ImGui::PushItemWidth(80);
517 bool changed = ImGui::InputScalar("##hex", ImGuiDataType_U16, value, nullptr,
518 nullptr, "%04X",
519 ImGuiInputTextFlags_CharsHexadecimal);
520 ImGui::PopItemWidth();
521 EndField();
522 return changed;
523}
524
525bool IconCombo(const char* icon, const char* label, int* current,
526 const char* const items[], int count) {
527 ImGui::Text("%s", icon);
528 ImGui::SameLine();
529 return ImGui::Combo(label, current, items, count);
530}
531
532} // namespace gui
533} // namespace yaze
static ThemeManager & Get()
const EnhancedTheme & GetCurrentTheme() const
#define ICON_MD_INFO
Definition icons.h:991
#define ICON_MD_LOCK_OPEN
Definition icons.h:1140
#define ICON_MD_LOCK
Definition icons.h:1138
#define ICON_MD_CHECK_CIRCLE
Definition icons.h:398
#define ICON_MD_HELP_OUTLINE
Definition icons.h:933
void RightAlign(float width)
void VerticalSpacing(float pixels)
void EndCanvasContainer()
void BeginToolset(const char *id)
ImVec4 ConvertColorToImVec4(const Color &color)
Definition color.h:21
ImVec4 GetVanillaRomColor()
Definition ui_helpers.cc:77
void EndPropertyTable()
void PropertyRow(const char *label, const char *value)
bool ToggleIconButton(const char *icon_on, const char *icon_off, bool *state, const char *tooltip)
ImVec4 GetSuccessColor()
Definition ui_helpers.cc:21
void BeginField(const char *label, float label_width)
Definition ui_helpers.cc:94
void SeparatorText(const char *label)
void EndField()
ImVec4 GetLockedColor()
Definition ui_helpers.cc:72
void StatusIndicator(const char *label, bool active, const char *tooltip)
ImVec4 GetSelectedColor()
Definition ui_helpers.cc:67
void BeginTwoColumns(const char *id, float split_ratio)
void RomVersionBadge(const char *version, bool is_vanilla)
bool EditorTabItem(const char *icon, const char *label, bool *p_open)
ImVec4 GetEntranceColor()
Definition ui_helpers.cc:47
void CenterText(const char *text)
bool LabeledInputHex(const char *label, uint8_t *value)
void SectionHeader(const char *icon, const char *label, const ImVec4 &color)
void LockIndicator(bool locked, const char *label)
bool IconButton(const char *icon, const char *label, const ImVec2 &size)
void EndToolset()
ImVec4 GetSpriteColor()
Definition ui_helpers.cc:62
void PropertyRowHex(const char *label, uint8_t value)
float GetResponsiveWidth(float min_width, float max_width, float ratio)
void SwitchColumn()
void SetupResponsiveColumns(int count, float min_col_width)
bool ColoredButton(const char *label, ButtonType type, const ImVec2 &size)
bool BeginPropertyTable(const char *id, int columns, ImGuiTableFlags extra_flags)
void BeginCanvasContainer(const char *id, bool scrollable)
ImVec4 GetItemColor()
Definition ui_helpers.cc:57
bool IconCombo(const char *icon, const char *label, int *current, const char *const items[], int count)
void LoadingSpinner(const char *label, float radius)
ImVec4 GetErrorColor()
Definition ui_helpers.cc:31
void ToolsetButton(const char *icon, bool selected, const char *tooltip, std::function< void()> on_click)
ImVec4 GetWarningColor()
Definition ui_helpers.cc:26
bool ConfirmationDialog(const char *id, const char *title, const char *message, const char *confirm_text, const char *cancel_text)
void EndTwoColumns()
ImVec4 GetModifiedColor()
Definition ui_helpers.cc:85
void PopPulseEffect()
ImVec4 GetInfoColor()
Definition ui_helpers.cc:36
float GetFadeIn(float duration)
float GetPulseAlpha(float speed)
void PushPulseEffect(float speed)
ImVec4 GetExitColor()
Definition ui_helpers.cc:52
void HelpMarker(const char *desc)
Color GetThemeColor(const std::string &color_name)
void StatusBadge(const char *text, ButtonType type)
void HorizontalSpacing(float pixels)
ImVec4 GetAccentColor()
Definition ui_helpers.cc:41
ImVec4 GetCustomRomColor()
Definition ui_helpers.cc:81
Main namespace for the application.
Definition controller.cc:20
#define IM_PI