| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/gui/popup_menu.h" |
| 34 |
|
| 35 |
class MenuBar : public Control { |
| 36 |
GDCLASS(MenuBar, Control); |
| 37 |
|
| 38 |
Mutex mutex; |
| 39 |
|
| 40 |
bool switch_on_hover = true; |
| 41 |
bool disable_shortcuts = false; |
| 42 |
bool prefer_native = true; |
| 43 |
bool flat = false; |
| 44 |
int start_index = -1; |
| 45 |
|
| 46 |
String language; |
| 47 |
TextDirection text_direction = TEXT_DIRECTION_AUTO; |
| 48 |
|
| 49 |
struct Menu { |
| 50 |
String name; |
| 51 |
String tooltip; |
| 52 |
|
| 53 |
Ref<TextLine> text_buf; |
| 54 |
bool hidden = false; |
| 55 |
bool disabled = false; |
| 56 |
RID submenu_rid; |
| 57 |
NativeMenu::SystemMenus sysmenu_id = NativeMenu::INVALID_MENU_ID; |
| 58 |
|
| 59 |
Menu(const String &p_name) { |
| 60 |
name = p_name; |
| 61 |
text_buf.instantiate(); |
| 62 |
} |
| 63 |
|
| 64 |
Menu() { |
| 65 |
text_buf.instantiate(); |
| 66 |
} |
| 67 |
}; |
| 68 |
Vector<Menu> menu_cache; |
| 69 |
|
| 70 |
int focused_menu = -1; |
| 71 |
int selected_menu = -1; |
| 72 |
int active_menu = -1; |
| 73 |
|
| 74 |
Vector2i old_mouse_pos; |
| 75 |
|
| 76 |
struct ThemeCache { |
| 77 |
Ref<StyleBox> normal; |
| 78 |
Ref<StyleBox> normal_mirrored; |
| 79 |
Ref<StyleBox> disabled; |
| 80 |
Ref<StyleBox> disabled_mirrored; |
| 81 |
Ref<StyleBox> pressed; |
| 82 |
Ref<StyleBox> pressed_mirrored; |
| 83 |
Ref<StyleBox> hover; |
| 84 |
Ref<StyleBox> hover_mirrored; |
| 85 |
Ref<StyleBox> hover_pressed; |
| 86 |
Ref<StyleBox> hover_pressed_mirrored; |
| 87 |
|
| 88 |
Ref<Font> font; |
| 89 |
int font_size = 0; |
| 90 |
int outline_size = 0; |
| 91 |
Color font_outline_color; |
| 92 |
|
| 93 |
Color font_color; |
| 94 |
Color font_disabled_color; |
| 95 |
Color font_pressed_color; |
| 96 |
Color font_hover_color; |
| 97 |
Color font_hover_pressed_color; |
| 98 |
Color font_focus_color; |
| 99 |
|
| 100 |
int h_separation = 0; |
| 101 |
} theme_cache; |
| 102 |
|
| 103 |
int _get_index_at_point(const Point2 &p_point) const; |
| 104 |
Rect2 _get_menu_item_rect(int p_index) const; |
| 105 |
void _draw_menu_item(int p_index); |
| 106 |
|
| 107 |
void shape(Menu &p_menu); |
| 108 |
void _refresh_menu_names(); |
| 109 |
Vector<PopupMenu *> _get_popups() const; |
| 110 |
int get_menu_idx_from_control(PopupMenu *p_child) const; |
| 111 |
|
| 112 |
void _open_popup(int p_index, bool p_focus_item = false); |
| 113 |
void _popup_visibility_changed(bool p_visible); |
| 114 |
|
| 115 |
String global_menu_tag; |
| 116 |
|
| 117 |
int _find_global_start_index() { |
| 118 |
if (global_menu_tag.is_empty()) { |
| 119 |
return -1; |
| 120 |
} |
| 121 |
|
| 122 |
NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 123 |
if (!nmenu) { |
| 124 |
return -1; |
| 125 |
} |
| 126 |
RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID); |
| 127 |
int count = nmenu->get_item_count(main_menu); |
| 128 |
for (int i = 0; i < count; i++) { |
| 129 |
if (nmenu->get_item_tag(main_menu, i).operator String().begins_with(global_menu_tag)) { |
| 130 |
return i; |
| 131 |
} |
| 132 |
} |
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
void _popup_changed(ObjectID p_menu); |
| 137 |
|
| 138 |
void bind_global_menu(); |
| 139 |
void unbind_global_menu(); |
| 140 |
|
| 141 |
protected: |
| 142 |
virtual void shortcut_input(const Ref<InputEvent> &p_event) override; |
| 143 |
|
| 144 |
void _notification(int p_what); |
| 145 |
virtual void add_child_notify(Node *p_child) override; |
| 146 |
virtual void move_child_notify(Node *p_child) override; |
| 147 |
virtual void remove_child_notify(Node *p_child) override; |
| 148 |
static void _bind_methods(); |
| 149 |
|
| 150 |
public: |
| 151 |
virtual void gui_input(const Ref<InputEvent> &p_event) override; |
| 152 |
|
| 153 |
void set_switch_on_hover(bool p_enabled); |
| 154 |
bool is_switch_on_hover(); |
| 155 |
void set_disable_shortcuts(bool p_disabled); |
| 156 |
|
| 157 |
void set_prefer_global_menu(bool p_enabled); |
| 158 |
bool is_prefer_global_menu() const; |
| 159 |
|
| 160 |
bool is_native_menu() const; |
| 161 |
|
| 162 |
virtual Size2 get_minimum_size() const override; |
| 163 |
|
| 164 |
int get_menu_count() const; |
| 165 |
|
| 166 |
void set_text_direction(TextDirection p_text_direction); |
| 167 |
TextDirection get_text_direction() const; |
| 168 |
|
| 169 |
void set_language(const String &p_language); |
| 170 |
String get_language() const; |
| 171 |
|
| 172 |
void set_start_index(int p_index); |
| 173 |
int get_start_index() const; |
| 174 |
|
| 175 |
void set_flat(bool p_enabled); |
| 176 |
bool is_flat() const; |
| 177 |
|
| 178 |
void set_menu_title(int p_menu, const String &p_title); |
| 179 |
String get_menu_title(int p_menu) const; |
| 180 |
|
| 181 |
void set_menu_tooltip(int p_menu, const String &p_tooltip); |
| 182 |
String get_menu_tooltip(int p_menu) const; |
| 183 |
|
| 184 |
void set_menu_disabled(int p_menu, bool p_disabled); |
| 185 |
bool is_menu_disabled(int p_menu) const; |
| 186 |
|
| 187 |
void set_menu_hidden(int p_menu, bool p_hidden); |
| 188 |
bool is_menu_hidden(int p_menu) const; |
| 189 |
|
| 190 |
PopupMenu *get_menu_popup(int p_menu) const; |
| 191 |
|
| 192 |
virtual String get_tooltip(const Point2 &p_pos) const override; |
| 193 |
|
| 194 |
MenuBar(); |
| 195 |
~MenuBar(); |
| 196 |
}; |