popup_menu.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* popup_menu.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. bool checkable;
  44. bool separator;
  45. bool disabled;
  46. int ID;
  47. Variant metadata;
  48. String submenu;
  49. String tooltip;
  50. uint32_t accel;
  51. int _ofs_cache;
  52. int h_ofs;
  53. Ref<ShortCut> shortcut;
  54. bool shortcut_is_global;
  55. Item() {
  56. checked = false;
  57. checkable = false;
  58. separator = false;
  59. accel = 0;
  60. disabled = false;
  61. _ofs_cache = 0;
  62. h_ofs = 0;
  63. shortcut_is_global = false;
  64. }
  65. };
  66. Timer *submenu_timer;
  67. List<Rect2> autohide_areas;
  68. Vector<Item> items;
  69. int mouse_over;
  70. int submenu_over;
  71. Rect2 parent_rect;
  72. String _get_accel_text(int p_item) const;
  73. int _get_mouse_over(const Point2 &p_over) const;
  74. virtual Size2 get_minimum_size() const;
  75. void _gui_input(const Ref<InputEvent> &p_event);
  76. void _activate_submenu(int over);
  77. void _submenu_timeout();
  78. bool invalidated_click;
  79. bool hide_on_item_selection;
  80. bool hide_on_checkable_item_selection;
  81. Vector2 moved;
  82. Array _get_items() const;
  83. void _set_items(const Array &p_items);
  84. Map<Ref<ShortCut>, int> shortcut_refcount;
  85. void _ref_shortcut(Ref<ShortCut> p_sc);
  86. void _unref_shortcut(Ref<ShortCut> p_sc);
  87. protected:
  88. virtual bool has_point(const Point2 &p_point) const;
  89. friend class MenuButton;
  90. void _notification(int p_what);
  91. static void _bind_methods();
  92. public:
  93. void add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID = -1, uint32_t p_accel = 0);
  94. void add_item(const String &p_label, int p_ID = -1, uint32_t p_accel = 0);
  95. void add_icon_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID = -1, uint32_t p_accel = 0);
  96. void add_check_item(const String &p_label, int p_ID = -1, uint32_t p_accel = 0);
  97. void add_submenu_item(const String &p_label, const String &p_submenu, int p_ID = -1);
  98. void add_icon_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false);
  99. void add_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false);
  100. void add_icon_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false);
  101. void add_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false);
  102. void set_item_text(int p_idx, const String &p_text);
  103. void set_item_icon(int p_idx, const Ref<Texture> &p_icon);
  104. void set_item_checked(int p_idx, bool p_checked);
  105. void set_item_id(int p_idx, int p_ID);
  106. void set_item_accelerator(int p_idx, uint32_t p_accel);
  107. void set_item_metadata(int p_idx, const Variant &p_meta);
  108. void set_item_disabled(int p_idx, bool p_disabled);
  109. void set_item_submenu(int p_idx, const String &p_submenu);
  110. void set_item_as_separator(int p_idx, bool p_separator);
  111. void set_item_as_checkable(int p_idx, bool p_checkable);
  112. void set_item_tooltip(int p_idx, const String &p_tooltip);
  113. void set_item_shortcut(int p_idx, const Ref<ShortCut> &p_shortcut, bool p_global = false);
  114. void set_item_h_offset(int p_idx, int p_offset);
  115. void toggle_item_checked(int p_idx);
  116. String get_item_text(int p_idx) const;
  117. int get_item_idx_from_text(const String &text) const;
  118. Ref<Texture> get_item_icon(int p_idx) const;
  119. bool is_item_checked(int p_idx) const;
  120. int get_item_id(int p_idx) const;
  121. int get_item_index(int p_ID) const;
  122. uint32_t get_item_accelerator(int p_idx) const;
  123. Variant get_item_metadata(int p_idx) const;
  124. bool is_item_disabled(int p_idx) const;
  125. String get_item_submenu(int p_idx) const;
  126. bool is_item_separator(int p_idx) const;
  127. bool is_item_checkable(int p_idx) const;
  128. String get_item_tooltip(int p_idx) const;
  129. Ref<ShortCut> get_item_shortcut(int p_idx) const;
  130. int get_item_count() const;
  131. bool activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only = false);
  132. void activate_item(int p_item);
  133. void remove_item(int p_idx);
  134. void add_separator();
  135. void clear();
  136. void set_parent_rect(const Rect2 &p_rect);
  137. virtual String get_tooltip(const Point2 &p_pos) const;
  138. virtual void get_translatable_strings(List<String> *p_strings) const;
  139. void add_autohide_area(const Rect2 &p_area);
  140. void clear_autohide_areas();
  141. void set_invalidate_click_until_motion();
  142. void set_hide_on_item_selection(bool p_enabled);
  143. bool is_hide_on_item_selection();
  144. void set_hide_on_checkable_item_selection(bool p_enabled);
  145. bool is_hide_on_checkable_item_selection();
  146. PopupMenu();
  147. ~PopupMenu();
  148. };
  149. #endif