2
0

popup_menu.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* popup_menu.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/input/shortcut.h"
  33. #include "scene/gui/margin_container.h"
  34. #include "scene/gui/popup.h"
  35. #include "scene/gui/scroll_container.h"
  36. #include "scene/resources/text_line.h"
  37. class PopupMenu : public Popup {
  38. GDCLASS(PopupMenu, Popup);
  39. struct Item {
  40. Ref<Texture2D> icon;
  41. String text;
  42. String xl_text;
  43. Ref<TextLine> text_buf;
  44. Ref<TextLine> accel_text_buf;
  45. Dictionary opentype_features;
  46. String language;
  47. Control::TextDirection text_direction = Control::TEXT_DIRECTION_AUTO;
  48. bool checked = false;
  49. enum {
  50. CHECKABLE_TYPE_NONE,
  51. CHECKABLE_TYPE_CHECK_BOX,
  52. CHECKABLE_TYPE_RADIO_BUTTON,
  53. } checkable_type;
  54. int max_states = 0;
  55. int state = 0;
  56. bool separator = false;
  57. bool disabled = false;
  58. bool dirty = true;
  59. int id = 0;
  60. Variant metadata;
  61. String submenu;
  62. String tooltip;
  63. uint32_t accel = 0;
  64. int _ofs_cache = 0;
  65. int _height_cache = 0;
  66. int h_ofs = 0;
  67. Ref<Shortcut> shortcut;
  68. bool shortcut_is_global = false;
  69. bool shortcut_is_disabled = false;
  70. // Returns (0,0) if icon is null.
  71. Size2 get_icon_size() const {
  72. return icon.is_null() ? Size2() : icon->get_size();
  73. }
  74. Item() {
  75. text_buf.instantiate();
  76. accel_text_buf.instantiate();
  77. checkable_type = CHECKABLE_TYPE_NONE;
  78. }
  79. };
  80. bool close_allowed = false;
  81. Timer *minimum_lifetime_timer = nullptr;
  82. Timer *submenu_timer;
  83. List<Rect2> autohide_areas;
  84. Vector<Item> items;
  85. int initial_button_mask = 0;
  86. bool during_grabbed_click = false;
  87. int mouse_over = -1;
  88. int submenu_over = -1;
  89. Rect2 parent_rect;
  90. String _get_accel_text(const Item &p_item) const;
  91. int _get_mouse_over(const Point2 &p_over) const;
  92. virtual Size2 _get_contents_minimum_size() const override;
  93. int _get_item_height(int p_item) const;
  94. int _get_items_total_height() const;
  95. void _scroll_to_item(int p_item);
  96. void _shape_item(int p_item);
  97. virtual void gui_input(const Ref<InputEvent> &p_event);
  98. void _activate_submenu(int p_over);
  99. void _submenu_timeout();
  100. uint64_t popup_time_msec = 0;
  101. bool hide_on_item_selection = true;
  102. bool hide_on_checkable_item_selection = true;
  103. bool hide_on_multistate_item_selection = false;
  104. Vector2 moved;
  105. Map<Ref<Shortcut>, int> shortcut_refcount;
  106. void _ref_shortcut(Ref<Shortcut> p_sc);
  107. void _unref_shortcut(Ref<Shortcut> p_sc);
  108. bool allow_search = true;
  109. uint64_t search_time_msec = 0;
  110. String search_string = "";
  111. MarginContainer *margin_container;
  112. ScrollContainer *scroll_container;
  113. Control *control;
  114. void _draw_items();
  115. void _draw_background();
  116. void _minimum_lifetime_timeout();
  117. void _close_pressed();
  118. protected:
  119. void _notification(int p_what);
  120. bool _set(const StringName &p_name, const Variant &p_value);
  121. bool _get(const StringName &p_name, Variant &r_ret) const;
  122. void _get_property_list(List<PropertyInfo> *p_list) const;
  123. static void _bind_methods();
  124. public:
  125. // ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes,
  126. // this value should be updated to reflect the new size.
  127. static const int ITEM_PROPERTY_SIZE = 10;
  128. void add_item(const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  129. void add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  130. void add_check_item(const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  131. void add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  132. void add_radio_check_item(const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  133. void add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, uint32_t p_accel = 0);
  134. 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);
  135. void add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  136. void add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  137. void add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  138. void add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  139. void add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  140. void add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
  141. void add_submenu_item(const String &p_label, const String &p_submenu, int p_id = -1);
  142. void set_item_text(int p_idx, const String &p_text);
  143. void set_item_text_direction(int p_idx, Control::TextDirection p_text_direction);
  144. void set_item_opentype_feature(int p_idx, const String &p_name, int p_value);
  145. void clear_item_opentype_features(int p_idx);
  146. void set_item_language(int p_idx, const String &p_language);
  147. void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon);
  148. void set_item_checked(int p_idx, bool p_checked);
  149. void set_item_id(int p_idx, int p_id);
  150. void set_item_accelerator(int p_idx, uint32_t p_accel);
  151. void set_item_metadata(int p_idx, const Variant &p_meta);
  152. void set_item_disabled(int p_idx, bool p_disabled);
  153. void set_item_submenu(int p_idx, const String &p_submenu);
  154. void set_item_as_separator(int p_idx, bool p_separator);
  155. void set_item_as_checkable(int p_idx, bool p_checkable);
  156. void set_item_as_radio_checkable(int p_idx, bool p_radio_checkable);
  157. void set_item_tooltip(int p_idx, const String &p_tooltip);
  158. void set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global = false);
  159. void set_item_h_offset(int p_idx, int p_offset);
  160. void set_item_multistate(int p_idx, int p_state);
  161. void toggle_item_multistate(int p_idx);
  162. void set_item_shortcut_disabled(int p_idx, bool p_disabled);
  163. void toggle_item_checked(int p_idx);
  164. String get_item_text(int p_idx) const;
  165. Control::TextDirection get_item_text_direction(int p_idx) const;
  166. int get_item_opentype_feature(int p_idx, const String &p_name) const;
  167. String get_item_language(int p_idx) const;
  168. int get_item_idx_from_text(const String &text) const;
  169. Ref<Texture2D> get_item_icon(int p_idx) const;
  170. bool is_item_checked(int p_idx) const;
  171. int get_item_id(int p_idx) const;
  172. int get_item_index(int p_id) const;
  173. uint32_t get_item_accelerator(int p_idx) const;
  174. Variant get_item_metadata(int p_idx) const;
  175. bool is_item_disabled(int p_idx) const;
  176. String get_item_submenu(int p_idx) const;
  177. bool is_item_separator(int p_idx) const;
  178. bool is_item_checkable(int p_idx) const;
  179. bool is_item_radio_checkable(int p_idx) const;
  180. bool is_item_shortcut_disabled(int p_idx) const;
  181. String get_item_tooltip(int p_idx) const;
  182. Ref<Shortcut> get_item_shortcut(int p_idx) const;
  183. int get_item_state(int p_idx) const;
  184. int get_current_index() const;
  185. void set_item_count(int p_count);
  186. int get_item_count() const;
  187. bool activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only = false);
  188. void activate_item(int p_item);
  189. void remove_item(int p_idx);
  190. void add_separator(const String &p_text = String(), int p_id = -1);
  191. void clear();
  192. void set_parent_rect(const Rect2 &p_rect);
  193. virtual String get_tooltip(const Point2 &p_pos) const;
  194. virtual void get_translatable_strings(List<String> *p_strings) const override;
  195. void add_autohide_area(const Rect2 &p_area);
  196. void clear_autohide_areas();
  197. void set_hide_on_item_selection(bool p_enabled);
  198. bool is_hide_on_item_selection() const;
  199. void set_hide_on_checkable_item_selection(bool p_enabled);
  200. bool is_hide_on_checkable_item_selection() const;
  201. void set_hide_on_multistate_item_selection(bool p_enabled);
  202. bool is_hide_on_multistate_item_selection() const;
  203. void set_submenu_popup_delay(float p_time);
  204. float get_submenu_popup_delay() const;
  205. void set_allow_search(bool p_allow);
  206. bool get_allow_search() const;
  207. virtual void popup(const Rect2 &p_bounds = Rect2());
  208. void take_mouse_focus();
  209. PopupMenu();
  210. ~PopupMenu();
  211. };
  212. #endif