popup_menu.h 14 KB

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