| 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 "core/os/keyboard.h" |
| 34 | #include "scene/main/window.h" |
| 35 | |
| 36 | void MenuButton::(const Ref<InputEvent> &p_event) { |
| 37 | ERR_FAIL_COND(p_event.is_null()); |
| 38 | |
| 39 | if (disable_shortcuts) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (p_event->is_pressed() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) { |
| 44 | accept_event(); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | Button::shortcut_input(p_event); |
| 49 | } |
| 50 | |
| 51 | void MenuButton::(bool p_visible) { |
| 52 | set_pressed(p_visible); |
| 53 | |
| 54 | if (!p_visible) { |
| 55 | set_process_internal(false); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (switch_on_hover) { |
| 60 | Window *wnd = Object::cast_to<Window>(get_viewport()); |
| 61 | if (wnd) { |
| 62 | mouse_pos_adjusted = wnd->get_position(); |
| 63 | |
| 64 | if (wnd->is_embedded()) { |
| 65 | Window *wnd_parent = Object::cast_to<Window>(wnd->get_parent()->get_viewport()); |
| 66 | while (wnd_parent) { |
| 67 | if (!wnd_parent->is_embedded()) { |
| 68 | mouse_pos_adjusted += wnd_parent->get_position(); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | wnd_parent = Object::cast_to<Window>(wnd_parent->get_parent()->get_viewport()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | set_process_internal(true); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void MenuButton::() { |
| 82 | if (popup->is_visible()) { |
| 83 | popup->hide(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | show_popup(); |
| 88 | } |
| 89 | |
| 90 | PopupMenu *MenuButton::() const { |
| 91 | return popup; |
| 92 | } |
| 93 | |
| 94 | void MenuButton::() { |
| 95 | if (!get_viewport()) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | emit_signal(SNAME("about_to_popup" )); |
| 100 | Rect2 rect = get_screen_rect(); |
| 101 | rect.position.y += rect.size.height; |
| 102 | rect.size.height = 0; |
| 103 | popup->set_size(rect.size); |
| 104 | if (is_layout_rtl()) { |
| 105 | rect.position.x += rect.size.width - popup->get_size().width; |
| 106 | } |
| 107 | popup->set_position(rect.position); |
| 108 | |
| 109 | // If not triggered by the mouse, start the popup with its first enabled item focused. |
| 110 | if (!_was_pressed_by_mouse()) { |
| 111 | for (int i = 0; i < popup->get_item_count(); i++) { |
| 112 | if (!popup->is_item_disabled(i)) { |
| 113 | popup->set_focused_item(i); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | popup->popup(); |
| 120 | } |
| 121 | |
| 122 | void MenuButton::(bool p_enabled) { |
| 123 | switch_on_hover = p_enabled; |
| 124 | } |
| 125 | |
| 126 | bool MenuButton::() { |
| 127 | return switch_on_hover; |
| 128 | } |
| 129 | |
| 130 | void MenuButton::(int p_count) { |
| 131 | ERR_FAIL_COND(p_count < 0); |
| 132 | |
| 133 | if (popup->get_item_count() == p_count) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | popup->set_item_count(p_count); |
| 138 | notify_property_list_changed(); |
| 139 | } |
| 140 | |
| 141 | int MenuButton::() const { |
| 142 | return popup->get_item_count(); |
| 143 | } |
| 144 | |
| 145 | void MenuButton::(int p_what) { |
| 146 | switch (p_what) { |
| 147 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { |
| 148 | popup->set_layout_direction((Window::LayoutDirection)get_layout_direction()); |
| 149 | } break; |
| 150 | |
| 151 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 152 | if (!is_visible_in_tree()) { |
| 153 | popup->hide(); |
| 154 | } |
| 155 | } break; |
| 156 | |
| 157 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 158 | Vector2i mouse_pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted; |
| 159 | MenuButton * = Object::cast_to<MenuButton>(get_viewport()->gui_find_control(mouse_pos)); |
| 160 | |
| 161 | if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() && |
| 162 | (get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) { |
| 163 | popup->hide(); |
| 164 | |
| 165 | menu_btn_other->pressed(); |
| 166 | // As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually. |
| 167 | menu_btn_other->get_popup()->set_focused_item(-1); |
| 168 | } |
| 169 | } break; |
| 170 | |
| 171 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 172 | popup->set_auto_translate(is_auto_translating()); |
| 173 | } break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bool MenuButton::(const StringName &p_name, const Variant &p_value) { |
| 178 | Vector<String> components = String(p_name).split("/" , true, 2); |
| 179 | if (components.size() >= 2 && components[0] == "popup" ) { |
| 180 | bool valid; |
| 181 | popup->set(String(p_name).trim_prefix("popup/" ), p_value, &valid); |
| 182 | return valid; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool MenuButton::(const StringName &p_name, Variant &r_ret) const { |
| 188 | Vector<String> components = String(p_name).split("/" , true, 2); |
| 189 | if (components.size() >= 2 && components[0] == "popup" ) { |
| 190 | bool valid; |
| 191 | r_ret = popup->get(String(p_name).trim_prefix("popup/" ), &valid); |
| 192 | return valid; |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | void MenuButton::(List<PropertyInfo> *p_list) const { |
| 198 | for (int i = 0; i < popup->get_item_count(); i++) { |
| 199 | p_list->push_back(PropertyInfo(Variant::STRING, vformat("popup/item_%d/text" , i))); |
| 200 | |
| 201 | PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("popup/item_%d/icon" , i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D" ); |
| 202 | pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0); |
| 203 | p_list->push_back(pi); |
| 204 | |
| 205 | pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/checkable" , i), PROPERTY_HINT_ENUM, "No,As Checkbox,As Radio Button" ); |
| 206 | pi.usage &= ~(!popup->is_item_checkable(i) ? PROPERTY_USAGE_STORAGE : 0); |
| 207 | p_list->push_back(pi); |
| 208 | |
| 209 | pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/checked" , i)); |
| 210 | pi.usage &= ~(!popup->is_item_checked(i) ? PROPERTY_USAGE_STORAGE : 0); |
| 211 | p_list->push_back(pi); |
| 212 | |
| 213 | pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id" , i), PROPERTY_HINT_RANGE, "0,10,1,or_greater" ); |
| 214 | p_list->push_back(pi); |
| 215 | |
| 216 | pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/disabled" , i)); |
| 217 | pi.usage &= ~(!popup->is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0); |
| 218 | p_list->push_back(pi); |
| 219 | |
| 220 | pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/separator" , i)); |
| 221 | pi.usage &= ~(!popup->is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0); |
| 222 | p_list->push_back(pi); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void MenuButton::() { |
| 227 | ClassDB::bind_method(D_METHOD("get_popup" ), &MenuButton::get_popup); |
| 228 | ClassDB::bind_method(D_METHOD("show_popup" ), &MenuButton::show_popup); |
| 229 | ClassDB::bind_method(D_METHOD("set_switch_on_hover" , "enable" ), &MenuButton::set_switch_on_hover); |
| 230 | ClassDB::bind_method(D_METHOD("is_switch_on_hover" ), &MenuButton::is_switch_on_hover); |
| 231 | ClassDB::bind_method(D_METHOD("set_disable_shortcuts" , "disabled" ), &MenuButton::set_disable_shortcuts); |
| 232 | |
| 233 | ClassDB::bind_method(D_METHOD("set_item_count" , "count" ), &MenuButton::set_item_count); |
| 234 | ClassDB::bind_method(D_METHOD("get_item_count" ), &MenuButton::get_item_count); |
| 235 | |
| 236 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover" ), "set_switch_on_hover" , "is_switch_on_hover" ); |
| 237 | ADD_ARRAY_COUNT("Items" , "item_count" , "set_item_count" , "get_item_count" , "popup/item_" ); |
| 238 | |
| 239 | ADD_SIGNAL(MethodInfo("about_to_popup" )); |
| 240 | } |
| 241 | |
| 242 | void MenuButton::(bool p_disabled) { |
| 243 | disable_shortcuts = p_disabled; |
| 244 | } |
| 245 | |
| 246 | MenuButton::(const String &p_text) : |
| 247 | Button(p_text) { |
| 248 | set_flat(true); |
| 249 | set_toggle_mode(true); |
| 250 | set_disable_shortcuts(false); |
| 251 | set_process_shortcut_input(true); |
| 252 | set_focus_mode(FOCUS_NONE); |
| 253 | set_action_mode(ACTION_MODE_BUTTON_PRESS); |
| 254 | |
| 255 | popup = memnew(PopupMenu); |
| 256 | popup->hide(); |
| 257 | add_child(popup, false, INTERNAL_MODE_FRONT); |
| 258 | popup->connect("about_to_popup" , callable_mp(this, &MenuButton::_popup_visibility_changed).bind(true)); |
| 259 | popup->connect("popup_hide" , callable_mp(this, &MenuButton::_popup_visibility_changed).bind(false)); |
| 260 | } |
| 261 | |
| 262 | MenuButton::() { |
| 263 | } |
| 264 | |