1/**************************************************************************/
2/* popup_menu.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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 "core/input/shortcut.h"
35#include "scene/gui/margin_container.h"
36#include "scene/gui/popup.h"
37#include "scene/gui/scroll_container.h"
38#include "scene/resources/text_line.h"
39
40class PopupMenu : public Popup {
41 GDCLASS(PopupMenu, Popup);
42
43 struct Item {
44 Ref<Texture2D> icon;
45 int icon_max_width = 0;
46 Color icon_modulate = Color(1, 1, 1, 1);
47 String text;
48 String xl_text;
49 Ref<TextLine> text_buf;
50 Ref<TextLine> accel_text_buf;
51
52 String language;
53 Control::TextDirection text_direction = Control::TEXT_DIRECTION_AUTO;
54
55 bool checked = false;
56 enum {
57 CHECKABLE_TYPE_NONE,
58 CHECKABLE_TYPE_CHECK_BOX,
59 CHECKABLE_TYPE_RADIO_BUTTON,
60 } checkable_type;
61 int max_states = 0;
62 int state = 0;
63 bool separator = false;
64 bool disabled = false;
65 bool dirty = true;
66 int id = 0;
67 Variant metadata;
68 String submenu;
69 String tooltip;
70 Key accel = Key::NONE;
71 int _ofs_cache = 0;
72 int _height_cache = 0;
73 int indent = 0;
74 Ref<Shortcut> shortcut;
75 bool shortcut_is_global = false;
76 bool shortcut_is_disabled = false;
77 bool allow_echo = false;
78
79 // Returns (0,0) if icon is null.
80 Size2 get_icon_size() const {
81 return icon.is_null() ? Size2() : icon->get_size();
82 }
83
84 Item() {
85 text_buf.instantiate();
86 accel_text_buf.instantiate();
87 checkable_type = CHECKABLE_TYPE_NONE;
88 }
89 };
90
91 bool close_allowed = false;
92 bool activated_by_keyboard = false;
93
94 Timer *minimum_lifetime_timer = nullptr;
95 Timer *submenu_timer = nullptr;
96 List<Rect2> autohide_areas;
97 Vector<Item> items;
98 BitField<MouseButtonMask> initial_button_mask;
99 bool during_grabbed_click = false;
100 int mouse_over = -1;
101 int submenu_over = -1;
102 String _get_accel_text(const Item &p_item) const;
103 int _get_mouse_over(const Point2 &p_over) const;
104 virtual Size2 _get_contents_minimum_size() const override;
105
106 int _get_item_height(int p_idx) const;
107 int _get_items_total_height() const;
108 Size2 _get_item_icon_size(int p_idx) const;
109
110 void _shape_item(int p_idx);
111
112 virtual void gui_input(const Ref<InputEvent> &p_event);
113 void _activate_submenu(int p_over, bool p_by_keyboard = false);
114 void _submenu_timeout();
115
116 uint64_t popup_time_msec = 0;
117 bool hide_on_item_selection = true;
118 bool hide_on_checkable_item_selection = true;
119 bool hide_on_multistate_item_selection = false;
120 Vector2 moved;
121
122 HashMap<Ref<Shortcut>, int> shortcut_refcount;
123
124 void _ref_shortcut(Ref<Shortcut> p_sc);
125 void _unref_shortcut(Ref<Shortcut> p_sc);
126
127 void _shortcut_changed();
128
129 bool allow_search = true;
130 uint64_t search_time_msec = 0;
131 String search_string = "";
132
133 MarginContainer *margin_container = nullptr;
134 ScrollContainer *scroll_container = nullptr;
135 Control *control = nullptr;
136
137 const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
138 const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
139 float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
140
141 struct ThemeCache {
142 Ref<StyleBox> panel_style;
143 Ref<StyleBox> hover_style;
144
145 Ref<StyleBox> separator_style;
146 Ref<StyleBox> labeled_separator_left;
147 Ref<StyleBox> labeled_separator_right;
148
149 int v_separation = 0;
150 int h_separation = 0;
151 int indent = 0;
152 int item_start_padding = 0;
153 int item_end_padding = 0;
154 int icon_max_width = 0;
155
156 Ref<Texture2D> checked;
157 Ref<Texture2D> checked_disabled;
158 Ref<Texture2D> unchecked;
159 Ref<Texture2D> unchecked_disabled;
160 Ref<Texture2D> radio_checked;
161 Ref<Texture2D> radio_checked_disabled;
162 Ref<Texture2D> radio_unchecked;
163 Ref<Texture2D> radio_unchecked_disabled;
164
165 Ref<Texture2D> submenu;
166 Ref<Texture2D> submenu_mirrored;
167
168 Ref<Font> font;
169 int font_size = 0;
170 Ref<Font> font_separator;
171 int font_separator_size = 0;
172
173 Color font_color;
174 Color font_hover_color;
175 Color font_disabled_color;
176 Color font_accelerator_color;
177 int font_outline_size = 0;
178 Color font_outline_color;
179
180 Color font_separator_color;
181 int font_separator_outline_size = 0;
182 Color font_separator_outline_color;
183 } theme_cache;
184
185 void _draw_items();
186 void _draw_background();
187
188 void _minimum_lifetime_timeout();
189 void _close_pressed();
190 void _menu_changed();
191
192protected:
193 virtual void add_child_notify(Node *p_child) override;
194 virtual void remove_child_notify(Node *p_child) override;
195
196 void _notification(int p_what);
197 bool _set(const StringName &p_name, const Variant &p_value);
198 bool _get(const StringName &p_name, Variant &r_ret) const;
199 void _get_property_list(List<PropertyInfo> *p_list) const;
200 static void _bind_methods();
201
202#ifndef DISABLE_DEPRECATED
203 void _add_shortcut_bind_compat_36493(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
204 void _add_icon_shortcut_bind_compat_36493(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
205 static void _bind_compatibility_methods();
206#endif
207
208public:
209 // ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes,
210 // this value should be updated to reflect the new size.
211 static const int ITEM_PROPERTY_SIZE = 10;
212
213 virtual void _parent_focused() override;
214
215 void add_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
216 void add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
217 void add_check_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
218 void add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
219 void add_radio_check_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
220 void add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
221
222 void add_multistate_item(const String &p_label, int p_max_states, int p_default_state = 0, int p_id = -1, Key p_accel = Key::NONE);
223
224 void add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false, bool p_allow_echo = false);
225 void add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false, bool p_allow_echo = false);
226 void add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
227 void add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
228 void add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
229 void add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
230
231 void add_submenu_item(const String &p_label, const String &p_submenu, int p_id = -1);
232
233 void set_item_text(int p_idx, const String &p_text);
234
235 void set_item_text_direction(int p_idx, Control::TextDirection p_text_direction);
236 void set_item_language(int p_idx, const String &p_language);
237 void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon);
238 void set_item_icon_max_width(int p_idx, int p_width);
239 void set_item_icon_modulate(int p_idx, const Color &p_modulate);
240 void set_item_checked(int p_idx, bool p_checked);
241 void set_item_id(int p_idx, int p_id);
242 void set_item_accelerator(int p_idx, Key p_accel);
243 void set_item_metadata(int p_idx, const Variant &p_meta);
244 void set_item_disabled(int p_idx, bool p_disabled);
245 void set_item_submenu(int p_idx, const String &p_submenu);
246 void set_item_as_separator(int p_idx, bool p_separator);
247 void set_item_as_checkable(int p_idx, bool p_checkable);
248 void set_item_as_radio_checkable(int p_idx, bool p_radio_checkable);
249 void set_item_tooltip(int p_idx, const String &p_tooltip);
250 void set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global = false);
251 void set_item_indent(int p_idx, int p_indent);
252 void set_item_multistate(int p_idx, int p_state);
253 void toggle_item_multistate(int p_idx);
254 void set_item_shortcut_disabled(int p_idx, bool p_disabled);
255
256 void toggle_item_checked(int p_idx);
257
258 String get_item_text(int p_idx) const;
259 String get_item_xl_text(int p_idx) const;
260 Control::TextDirection get_item_text_direction(int p_idx) const;
261 String get_item_language(int p_idx) const;
262 int get_item_idx_from_text(const String &text) const;
263 Ref<Texture2D> get_item_icon(int p_idx) const;
264 int get_item_icon_max_width(int p_idx) const;
265 Color get_item_icon_modulate(int p_idx) const;
266 bool is_item_checked(int p_idx) const;
267 int get_item_id(int p_idx) const;
268 int get_item_index(int p_id) const;
269 Key get_item_accelerator(int p_idx) const;
270 Variant get_item_metadata(int p_idx) const;
271 bool is_item_disabled(int p_idx) const;
272 String get_item_submenu(int p_idx) const;
273 bool is_item_separator(int p_idx) const;
274 bool is_item_checkable(int p_idx) const;
275 bool is_item_radio_checkable(int p_idx) const;
276 bool is_item_shortcut_disabled(int p_idx) const;
277 bool is_item_shortcut_global(int p_idx) const;
278 String get_item_tooltip(int p_idx) const;
279 Ref<Shortcut> get_item_shortcut(int p_idx) const;
280 int get_item_indent(int p_idx) const;
281 int get_item_max_states(int p_idx) const;
282 int get_item_state(int p_idx) const;
283
284 void set_focused_item(int p_idx);
285 int get_focused_item() const;
286
287 void set_item_count(int p_count);
288 int get_item_count() const;
289
290 void scroll_to_item(int p_idx);
291
292 bool activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only = false);
293 void activate_item(int p_idx);
294
295 void remove_item(int p_idx);
296
297 void add_separator(const String &p_text = String(), int p_id = -1);
298
299 void clear();
300
301 virtual String get_tooltip(const Point2 &p_pos) const;
302
303 void add_autohide_area(const Rect2 &p_area);
304 void clear_autohide_areas();
305
306 void set_hide_on_item_selection(bool p_enabled);
307 bool is_hide_on_item_selection() const;
308
309 void set_hide_on_checkable_item_selection(bool p_enabled);
310 bool is_hide_on_checkable_item_selection() const;
311
312 void set_hide_on_multistate_item_selection(bool p_enabled);
313 bool is_hide_on_multistate_item_selection() const;
314
315 void set_submenu_popup_delay(float p_time);
316 float get_submenu_popup_delay() const;
317
318 void set_allow_search(bool p_allow);
319 bool get_allow_search() const;
320
321 virtual void popup(const Rect2i &p_bounds = Rect2i()) override;
322
323 void take_mouse_focus();
324
325 PopupMenu();
326 ~PopupMenu();
327};
328
329#endif // POPUP_MENU_H
330