Debian Codegraph @

scene/gui/menu_button.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* menu_button.cpp */
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 #include "menu_button.h"
32
33 #include "scene/main/window.h"
34
35 void MenuButton::shortcut_input(const Ref<InputEvent> &p_event) {
36 ERR_FAIL_COND(p_event.is_null());
37
38 if (disable_shortcuts) {
39 return;
40 }
41
42 if (p_event->is_pressed() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) {
43 accept_event();
44 return;
45 }
46
47 Button::shortcut_input(p_event);
48 }
49
50 void MenuButton::_popup_visibility_changed(bool p_visible) {
51 set_pressed(p_visible);
52
53 if (!p_visible) {
54 set_process_internal(false);
55 return;
56 }
57
58 if (switch_on_hover) {
59 set_process_internal(true);
60 }
61 }
62
63 void MenuButton::pressed() {
64 if (popup->is_visible()) {
65 popup->hide();
66 return;
67 }
68
69 show_popup();
70 }
71
72 PopupMenu *MenuButton::get_popup() const {
73 return popup;
74 }
75
76 void MenuButton::show_popup() {
77 if (!get_viewport()) {
78 return;
79 }
80
81 emit_signal(SNAME("about_to_popup"));
82 Rect2 rect = get_screen_rect();
83 rect.position.y += rect.size.height;
84 if (get_viewport()->is_embedding_subwindows() && popup->get_force_native()) {
85 Transform2D xform = get_viewport()->get_popup_base_transform_native();
86 rect = xform.xform(rect);
87 }
88 Rect2i scr_usable = DisplayServer::get_singleton()->screen_get_usable_rect(get_window()->get_current_screen());
89 Size2i max_size;
90 if (scr_usable.has_area()) {
91 real_t max_h = scr_usable.get_end().y - rect.position.y;
92 if (max_h >= 4 * rect.size.height) {
93 max_size = Size2(RS::get_singleton()->get_maximum_viewport_size().width, max_h);
94 }
95 }
96 popup->set_max_size(max_size);
97 if (is_layout_rtl()) {
98 rect.position.x += rect.size.width - popup->get_size().width;
99 }
100 popup->set_position(rect.position);
101
102 // If not triggered by the mouse, start the popup with its first enabled item focused.
103 if (!_was_pressed_by_mouse()) {
104 for (int i = 0; i < popup->get_item_count(); i++) {
105 if (!popup->is_item_disabled(i)) {
106 popup->set_focused_item(i);
107 break;
108 }
109 }
110 }
111
112 popup->popup();
113 }
114
115 void MenuButton::set_switch_on_hover(bool p_enabled) {
116 switch_on_hover = p_enabled;
117 }
118
119 bool MenuButton::is_switch_on_hover() {
120 return switch_on_hover;
121 }
122
123 void MenuButton::set_item_count(int p_count) {
124 ERR_FAIL_COND(p_count < 0);
125
126 if (popup->get_item_count() == p_count) {
127 return;
128 }
129
130 popup->set_item_count(p_count);
131 notify_property_list_changed();
132 }
133
134 int MenuButton::get_item_count() const {
135 return popup->get_item_count();
136 }
137
138 void MenuButton::_notification(int p_what) {
139 switch (p_what) {
140 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
141 RID ae = get_accessibility_element();
142 ERR_FAIL_COND(ae.is_null());
143
144 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
145 DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
146 } break;
147
148 case NOTIFICATION_TRANSLATION_CHANGED:
149 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
150 popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
151 } break;
152
153 case NOTIFICATION_VISIBILITY_CHANGED: {
154 if (!is_visible_in_tree()) {
155 popup->hide();
156 }
157 } break;
158
159 case NOTIFICATION_INTERNAL_PROCESS: {
160 MenuButton *menu_btn_other = Object::cast_to<MenuButton>(get_viewport()->gui_get_hovered_control());
161
162 if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() &&
163 (get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) {
164 popup->hide();
165
166 menu_btn_other->pressed();
167 // As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually.
168 menu_btn_other->get_popup()->set_focused_item(-1);
169 }
170 } break;
171 }
172 }
173
174 bool MenuButton::_set(const StringName &p_name, const Variant &p_value) {
175 const String sname = p_name;
176 if (property_helper.is_property_valid(sname)) {
177 bool valid;
178 popup->set(sname.trim_prefix("popup/"), p_value, &valid);
179 return valid;
180 }
181 return false;
182 }
183
184 bool MenuButton::_get(const StringName &p_name, Variant &r_ret) const {
185 const String sname = p_name;
186 if (property_helper.is_property_valid(sname)) {
187 bool valid;
188 r_ret = popup->get(sname.trim_prefix("popup/"), &valid);
189 return valid;
190 }
191 return false;
192 }
193
194 void MenuButton::_bind_methods() {
195 ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
196 ClassDB::bind_method(D_METHOD("show_popup"), &MenuButton::show_popup);
197 ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
198 ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
199 ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);
200
201 ClassDB::bind_method(D_METHOD("set_item_count", "count"), &MenuButton::set_item_count);
202 ClassDB::bind_method(D_METHOD("get_item_count"), &MenuButton::get_item_count);
203
204 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
205 ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
206
207 ADD_SIGNAL(MethodInfo("about_to_popup"));
208
209 ADD_CLASS_DEPENDENCY("PopupMenu");
210
211 PopupMenu::Item defaults(true);
212
213 base_property_helper.set_prefix("popup/item_");
214 base_property_helper.set_array_length_getter(&MenuButton::get_item_count);
215 base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text);
216 base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon);
217 base_property_helper.register_property(PropertyInfo(Variant::INT, "checkable", PROPERTY_HINT_ENUM, "No,As Checkbox,As Radio Button"), defaults.checkable_type);
218 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "checked"), defaults.checked);
219 base_property_helper.register_property(PropertyInfo(Variant::INT, "id", PROPERTY_HINT_RANGE, "0,10,1,or_greater", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL), defaults.id);
220 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled);
221 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator);
222 PropertyListHelper::register_base_helper(&base_property_helper);
223 }
224
225 void MenuButton::set_disable_shortcuts(bool p_disabled) {
226 disable_shortcuts = p_disabled;
227 }
228
229 #ifdef TOOLS_ENABLED
230 PackedStringArray MenuButton::get_configuration_warnings() const {
231 PackedStringArray warnings = Button::get_configuration_warnings();
232 warnings.append_array(popup->get_configuration_warnings());
233 return warnings;
234 }
235 #endif
236
237 MenuButton::MenuButton(const String &p_text) :
238 Button(p_text) {
239 set_flat(true);
240 set_toggle_mode(true);
241 set_disable_shortcuts(false);
242 set_process_shortcut_input(true);
243 set_focus_mode(FOCUS_ACCESSIBILITY);
244 set_action_mode(ACTION_MODE_BUTTON_PRESS);
245
246 popup = memnew(PopupMenu);
247 popup->hide();
248 add_child(popup, false, INTERNAL_MODE_FRONT);
249 popup->connect("about_to_popup", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(true));
250 popup->connect("popup_hide", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(false));
251
252 property_helper.setup_for_instance(base_property_helper, this);
253 }
254
255 MenuButton::~MenuButton() {
256 }