popup_menu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**************************************************************************/
  2. /* popup_menu.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. #ifndef POPUP_MENU_H
  31. #define POPUP_MENU_H
  32. #include "core/input/shortcut.h"
  33. #include "scene/gui/popup.h"
  34. #include "scene/gui/scroll_container.h"
  35. #include "scene/property_list_helper.h"
  36. #include "scene/resources/text_line.h"
  37. class PopupMenu : public Popup {
  38. GDCLASS(PopupMenu, Popup);
  39. static HashMap<NativeMenu::SystemMenus, PopupMenu *> system_menus;
  40. struct Item {
  41. Ref<Texture2D> icon;
  42. int icon_max_width = 0;
  43. Color icon_modulate = Color(1, 1, 1, 1);
  44. String text;
  45. String xl_text;
  46. Ref<TextLine> text_buf;
  47. Ref<TextLine> accel_text_buf;
  48. String language;
  49. Control::TextDirection text_direction = Control::TEXT_DIRECTION_AUTO;
  50. bool checked = false;
  51. enum {
  52. CHECKABLE_TYPE_NONE,
  53. CHECKABLE_TYPE_CHECK_BOX,
  54. CHECKABLE_TYPE_RADIO_BUTTON,
  55. } checkable_type = CHECKABLE_TYPE_NONE;
  56. int max_states = 0;
  57. int state = 0;
  58. bool separator = false;
  59. bool disabled = false;
  60. bool dirty = true;
  61. int id = 0;
  62. Variant metadata;
  63. String submenu_name; // Compatibility.
  64. PopupMenu *submenu = nullptr;
  65. String tooltip;
  66. Key accel = Key::NONE;
  67. int _ofs_cache = 0;
  68. int _height_cache = 0;
  69. int indent = 0;
  70. Ref<Shortcut> shortcut;
  71. bool shortcut_is_global = false;
  72. bool shortcut_is_disabled = false;
  73. bool allow_echo = false;
  74. bool submenu_bound = false;
  75. // Returns (0,0) if icon is null.
  76. Size2 get_icon_size() const {
  77. return icon.is_null() ? Size2() : icon->get_size();
  78. }
  79. Item() {
  80. text_buf.instantiate();
  81. accel_text_buf.instantiate();
  82. checkable_type = CHECKABLE_TYPE_NONE;
  83. }
  84. Item(bool p_dummy) {}
  85. };
  86. static inline PropertyListHelper base_property_helper;
  87. PropertyListHelper property_helper;
  88. // To make Item available.
  89. friend class OptionButton;
  90. friend class MenuButton;
  91. RID global_menu;
  92. RID system_menu;
  93. NativeMenu::SystemMenus system_menu_id = NativeMenu::INVALID_MENU_ID;
  94. bool prefer_native = false;
  95. bool close_allowed = false;
  96. bool activated_by_keyboard = false;
  97. Timer *minimum_lifetime_timer = nullptr;
  98. Timer *submenu_timer = nullptr;
  99. List<Rect2> autohide_areas;
  100. mutable Vector<Item> items;
  101. BitField<MouseButtonMask> initial_button_mask;
  102. bool during_grabbed_click = false;
  103. bool is_scrolling = false;
  104. int mouse_over = -1;
  105. int submenu_over = -1;
  106. String _get_accel_text(const Item &p_item) const;
  107. int _get_mouse_over(const Point2 &p_over) const;
  108. void _mouse_over_update(const Point2 &p_over);
  109. virtual Size2 _get_contents_minimum_size() const override;
  110. int _get_item_height(int p_idx) const;
  111. int _get_items_total_height() const;
  112. Size2 _get_item_icon_size(int p_idx) const;
  113. void _shape_item(int p_idx) const;
  114. void _activate_submenu(int p_over, bool p_by_keyboard = false);
  115. void _submenu_timeout();
  116. uint64_t popup_time_msec = 0;
  117. bool hide_on_item_selection = true;
  118. bool hide_on_checkable_item_selection = true;
  119. bool hide_on_multistate_item_selection = false;
  120. Vector2 moved;
  121. HashMap<Ref<Shortcut>, int> shortcut_refcount;
  122. void _ref_shortcut(Ref<Shortcut> p_sc);
  123. void _unref_shortcut(Ref<Shortcut> p_sc);
  124. void _shortcut_changed();
  125. bool allow_search = true;
  126. uint64_t search_time_msec = 0;
  127. String search_string = "";
  128. ScrollContainer *scroll_container = nullptr;
  129. Control *control = nullptr;
  130. const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
  131. const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
  132. float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  133. struct ThemeCache {
  134. Ref<StyleBox> panel_style;
  135. Ref<StyleBox> hover_style;
  136. Ref<StyleBox> separator_style;
  137. Ref<StyleBox> labeled_separator_left;
  138. Ref<StyleBox> labeled_separator_right;
  139. int v_separation = 0;
  140. int h_separation = 0;
  141. int indent = 0;
  142. int item_start_padding = 0;
  143. int item_end_padding = 0;
  144. int icon_max_width = 0;
  145. Ref<Texture2D> checked;
  146. Ref<Texture2D> checked_disabled;
  147. Ref<Texture2D> unchecked;
  148. Ref<Texture2D> unchecked_disabled;
  149. Ref<Texture2D> radio_checked;
  150. Ref<Texture2D> radio_checked_disabled;
  151. Ref<Texture2D> radio_unchecked;
  152. Ref<Texture2D> radio_unchecked_disabled;
  153. Ref<Texture2D> submenu;
  154. Ref<Texture2D> submenu_mirrored;
  155. Ref<Font> font;
  156. int font_size = 0;
  157. Ref<Font> font_separator;
  158. int font_separator_size = 0;
  159. Color font_color;
  160. Color font_hover_color;
  161. Color font_disabled_color;
  162. Color font_accelerator_color;
  163. int font_outline_size = 0;
  164. Color font_outline_color;
  165. Color font_separator_color;
  166. int font_separator_outline_size = 0;
  167. Color font_separator_outline_color;
  168. } theme_cache;
  169. void _draw_items();
  170. void _minimum_lifetime_timeout();
  171. void _close_pressed();
  172. void _menu_changed();
  173. void _input_from_window_internal(const Ref<InputEvent> &p_event);
  174. bool _set_item_accelerator(int p_index, const Ref<InputEventKey> &p_ie);
  175. void _set_item_checkable_type(int p_index, int p_checkable_type);
  176. int _get_item_checkable_type(int p_index) const;
  177. protected:
  178. virtual void add_child_notify(Node *p_child) override;
  179. virtual void remove_child_notify(Node *p_child) override;
  180. virtual void _input_from_window(const Ref<InputEvent> &p_event) override;
  181. void _notification(int p_what);
  182. bool _set(const StringName &p_name, const Variant &p_value);
  183. bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
  184. void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
  185. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  186. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  187. static void _bind_methods();
  188. #ifndef DISABLE_DEPRECATED
  189. void _add_shortcut_bind_compat_36493(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  190. void _add_icon_shortcut_bind_compat_36493(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  191. void _clear_bind_compat_79965();
  192. void _set_system_menu_root_compat_87452(const String &p_special);
  193. String _get_system_menu_root_compat_87452() const;
  194. static void _bind_compatibility_methods();
  195. #endif
  196. public:
  197. // ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes,
  198. // this value should be updated to reflect the new size.
  199. static const int ITEM_PROPERTY_SIZE = 10;
  200. virtual void _parent_focused() override;
  201. RID bind_global_menu();
  202. void unbind_global_menu();
  203. bool is_system_menu() const;
  204. void set_system_menu(NativeMenu::SystemMenus p_system_menu_id);
  205. NativeMenu::SystemMenus get_system_menu() const;
  206. void add_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
  207. void add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
  208. void add_check_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
  209. void add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
  210. void add_radio_check_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
  211. void add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
  212. void add_multistate_item(const String &p_label, int p_max_states, int p_default_state = 0, int p_id = -1, Key p_accel = Key::NONE);
  213. void add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false, bool p_allow_echo = false);
  214. void add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false, bool p_allow_echo = false);
  215. void add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  216. void add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  217. void add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  218. void add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  219. void add_submenu_item(const String &p_label, const String &p_submenu, int p_id = -1);
  220. void add_submenu_node_item(const String &p_label, PopupMenu *p_submenu, int p_id = -1);
  221. void set_item_text(int p_idx, const String &p_text);
  222. void set_item_text_direction(int p_idx, Control::TextDirection p_text_direction);
  223. void set_item_language(int p_idx, const String &p_language);
  224. void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon);
  225. void set_item_icon_max_width(int p_idx, int p_width);
  226. void set_item_icon_modulate(int p_idx, const Color &p_modulate);
  227. void set_item_checked(int p_idx, bool p_checked);
  228. void set_item_id(int p_idx, int p_id);
  229. void set_item_accelerator(int p_idx, Key p_accel);
  230. void set_item_metadata(int p_idx, const Variant &p_meta);
  231. void set_item_disabled(int p_idx, bool p_disabled);
  232. void set_item_submenu(int p_idx, const String &p_submenu);
  233. void set_item_submenu_node(int p_idx, PopupMenu *p_submenu);
  234. void set_item_as_separator(int p_idx, bool p_separator);
  235. void set_item_as_checkable(int p_idx, bool p_checkable);
  236. void set_item_as_radio_checkable(int p_idx, bool p_radio_checkable);
  237. void set_item_tooltip(int p_idx, const String &p_tooltip);
  238. void set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global = false);
  239. void set_item_indent(int p_idx, int p_indent);
  240. void set_item_max_states(int p_idx, int p_max_states);
  241. void set_item_multistate(int p_idx, int p_state);
  242. void toggle_item_multistate(int p_idx);
  243. void set_item_shortcut_disabled(int p_idx, bool p_disabled);
  244. void toggle_item_checked(int p_idx);
  245. String get_item_text(int p_idx) const;
  246. String get_item_xl_text(int p_idx) const;
  247. Control::TextDirection get_item_text_direction(int p_idx) const;
  248. String get_item_language(int p_idx) const;
  249. int get_item_idx_from_text(const String &text) const;
  250. Ref<Texture2D> get_item_icon(int p_idx) const;
  251. int get_item_icon_max_width(int p_idx) const;
  252. Color get_item_icon_modulate(int p_idx) const;
  253. bool is_item_checked(int p_idx) const;
  254. int get_item_id(int p_idx) const;
  255. int get_item_index(int p_id) const;
  256. Key get_item_accelerator(int p_idx) const;
  257. Variant get_item_metadata(int p_idx) const;
  258. bool is_item_disabled(int p_idx) const;
  259. String get_item_submenu(int p_idx) const;
  260. PopupMenu *get_item_submenu_node(int p_idx) const;
  261. bool is_item_separator(int p_idx) const;
  262. bool is_item_checkable(int p_idx) const;
  263. bool is_item_radio_checkable(int p_idx) const;
  264. bool is_item_shortcut_disabled(int p_idx) const;
  265. bool is_item_shortcut_global(int p_idx) const;
  266. String get_item_tooltip(int p_idx) const;
  267. Ref<Shortcut> get_item_shortcut(int p_idx) const;
  268. int get_item_indent(int p_idx) const;
  269. int get_item_max_states(int p_idx) const;
  270. int get_item_state(int p_idx) const;
  271. void set_focused_item(int p_idx);
  272. int get_focused_item() const;
  273. void set_item_count(int p_count);
  274. int get_item_count() const;
  275. void set_prefer_native_menu(bool p_enabled);
  276. bool is_prefer_native_menu() const;
  277. bool is_native_menu() const;
  278. void scroll_to_item(int p_idx);
  279. bool activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only = false);
  280. void activate_item(int p_idx);
  281. void _about_to_popup();
  282. void _about_to_close();
  283. void remove_item(int p_idx);
  284. void add_separator(const String &p_text = String(), int p_id = -1);
  285. void clear(bool p_free_submenus = true);
  286. virtual String get_tooltip(const Point2 &p_pos) const;
  287. void add_autohide_area(const Rect2 &p_area);
  288. void clear_autohide_areas();
  289. void set_hide_on_item_selection(bool p_enabled);
  290. bool is_hide_on_item_selection() const;
  291. void set_hide_on_checkable_item_selection(bool p_enabled);
  292. bool is_hide_on_checkable_item_selection() const;
  293. void set_hide_on_multistate_item_selection(bool p_enabled);
  294. bool is_hide_on_multistate_item_selection() const;
  295. void set_submenu_popup_delay(float p_time);
  296. float get_submenu_popup_delay() const;
  297. void set_allow_search(bool p_allow);
  298. bool get_allow_search() const;
  299. virtual void popup(const Rect2i &p_bounds = Rect2i()) override;
  300. virtual void set_visible(bool p_visible) override;
  301. PopupMenu();
  302. ~PopupMenu();
  303. };
  304. #endif // POPUP_MENU_H