menu_bar.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**************************************************************************/
  2. /* menu_bar.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "scene/gui/popup_menu.h"
  32. class MenuBar : public Control {
  33. GDCLASS(MenuBar, Control);
  34. Mutex mutex;
  35. bool switch_on_hover = true;
  36. bool disable_shortcuts = false;
  37. bool prefer_native = true;
  38. bool flat = false;
  39. int start_index = -1;
  40. String language;
  41. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  42. struct Menu {
  43. String name;
  44. String tooltip;
  45. Ref<TextLine> text_buf;
  46. bool hidden = false;
  47. bool disabled = false;
  48. RID submenu_rid;
  49. Menu(const String &p_name) {
  50. name = p_name;
  51. text_buf.instantiate();
  52. }
  53. Menu() {
  54. text_buf.instantiate();
  55. }
  56. };
  57. Vector<Menu> menu_cache;
  58. int focused_menu = -1;
  59. int selected_menu = -1;
  60. int active_menu = -1;
  61. Vector2i old_mouse_pos;
  62. ObjectID shortcut_context;
  63. struct ThemeCache {
  64. Ref<StyleBox> normal;
  65. Ref<StyleBox> normal_mirrored;
  66. Ref<StyleBox> disabled;
  67. Ref<StyleBox> disabled_mirrored;
  68. Ref<StyleBox> pressed;
  69. Ref<StyleBox> pressed_mirrored;
  70. Ref<StyleBox> hover;
  71. Ref<StyleBox> hover_mirrored;
  72. Ref<StyleBox> hover_pressed;
  73. Ref<StyleBox> hover_pressed_mirrored;
  74. Ref<Font> font;
  75. int font_size = 0;
  76. int outline_size = 0;
  77. Color font_outline_color;
  78. Color font_color;
  79. Color font_disabled_color;
  80. Color font_pressed_color;
  81. Color font_hover_color;
  82. Color font_hover_pressed_color;
  83. Color font_focus_color;
  84. int h_separation = 0;
  85. } theme_cache;
  86. int _get_index_at_point(const Point2 &p_point) const;
  87. Rect2 _get_menu_item_rect(int p_index) const;
  88. void _draw_menu_item(int p_index);
  89. void shape(Menu &p_menu);
  90. void _refresh_menu_names();
  91. Vector<PopupMenu *> _get_popups() const;
  92. int get_menu_idx_from_control(PopupMenu *p_child) const;
  93. void _open_popup(int p_index, bool p_focus_item = false);
  94. void _popup_visibility_changed(bool p_visible);
  95. String global_menu_tag;
  96. int _find_global_start_index() {
  97. if (global_menu_tag.is_empty()) {
  98. return -1;
  99. }
  100. NativeMenu *nmenu = NativeMenu::get_singleton();
  101. if (!nmenu) {
  102. return -1;
  103. }
  104. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  105. int count = nmenu->get_item_count(main_menu);
  106. for (int i = 0; i < count; i++) {
  107. if (nmenu->get_item_tag(main_menu, i).operator String().begins_with(global_menu_tag)) {
  108. return i;
  109. }
  110. }
  111. return -1;
  112. }
  113. void _popup_changed(ObjectID p_menu);
  114. void bind_global_menu();
  115. void unbind_global_menu();
  116. protected:
  117. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  118. void _notification(int p_what);
  119. virtual void add_child_notify(Node *p_child) override;
  120. virtual void move_child_notify(Node *p_child) override;
  121. virtual void remove_child_notify(Node *p_child) override;
  122. static void _bind_methods();
  123. public:
  124. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  125. void set_switch_on_hover(bool p_enabled);
  126. bool is_switch_on_hover();
  127. void set_disable_shortcuts(bool p_disabled);
  128. void set_prefer_global_menu(bool p_enabled);
  129. bool is_prefer_global_menu() const;
  130. bool is_native_menu() const;
  131. virtual Size2 get_minimum_size() const override;
  132. int get_menu_count() const;
  133. void set_text_direction(TextDirection p_text_direction);
  134. TextDirection get_text_direction() const;
  135. void set_language(const String &p_language);
  136. String get_language() const;
  137. void set_start_index(int p_index);
  138. int get_start_index() const;
  139. void set_flat(bool p_enabled);
  140. bool is_flat() const;
  141. void set_menu_title(int p_menu, const String &p_title);
  142. String get_menu_title(int p_menu) const;
  143. void set_menu_tooltip(int p_menu, const String &p_tooltip);
  144. String get_menu_tooltip(int p_menu) const;
  145. void set_menu_disabled(int p_menu, bool p_disabled);
  146. bool is_menu_disabled(int p_menu) const;
  147. void set_menu_hidden(int p_menu, bool p_hidden);
  148. bool is_menu_hidden(int p_menu) const;
  149. PopupMenu *get_menu_popup(int p_menu) const;
  150. virtual String get_tooltip(const Point2 &p_pos) const override;
  151. MenuBar();
  152. ~MenuBar();
  153. };