popup_menu.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* popup_menu.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "scene/gui/popup.h"
  33. class PopupMenu : public Popup {
  34. GDCLASS(PopupMenu, Popup);
  35. struct Item {
  36. Ref<Texture> icon;
  37. String text;
  38. String xl_text;
  39. bool checked;
  40. enum {
  41. CHECKABLE_TYPE_NONE,
  42. CHECKABLE_TYPE_CHECK_BOX,
  43. CHECKABLE_TYPE_RADIO_BUTTON,
  44. } checkable_type;
  45. int max_states;
  46. int state;
  47. bool separator;
  48. bool disabled;
  49. int id;
  50. Variant metadata;
  51. String submenu;
  52. String tooltip;
  53. uint32_t accel;
  54. int _ofs_cache;
  55. int h_ofs;
  56. Ref<ShortCut> shortcut;
  57. bool shortcut_is_global;
  58. bool shortcut_is_disabled;
  59. Item() {
  60. checked = false;
  61. checkable_type = CHECKABLE_TYPE_NONE;
  62. separator = false;
  63. max_states = 0;
  64. state = 0;
  65. accel = 0;
  66. disabled = false;
  67. _ofs_cache = 0;
  68. h_ofs = 0;
  69. shortcut_is_global = false;
  70. shortcut_is_disabled = false;
  71. }
  72. };
  73. Timer *submenu_timer;
  74. List<Rect2> autohide_areas;
  75. Vector<Item> items;
  76. int initial_button_mask;
  77. bool during_grabbed_click;
  78. int mouse_over;
  79. int submenu_over;
  80. Rect2 parent_rect;
  81. String _get_accel_text(int p_item) const;
  82. int _get_mouse_over(const Point2 &p_over) const;
  83. virtual Size2 get_minimum_size() const;
  84. void _scroll(float p_factor, const Point2 &p_over);
  85. void _gui_input(const Ref<InputEvent> &p_event);
  86. void _activate_submenu(int over);
  87. void _submenu_timeout();
  88. bool invalidated_click;
  89. bool hide_on_item_selection;
  90. bool hide_on_checkable_item_selection;
  91. bool hide_on_multistate_item_selection;
  92. bool hide_on_window_lose_focus;
  93. Vector2 moved;
  94. Array _get_items() const;
  95. void _set_items(const Array &p_items);
  96. Map<Ref<ShortCut>, int> shortcut_refcount;
  97. void _ref_shortcut(Ref<ShortCut> p_sc);
  98. void _unref_shortcut(Ref<ShortCut> p_sc);
  99. bool allow_search;
  100. uint64_t search_time_msec;
  101. String search_string;
  102. protected:
  103. virtual bool has_point(const Point2 &p_point) const;
  104. friend class MenuButton;
  105. void _notification(int p_what);
  106. static void _bind_methods();
  107. public:
  108. void add_item(const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  109. void add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  110. void add_check_item(const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  111. void add_icon_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  112. void add_radio_check_item(const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  113. void add_icon_radio_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  114. void add_multistate_item(const String &p_label, int p_max_states, int p_default_state = 0, int p_id = -1, uint32_t p_accel = 0);
  115. void add_shortcut(const Ref<ShortCut> &p_shortcut, int p_id = -1, bool p_global = false);
  116. void add_icon_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_id = -1, bool p_global = false);
  117. void add_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_id = -1, bool p_global = false);
  118. void add_icon_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_id = -1, bool p_global = false);
  119. void add_radio_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_id = -1, bool p_global = false);
  120. void add_icon_radio_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_id = -1, bool p_global = false);
  121. void add_submenu_item(const String &p_label, const String &p_submenu, int p_id = -1);
  122. void set_item_text(int p_idx, const String &p_text);
  123. void set_item_icon(int p_idx, const Ref<Texture> &p_icon);
  124. void set_item_checked(int p_idx, bool p_checked);
  125. void set_item_id(int p_idx, int p_id);
  126. void set_item_accelerator(int p_idx, uint32_t p_accel);
  127. void set_item_metadata(int p_idx, const Variant &p_meta);
  128. void set_item_disabled(int p_idx, bool p_disabled);
  129. void set_item_submenu(int p_idx, const String &p_submenu);
  130. void set_item_as_separator(int p_idx, bool p_separator);
  131. void set_item_as_checkable(int p_idx, bool p_checkable);
  132. void set_item_as_radio_checkable(int p_idx, bool p_radio_checkable);
  133. void set_item_tooltip(int p_idx, const String &p_tooltip);
  134. void set_item_shortcut(int p_idx, const Ref<ShortCut> &p_shortcut, bool p_global = false);
  135. void set_item_h_offset(int p_idx, int p_offset);
  136. void set_item_multistate(int p_idx, int p_state);
  137. void toggle_item_multistate(int p_idx);
  138. void set_item_shortcut_disabled(int p_idx, bool p_disabled);
  139. void toggle_item_checked(int p_idx);
  140. String get_item_text(int p_idx) const;
  141. int get_item_idx_from_text(const String &text) const;
  142. Ref<Texture> get_item_icon(int p_idx) const;
  143. bool is_item_checked(int p_idx) const;
  144. int get_item_id(int p_idx) const;
  145. int get_item_index(int p_id) const;
  146. uint32_t get_item_accelerator(int p_idx) const;
  147. Variant get_item_metadata(int p_idx) const;
  148. bool is_item_disabled(int p_idx) const;
  149. String get_item_submenu(int p_idx) const;
  150. bool is_item_separator(int p_idx) const;
  151. bool is_item_checkable(int p_idx) const;
  152. bool is_item_radio_checkable(int p_idx) const;
  153. bool is_item_shortcut_disabled(int p_idx) const;
  154. String get_item_tooltip(int p_idx) const;
  155. Ref<ShortCut> get_item_shortcut(int p_idx) const;
  156. int get_item_state(int p_idx) const;
  157. int get_current_index() const;
  158. int get_item_count() const;
  159. bool activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only = false);
  160. void activate_item(int p_item);
  161. void remove_item(int p_idx);
  162. void add_separator(const String &p_text = String(), int p_id = -1);
  163. void clear();
  164. void set_parent_rect(const Rect2 &p_rect);
  165. virtual String get_tooltip(const Point2 &p_pos) const;
  166. virtual void get_translatable_strings(List<String> *p_strings) const;
  167. void add_autohide_area(const Rect2 &p_area);
  168. void clear_autohide_areas();
  169. void set_hide_on_item_selection(bool p_enabled);
  170. bool is_hide_on_item_selection() const;
  171. void set_hide_on_checkable_item_selection(bool p_enabled);
  172. bool is_hide_on_checkable_item_selection() const;
  173. void set_hide_on_multistate_item_selection(bool p_enabled);
  174. bool is_hide_on_multistate_item_selection() const;
  175. void set_submenu_popup_delay(float p_time);
  176. float get_submenu_popup_delay() const;
  177. void set_allow_search(bool p_allow);
  178. bool get_allow_search() const;
  179. virtual void popup(const Rect2 &p_bounds = Rect2());
  180. void set_hide_on_window_lose_focus(bool p_enabled);
  181. bool is_hide_on_window_lose_focus() const;
  182. PopupMenu();
  183. ~PopupMenu();
  184. };
  185. #endif