yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_menu.cc
Go to the documentation of this file.
1#include "canvas_menu.h"
2
3namespace yaze {
4namespace gui {
5
7 const CanvasMenuItem& item,
8 std::function<void(const std::string&, std::function<void()>)>
9 popup_opened_callback) {
10 // Check visibility
11 if (!item.visible_condition()) {
12 return;
13 }
14
15 const bool enabled = item.enabled_condition();
16 const bool checked =
17 item.checked_condition ? item.checked_condition() : false;
18
19 // Apply disabled state if needed
20 if (!enabled) {
21 ImGui::BeginDisabled();
22 }
23
24 // Build label with icon if present
25 std::string display_label = item.label;
26 if (!item.icon.empty()) {
27 display_label = item.icon + " " + item.label;
28 }
29
30 // Render menu item based on type
31 if (item.subitems.empty()) {
32 // Simple menu item
33 bool selected = false;
34 if (item.color.x != 1.0f || item.color.y != 1.0f || item.color.z != 1.0f ||
35 item.color.w != 1.0f) {
36 // Render with custom color
37 ImGui::PushStyleColor(ImGuiCol_Text, item.color);
38 selected =
40 ? ImGui::MenuItem(
41 display_label.c_str(),
42 item.shortcut.empty() ? nullptr : item.shortcut.c_str(),
43 checked)
44 : ImGui::MenuItem(
45 display_label.c_str(),
46 item.shortcut.empty() ? nullptr : item.shortcut.c_str());
47 ImGui::PopStyleColor();
48 } else {
49 selected =
51 ? ImGui::MenuItem(
52 display_label.c_str(),
53 item.shortcut.empty() ? nullptr : item.shortcut.c_str(),
54 checked)
55 : ImGui::MenuItem(
56 display_label.c_str(),
57 item.shortcut.empty() ? nullptr : item.shortcut.c_str());
58 }
59
60 if (selected) {
61 // Invoke callback
62 if (item.callback) {
63 item.callback();
64 }
65
66 // Handle popup if defined
67 if (item.popup.has_value() && item.popup->auto_open_on_select &&
68 popup_opened_callback) {
69 popup_opened_callback(item.popup->popup_id,
70 item.popup->render_callback);
71 }
72 }
73 } else {
74 // Submenu
75 if (ImGui::BeginMenu(display_label.c_str())) {
76 for (const auto& subitem : item.subitems) {
77 RenderMenuItem(subitem, popup_opened_callback);
78 }
79 ImGui::EndMenu();
80 }
81 }
82
83 // Restore enabled state
84 if (!enabled) {
85 ImGui::EndDisabled();
86 }
87
88 // Render separator if requested
89 if (item.separator_after) {
90 ImGui::Separator();
91 }
92}
93
95 const CanvasMenuSection& section,
96 std::function<void(const std::string&, std::function<void()>)>
97 popup_opened_callback) {
98 // Skip empty sections
99 if (section.items.empty()) {
100 return;
101 }
102
103 // Render section title if present
104 if (!section.title.empty()) {
105 ImGui::TextColored(section.title_color, "%s", section.title.c_str());
106 ImGui::Separator();
107 }
108
109 // Render all items in section
110 for (const auto& item : section.items) {
111 RenderMenuItem(item, popup_opened_callback);
112 }
113
114 // Render separator after section if requested
115 if (section.separator_after) {
116 ImGui::Separator();
117 }
118}
119
121 const CanvasMenuDefinition& menu,
122 std::function<void(const std::string&, std::function<void()>)>
123 popup_opened_callback) {
124 // Skip disabled menus
125 if (!menu.enabled) {
126 return;
127 }
128
129 // Render all sections
130 for (const auto& section : menu.sections) {
131 RenderMenuSection(section, popup_opened_callback);
132 }
133}
134
135} // namespace gui
136} // namespace yaze
void RenderCanvasMenu(const CanvasMenuDefinition &menu, std::function< void(const std::string &, std::function< void()>)> popup_opened_callback)
Render a complete menu definition.
void RenderMenuSection(const CanvasMenuSection &section, std::function< void(const std::string &, std::function< void()>)> popup_opened_callback)
Render a menu section.
void RenderMenuItem(const CanvasMenuItem &item, std::function< void(const std::string &, std::function< void()>)> popup_opened_callback)
Render a single menu item.
Definition canvas_menu.cc:6
Complete menu definition.
std::vector< CanvasMenuSection > sections
Declarative menu item definition.
Definition canvas_menu.h:64
std::function< bool()> checked_condition
Definition canvas_menu.h:91
std::vector< CanvasMenuItem > subitems
Definition canvas_menu.h:94
std::function< bool()> enabled_condition
Definition canvas_menu.h:81
std::function< void()> callback
Definition canvas_menu.h:75
std::optional< CanvasPopupDefinition > popup
Definition canvas_menu.h:78
std::function< bool()> visible_condition
Definition canvas_menu.h:86
Menu section grouping related menu items.
std::vector< CanvasMenuItem > items