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