popup_menu.h 8.2 KB

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