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 // Apply disabled state if needed
16 if (!item.enabled_condition()) {
17 ImGui::BeginDisabled();
18 }
19
20 // Build label with icon if present
21 std::string display_label = item.label;
22 if (!item.icon.empty()) {
23 display_label = item.icon + " " + item.label;
24 }
25
26 // Render menu item based on type
27 if (item.subitems.empty()) {
28 // Simple menu item
29 bool selected = false;
30 if (item.color.x != 1.0f || item.color.y != 1.0f || item.color.z != 1.0f ||
31 item.color.w != 1.0f) {
32 // Render with custom color
33 ImGui::PushStyleColor(ImGuiCol_Text, item.color);
34 selected = ImGui::MenuItem(
35 display_label.c_str(),
36 item.shortcut.empty() ? nullptr : item.shortcut.c_str());
37 ImGui::PopStyleColor();
38 } else {
39 selected = ImGui::MenuItem(
40 display_label.c_str(),
41 item.shortcut.empty() ? nullptr : item.shortcut.c_str());
42 }
43
44 if (selected) {
45 // Invoke callback
46 if (item.callback) {
47 item.callback();
48 }
49
50 // Handle popup if defined
51 if (item.popup.has_value() && item.popup->auto_open_on_select &&
52 popup_opened_callback) {
53 popup_opened_callback(item.popup->popup_id,
54 item.popup->render_callback);
55 }
56 }
57 } else {
58 // Submenu
59 if (ImGui::BeginMenu(display_label.c_str())) {
60 for (const auto& subitem : item.subitems) {
61 RenderMenuItem(subitem, popup_opened_callback);
62 }
63 ImGui::EndMenu();
64 }
65 }
66
67 // Restore enabled state
68 if (!item.enabled_condition()) {
69 ImGui::EndDisabled();
70 }
71
72 // Render separator if requested
73 if (item.separator_after) {
74 ImGui::Separator();
75 }
76}
77
79 const CanvasMenuSection& section,
80 std::function<void(const std::string&, std::function<void()>)>
81 popup_opened_callback) {
82 // Skip empty sections
83 if (section.items.empty()) {
84 return;
85 }
86
87 // Render section title if present
88 if (!section.title.empty()) {
89 ImGui::TextColored(section.title_color, "%s", section.title.c_str());
90 ImGui::Separator();
91 }
92
93 // Render all items in section
94 for (const auto& item : section.items) {
95 RenderMenuItem(item, popup_opened_callback);
96 }
97
98 // Render separator after section if requested
99 if (section.separator_after) {
100 ImGui::Separator();
101 }
102}
103
105 const CanvasMenuDefinition& menu,
106 std::function<void(const std::string&, std::function<void()>)>
107 popup_opened_callback) {
108 // Skip disabled menus
109 if (!menu.enabled) {
110 return;
111 }
112
113 // Render all sections
114 for (const auto& section : menu.sections) {
115 RenderMenuSection(section, popup_opened_callback);
116 }
117}
118
119} // namespace gui
120} // 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::vector< CanvasMenuItem > subitems
Definition canvas_menu.h:91
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