| 1 | /**************************************************************************/ |
| 2 | /* popup_menu.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 "popup_menu.h" |
| 32 | #include "popup_menu.compat.inc" |
| 33 | |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/input/input.h" |
| 36 | #include "core/os/keyboard.h" |
| 37 | #include "core/os/os.h" |
| 38 | #include "scene/gui/graph_edit.h" |
| 39 | #include "scene/gui/menu_bar.h" |
| 40 | #include "scene/gui/panel_container.h" |
| 41 | #include "scene/main/timer.h" |
| 42 | #include "scene/resources/style_box_flat.h" |
| 43 | #include "scene/theme/theme_db.h" |
| 44 | |
| 45 | HashMap<NativeMenu::SystemMenus, PopupMenu *> PopupMenu::system_menus; |
| 46 | |
| 47 | bool PopupMenu::_set_item_accelerator(int p_index, const Ref<InputEventKey> &p_ie) { |
| 48 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 49 | if (p_ie->get_physical_keycode() == Key::NONE && p_ie->get_keycode() == Key::NONE && p_ie->get_key_label() != Key::NONE) { |
| 50 | nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_key_label_with_modifiers()); |
| 51 | return true; |
| 52 | } else if (p_ie->get_keycode() != Key::NONE) { |
| 53 | nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_keycode_with_modifiers()); |
| 54 | return true; |
| 55 | } else if (p_ie->get_physical_keycode() != Key::NONE) { |
| 56 | nmenu->set_item_accelerator(global_menu, p_index, DisplayServer::get_singleton()->keyboard_get_keycode_from_physical(p_ie->get_physical_keycode_with_modifiers())); |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | void PopupMenu::_set_item_checkable_type(int p_index, int p_checkable_type) { |
| 63 | switch (p_checkable_type) { |
| 64 | case Item::CHECKABLE_TYPE_NONE: { |
| 65 | set_item_as_checkable(p_index, false); |
| 66 | } break; |
| 67 | case Item::CHECKABLE_TYPE_CHECK_BOX: { |
| 68 | set_item_as_checkable(p_index, true); |
| 69 | } break; |
| 70 | case Item::CHECKABLE_TYPE_RADIO_BUTTON: { |
| 71 | set_item_as_radio_checkable(p_index, true); |
| 72 | } break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int PopupMenu::_get_item_checkable_type(int p_index) const { |
| 77 | ERR_FAIL_INDEX_V(p_index, items.size(), Item::CHECKABLE_TYPE_NONE); |
| 78 | return items[p_index].checkable_type; |
| 79 | } |
| 80 | |
| 81 | RID PopupMenu::bind_global_menu() { |
| 82 | #ifdef TOOLS_ENABLED |
| 83 | if (is_part_of_edited_scene()) { |
| 84 | return RID(); |
| 85 | } |
| 86 | #endif |
| 87 | if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_POPUP_MENU)) { |
| 88 | return RID(); |
| 89 | } |
| 90 | |
| 91 | if (global_menu.is_valid()) { |
| 92 | return global_menu; // Already bound; |
| 93 | } |
| 94 | |
| 95 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 96 | |
| 97 | if (system_menu_id != NativeMenu::INVALID_MENU_ID && nmenu->has_system_menu(system_menu_id)) { |
| 98 | if (system_menus.has(system_menu_id)) { |
| 99 | WARN_PRINT(vformat("Attempting to bind PopupMenu to the system menu %s, but another menu is already bound to it. This menu: %s, current menu: %s", nmenu->get_system_menu_name(system_menu_id), get_description(), system_menus[system_menu_id]->get_description())); |
| 100 | global_menu = nmenu->create_menu(); |
| 101 | } else { |
| 102 | system_menus[system_menu_id] = this; |
| 103 | system_menu = nmenu->get_system_menu(system_menu_id); |
| 104 | global_menu = system_menu; |
| 105 | } |
| 106 | } else { |
| 107 | global_menu = nmenu->create_menu(); |
| 108 | } |
| 109 | |
| 110 | nmenu->set_interface_direction(global_menu, control->is_layout_rtl()); |
| 111 | nmenu->set_popup_open_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_popup)); |
| 112 | nmenu->set_popup_close_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_close)); |
| 113 | for (int i = 0; i < items.size(); i++) { |
| 114 | Item &item = items.write[i]; |
| 115 | if (item.separator) { |
| 116 | nmenu->add_separator(global_menu); |
| 117 | } else { |
| 118 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), item.shortcut_is_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), i); |
| 119 | if (item.submenu) { |
| 120 | RID submenu_rid = item.submenu->bind_global_menu(); |
| 121 | nmenu->set_item_submenu(global_menu, index, submenu_rid); |
| 122 | item.submenu_bound = true; |
| 123 | if (item.submenu->is_connected("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden))) { |
| 124 | item.submenu->disconnect("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden)); |
| 125 | } |
| 126 | } |
| 127 | if (item.checkable_type == Item::CHECKABLE_TYPE_CHECK_BOX) { |
| 128 | nmenu->set_item_checkable(global_menu, index, true); |
| 129 | } else if (item.checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON) { |
| 130 | nmenu->set_item_radio_checkable(global_menu, index, true); |
| 131 | } |
| 132 | nmenu->set_item_checked(global_menu, index, item.checked); |
| 133 | nmenu->set_item_disabled(global_menu, index, item.disabled); |
| 134 | nmenu->set_item_max_states(global_menu, index, item.max_states); |
| 135 | nmenu->set_item_icon(global_menu, index, item.icon); |
| 136 | nmenu->set_item_state(global_menu, index, item.state); |
| 137 | nmenu->set_item_indentation_level(global_menu, index, item.indent); |
| 138 | nmenu->set_item_tooltip(global_menu, index, item.tooltip); |
| 139 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 140 | Array events = item.shortcut->get_events(); |
| 141 | for (const Variant &v : events) { |
| 142 | Ref<InputEventKey> ie = v; |
| 143 | if (ie.is_valid() && _set_item_accelerator(index, ie)) { |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } else if (item.accel != Key::NONE) { |
| 148 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return global_menu; |
| 153 | } |
| 154 | |
| 155 | void PopupMenu::unbind_global_menu() { |
| 156 | if (global_menu.is_null()) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (global_menu == system_menu && system_menus[system_menu_id] == this) { |
| 161 | system_menus.erase(system_menu_id); |
| 162 | } |
| 163 | |
| 164 | for (int i = 0; i < items.size(); i++) { |
| 165 | Item &item = items.write[i]; |
| 166 | if (item.submenu) { |
| 167 | item.submenu->unbind_global_menu(); |
| 168 | item.submenu_bound = false; |
| 169 | } |
| 170 | } |
| 171 | if (system_menu != global_menu) { |
| 172 | NativeMenu::get_singleton()->free_menu(global_menu); |
| 173 | } else { |
| 174 | NativeMenu::get_singleton()->clear(global_menu); |
| 175 | } |
| 176 | |
| 177 | system_menu = RID(); |
| 178 | global_menu = RID(); |
| 179 | } |
| 180 | |
| 181 | bool PopupMenu::is_system_menu() const { |
| 182 | return (global_menu == system_menu) && (system_menu_id != NativeMenu::INVALID_MENU_ID); |
| 183 | } |
| 184 | |
| 185 | void PopupMenu::set_system_menu(NativeMenu::SystemMenus p_system_menu_id) { |
| 186 | if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) { |
| 187 | unbind_global_menu(); |
| 188 | } |
| 189 | system_menu_id = p_system_menu_id; |
| 190 | if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) { |
| 191 | bind_global_menu(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | NativeMenu::SystemMenus PopupMenu::get_system_menu() const { |
| 196 | return system_menu_id; |
| 197 | } |
| 198 | |
| 199 | String PopupMenu::_get_accel_text(const Item &p_item) const { |
| 200 | if (p_item.shortcut.is_valid()) { |
| 201 | return p_item.shortcut->get_as_text(); |
| 202 | } else if (p_item.accel != Key::NONE) { |
| 203 | return keycode_get_string(p_item.accel); |
| 204 | } |
| 205 | return String(); |
| 206 | } |
| 207 | |
| 208 | Size2 PopupMenu::_get_item_icon_size(int p_idx) const { |
| 209 | const PopupMenu::Item &item = items[p_idx]; |
| 210 | Size2 icon_size = item.get_icon_size(); |
| 211 | |
| 212 | int max_width = 0; |
| 213 | if (theme_cache.icon_max_width > 0) { |
| 214 | max_width = theme_cache.icon_max_width; |
| 215 | } |
| 216 | if (item.icon_max_width > 0 && (max_width == 0 || item.icon_max_width < max_width)) { |
| 217 | max_width = item.icon_max_width; |
| 218 | } |
| 219 | |
| 220 | if (max_width > 0 && icon_size.width > max_width) { |
| 221 | icon_size.height = icon_size.height * max_width / icon_size.width; |
| 222 | icon_size.width = max_width; |
| 223 | } |
| 224 | |
| 225 | return icon_size; |
| 226 | } |
| 227 | |
| 228 | Size2 PopupMenu::_get_contents_minimum_size() const { |
| 229 | Size2 minsize = theme_cache.panel_style->get_minimum_size(); |
| 230 | minsize.width += scroll_container->get_v_scroll_bar()->get_size().width; |
| 231 | // Take shadows into account. |
| 232 | minsize.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT); |
| 233 | minsize.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM); |
| 234 | |
| 235 | real_t body_max_w = 0.0; // Indentation, text, and submenu arrow. |
| 236 | real_t icon_max_w = 0.0; |
| 237 | real_t accel_max_w = 0.0; |
| 238 | bool has_check_gutter = false; |
| 239 | bool gutter_compact = theme_cache.gutter_compact; |
| 240 | |
| 241 | for (int i = 0; i < items.size(); i++) { |
| 242 | _shape_item(i); |
| 243 | |
| 244 | icon_max_w = MAX(_get_item_icon_size(i).width, icon_max_w); |
| 245 | |
| 246 | if (items[i].checkable_type && !items[i].separator) { |
| 247 | has_check_gutter = true; |
| 248 | if (items[i].icon.is_valid()) { |
| 249 | gutter_compact = false; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) { |
| 254 | real_t accel_w = theme_cache.h_separation * 2 + items[i].accel_text_buf->get_size().x; |
| 255 | accel_max_w = MAX(accel_w, accel_max_w); |
| 256 | } |
| 257 | |
| 258 | real_t body_w = items[i].indent * theme_cache.indent + items[i].text_buf->get_size().x; |
| 259 | if (items[i].submenu) { |
| 260 | body_w += theme_cache.submenu->get_width(); |
| 261 | } |
| 262 | body_max_w = MAX(body_max_w, body_w); |
| 263 | |
| 264 | minsize.height += _get_item_height(i) + theme_cache.v_separation; |
| 265 | } |
| 266 | |
| 267 | minsize.width += theme_cache.item_start_padding + body_max_w + accel_max_w + theme_cache.item_end_padding; |
| 268 | |
| 269 | const int check_w = MAX(theme_cache.checked->get_width(), theme_cache.radio_checked->get_width()); |
| 270 | if (gutter_compact) { |
| 271 | minsize.width += MAX(icon_max_w, check_w) + theme_cache.h_separation; |
| 272 | } else { |
| 273 | if (icon_max_w > 0) { |
| 274 | minsize.width += icon_max_w + theme_cache.h_separation; |
| 275 | } |
| 276 | if (has_check_gutter) { |
| 277 | minsize.width += check_w + theme_cache.h_separation; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (is_inside_tree()) { |
| 282 | int height_limit = get_usable_parent_rect().size.height; |
| 283 | if (minsize.height > height_limit) { |
| 284 | minsize.height = height_limit; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing. |
| 289 | return minsize; |
| 290 | } |
| 291 | |
| 292 | int PopupMenu::_get_item_height(int p_idx) const { |
| 293 | ERR_FAIL_INDEX_V(p_idx, items.size(), 0); |
| 294 | |
| 295 | Size2 icon_size = _get_item_icon_size(p_idx); |
| 296 | int icon_height = icon_size.height; |
| 297 | if (items[p_idx].checkable_type && !items[p_idx].separator) { |
| 298 | icon_height = MAX(icon_height, MAX(theme_cache.checked->get_height(), theme_cache.radio_checked->get_height())); |
| 299 | } |
| 300 | |
| 301 | int text_height = items[p_idx].text_buf->get_size().height; |
| 302 | if (text_height == 0 && !items[p_idx].separator) { |
| 303 | text_height = theme_cache.font->get_height(theme_cache.font_size); |
| 304 | } |
| 305 | |
| 306 | int separator_height = 0; |
| 307 | if (items[p_idx].separator) { |
| 308 | separator_height = MAX(theme_cache.separator_style->get_minimum_size().height, MAX(theme_cache.labeled_separator_left->get_minimum_size().height, theme_cache.labeled_separator_right->get_minimum_size().height)); |
| 309 | } |
| 310 | |
| 311 | return MAX(separator_height, MAX(text_height, icon_height)); |
| 312 | } |
| 313 | |
| 314 | int PopupMenu::_get_items_total_height() const { |
| 315 | // Get total height of all items by taking max of icon height and font height |
| 316 | int items_total_height = 0; |
| 317 | for (int i = 0; i < items.size(); i++) { |
| 318 | items_total_height += _get_item_height(i) + theme_cache.v_separation; |
| 319 | } |
| 320 | |
| 321 | return items_total_height; |
| 322 | } |
| 323 | |
| 324 | int PopupMenu::_get_mouse_over(const Point2 &p_over) const { |
| 325 | // Transform to scroll_container local coordinates. |
| 326 | const Point2 scaled_pos = p_over / get_content_scale_factor(); |
| 327 | const Point2 over_scroll_container = |
| 328 | scroll_container->get_global_transform_with_canvas().xform_inv(scaled_pos); |
| 329 | |
| 330 | // Check if point is inside the item control as clipped by scroll_container. |
| 331 | const Rect2 scroll_container_rect = Rect2(Point2(), scroll_container->get_size()); |
| 332 | const Rect2 bounding_rect = scroll_container_rect.intersection(control->get_rect()); |
| 333 | if (!bounding_rect.has_point(over_scroll_container)) { |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | // Perform item hit check in control node local space, |
| 338 | // so we don't need to worry about any of the container theming. |
| 339 | const float over_control_y = control->get_transform().xform_inv(over_scroll_container).y; |
| 340 | float bottom_edge = 0; |
| 341 | for (int i = 0; i < items.size(); i++) { |
| 342 | bottom_edge += theme_cache.v_separation; |
| 343 | bottom_edge += _get_item_height(i); |
| 344 | if (bottom_edge > over_control_y) { |
| 345 | return i; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | void PopupMenu::_activate_submenu(int p_over, bool p_by_keyboard) { |
| 353 | ERR_FAIL_INDEX_MSG(p_over, items.size(), vformat("Invalid submenu index %d in _activate_submenu.", p_over)); |
| 354 | PopupMenu *submenu_popup = items[p_over].submenu; |
| 355 | if (submenu_popup->is_visible()) { |
| 356 | WARN_VERBOSE(vformat("_activate_submenu should not be called on an open submenu - index: %d.", p_over)); |
| 357 | return; |
| 358 | } |
| 359 | submenu_popup->this_submenu_index = p_over; |
| 360 | active_submenu_index = p_over; |
| 361 | |
| 362 | submenu_popup->get_window()->set_exclusive(false); // Ensure mouse inputs to parent menu are not inhibited by the submenu in exclusive mode. |
| 363 | |
| 364 | const float win_scale = get_content_scale_factor(); |
| 365 | |
| 366 | const Point2 this_pos = get_position(); |
| 367 | Rect2 this_rect = Rect2(this_pos, panel->get_size()); |
| 368 | |
| 369 | submenu_popup->reset_size(); // Shrink the popup size to its contents. |
| 370 | const Size2 submenu_size = submenu_popup->get_size(); |
| 371 | |
| 372 | // Calculate the submenu's position. |
| 373 | Point2 submenu_pos = Point2(0, 0); |
| 374 | Rect2i screen_rect = is_embedded() ? Rect2i(get_embedder()->get_visible_rect()) : get_parent_rect(); |
| 375 | active_submenu_target_line.clear(); |
| 376 | |
| 377 | panel_offset_start = Point2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * win_scale; |
| 378 | const Point2 panel_offset_end = Point2(-panel->get_offset(SIDE_RIGHT), -panel->get_offset(SIDE_BOTTOM)) * win_scale; |
| 379 | const Vector2 scaled_this_size = this_rect.size * win_scale; |
| 380 | const float scaled_theme_v_separation = theme_cache.v_separation * win_scale; |
| 381 | const float scroll_offset = control->get_position().y; |
| 382 | const float scaled_ofs_cache = items[p_over]._ofs_cache * win_scale; |
| 383 | const float scaled_height_cache = items[p_over]._height_cache * win_scale; |
| 384 | |
| 385 | if (is_layout_rtl()) { |
| 386 | is_active_submenu_left = true; |
| 387 | submenu_pos += this_pos + Point2(-submenu_size.width + panel_offset_end.x, scaled_ofs_cache + scroll_offset - int(scaled_theme_v_separation * 0.5) + panel_offset_start.y); |
| 388 | if (submenu_pos.x < screen_rect.position.x) { |
| 389 | submenu_pos.x = this_pos.x + this_rect.size.width - panel_offset_start.x; |
| 390 | is_active_submenu_left = false; |
| 391 | } |
| 392 | |
| 393 | } else { |
| 394 | is_active_submenu_left = false; |
| 395 | submenu_pos += this_pos + Point2(scaled_this_size.x + panel_offset_start.x, scaled_ofs_cache + scroll_offset - int(scaled_theme_v_separation * 0.5) + panel_offset_start.y); |
| 396 | if (submenu_pos.x + submenu_size.width > screen_rect.position.x + screen_rect.size.width) { |
| 397 | submenu_pos.x = this_pos.x - submenu_size.width + panel_offset_end.x; |
| 398 | is_active_submenu_left = true; |
| 399 | } |
| 400 | } |
| 401 | submenu_popup->set_position(submenu_pos); |
| 402 | submenu_popup->activated_by_keyboard = p_by_keyboard; |
| 403 | // If not triggered by the mouse, start the popup with its first enabled item focused. |
| 404 | if (p_by_keyboard) { |
| 405 | for (int i = 0; i < submenu_popup->get_item_count(); i++) { |
| 406 | if (!submenu_popup->is_item_disabled(i)) { |
| 407 | submenu_popup->set_focused_item(i); |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | submenu_popup->popup(); |
| 413 | // The autohide areas are set on the submenu, but are aligned over the parent menu, |
| 414 | // so we spoof `this_rect` position as the negative relative offset of the parent from the submenu. |
| 415 | this_rect.position = -(submenu_popup->get_position() - this_pos); |
| 416 | |
| 417 | const Rect2 safe_area(get_position(), get_size()); |
| 418 | Viewport *vp = submenu_popup->get_embedder(); |
| 419 | if (vp) { |
| 420 | vp->subwindow_set_popup_safe_rect(submenu_popup, safe_area); |
| 421 | } else { |
| 422 | DisplayServer::get_singleton()->window_set_popup_safe_rect(submenu_popup->get_window_id(), safe_area); |
| 423 | } |
| 424 | // Set the mouse movement target line at the top and bottom points of the submenu vertical side abutting the parent menu. |
| 425 | if (is_active_submenu_left) { |
| 426 | active_submenu_target_line.push_back(Point2(submenu_popup->get_position().x + submenu_popup->get_size().x, submenu_popup->get_position().y)); |
| 427 | } else { |
| 428 | active_submenu_target_line.push_back(submenu_popup->get_position()); |
| 429 | } |
| 430 | active_submenu_target_line.push_back(Point2(active_submenu_target_line[0].x, active_submenu_target_line[0].y + submenu_popup->get_size().y)); |
| 431 | |
| 432 | submenu_popup->clear_autohide_areas(); |
| 433 | // Add an autohide area above the submenu item unless it's the top item. |
| 434 | // This avoids a narrow strip of area that can trigger the submenu to reload when reentering the parent item from the top. |
| 435 | const int y_to_item_top = scaled_ofs_cache + scroll_offset - int(scaled_theme_v_separation * 0.5) + theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale; |
| 436 | Rect2 top_rect = Rect2(this_rect.position.x, this_rect.position.y, scaled_this_size.width, y_to_item_top); |
| 437 | if (active_submenu_index != 0) { |
| 438 | submenu_popup->add_autohide_area(top_rect); |
| 439 | } |
| 440 | // If there is an area below the submenu item, add an autohide area there unless it's the last item. |
| 441 | if (active_submenu_index != items.size() - 1) { |
| 442 | const int y_to_item_bottom = y_to_item_top + scaled_height_cache + scaled_theme_v_separation; |
| 443 | submenu_popup->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + y_to_item_bottom, scaled_this_size.x, scaled_this_size.y - y_to_item_bottom)); |
| 444 | } |
| 445 | queue_accessibility_update(); |
| 446 | control->queue_redraw(); |
| 447 | } |
| 448 | |
| 449 | void PopupMenu::_parent_focused() { |
| 450 | if (is_embedded()) { |
| 451 | Point2 mouse_pos_adjusted; |
| 452 | Window *window_parent = Object::cast_to<Window>(get_parent()->get_viewport()); |
| 453 | while (window_parent) { |
| 454 | if (!window_parent->is_embedded()) { |
| 455 | mouse_pos_adjusted += window_parent->get_position(); |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport()); |
| 460 | } |
| 461 | |
| 462 | Rect2 safe_area = get_embedder()->subwindow_get_popup_safe_rect(this); |
| 463 | Point2 pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted; |
| 464 | if (safe_area == Rect2i() || !safe_area.has_point(pos)) { |
| 465 | Popup::_parent_focused(); |
| 466 | } else { |
| 467 | grab_focus(); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void PopupMenu::_submenu_timeout() { |
| 473 | if (mouse_over == submenu_over) { |
| 474 | _activate_submenu(mouse_over); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void PopupMenu::_input_from_window(const Ref<InputEvent> &p_event) { |
| 479 | if (p_event.is_valid()) { |
| 480 | _input_from_window_internal(p_event); |
| 481 | } else { |
| 482 | WARN_PRINT_ONCE("PopupMenu has received an invalid InputEvent. Consider filtering out invalid events."); |
| 483 | } |
| 484 | Popup::_input_from_window(p_event); |
| 485 | } |
| 486 | |
| 487 | void PopupMenu::_input_from_window_internal(const Ref<InputEvent> &p_event) { |
| 488 | if (!items.is_empty()) { |
| 489 | Input *input = Input::get_singleton(); |
| 490 | Ref<InputEventJoypadMotion> joypadmotion_event = p_event; |
| 491 | Ref<InputEventJoypadButton> joypadbutton_event = p_event; |
| 492 | bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid()); |
| 493 | |
| 494 | if (is_joypad_event && !has_focus_or_active_popup()) { |
| 495 | return; |
| 496 | } |
| 497 | if (p_event->is_action("ui_down", true) && p_event->is_pressed()) { |
| 498 | if (is_joypad_event) { |
| 499 | if (!input->is_action_just_pressed_by_event("ui_down", p_event, true)) { |
| 500 | return; |
| 501 | } |
| 502 | joypad_event_process = true; |
| 503 | set_process_internal(true); |
| 504 | } |
| 505 | int search_from = mouse_over + 1; |
| 506 | if (search_from >= items.size()) { |
| 507 | search_from = 0; |
| 508 | } |
| 509 | |
| 510 | bool match_found = false; |
| 511 | for (int i = search_from; i < items.size(); i++) { |
| 512 | if (!items[i].separator && !items[i].disabled) { |
| 513 | prev_mouse_over = mouse_over; |
| 514 | mouse_over = i; |
| 515 | emit_signal(SNAME("id_focused"), items[i].id); |
| 516 | scroll_to_item(i); |
| 517 | queue_accessibility_update(); |
| 518 | control->queue_redraw(); |
| 519 | set_input_as_handled(); |
| 520 | match_found = true; |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (!match_found) { |
| 526 | // If the last item is not selectable, try re-searching from the start. |
| 527 | for (int i = 0; i < search_from; i++) { |
| 528 | if (!items[i].separator && !items[i].disabled) { |
| 529 | prev_mouse_over = mouse_over; |
| 530 | mouse_over = i; |
| 531 | emit_signal(SNAME("id_focused"), items[i].id); |
| 532 | scroll_to_item(i); |
| 533 | queue_accessibility_update(); |
| 534 | control->queue_redraw(); |
| 535 | set_input_as_handled(); |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | } else if (p_event->is_action("ui_up", true) && p_event->is_pressed()) { |
| 541 | if (is_joypad_event) { |
| 542 | if (!input->is_action_just_pressed_by_event("ui_up", p_event, true)) { |
| 543 | return; |
| 544 | } |
| 545 | joypad_event_process = true; |
| 546 | set_process_internal(true); |
| 547 | } |
| 548 | int search_from = mouse_over - 1; |
| 549 | if (search_from < 0) { |
| 550 | search_from = items.size() - 1; |
| 551 | } |
| 552 | |
| 553 | bool match_found = false; |
| 554 | for (int i = search_from; i >= 0; i--) { |
| 555 | if (!items[i].separator && !items[i].disabled) { |
| 556 | prev_mouse_over = mouse_over; |
| 557 | mouse_over = i; |
| 558 | emit_signal(SNAME("id_focused"), items[i].id); |
| 559 | scroll_to_item(i); |
| 560 | queue_accessibility_update(); |
| 561 | control->queue_redraw(); |
| 562 | set_input_as_handled(); |
| 563 | match_found = true; |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if (!match_found) { |
| 569 | // If the first item is not selectable, try re-searching from the end. |
| 570 | for (int i = items.size() - 1; i >= search_from; i--) { |
| 571 | if (!items[i].separator && !items[i].disabled) { |
| 572 | prev_mouse_over = mouse_over; |
| 573 | mouse_over = i; |
| 574 | emit_signal(SNAME("id_focused"), items[i].id); |
| 575 | scroll_to_item(i); |
| 576 | queue_accessibility_update(); |
| 577 | control->queue_redraw(); |
| 578 | set_input_as_handled(); |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | } else if (p_event->is_action("ui_left", true) && p_event->is_pressed()) { |
| 584 | Node *n = get_parent(); |
| 585 | if (n) { |
| 586 | if (PopupMenu *parent_popup = Object::cast_to<PopupMenu>(n)) { |
| 587 | parent_popup->activated_by_keyboard = true; |
| 588 | hide(); |
| 589 | set_input_as_handled(); |
| 590 | } else if (Object::cast_to<MenuBar>(n)) { |
| 591 | Object::cast_to<MenuBar>(n)->gui_input(p_event); |
| 592 | set_input_as_handled(); |
| 593 | return; |
| 594 | } |
| 595 | } |
| 596 | } else if (p_event->is_action("ui_right", true) && p_event->is_pressed()) { |
| 597 | if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu && submenu_over != mouse_over) { |
| 598 | _activate_submenu(mouse_over, true); |
| 599 | set_input_as_handled(); |
| 600 | } else { |
| 601 | Node *n = get_parent(); |
| 602 | if (n && Object::cast_to<MenuBar>(n)) { |
| 603 | Object::cast_to<MenuBar>(n)->gui_input(p_event); |
| 604 | set_input_as_handled(); |
| 605 | return; |
| 606 | } |
| 607 | } |
| 608 | } else if (p_event->is_action("ui_accept", true) && p_event->is_pressed()) { |
| 609 | if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) { |
| 610 | if (items[mouse_over].submenu && submenu_over != mouse_over) { |
| 611 | _activate_submenu(mouse_over, true); |
| 612 | } else { |
| 613 | activate_item(mouse_over); |
| 614 | } |
| 615 | set_input_as_handled(); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // Make the item area exclude shadows and the vertical margins and scrollbar. |
| 621 | Rect2 item_clickable_area = panel->get_global_rect(); |
| 622 | if (scroll_container->get_v_scroll_bar()->is_visible_in_tree()) { |
| 623 | int scroll_width = scroll_container->get_v_scroll_bar()->get_size().width; |
| 624 | if (is_layout_rtl()) { |
| 625 | item_clickable_area.position.x += scroll_width; |
| 626 | item_clickable_area.size.width -= scroll_width; |
| 627 | } |
| 628 | item_clickable_area.size.width -= scroll_width; |
| 629 | } |
| 630 | |
| 631 | float win_scale = get_content_scale_factor(); |
| 632 | item_clickable_area.position.x += theme_cache.panel_style->get_margin(SIDE_LEFT); |
| 633 | item_clickable_area.position.y += theme_cache.panel_style->get_margin(SIDE_TOP); |
| 634 | item_clickable_area.position *= win_scale; |
| 635 | item_clickable_area.size.width -= theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT); |
| 636 | item_clickable_area.size.y -= theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM); |
| 637 | item_clickable_area.size *= win_scale; |
| 638 | |
| 639 | Ref<InputEventMouseButton> b = p_event; |
| 640 | |
| 641 | if (b.is_valid()) { |
| 642 | MouseButton button_idx = b->get_button_index(); |
| 643 | // Activate the item on release of either the left mouse button or |
| 644 | // any mouse button held down when the popup was opened. |
| 645 | // This allows for opening the popup and triggering an action in a single mouse click. |
| 646 | if (button_idx == MouseButton::LEFT || initial_button_mask.has_flag(mouse_button_to_mask(button_idx))) { |
| 647 | if (b->is_pressed()) { |
| 648 | during_grabbed_click = false; |
| 649 | is_scrolling = is_layout_rtl() ? b->get_position().x < item_clickable_area.position.x - item_clickable_area.size.width : b->get_position().x > item_clickable_area.size.width + item_clickable_area.position.x; |
| 650 | |
| 651 | // Hide it if the shadows have been clicked. |
| 652 | if (get_flag(FLAG_POPUP)) { |
| 653 | Rect2 panel_area = panel->get_global_rect(); |
| 654 | panel_area.position *= win_scale; |
| 655 | panel_area.size *= win_scale; |
| 656 | if (!panel_area.has_point(b->get_position())) { |
| 657 | _close_pressed(); |
| 658 | return; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (!item_clickable_area.has_point(b->get_position())) { |
| 663 | return; |
| 664 | } |
| 665 | int over = _get_mouse_over(b->get_position()); |
| 666 | if (over < 0 || items[over].separator || items[over].disabled || (items[over].submenu && items[over].submenu->is_visible())) { |
| 667 | return; |
| 668 | } else { |
| 669 | _mouse_over_update(b->get_position()); |
| 670 | } |
| 671 | } else { |
| 672 | if (is_scrolling) { |
| 673 | is_scrolling = false; |
| 674 | return; |
| 675 | } |
| 676 | bool was_during_grabbed_click = during_grabbed_click; |
| 677 | during_grabbed_click = false; |
| 678 | initial_button_mask.clear(); |
| 679 | |
| 680 | if (!item_clickable_area.has_point(b->get_position())) { |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | // Disable clicks under a time threshold to avoid selection right when opening the popup. |
| 685 | if (was_during_grabbed_click && OS::get_singleton()->get_ticks_msec() - popup_time_msec < 400) { |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | int over = _get_mouse_over(b->get_position()); |
| 690 | if (over < 0) { |
| 691 | if (!was_during_grabbed_click) { |
| 692 | hide(); |
| 693 | } |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (items[over].separator || items[over].disabled) { |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | if (items[over].submenu) { |
| 702 | if (!items[over].submenu->is_visible()) { |
| 703 | _activate_submenu(over); |
| 704 | } |
| 705 | return; |
| 706 | } |
| 707 | activate_item(over); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | Ref<InputEventMouseMotion> m = p_event; |
| 713 | |
| 714 | if (m.is_valid()) { |
| 715 | if (m->get_velocity().is_zero_approx() || m->get_relative() == Vector2(0, 0)) { |
| 716 | return; |
| 717 | } |
| 718 | activated_by_keyboard = false; |
| 719 | |
| 720 | if (this_submenu_index != -1) { // Is a submenu. |
| 721 | PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent()); |
| 722 | ERR_FAIL_NULL(parent_popup); |
| 723 | Point2 areas_mouse_pos = get_mouse_position() - parent_popup->panel_offset_start; |
| 724 | for (const Rect2 &E : autohide_areas) { |
| 725 | if (!scroll_container->get_global_rect().has_point(m->get_position()) && E.has_point(areas_mouse_pos)) { |
| 726 | _close_or_suspend(); |
| 727 | return; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | if (this_submenu_index != -1) { // Is a submenu. |
| 733 | // The mouse left the safe area, but came back again, so cancel the auto-closing. |
| 734 | if (PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent())) { |
| 735 | if (!parent_popup->close_suspended_timer->is_stopped()) { |
| 736 | parent_popup->close_suspended_timer->stop(); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | if (mouse_over == -1 && !item_clickable_area.has_point(m->get_position())) { |
| 742 | Vector2 m_relative = m->get_relative(); |
| 743 | if (is_embedded() && m_relative != Vector2()) { // Only if the mouse has moved relative. |
| 744 | if (PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent())) { // Is a submenu. |
| 745 | parent_popup->last_submenu_mouse_position = m->get_position(); |
| 746 | } else { |
| 747 | last_submenu_mouse_position = m->get_position(); |
| 748 | } |
| 749 | } |
| 750 | return; |
| 751 | } |
| 752 | _mouse_over_update(m->get_position()); |
| 753 | } |
| 754 | |
| 755 | Ref<InputEventKey> k = p_event; |
| 756 | |
| 757 | if (allow_search && k.is_valid() && k->get_unicode() && k->is_pressed()) { |
| 758 | uint64_t now = OS::get_singleton()->get_ticks_msec(); |
| 759 | uint64_t diff = now - search_time_msec; |
| 760 | uint64_t max_interval = uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec")); |
| 761 | search_time_msec = now; |
| 762 | |
| 763 | if (diff > max_interval) { |
| 764 | search_string = ""; |
| 765 | } |
| 766 | |
| 767 | if (String::chr(k->get_unicode()) != search_string) { |
| 768 | search_string += String::chr(k->get_unicode()); |
| 769 | } |
| 770 | |
| 771 | for (int i = mouse_over + 1; i <= items.size(); i++) { |
| 772 | if (i == items.size()) { |
| 773 | if (mouse_over <= 0) { |
| 774 | break; |
| 775 | } else { |
| 776 | i = 0; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (i == mouse_over) { |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | if (items[i].text.findn(search_string) == 0) { |
| 785 | prev_mouse_over = mouse_over; |
| 786 | mouse_over = i; |
| 787 | emit_signal(SNAME("id_focused"), items[i].id); |
| 788 | scroll_to_item(i); |
| 789 | queue_accessibility_update(); |
| 790 | control->queue_redraw(); |
| 791 | set_input_as_handled(); |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | bool PopupMenu::_is_mouse_moving_toward_submenu(const Vector2 &p_relative, bool p_is_submenu_left, const Vector2 &p_mouse_position, const Vector<Point2> &p_active_submenu_target_line) const { |
| 799 | Vector2 top_target = (p_active_submenu_target_line[0] - p_mouse_position).rotated(p_is_submenu_left ? -Math::PI * 0.5 : Math::PI * 0.5); |
| 800 | Vector2 bottom_target = (p_active_submenu_target_line[1] - p_mouse_position).rotated(p_is_submenu_left ? Math::PI * 0.5 : -Math::PI * 0.5); |
| 801 | // The top_target vector is perpendicular to the vector between the mouse position and the top point of the submenu target line. |
| 802 | // The dot product is > 0 if the relative vector is +/- 90 degrees of the top_vector. |
| 803 | // The bottom_target vector is perpendicular to the vector between the mouse position and the bottom point of the submenu target line, |
| 804 | // but pointing in the opposite direction of the top_target vector. Thus, the intersection of top and bottom test semicircles is the area |
| 805 | // in which the relative vector is deemed to be moving toward the submenu. |
| 806 | // Without normalization, the comparison is correct by testing only the sign, but would not work against a range of +/- 1. |
| 807 | return bottom_target.dot(p_relative) > 0 && top_target.dot(p_relative) > 0; |
| 808 | } |
| 809 | |
| 810 | void PopupMenu::_mouse_over_update(const Point2 &p_over) { |
| 811 | int over_index = _get_mouse_over(p_over); |
| 812 | int id = (over_index < 0 || items[over_index].separator || items[over_index].disabled) ? -1 : (items[over_index].id >= 0 ? items[over_index].id : over_index); |
| 813 | if (id < 0) { |
| 814 | mouse_over = -1; |
| 815 | submenu_over = -1; |
| 816 | submenu_timer->stop(); |
| 817 | queue_accessibility_update(); |
| 818 | control->queue_redraw(); |
| 819 | return; |
| 820 | } |
| 821 | if (items[over_index].submenu) { |
| 822 | if (items[over_index].submenu->is_visible()) { |
| 823 | if (submenu_over == over_index) { |
| 824 | last_submenu_mouse_position = is_embedded() ? p_over : Point2(DisplayServer::get_singleton()->mouse_get_position() - get_position()); |
| 825 | } |
| 826 | if (items[over_index].submenu->active_submenu_index != -1) { // Close any secondary popup open on the visible submenu when the mouse is on the parent item. |
| 827 | PopupMenu *secondary_popup = Object::cast_to<PopupMenu>(items[over_index].submenu); |
| 828 | ERR_FAIL_INDEX_MSG(secondary_popup->active_submenu_index, secondary_popup->items.size(), vformat("Invalid active_submenu_index index %d in _mouse_over_update.", secondary_popup->active_submenu_index)); |
| 829 | secondary_popup->items[secondary_popup->active_submenu_index].submenu->_close_pressed(); |
| 830 | } |
| 831 | } else if (!is_scrolling && submenu_over != over_index && !close_was_suspended && active_submenu_index == -1) { |
| 832 | submenu_timer->start(); |
| 833 | submenu_over = over_index; |
| 834 | } |
| 835 | } else { |
| 836 | submenu_timer->stop(); |
| 837 | submenu_over = -1; |
| 838 | } |
| 839 | |
| 840 | if (over_index != mouse_over) { |
| 841 | mouse_over = over_index; |
| 842 | queue_accessibility_update(); |
| 843 | control->queue_redraw(); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | void PopupMenu::_draw_items() { |
| 848 | control->set_custom_minimum_size(Size2(0, _get_items_total_height())); |
| 849 | RID ci = control->get_canvas_item(); |
| 850 | |
| 851 | // Space between the item content and the sides of popup menu. |
| 852 | bool rtl = control->is_layout_rtl(); |
| 853 | // In Item::checkable_type enum order (less the non-checkable member), with disabled repeated at the end. |
| 854 | Ref<Texture2D> check[] = { theme_cache.checked, theme_cache.radio_checked, theme_cache.checked_disabled, theme_cache.radio_checked_disabled }; |
| 855 | Ref<Texture2D> uncheck[] = { theme_cache.unchecked, theme_cache.radio_unchecked, theme_cache.unchecked_disabled, theme_cache.radio_unchecked_disabled }; |
| 856 | Ref<Texture2D> submenu; |
| 857 | if (rtl) { |
| 858 | submenu = theme_cache.submenu_mirrored; |
| 859 | } else { |
| 860 | submenu = theme_cache.submenu; |
| 861 | } |
| 862 | |
| 863 | float display_width = control->get_size().width; |
| 864 | |
| 865 | // Find the widest icon and whether any items have a checkbox, and store the offsets for each. |
| 866 | real_t icon_max_w = 0.0; |
| 867 | real_t check_max_w = 0.0; |
| 868 | bool has_check_gutter = false; |
| 869 | bool gutter_compact = theme_cache.gutter_compact; |
| 870 | for (int i = 0; i < items.size(); i++) { |
| 871 | if (items[i].separator) { |
| 872 | continue; |
| 873 | } |
| 874 | |
| 875 | icon_max_w = MAX(_get_item_icon_size(i).width, icon_max_w); |
| 876 | |
| 877 | if (items[i].checkable_type) { |
| 878 | has_check_gutter = true; |
| 879 | if (items[i].icon.is_valid()) { |
| 880 | gutter_compact = false; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | if (has_check_gutter) { |
| 885 | for (int i = 0; i < 4; i++) { |
| 886 | check_max_w = MAX(check_max_w, check[i]->get_width()); |
| 887 | check_max_w = MAX(check_max_w, uncheck[i]->get_width()); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | Point2 ofs; |
| 892 | |
| 893 | // Loop through all items and draw each. |
| 894 | for (int i = 0; i < items.size(); i++) { |
| 895 | // For the first item only add half a separation. For all other items, add a whole separation to the offset. |
| 896 | ofs.y += i > 0 ? theme_cache.v_separation : theme_cache.v_separation / 2; |
| 897 | |
| 898 | _shape_item(i); |
| 899 | |
| 900 | Point2 item_ofs = ofs; |
| 901 | Size2 icon_size = _get_item_icon_size(i); |
| 902 | float h = _get_item_height(i); |
| 903 | if ((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) { |
| 904 | theme_cache.hover_style->draw(ci, Rect2(item_ofs + Point2(0, -theme_cache.v_separation / 2), Size2(display_width, h + theme_cache.v_separation))); |
| 905 | } |
| 906 | |
| 907 | String text = items[i].xl_text; |
| 908 | |
| 909 | // Separator |
| 910 | item_ofs.x += items[i].indent * theme_cache.indent; |
| 911 | if (items[i].separator) { |
| 912 | if (!text.is_empty() || items[i].icon.is_valid()) { |
| 913 | int content_size = items[i].text_buf->get_size().width + theme_cache.h_separation * 2; |
| 914 | if (items[i].icon.is_valid()) { |
| 915 | content_size += icon_size.width + theme_cache.h_separation; |
| 916 | } |
| 917 | |
| 918 | int content_center = display_width / 2; |
| 919 | int content_left = content_center - content_size / 2; |
| 920 | int content_right = content_center + content_size / 2; |
| 921 | if (content_left > item_ofs.x) { |
| 922 | int sep_h = theme_cache.labeled_separator_left->get_minimum_size().height; |
| 923 | int sep_ofs = Math::floor((h - sep_h) / 2.0); |
| 924 | theme_cache.labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(MAX(0, content_left - item_ofs.x), sep_h))); |
| 925 | } |
| 926 | if (content_right < display_width) { |
| 927 | int sep_h = theme_cache.labeled_separator_right->get_minimum_size().height; |
| 928 | int sep_ofs = Math::floor((h - sep_h) / 2.0); |
| 929 | theme_cache.labeled_separator_right->draw(ci, Rect2(Point2(content_right, item_ofs.y + sep_ofs), Size2(MAX(0, display_width - content_right), sep_h))); |
| 930 | } |
| 931 | } else { |
| 932 | int sep_h = theme_cache.separator_style->get_minimum_size().height; |
| 933 | int sep_ofs = Math::floor((h - sep_h) / 2.0); |
| 934 | theme_cache.separator_style->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(display_width, sep_h))); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | Color icon_color = items[i].icon_modulate; |
| 939 | |
| 940 | // For non-separator items, add some padding for the content. |
| 941 | if (!items[i].separator) { |
| 942 | item_ofs.x += theme_cache.item_start_padding; |
| 943 | } |
| 944 | |
| 945 | // Checkboxes |
| 946 | if (items[i].checkable_type && !items[i].separator) { |
| 947 | int disabled = int(items[i].disabled) * 2; |
| 948 | Texture2D *icon = (items[i].checked ? check[items[i].checkable_type - 1 + disabled] : uncheck[items[i].checkable_type - 1 + disabled]).ptr(); |
| 949 | if (rtl) { |
| 950 | icon->draw(ci, Size2(control->get_size().width - item_ofs.x - icon->get_width(), item_ofs.y) + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color); |
| 951 | } else { |
| 952 | icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | int separator_ofs = (display_width - items[i].text_buf->get_size().width) / 2; |
| 957 | |
| 958 | // Icon |
| 959 | if (items[i].icon.is_valid()) { |
| 960 | const Point2 icon_offset = Point2(0, Math::floor((h - icon_size.height) / 2.0)); |
| 961 | Point2 icon_pos; |
| 962 | |
| 963 | if (items[i].separator) { |
| 964 | separator_ofs -= (icon_size.width + theme_cache.h_separation) / 2; |
| 965 | |
| 966 | if (rtl) { |
| 967 | icon_pos = Size2(control->get_size().width - item_ofs.x - separator_ofs - icon_size.width, item_ofs.y); |
| 968 | } else { |
| 969 | icon_pos = item_ofs + Size2(separator_ofs, 0); |
| 970 | separator_ofs += icon_size.width + theme_cache.h_separation; |
| 971 | } |
| 972 | } else { |
| 973 | const real_t check_w = (gutter_compact || !has_check_gutter) ? 0 : (check_max_w + theme_cache.h_separation); |
| 974 | if (rtl) { |
| 975 | icon_pos = Size2(control->get_size().width - item_ofs.x - check_w - icon_size.width, item_ofs.y); |
| 976 | } else { |
| 977 | icon_pos = item_ofs + Size2(check_w, 0); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | items[i].icon->draw_rect(ci, Rect2(icon_pos + icon_offset, icon_size), false, icon_color); |
| 982 | } |
| 983 | |
| 984 | // Submenu arrow on right hand side. |
| 985 | if (items[i].submenu) { |
| 986 | if (rtl) { |
| 987 | submenu->draw(ci, Point2(theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color); |
| 988 | } else { |
| 989 | submenu->draw(ci, Point2(display_width - submenu->get_width() - theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | // Text |
| 994 | if (items[i].separator) { |
| 995 | if (!text.is_empty()) { |
| 996 | Vector2 text_pos = Point2(separator_ofs, item_ofs.y + Math::floor((h - items[i].text_buf->get_size().y) / 2.0)); |
| 997 | |
| 998 | if (theme_cache.font_separator_outline_size > 0 && theme_cache.font_separator_outline_color.a > 0) { |
| 999 | items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_separator_outline_size, theme_cache.font_separator_outline_color); |
| 1000 | } |
| 1001 | items[i].text_buf->draw(ci, text_pos, theme_cache.font_separator_color); |
| 1002 | } |
| 1003 | } else { |
| 1004 | if (gutter_compact) { |
| 1005 | item_ofs.x += MAX(icon_max_w, check_max_w) + theme_cache.h_separation; |
| 1006 | } else { |
| 1007 | if (icon_max_w > 0) { |
| 1008 | item_ofs.x += icon_max_w + theme_cache.h_separation; |
| 1009 | } |
| 1010 | if (has_check_gutter) { |
| 1011 | item_ofs.x += check_max_w + theme_cache.h_separation; |
| 1012 | } |
| 1013 | } |
| 1014 | if (rtl) { |
| 1015 | Vector2 text_pos = Size2(control->get_size().width - items[i].text_buf->get_size().width - item_ofs.x, item_ofs.y) + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0)); |
| 1016 | if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) { |
| 1017 | items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color); |
| 1018 | } |
| 1019 | items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) ? theme_cache.font_hover_color : theme_cache.font_color)); |
| 1020 | } else { |
| 1021 | Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0)); |
| 1022 | if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) { |
| 1023 | items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color); |
| 1024 | } |
| 1025 | items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) ? theme_cache.font_hover_color : theme_cache.font_color)); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // Accelerator / Shortcut |
| 1030 | if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) { |
| 1031 | if (rtl) { |
| 1032 | item_ofs.x = theme_cache.item_end_padding; |
| 1033 | } else { |
| 1034 | item_ofs.x = display_width - items[i].accel_text_buf->get_size().x - theme_cache.item_end_padding; |
| 1035 | } |
| 1036 | Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].accel_text_buf->get_size().y) / 2.0)); |
| 1037 | if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) { |
| 1038 | items[i].accel_text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color); |
| 1039 | } |
| 1040 | items[i].accel_text_buf->draw(ci, text_pos, ((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) ? theme_cache.font_hover_color : theme_cache.font_accelerator_color); |
| 1041 | } |
| 1042 | |
| 1043 | // Cache the item vertical offset from the first item and the height. |
| 1044 | items.write[i]._ofs_cache = ofs.y; |
| 1045 | items.write[i]._height_cache = h; |
| 1046 | |
| 1047 | ofs.y += h; |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | void PopupMenu::_close_pressed() { |
| 1052 | if (this_submenu_index != -1 && active_submenu_index != -1) { // Close secondary submenus on first submenu close. |
| 1053 | ERR_FAIL_INDEX_MSG(active_submenu_index, items.size(), vformat("Invalid active_submenu_index index %d in _close_pressed.", active_submenu_index)); |
| 1054 | items[active_submenu_index].submenu->_close_pressed(); |
| 1055 | } |
| 1056 | Popup::_close_pressed(); |
| 1057 | } |
| 1058 | |
| 1059 | void PopupMenu::_close_or_suspend() { |
| 1060 | if (this_submenu_index != -1) { // Is a submenu. |
| 1061 | PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent()); |
| 1062 | ERR_FAIL_NULL(parent_popup); |
| 1063 | Point2 mouse_pos = is_embedded() ? parent_popup->get_mouse_position() : Point2(DisplayServer::get_singleton()->mouse_get_position() - parent_popup->get_position()); |
| 1064 | if (parent_popup->_get_mouse_over(mouse_pos) == this_submenu_index) { |
| 1065 | parent_popup->submenu_mouse_exited_ticks_msec = -1; |
| 1066 | parent_popup->mouse_movement_was_tested = false; |
| 1067 | parent_popup->close_suspended_timer->stop(); |
| 1068 | parent_popup->close_was_suspended = false; |
| 1069 | parent_popup->submenu_timer->stop(); |
| 1070 | return; |
| 1071 | } |
| 1072 | if (parent_popup->submenu_mouse_exited_ticks_msec == -1) { // ticks are reset to -1 when any open submenu is hidden. |
| 1073 | parent_popup->submenu_mouse_exited_ticks_msec = OS::get_singleton()->get_ticks_msec(); |
| 1074 | } |
| 1075 | if (!parent_popup->close_was_suspended && parent_popup->close_suspended_timer->is_stopped()) { // Provisionally suspend submenu close until the check below after several msecs. |
| 1076 | parent_popup->close_suspended_timer->start(); |
| 1077 | parent_popup->close_was_suspended = true; // Stays true until the submenu is closed so it can only be suspended once. |
| 1078 | parent_popup->submenu_timer->stop(); |
| 1079 | } else if (!parent_popup->mouse_movement_was_tested && OS::get_singleton()->get_ticks_msec() - parent_popup->submenu_mouse_exited_ticks_msec > 30) { |
| 1080 | // Allow the mouse to move away for several msecs before calculating the relative vector to the mouse exit point out of the submenu item. |
| 1081 | parent_popup->mouse_movement_was_tested = true; |
| 1082 | if (is_embedded()) { // Correct for relative positioning of embedded subwindows. |
| 1083 | parent_popup->last_submenu_mouse_position += get_position() - parent_popup->get_position(); |
| 1084 | } |
| 1085 | const Vector2 screen_mouse_position = mouse_pos + parent_popup->get_position(); |
| 1086 | Vector2 mouse_relative_to_submenu_exit = mouse_pos - parent_popup->last_submenu_mouse_position; |
| 1087 | // If the mouse is not moving toward the submenu target line, force the submenu closed. |
| 1088 | if (!_is_mouse_moving_toward_submenu(mouse_relative_to_submenu_exit, parent_popup->is_active_submenu_left, screen_mouse_position, parent_popup->active_submenu_target_line)) { |
| 1089 | parent_popup->close_suspended_timer->stop(); |
| 1090 | parent_popup->close_was_suspended = false; |
| 1091 | _close_pressed(); |
| 1092 | } |
| 1093 | } else if (parent_popup->mouse_movement_was_tested && OS::get_singleton()->get_ticks_msec() - parent_popup->submenu_mouse_exited_ticks_msec > CLOSE_SUSPENDED_TIMER_DELAY * 1000) { |
| 1094 | _close_pressed(); // This is called when the mouse is moved off an open submenu onto an autohide area. |
| 1095 | } |
| 1096 | } else { // Is the parent popup menu. |
| 1097 | _close_pressed(); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | void PopupMenu::_shape_item(int p_idx) const { |
| 1102 | if (items.write[p_idx].dirty) { |
| 1103 | items.write[p_idx].text_buf->clear(); |
| 1104 | |
| 1105 | Ref<Font> font = items[p_idx].separator ? theme_cache.font_separator : theme_cache.font; |
| 1106 | int font_size = items[p_idx].separator ? theme_cache.font_separator_size : theme_cache.font_size; |
| 1107 | |
| 1108 | if (items[p_idx].text_direction == Control::TEXT_DIRECTION_INHERITED) { |
| 1109 | items.write[p_idx].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); |
| 1110 | } else { |
| 1111 | items.write[p_idx].text_buf->set_direction((TextServer::Direction)items[p_idx].text_direction); |
| 1112 | } |
| 1113 | const String &lang = items[p_idx].language.is_empty() ? _get_locale() : items[p_idx].language; |
| 1114 | items.write[p_idx].text_buf->add_string(items.write[p_idx].xl_text, font, font_size, lang); |
| 1115 | |
| 1116 | items.write[p_idx].accel_text_buf->clear(); |
| 1117 | items.write[p_idx].accel_text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); |
| 1118 | items.write[p_idx].accel_text_buf->add_string(_get_accel_text(items.write[p_idx]), font, font_size, lang); |
| 1119 | items.write[p_idx].dirty = false; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | void PopupMenu::_menu_changed() { |
| 1124 | emit_signal(SNAME("menu_changed")); |
| 1125 | } |
| 1126 | |
| 1127 | void PopupMenu::_update_shadow_offsets() const { |
| 1128 | if (!DisplayServer::get_singleton()->is_window_transparency_available() && !is_embedded()) { |
| 1129 | panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | Ref<StyleBoxFlat> sb = theme_cache.panel_style; |
| 1134 | if (sb.is_null()) { |
| 1135 | panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0); |
| 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | const int shadow_size = sb->get_shadow_size(); |
| 1140 | if (shadow_size == 0) { |
| 1141 | panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0); |
| 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | // Offset the background panel so it leaves space inside the window for the shadows to be drawn. |
| 1146 | const Point2 shadow_offset = sb->get_shadow_offset(); |
| 1147 | if (is_layout_rtl()) { |
| 1148 | panel->set_offset(SIDE_LEFT, MAX(0, shadow_size + shadow_offset.x)); |
| 1149 | panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size + shadow_offset.x)); |
| 1150 | } else { |
| 1151 | panel->set_offset(SIDE_LEFT, MAX(0, shadow_size - shadow_offset.x)); |
| 1152 | panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size - shadow_offset.x)); |
| 1153 | } |
| 1154 | panel->set_offset(SIDE_TOP, MAX(0, shadow_size - shadow_offset.y)); |
| 1155 | panel->set_offset(SIDE_BOTTOM, MIN(0, -shadow_size - shadow_offset.y)); |
| 1156 | } |
| 1157 | |
| 1158 | Rect2i PopupMenu::_popup_adjust_rect() const { |
| 1159 | Rect2i current = Popup::_popup_adjust_rect(); |
| 1160 | if (current == Rect2i()) { |
| 1161 | return current; |
| 1162 | } |
| 1163 | |
| 1164 | pre_popup_rect = current; |
| 1165 | |
| 1166 | _update_shadow_offsets(); |
| 1167 | |
| 1168 | if (is_layout_rtl()) { |
| 1169 | current.position -= Vector2(-panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor(); |
| 1170 | } else { |
| 1171 | current.position -= Vector2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor(); |
| 1172 | } |
| 1173 | current.size += Vector2(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM)) * get_content_scale_factor(); |
| 1174 | |
| 1175 | return current; |
| 1176 | } |
| 1177 | |
| 1178 | void PopupMenu::add_child_notify(Node *p_child) { |
| 1179 | Window::add_child_notify(p_child); |
| 1180 | |
| 1181 | if (global_menu.is_valid()) { |
| 1182 | PopupMenu *pm = Object::cast_to<PopupMenu>(p_child); |
| 1183 | for (int i = 0; i < items.size(); i++) { |
| 1184 | if (items[i].submenu == p_child) { |
| 1185 | RID submenu_rid = pm->bind_global_menu(); |
| 1186 | NativeMenu::get_singleton()->set_item_submenu(global_menu, i, submenu_rid); |
| 1187 | items.write[i].submenu_bound = true; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | _menu_changed(); |
| 1192 | } |
| 1193 | |
| 1194 | void PopupMenu::remove_child_notify(Node *p_child) { |
| 1195 | Window::remove_child_notify(p_child); |
| 1196 | |
| 1197 | PopupMenu *pm = Object::cast_to<PopupMenu>(p_child); |
| 1198 | if (!pm) { |
| 1199 | return; |
| 1200 | } |
| 1201 | if (Object::cast_to<PopupMenu>(p_child) && global_menu.is_valid()) { |
| 1202 | for (int i = 0; i < items.size(); i++) { |
| 1203 | if (items[i].submenu == p_child) { |
| 1204 | NativeMenu::get_singleton()->set_item_submenu(global_menu, i, RID()); |
| 1205 | items.write[i].submenu_bound = false; |
| 1206 | } |
| 1207 | } |
| 1208 | pm->unbind_global_menu(); |
| 1209 | } |
| 1210 | _menu_changed(); |
| 1211 | } |
| 1212 | |
| 1213 | void PopupMenu::_accessibility_action_click(const Variant &p_data, int p_idx) { |
| 1214 | activate_item(p_idx); |
| 1215 | } |
| 1216 | |
| 1217 | RID PopupMenu::get_focused_accessibility_element() const { |
| 1218 | if (mouse_over == -1) { |
| 1219 | return get_accessibility_element(); |
| 1220 | } else { |
| 1221 | const Item &item = items[mouse_over]; |
| 1222 | return item.accessibility_item_element; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | void PopupMenu::_notification(int p_what) { |
| 1227 | switch (p_what) { |
| 1228 | case NOTIFICATION_EXIT_TREE: { |
| 1229 | if (system_menu_id != NativeMenu::INVALID_MENU_ID) { |
| 1230 | unbind_global_menu(); |
| 1231 | } |
| 1232 | [[fallthrough]]; |
| 1233 | } |
| 1234 | |
| 1235 | case NOTIFICATION_ACCESSIBILITY_INVALIDATE: { |
| 1236 | for (int i = 0; i < items.size(); i++) { |
| 1237 | items.write[i].accessibility_item_element = RID(); |
| 1238 | } |
| 1239 | accessibility_scroll_element = RID(); |
| 1240 | } break; |
| 1241 | |
| 1242 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 1243 | RID ae = get_accessibility_element(); |
| 1244 | ERR_FAIL_COND(ae.is_null()); |
| 1245 | |
| 1246 | if (has_meta("_menu_name")) { |
| 1247 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_meta("_menu_name", get_name())); |
| 1248 | } |
| 1249 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_MENU); |
| 1250 | DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, items.size()); |
| 1251 | |
| 1252 | if (accessibility_scroll_element.is_null()) { |
| 1253 | accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER); |
| 1254 | } |
| 1255 | |
| 1256 | Transform2D scroll_xform; |
| 1257 | scroll_xform.set_origin(Vector2i(0, -scroll_container->get_v_scroll_bar()->get_value())); |
| 1258 | DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform); |
| 1259 | DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, Rect2(0, 0, get_size().x, scroll_container->get_v_scroll_bar()->get_max())); |
| 1260 | |
| 1261 | float scroll_width = scroll_container->get_v_scroll_bar()->is_visible_in_tree() ? scroll_container->get_v_scroll_bar()->get_size().width : 0; |
| 1262 | float display_width = control->get_size().width - scroll_width; |
| 1263 | Point2 ofs; |
| 1264 | |
| 1265 | for (int i = 0; i < items.size(); i++) { |
| 1266 | const Item &item = items.write[i]; |
| 1267 | |
| 1268 | // Avoid discrepancy between min size and drawn items due to rounding. |
| 1269 | ofs.y += i > 0 ? theme_cache.v_separation : theme_cache.v_separation - (theme_cache.v_separation / 2); |
| 1270 | |
| 1271 | Point2 item_ofs = ofs; |
| 1272 | if (item.accessibility_item_element.is_null()) { |
| 1273 | item.accessibility_item_element = DisplayServer::get_singleton()->accessibility_create_sub_element(accessibility_scroll_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM); |
| 1274 | item.accessibility_item_dirty = true; |
| 1275 | } |
| 1276 | |
| 1277 | item_ofs.x += item.indent * theme_cache.indent; |
| 1278 | float h = _get_item_height(i); |
| 1279 | |
| 1280 | if (item.accessibility_item_dirty || i == prev_mouse_over || i == mouse_over) { |
| 1281 | switch (item.checkable_type) { |
| 1282 | case Item::CHECKABLE_TYPE_NONE: { |
| 1283 | DisplayServer::get_singleton()->accessibility_update_set_role(item.accessibility_item_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM); |
| 1284 | } break; |
| 1285 | case Item::CHECKABLE_TYPE_CHECK_BOX: { |
| 1286 | DisplayServer::get_singleton()->accessibility_update_set_role(item.accessibility_item_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM_CHECK_BOX); |
| 1287 | DisplayServer::get_singleton()->accessibility_update_set_checked(item.accessibility_item_element, item.checked); |
| 1288 | } break; |
| 1289 | case Item::CHECKABLE_TYPE_RADIO_BUTTON: { |
| 1290 | DisplayServer::get_singleton()->accessibility_update_set_role(item.accessibility_item_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM_RADIO); |
| 1291 | DisplayServer::get_singleton()->accessibility_update_set_checked(item.accessibility_item_element, item.checked); |
| 1292 | } break; |
| 1293 | } |
| 1294 | |
| 1295 | DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &PopupMenu::_accessibility_action_click).bind(i)); |
| 1296 | DisplayServer::get_singleton()->accessibility_update_set_list_item_index(item.accessibility_item_element, i); |
| 1297 | DisplayServer::get_singleton()->accessibility_update_set_list_item_level(item.accessibility_item_element, 0); |
| 1298 | DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(item.accessibility_item_element, i == mouse_over); |
| 1299 | DisplayServer::get_singleton()->accessibility_update_set_name(item.accessibility_item_element, item.xl_text); |
| 1300 | DisplayServer::get_singleton()->accessibility_update_set_flag(item.accessibility_item_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, item.disabled); |
| 1301 | DisplayServer::get_singleton()->accessibility_update_set_tooltip(item.accessibility_item_element, item.tooltip); |
| 1302 | |
| 1303 | DisplayServer::get_singleton()->accessibility_update_set_bounds(item.accessibility_item_element, Rect2(item_ofs, Size2(display_width, h + theme_cache.v_separation))); |
| 1304 | |
| 1305 | item.accessibility_item_dirty = false; |
| 1306 | } |
| 1307 | ofs.y += h; |
| 1308 | } |
| 1309 | prev_mouse_over = -1; |
| 1310 | |
| 1311 | } break; |
| 1312 | |
| 1313 | case NOTIFICATION_ENTER_TREE: { |
| 1314 | PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent()); |
| 1315 | if (pm) { |
| 1316 | // Inherit submenu's popup delay time from parent menu. |
| 1317 | float pm_delay = pm->get_submenu_popup_delay(); |
| 1318 | set_submenu_popup_delay(pm_delay); |
| 1319 | } |
| 1320 | if (system_menu_id != NativeMenu::INVALID_MENU_ID) { |
| 1321 | bind_global_menu(); |
| 1322 | } |
| 1323 | } break; |
| 1324 | |
| 1325 | case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
| 1326 | case NOTIFICATION_THEME_CHANGED: { |
| 1327 | panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style); |
| 1328 | |
| 1329 | if (is_visible()) { |
| 1330 | _update_shadow_offsets(); |
| 1331 | } |
| 1332 | |
| 1333 | #ifdef TOOLS_ENABLED |
| 1334 | update_configuration_warnings(); |
| 1335 | #endif |
| 1336 | |
| 1337 | [[fallthrough]]; |
| 1338 | } |
| 1339 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 1340 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1341 | bool is_global = global_menu.is_valid(); |
| 1342 | if (is_global) { |
| 1343 | nmenu->set_interface_direction(global_menu, control->is_layout_rtl()); |
| 1344 | } |
| 1345 | for (int i = 0; i < items.size(); i++) { |
| 1346 | Item &item = items.write[i]; |
| 1347 | item.xl_text = _atr(i, item.text); |
| 1348 | item.dirty = true; |
| 1349 | if (is_global) { |
| 1350 | nmenu->set_item_text(global_menu, i, item.xl_text); |
| 1351 | } |
| 1352 | item.accessibility_item_dirty = true; |
| 1353 | _shape_item(i); |
| 1354 | queue_accessibility_update(); |
| 1355 | } |
| 1356 | |
| 1357 | child_controls_changed(); |
| 1358 | _menu_changed(); |
| 1359 | control->queue_redraw(); |
| 1360 | } break; |
| 1361 | |
| 1362 | case NOTIFICATION_WM_MOUSE_ENTER: { |
| 1363 | grab_focus(); |
| 1364 | } break; |
| 1365 | |
| 1366 | case NOTIFICATION_WM_MOUSE_EXIT: { |
| 1367 | mouse_over = -1; |
| 1368 | queue_accessibility_update(); |
| 1369 | control->queue_redraw(); |
| 1370 | } break; |
| 1371 | |
| 1372 | case NOTIFICATION_WM_SIZE_CHANGED: { |
| 1373 | if (is_visible()) { |
| 1374 | const Vector2i offsets = Vector2i(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM)); |
| 1375 | // Check if the size actually changed. |
| 1376 | if (pre_popup_rect.size + offsets != get_size()) { |
| 1377 | // Play safe, and stick with the new size. |
| 1378 | pre_popup_rect = Rect2i(); |
| 1379 | } |
| 1380 | } |
| 1381 | } break; |
| 1382 | |
| 1383 | case NOTIFICATION_POST_POPUP: { |
| 1384 | popup_time_msec = OS::get_singleton()->get_ticks_msec(); |
| 1385 | initial_button_mask = Input::get_singleton()->get_mouse_button_mask(); |
| 1386 | during_grabbed_click = (bool)initial_button_mask; |
| 1387 | } break; |
| 1388 | |
| 1389 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 1390 | Input *input = Input::get_singleton(); |
| 1391 | |
| 1392 | if (joypad_event_process && has_focus_or_active_popup()) { |
| 1393 | if (input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) { |
| 1394 | gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS; |
| 1395 | set_process_internal(false); |
| 1396 | return; |
| 1397 | } |
| 1398 | |
| 1399 | gamepad_event_delay_ms -= get_process_delta_time(); |
| 1400 | if (gamepad_event_delay_ms <= 0) { |
| 1401 | if (input->is_action_pressed("ui_down")) { |
| 1402 | gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms; |
| 1403 | int search_from = mouse_over + 1; |
| 1404 | if (search_from >= items.size()) { |
| 1405 | search_from = 0; |
| 1406 | } |
| 1407 | |
| 1408 | bool match_found = false; |
| 1409 | for (int i = search_from; i < items.size(); i++) { |
| 1410 | if (!items[i].separator && !items[i].disabled) { |
| 1411 | mouse_over = i; |
| 1412 | emit_signal(SNAME("id_focused"), items[i].id); |
| 1413 | scroll_to_item(i); |
| 1414 | control->queue_redraw(); |
| 1415 | match_found = true; |
| 1416 | break; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | if (!match_found) { |
| 1421 | // If the last item is not selectable, try re-searching from the start. |
| 1422 | for (int i = 0; i < search_from; i++) { |
| 1423 | if (!items[i].separator && !items[i].disabled) { |
| 1424 | mouse_over = i; |
| 1425 | emit_signal(SNAME("id_focused"), items[i].id); |
| 1426 | scroll_to_item(i); |
| 1427 | control->queue_redraw(); |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if (input->is_action_pressed("ui_up")) { |
| 1435 | gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms; |
| 1436 | int search_from = mouse_over - 1; |
| 1437 | if (search_from < 0) { |
| 1438 | search_from = items.size() - 1; |
| 1439 | } |
| 1440 | |
| 1441 | bool match_found = false; |
| 1442 | for (int i = search_from; i >= 0; i--) { |
| 1443 | if (!items[i].separator && !items[i].disabled) { |
| 1444 | mouse_over = i; |
| 1445 | emit_signal(SNAME("id_focused"), items[i].id); |
| 1446 | scroll_to_item(i); |
| 1447 | control->queue_redraw(); |
| 1448 | match_found = true; |
| 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | if (!match_found) { |
| 1454 | // If the first item is not selectable, try re-searching from the end. |
| 1455 | for (int i = items.size() - 1; i >= search_from; i--) { |
| 1456 | if (!items[i].separator && !items[i].disabled) { |
| 1457 | mouse_over = i; |
| 1458 | emit_signal(SNAME("id_focused"), items[i].id); |
| 1459 | scroll_to_item(i); |
| 1460 | control->queue_redraw(); |
| 1461 | break; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | // Only used when using operating system windows, and only on submenus. |
| 1470 | if (!activated_by_keyboard && !is_embedded() && autohide_areas.size() && this_submenu_index != -1) { |
| 1471 | PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent()); |
| 1472 | ERR_FAIL_NULL(parent_popup); |
| 1473 | const float win_scale = get_content_scale_factor(); |
| 1474 | Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position() - get_position(); |
| 1475 | Point2 areas_mouse_pos = mouse_pos - parent_popup->panel_offset_start; |
| 1476 | for (const Rect2 &E : autohide_areas) { |
| 1477 | if (!Rect2(Point2(), control->get_size() * win_scale).has_point(areas_mouse_pos) && E.has_point(areas_mouse_pos)) { |
| 1478 | _close_or_suspend(); |
| 1479 | return; |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | } break; |
| 1484 | |
| 1485 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 1486 | if (!is_visible()) { |
| 1487 | this_submenu_index = -1; |
| 1488 | |
| 1489 | if (mouse_over >= 0) { |
| 1490 | prev_mouse_over = mouse_over; |
| 1491 | mouse_over = -1; |
| 1492 | queue_accessibility_update(); |
| 1493 | control->queue_redraw(); |
| 1494 | } |
| 1495 | |
| 1496 | for (int i = 0; i < items.size(); i++) { |
| 1497 | if (!items[i].submenu) { |
| 1498 | continue; |
| 1499 | } |
| 1500 | items[i].submenu->hide(); |
| 1501 | } |
| 1502 | |
| 1503 | set_process_internal(false); |
| 1504 | |
| 1505 | // Remove the extra space used by the shadows, so they can be ignored when the popup is hidden. |
| 1506 | panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0); |
| 1507 | |
| 1508 | if (pre_popup_rect != Rect2i()) { |
| 1509 | set_position(pre_popup_rect.position); |
| 1510 | set_size(pre_popup_rect.size); |
| 1511 | |
| 1512 | pre_popup_rect = Rect2i(); |
| 1513 | } |
| 1514 | } else { |
| 1515 | if (!is_embedded()) { |
| 1516 | joypad_event_process = false; |
| 1517 | set_process_internal(true); |
| 1518 | } |
| 1519 | |
| 1520 | // The popup was made visible directly (without `popup_*()`), so just update the offsets without touching the rect. |
| 1521 | if (pre_popup_rect == Rect2i()) { |
| 1522 | _update_shadow_offsets(); |
| 1523 | } |
| 1524 | } |
| 1525 | } break; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | /* Methods to add items with or without icon, checkbox, shortcut. |
| 1530 | * Be sure to keep them in sync when adding new properties in the Item struct. |
| 1531 | */ |
| 1532 | |
| 1533 | #define ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel) \ |
| 1534 | item.text = p_label; \ |
| 1535 | item.xl_text = atr(p_label); \ |
| 1536 | item.id = p_id == -1 ? items.size() : p_id; \ |
| 1537 | item.accel = p_accel; |
| 1538 | |
| 1539 | void PopupMenu::add_item(const String &p_label, int p_id, Key p_accel) { |
| 1540 | Item item; |
| 1541 | ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel); |
| 1542 | items.push_back(item); |
| 1543 | |
| 1544 | if (global_menu.is_valid()) { |
| 1545 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1546 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1547 | if (item.accel != Key::NONE) { |
| 1548 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | _shape_item(items.size() - 1); |
| 1553 | queue_accessibility_update(); |
| 1554 | control->queue_redraw(); |
| 1555 | |
| 1556 | child_controls_changed(); |
| 1557 | notify_property_list_changed(); |
| 1558 | _menu_changed(); |
| 1559 | } |
| 1560 | |
| 1561 | void PopupMenu::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) { |
| 1562 | Item item; |
| 1563 | ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel); |
| 1564 | item.icon = p_icon; |
| 1565 | items.push_back(item); |
| 1566 | |
| 1567 | if (global_menu.is_valid()) { |
| 1568 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1569 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1570 | if (item.accel != Key::NONE) { |
| 1571 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 1572 | } |
| 1573 | nmenu->set_item_icon(global_menu, index, item.icon); |
| 1574 | } |
| 1575 | |
| 1576 | _shape_item(items.size() - 1); |
| 1577 | queue_accessibility_update(); |
| 1578 | control->queue_redraw(); |
| 1579 | |
| 1580 | child_controls_changed(); |
| 1581 | notify_property_list_changed(); |
| 1582 | _menu_changed(); |
| 1583 | } |
| 1584 | |
| 1585 | void PopupMenu::add_check_item(const String &p_label, int p_id, Key p_accel) { |
| 1586 | Item item; |
| 1587 | ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel); |
| 1588 | item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX; |
| 1589 | items.push_back(item); |
| 1590 | |
| 1591 | if (global_menu.is_valid()) { |
| 1592 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1593 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1594 | if (item.accel != Key::NONE) { |
| 1595 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 1596 | } |
| 1597 | nmenu->set_item_checkable(global_menu, index, true); |
| 1598 | } |
| 1599 | |
| 1600 | _shape_item(items.size() - 1); |
| 1601 | queue_accessibility_update(); |
| 1602 | control->queue_redraw(); |
| 1603 | |
| 1604 | child_controls_changed(); |
| 1605 | notify_property_list_changed(); |
| 1606 | _menu_changed(); |
| 1607 | } |
| 1608 | |
| 1609 | void PopupMenu::add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) { |
| 1610 | Item item; |
| 1611 | ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel); |
| 1612 | item.icon = p_icon; |
| 1613 | item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX; |
| 1614 | items.push_back(item); |
| 1615 | |
| 1616 | if (global_menu.is_valid()) { |
| 1617 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1618 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1619 | if (item.accel != Key::NONE) { |
| 1620 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 1621 | } |
| 1622 | nmenu->set_item_icon(global_menu, index, item.icon); |
| 1623 | nmenu->set_item_checkable(global_menu, index, true); |
| 1624 | } |
| 1625 | |
| 1626 | _shape_item(items.size() - 1); |
| 1627 | queue_accessibility_update(); |
| 1628 | control->queue_redraw(); |
| 1629 | |
| 1630 | child_controls_changed(); |
| 1631 | notify_property_list_changed(); |
| 1632 | _menu_changed(); |
| 1633 | } |
| 1634 | |
| 1635 | void PopupMenu::add_radio_check_item(const String &p_label, int p_id, Key p_accel) { |
| 1636 | Item item; |
| 1637 | ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel); |
| 1638 | item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON; |
| 1639 | items.push_back(item); |
| 1640 | |
| 1641 | if (global_menu.is_valid()) { |
| 1642 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1643 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1644 | if (item.accel != Key::NONE) { |
| 1645 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 1646 | } |
| 1647 | nmenu->set_item_radio_checkable(global_menu, index, true); |
| 1648 | } |
| 1649 | |
| 1650 | _shape_item(items.size() - 1); |
| 1651 | queue_accessibility_update(); |
| 1652 | control->queue_redraw(); |
| 1653 | |
| 1654 | child_controls_changed(); |
| 1655 | notify_property_list_changed(); |
| 1656 | _menu_changed(); |
| 1657 | } |
| 1658 | |
| 1659 | void PopupMenu::add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) { |
| 1660 | Item item; |
| 1661 | ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel); |
| 1662 | item.icon = p_icon; |
| 1663 | item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON; |
| 1664 | items.push_back(item); |
| 1665 | |
| 1666 | if (global_menu.is_valid()) { |
| 1667 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1668 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1669 | if (item.accel != Key::NONE) { |
| 1670 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 1671 | } |
| 1672 | nmenu->set_item_icon(global_menu, index, item.icon); |
| 1673 | nmenu->set_item_radio_checkable(global_menu, index, true); |
| 1674 | } |
| 1675 | |
| 1676 | _shape_item(items.size() - 1); |
| 1677 | queue_accessibility_update(); |
| 1678 | control->queue_redraw(); |
| 1679 | |
| 1680 | child_controls_changed(); |
| 1681 | notify_property_list_changed(); |
| 1682 | _menu_changed(); |
| 1683 | } |
| 1684 | |
| 1685 | void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int p_default_state, int p_id, Key p_accel) { |
| 1686 | Item item; |
| 1687 | ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel); |
| 1688 | item.max_states = p_max_states; |
| 1689 | item.state = p_default_state; |
| 1690 | items.push_back(item); |
| 1691 | |
| 1692 | if (global_menu.is_valid()) { |
| 1693 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1694 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1695 | if (item.accel != Key::NONE) { |
| 1696 | nmenu->set_item_accelerator(global_menu, index, item.accel); |
| 1697 | } |
| 1698 | nmenu->set_item_max_states(global_menu, index, item.max_states); |
| 1699 | nmenu->set_item_state(global_menu, index, item.state); |
| 1700 | } |
| 1701 | |
| 1702 | _shape_item(items.size() - 1); |
| 1703 | queue_accessibility_update(); |
| 1704 | control->queue_redraw(); |
| 1705 | |
| 1706 | child_controls_changed(); |
| 1707 | _menu_changed(); |
| 1708 | notify_property_list_changed(); |
| 1709 | } |
| 1710 | |
| 1711 | #define ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo) \ |
| 1712 | ERR_FAIL_COND_MSG(p_shortcut.is_null(), "Cannot add item with invalid Shortcut."); \ |
| 1713 | _ref_shortcut(p_shortcut); \ |
| 1714 | item.text = p_shortcut->get_name(); \ |
| 1715 | item.xl_text = atr(item.text); \ |
| 1716 | item.id = p_id == -1 ? items.size() : p_id; \ |
| 1717 | item.shortcut = p_shortcut; \ |
| 1718 | item.shortcut_is_global = p_global; \ |
| 1719 | item.allow_echo = p_allow_echo; |
| 1720 | |
| 1721 | void PopupMenu::add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) { |
| 1722 | Item item; |
| 1723 | ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo); |
| 1724 | items.push_back(item); |
| 1725 | |
| 1726 | if (global_menu.is_valid()) { |
| 1727 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1728 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1); |
| 1729 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 1730 | Array events = item.shortcut->get_events(); |
| 1731 | for (int j = 0; j < events.size(); j++) { |
| 1732 | Ref<InputEventKey> ie = events[j]; |
| 1733 | if (ie.is_valid() && _set_item_accelerator(index, ie)) { |
| 1734 | break; |
| 1735 | } |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | _shape_item(items.size() - 1); |
| 1741 | queue_accessibility_update(); |
| 1742 | control->queue_redraw(); |
| 1743 | |
| 1744 | child_controls_changed(); |
| 1745 | notify_property_list_changed(); |
| 1746 | _menu_changed(); |
| 1747 | } |
| 1748 | |
| 1749 | void PopupMenu::add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) { |
| 1750 | Item item; |
| 1751 | ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo); |
| 1752 | item.icon = p_icon; |
| 1753 | items.push_back(item); |
| 1754 | |
| 1755 | if (global_menu.is_valid()) { |
| 1756 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1757 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1); |
| 1758 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 1759 | Array events = item.shortcut->get_events(); |
| 1760 | for (int j = 0; j < events.size(); j++) { |
| 1761 | Ref<InputEventKey> ie = events[j]; |
| 1762 | if (ie.is_valid() && _set_item_accelerator(index, ie)) { |
| 1763 | break; |
| 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | nmenu->set_item_icon(global_menu, index, item.icon); |
| 1768 | } |
| 1769 | |
| 1770 | _shape_item(items.size() - 1); |
| 1771 | queue_accessibility_update(); |
| 1772 | control->queue_redraw(); |
| 1773 | |
| 1774 | child_controls_changed(); |
| 1775 | notify_property_list_changed(); |
| 1776 | _menu_changed(); |
| 1777 | } |
| 1778 | |
| 1779 | void PopupMenu::add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) { |
| 1780 | Item item; |
| 1781 | ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false); // Echo for check shortcuts doesn't make sense. |
| 1782 | item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX; |
| 1783 | items.push_back(item); |
| 1784 | |
| 1785 | if (global_menu.is_valid()) { |
| 1786 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1787 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1); |
| 1788 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 1789 | Array events = item.shortcut->get_events(); |
| 1790 | for (int j = 0; j < events.size(); j++) { |
| 1791 | Ref<InputEventKey> ie = events[j]; |
| 1792 | if (ie.is_valid() && _set_item_accelerator(index, ie)) { |
| 1793 | break; |
| 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | nmenu->set_item_checkable(global_menu, index, true); |
| 1798 | } |
| 1799 | |
| 1800 | _shape_item(items.size() - 1); |
| 1801 | queue_accessibility_update(); |
| 1802 | control->queue_redraw(); |
| 1803 | |
| 1804 | child_controls_changed(); |
| 1805 | notify_property_list_changed(); |
| 1806 | _menu_changed(); |
| 1807 | } |
| 1808 | |
| 1809 | void PopupMenu::add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) { |
| 1810 | Item item; |
| 1811 | ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false); |
| 1812 | item.icon = p_icon; |
| 1813 | item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX; |
| 1814 | items.push_back(item); |
| 1815 | |
| 1816 | if (global_menu.is_valid()) { |
| 1817 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1818 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1); |
| 1819 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 1820 | Array events = item.shortcut->get_events(); |
| 1821 | for (int j = 0; j < events.size(); j++) { |
| 1822 | Ref<InputEventKey> ie = events[j]; |
| 1823 | if (ie.is_valid() && _set_item_accelerator(index, ie)) { |
| 1824 | break; |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | nmenu->set_item_icon(global_menu, index, item.icon); |
| 1829 | nmenu->set_item_checkable(global_menu, index, true); |
| 1830 | } |
| 1831 | |
| 1832 | _shape_item(items.size() - 1); |
| 1833 | queue_accessibility_update(); |
| 1834 | control->queue_redraw(); |
| 1835 | |
| 1836 | child_controls_changed(); |
| 1837 | notify_property_list_changed(); |
| 1838 | _menu_changed(); |
| 1839 | } |
| 1840 | |
| 1841 | void PopupMenu::add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) { |
| 1842 | Item item; |
| 1843 | ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false); |
| 1844 | item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON; |
| 1845 | items.push_back(item); |
| 1846 | |
| 1847 | if (global_menu.is_valid()) { |
| 1848 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1849 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1); |
| 1850 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 1851 | Array events = item.shortcut->get_events(); |
| 1852 | for (int j = 0; j < events.size(); j++) { |
| 1853 | Ref<InputEventKey> ie = events[j]; |
| 1854 | if (ie.is_valid() && _set_item_accelerator(index, ie)) { |
| 1855 | break; |
| 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | nmenu->set_item_radio_checkable(global_menu, index, true); |
| 1860 | } |
| 1861 | |
| 1862 | _shape_item(items.size() - 1); |
| 1863 | queue_accessibility_update(); |
| 1864 | control->queue_redraw(); |
| 1865 | |
| 1866 | child_controls_changed(); |
| 1867 | notify_property_list_changed(); |
| 1868 | _menu_changed(); |
| 1869 | } |
| 1870 | |
| 1871 | void PopupMenu::add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) { |
| 1872 | Item item; |
| 1873 | ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false); |
| 1874 | item.icon = p_icon; |
| 1875 | item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON; |
| 1876 | items.push_back(item); |
| 1877 | |
| 1878 | if (global_menu.is_valid()) { |
| 1879 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1880 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1); |
| 1881 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 1882 | Array events = item.shortcut->get_events(); |
| 1883 | for (int j = 0; j < events.size(); j++) { |
| 1884 | Ref<InputEventKey> ie = events[j]; |
| 1885 | if (ie.is_valid() && _set_item_accelerator(index, ie)) { |
| 1886 | break; |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | nmenu->set_item_icon(global_menu, index, item.icon); |
| 1891 | nmenu->set_item_radio_checkable(global_menu, index, true); |
| 1892 | } |
| 1893 | |
| 1894 | _shape_item(items.size() - 1); |
| 1895 | queue_accessibility_update(); |
| 1896 | control->queue_redraw(); |
| 1897 | |
| 1898 | child_controls_changed(); |
| 1899 | notify_property_list_changed(); |
| 1900 | _menu_changed(); |
| 1901 | } |
| 1902 | |
| 1903 | void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_id) { |
| 1904 | PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(p_submenu)); |
| 1905 | ERR_FAIL_NULL_MSG(pm, vformat("Child PopupMenu \"%s\" does not exist.", p_submenu)); |
| 1906 | |
| 1907 | add_submenu_node_item(p_label, pm, p_id); |
| 1908 | } |
| 1909 | |
| 1910 | void PopupMenu::add_submenu_node_item(const String &p_label, PopupMenu *p_submenu, int p_id) { |
| 1911 | ERR_FAIL_NULL(p_submenu); |
| 1912 | |
| 1913 | if (p_submenu->get_parent() != this) { |
| 1914 | ERR_FAIL_COND_MSG(p_submenu->get_parent() != nullptr, vformat("The submenu \"%s\" already has a different parent.", p_submenu->get_name())); |
| 1915 | add_child(p_submenu); |
| 1916 | } |
| 1917 | |
| 1918 | Item item; |
| 1919 | item.text = p_label; |
| 1920 | item.xl_text = atr(p_label); |
| 1921 | item.id = p_id == -1 ? items.size() : p_id; |
| 1922 | item.accessibility_item_dirty = true; |
| 1923 | item.submenu = p_submenu; |
| 1924 | item.submenu_name = p_submenu->get_name(); |
| 1925 | items.push_back(item); |
| 1926 | |
| 1927 | if (global_menu.is_valid()) { |
| 1928 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 1929 | int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1); |
| 1930 | RID submenu_rid = p_submenu->bind_global_menu(); |
| 1931 | nmenu->set_item_submenu(global_menu, index, submenu_rid); |
| 1932 | items.write[index].submenu_bound = true; |
| 1933 | } else if (!p_submenu->is_connected("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden))) { |
| 1934 | p_submenu->connect("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden)); |
| 1935 | } |
| 1936 | |
| 1937 | _shape_item(items.size() - 1); |
| 1938 | queue_accessibility_update(); |
| 1939 | control->queue_redraw(); |
| 1940 | |
| 1941 | child_controls_changed(); |
| 1942 | notify_property_list_changed(); |
| 1943 | _menu_changed(); |
| 1944 | } |
| 1945 | |
| 1946 | #undef ITEM_SETUP_WITH_ACCEL |
| 1947 | #undef ITEM_SETUP_WITH_SHORTCUT |
| 1948 | |
| 1949 | /* Methods to modify existing items. */ |
| 1950 | |
| 1951 | void PopupMenu::set_item_text(int p_idx, const String &p_text) { |
| 1952 | if (p_idx < 0) { |
| 1953 | p_idx += get_item_count(); |
| 1954 | } |
| 1955 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 1956 | if (items[p_idx].text == p_text) { |
| 1957 | return; |
| 1958 | } |
| 1959 | items.write[p_idx].text = p_text; |
| 1960 | items.write[p_idx].xl_text = _atr(p_idx, p_text); |
| 1961 | items.write[p_idx].dirty = true; |
| 1962 | items.write[p_idx].accessibility_item_dirty = true; |
| 1963 | |
| 1964 | if (global_menu.is_valid()) { |
| 1965 | NativeMenu::get_singleton()->set_item_text(global_menu, p_idx, items[p_idx].xl_text); |
| 1966 | } |
| 1967 | |
| 1968 | _shape_item(p_idx); |
| 1969 | queue_accessibility_update(); |
| 1970 | control->queue_redraw(); |
| 1971 | |
| 1972 | child_controls_changed(); |
| 1973 | _menu_changed(); |
| 1974 | } |
| 1975 | |
| 1976 | void PopupMenu::set_item_text_direction(int p_idx, Control::TextDirection p_text_direction) { |
| 1977 | if (p_idx < 0) { |
| 1978 | p_idx += get_item_count(); |
| 1979 | } |
| 1980 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 1981 | ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); |
| 1982 | |
| 1983 | if (items[p_idx].text_direction != p_text_direction) { |
| 1984 | items.write[p_idx].text_direction = p_text_direction; |
| 1985 | items.write[p_idx].dirty = true; |
| 1986 | items.write[p_idx].accessibility_item_dirty = true; |
| 1987 | |
| 1988 | _shape_item(p_idx); |
| 1989 | queue_accessibility_update(); |
| 1990 | control->queue_redraw(); |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | void PopupMenu::set_item_language(int p_idx, const String &p_language) { |
| 1995 | if (p_idx < 0) { |
| 1996 | p_idx += get_item_count(); |
| 1997 | } |
| 1998 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 1999 | if (items[p_idx].language != p_language) { |
| 2000 | items.write[p_idx].language = p_language; |
| 2001 | items.write[p_idx].dirty = true; |
| 2002 | items.write[p_idx].accessibility_item_dirty = true; |
| 2003 | |
| 2004 | _shape_item(p_idx); |
| 2005 | queue_accessibility_update(); |
| 2006 | control->queue_redraw(); |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | void PopupMenu::set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode) { |
| 2011 | if (p_idx < 0) { |
| 2012 | p_idx += get_item_count(); |
| 2013 | } |
| 2014 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2015 | if (items[p_idx].auto_translate_mode == p_mode) { |
| 2016 | return; |
| 2017 | } |
| 2018 | items.write[p_idx].auto_translate_mode = p_mode; |
| 2019 | items.write[p_idx].xl_text = _atr(p_idx, items[p_idx].text); |
| 2020 | items.write[p_idx].dirty = true; |
| 2021 | control->queue_redraw(); |
| 2022 | } |
| 2023 | |
| 2024 | void PopupMenu::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) { |
| 2025 | if (p_idx < 0) { |
| 2026 | p_idx += get_item_count(); |
| 2027 | } |
| 2028 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2029 | |
| 2030 | if (items[p_idx].icon == p_icon) { |
| 2031 | return; |
| 2032 | } |
| 2033 | |
| 2034 | items.write[p_idx].icon = p_icon; |
| 2035 | |
| 2036 | if (global_menu.is_valid()) { |
| 2037 | NativeMenu::get_singleton()->set_item_icon(global_menu, p_idx, items[p_idx].icon); |
| 2038 | } |
| 2039 | |
| 2040 | control->queue_redraw(); |
| 2041 | child_controls_changed(); |
| 2042 | _menu_changed(); |
| 2043 | } |
| 2044 | |
| 2045 | void PopupMenu::set_item_icon_max_width(int p_idx, int p_width) { |
| 2046 | if (p_idx < 0) { |
| 2047 | p_idx += get_item_count(); |
| 2048 | } |
| 2049 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2050 | |
| 2051 | if (items[p_idx].icon_max_width == p_width) { |
| 2052 | return; |
| 2053 | } |
| 2054 | |
| 2055 | items.write[p_idx].icon_max_width = p_width; |
| 2056 | |
| 2057 | control->queue_redraw(); |
| 2058 | child_controls_changed(); |
| 2059 | _menu_changed(); |
| 2060 | } |
| 2061 | |
| 2062 | void PopupMenu::set_item_icon_modulate(int p_idx, const Color &p_modulate) { |
| 2063 | if (p_idx < 0) { |
| 2064 | p_idx += get_item_count(); |
| 2065 | } |
| 2066 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2067 | |
| 2068 | if (items[p_idx].icon_modulate == p_modulate) { |
| 2069 | return; |
| 2070 | } |
| 2071 | |
| 2072 | items.write[p_idx].icon_modulate = p_modulate; |
| 2073 | control->queue_redraw(); |
| 2074 | } |
| 2075 | |
| 2076 | void PopupMenu::set_item_checked(int p_idx, bool p_checked) { |
| 2077 | if (p_idx < 0) { |
| 2078 | p_idx += get_item_count(); |
| 2079 | } |
| 2080 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2081 | |
| 2082 | if (items[p_idx].checked == p_checked) { |
| 2083 | return; |
| 2084 | } |
| 2085 | |
| 2086 | items.write[p_idx].checked = p_checked; |
| 2087 | items.write[p_idx].accessibility_item_dirty = true; |
| 2088 | |
| 2089 | if (global_menu.is_valid()) { |
| 2090 | NativeMenu::get_singleton()->set_item_checked(global_menu, p_idx, p_checked); |
| 2091 | } |
| 2092 | |
| 2093 | queue_accessibility_update(); |
| 2094 | control->queue_redraw(); |
| 2095 | child_controls_changed(); |
| 2096 | _menu_changed(); |
| 2097 | } |
| 2098 | |
| 2099 | void PopupMenu::set_item_id(int p_idx, int p_id) { |
| 2100 | if (p_idx < 0) { |
| 2101 | p_idx += get_item_count(); |
| 2102 | } |
| 2103 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2104 | |
| 2105 | if (items[p_idx].id == p_id) { |
| 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | items.write[p_idx].id = p_id; |
| 2110 | |
| 2111 | // `global_menu` does not know about IDs so there is no need to update it. |
| 2112 | |
| 2113 | control->queue_redraw(); |
| 2114 | child_controls_changed(); |
| 2115 | _menu_changed(); |
| 2116 | } |
| 2117 | |
| 2118 | void PopupMenu::set_item_accelerator(int p_idx, Key p_accel) { |
| 2119 | if (p_idx < 0) { |
| 2120 | p_idx += get_item_count(); |
| 2121 | } |
| 2122 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2123 | |
| 2124 | if (items[p_idx].accel == p_accel) { |
| 2125 | return; |
| 2126 | } |
| 2127 | |
| 2128 | items.write[p_idx].accel = p_accel; |
| 2129 | items.write[p_idx].dirty = true; |
| 2130 | items.write[p_idx].accessibility_item_dirty = true; |
| 2131 | |
| 2132 | if (global_menu.is_valid()) { |
| 2133 | NativeMenu::get_singleton()->set_item_accelerator(global_menu, p_idx, p_accel); |
| 2134 | } |
| 2135 | |
| 2136 | queue_accessibility_update(); |
| 2137 | control->queue_redraw(); |
| 2138 | child_controls_changed(); |
| 2139 | _menu_changed(); |
| 2140 | } |
| 2141 | |
| 2142 | void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) { |
| 2143 | if (p_idx < 0) { |
| 2144 | p_idx += get_item_count(); |
| 2145 | } |
| 2146 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2147 | |
| 2148 | if (items[p_idx].metadata == p_meta) { |
| 2149 | return; |
| 2150 | } |
| 2151 | |
| 2152 | items.write[p_idx].metadata = p_meta; |
| 2153 | child_controls_changed(); |
| 2154 | _menu_changed(); |
| 2155 | } |
| 2156 | |
| 2157 | void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) { |
| 2158 | if (p_idx < 0) { |
| 2159 | p_idx += get_item_count(); |
| 2160 | } |
| 2161 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2162 | |
| 2163 | if (items[p_idx].disabled == p_disabled) { |
| 2164 | return; |
| 2165 | } |
| 2166 | |
| 2167 | items.write[p_idx].disabled = p_disabled; |
| 2168 | items.write[p_idx].accessibility_item_dirty = true; |
| 2169 | |
| 2170 | if (global_menu.is_valid()) { |
| 2171 | NativeMenu::get_singleton()->set_item_disabled(global_menu, p_idx, p_disabled); |
| 2172 | } |
| 2173 | |
| 2174 | queue_accessibility_update(); |
| 2175 | control->queue_redraw(); |
| 2176 | child_controls_changed(); |
| 2177 | _menu_changed(); |
| 2178 | } |
| 2179 | |
| 2180 | void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) { |
| 2181 | if (p_idx < 0) { |
| 2182 | p_idx += get_item_count(); |
| 2183 | } |
| 2184 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2185 | |
| 2186 | if (items[p_idx].submenu_name == p_submenu) { |
| 2187 | return; |
| 2188 | } |
| 2189 | |
| 2190 | PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(p_submenu)); |
| 2191 | ERR_FAIL_NULL_MSG(pm, vformat("Child PopupMenu \"%s\" does not exist.", p_submenu)); |
| 2192 | |
| 2193 | set_item_submenu_node(p_idx, pm); |
| 2194 | } |
| 2195 | |
| 2196 | void PopupMenu::set_item_submenu_node(int p_idx, PopupMenu *p_submenu) { |
| 2197 | ERR_FAIL_NULL(p_submenu); |
| 2198 | if (p_idx < 0) { |
| 2199 | p_idx += get_item_count(); |
| 2200 | } |
| 2201 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2202 | |
| 2203 | if (p_submenu->get_parent() != this) { |
| 2204 | ERR_FAIL_COND_MSG(p_submenu->get_parent() != nullptr, vformat("The submenu \"%s\" already has a different parent.", p_submenu->get_name())); |
| 2205 | add_child(p_submenu); |
| 2206 | } |
| 2207 | |
| 2208 | if (global_menu.is_valid()) { |
| 2209 | if (items[p_idx].submenu_bound) { |
| 2210 | PopupMenu *pm = items[p_idx].submenu; |
| 2211 | if (pm) { |
| 2212 | NativeMenu::get_singleton()->set_item_submenu(global_menu, p_idx, RID()); |
| 2213 | pm->unbind_global_menu(); |
| 2214 | } |
| 2215 | items.write[p_idx].submenu_bound = false; |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | items.write[p_idx].submenu = p_submenu; |
| 2220 | |
| 2221 | if (global_menu.is_valid()) { |
| 2222 | PopupMenu *pm = items[p_idx].submenu; |
| 2223 | if (pm) { |
| 2224 | RID submenu_rid = pm->bind_global_menu(); |
| 2225 | NativeMenu::get_singleton()->set_item_submenu(global_menu, p_idx, submenu_rid); |
| 2226 | items.write[p_idx].submenu_bound = true; |
| 2227 | } |
| 2228 | } else if (!p_submenu->is_connected("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden))) { |
| 2229 | p_submenu->connect("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden)); |
| 2230 | } |
| 2231 | control->queue_redraw(); |
| 2232 | child_controls_changed(); |
| 2233 | _menu_changed(); |
| 2234 | } |
| 2235 | |
| 2236 | void PopupMenu::_close_suspended_timeout() { |
| 2237 | if (submenu_over != -1 && is_embedded()) { |
| 2238 | ERR_FAIL_INDEX_MSG(submenu_over, items.size(), vformat("Invalid submenu index %d in _close_suspended_timeout.", submenu_over)); |
| 2239 | if (items[submenu_over].submenu->is_visible()) { |
| 2240 | items[submenu_over].submenu->_close_or_suspend(); |
| 2241 | } |
| 2242 | } |
| 2243 | if (active_submenu_index != -1 && items[active_submenu_index].submenu->is_visible()) { |
| 2244 | // Closes the submenu if the mouse is moved off the parent item toward the submenu, |
| 2245 | // but comes to a stop before reaching the submenu and the timeout is reached. |
| 2246 | items[active_submenu_index].submenu->_close_pressed(); |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | void PopupMenu::_submenu_hidden() { |
| 2251 | // Ensure the submenu_timer is not running to avoid any race conditions between opening and closing submenus. |
| 2252 | if (!submenu_timer->is_stopped()) { |
| 2253 | WARN_VERBOSE("The submenu_timer should never be running when the _submenu_hidden signal is emitted."); |
| 2254 | return; |
| 2255 | } |
| 2256 | if (active_submenu_index == -1) { |
| 2257 | WARN_VERBOSE("The active_submenu_index should never be -1 when _submenu_hidden is entered."); |
| 2258 | return; |
| 2259 | } |
| 2260 | active_submenu_index = -1; |
| 2261 | submenu_over = -1; |
| 2262 | submenu_mouse_exited_ticks_msec = -1; |
| 2263 | mouse_movement_was_tested = false; |
| 2264 | close_was_suspended = false; |
| 2265 | queue_accessibility_update(); |
| 2266 | control->queue_redraw(); |
| 2267 | if (!activated_by_keyboard) { |
| 2268 | Point2 mouse_pos = is_embedded() ? get_mouse_position() : Point2(DisplayServer::get_singleton()->mouse_get_position() - get_position()); |
| 2269 | _mouse_over_update(mouse_pos); |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | void PopupMenu::toggle_item_checked(int p_idx) { |
| 2274 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2275 | items.write[p_idx].checked = !items[p_idx].checked; |
| 2276 | items.write[p_idx].accessibility_item_dirty = true; |
| 2277 | |
| 2278 | if (global_menu.is_valid()) { |
| 2279 | NativeMenu::get_singleton()->set_item_checked(global_menu, p_idx, items[p_idx].checked); |
| 2280 | } |
| 2281 | |
| 2282 | queue_accessibility_update(); |
| 2283 | control->queue_redraw(); |
| 2284 | child_controls_changed(); |
| 2285 | _menu_changed(); |
| 2286 | } |
| 2287 | |
| 2288 | String PopupMenu::get_item_text(int p_idx) const { |
| 2289 | ERR_FAIL_INDEX_V(p_idx, items.size(), ""); |
| 2290 | return items[p_idx].text; |
| 2291 | } |
| 2292 | |
| 2293 | String PopupMenu::get_item_xl_text(int p_idx) const { |
| 2294 | ERR_FAIL_INDEX_V(p_idx, items.size(), ""); |
| 2295 | return items[p_idx].xl_text; |
| 2296 | } |
| 2297 | |
| 2298 | Control::TextDirection PopupMenu::get_item_text_direction(int p_idx) const { |
| 2299 | ERR_FAIL_INDEX_V(p_idx, items.size(), Control::TEXT_DIRECTION_INHERITED); |
| 2300 | return items[p_idx].text_direction; |
| 2301 | } |
| 2302 | |
| 2303 | String PopupMenu::get_item_language(int p_idx) const { |
| 2304 | ERR_FAIL_INDEX_V(p_idx, items.size(), ""); |
| 2305 | return items[p_idx].language; |
| 2306 | } |
| 2307 | |
| 2308 | Node::AutoTranslateMode PopupMenu::get_item_auto_translate_mode(int p_idx) const { |
| 2309 | ERR_FAIL_INDEX_V(p_idx, items.size(), AUTO_TRANSLATE_MODE_INHERIT); |
| 2310 | return items[p_idx].auto_translate_mode; |
| 2311 | } |
| 2312 | |
| 2313 | int PopupMenu::get_item_idx_from_text(const String &text) const { |
| 2314 | for (int idx = 0; idx < items.size(); idx++) { |
| 2315 | if (items[idx].text == text) { |
| 2316 | return idx; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | return -1; |
| 2321 | } |
| 2322 | |
| 2323 | Ref<Texture2D> PopupMenu::get_item_icon(int p_idx) const { |
| 2324 | ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>()); |
| 2325 | return items[p_idx].icon; |
| 2326 | } |
| 2327 | |
| 2328 | int PopupMenu::get_item_icon_max_width(int p_idx) const { |
| 2329 | ERR_FAIL_INDEX_V(p_idx, items.size(), 0); |
| 2330 | return items[p_idx].icon_max_width; |
| 2331 | } |
| 2332 | |
| 2333 | Color PopupMenu::get_item_icon_modulate(int p_idx) const { |
| 2334 | ERR_FAIL_INDEX_V(p_idx, items.size(), Color()); |
| 2335 | return items[p_idx].icon_modulate; |
| 2336 | } |
| 2337 | |
| 2338 | Key PopupMenu::get_item_accelerator(int p_idx) const { |
| 2339 | ERR_FAIL_INDEX_V(p_idx, items.size(), Key::NONE); |
| 2340 | return items[p_idx].accel; |
| 2341 | } |
| 2342 | |
| 2343 | Variant PopupMenu::get_item_metadata(int p_idx) const { |
| 2344 | ERR_FAIL_INDEX_V(p_idx, items.size(), Variant()); |
| 2345 | return items[p_idx].metadata; |
| 2346 | } |
| 2347 | |
| 2348 | bool PopupMenu::is_item_disabled(int p_idx) const { |
| 2349 | ERR_FAIL_INDEX_V(p_idx, items.size(), false); |
| 2350 | return items[p_idx].disabled; |
| 2351 | } |
| 2352 | |
| 2353 | bool PopupMenu::is_item_checked(int p_idx) const { |
| 2354 | ERR_FAIL_INDEX_V(p_idx, items.size(), false); |
| 2355 | return items[p_idx].checked; |
| 2356 | } |
| 2357 | |
| 2358 | int PopupMenu::get_item_id(int p_idx) const { |
| 2359 | ERR_FAIL_INDEX_V(p_idx, items.size(), 0); |
| 2360 | return items[p_idx].id; |
| 2361 | } |
| 2362 | |
| 2363 | int PopupMenu::get_item_index(int p_id) const { |
| 2364 | for (int i = 0; i < items.size(); i++) { |
| 2365 | if (items[i].id == p_id) { |
| 2366 | return i; |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | return -1; |
| 2371 | } |
| 2372 | |
| 2373 | String PopupMenu::get_item_submenu(int p_idx) const { |
| 2374 | ERR_FAIL_INDEX_V(p_idx, items.size(), ""); |
| 2375 | return items[p_idx].submenu_name; |
| 2376 | } |
| 2377 | |
| 2378 | PopupMenu *PopupMenu::get_item_submenu_node(int p_idx) const { |
| 2379 | ERR_FAIL_INDEX_V(p_idx, items.size(), nullptr); |
| 2380 | return items[p_idx].submenu; |
| 2381 | } |
| 2382 | |
| 2383 | String PopupMenu::get_item_tooltip(int p_idx) const { |
| 2384 | ERR_FAIL_INDEX_V(p_idx, items.size(), ""); |
| 2385 | return items[p_idx].tooltip; |
| 2386 | } |
| 2387 | |
| 2388 | Ref<Shortcut> PopupMenu::get_item_shortcut(int p_idx) const { |
| 2389 | ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Shortcut>()); |
| 2390 | return items[p_idx].shortcut; |
| 2391 | } |
| 2392 | |
| 2393 | int PopupMenu::get_item_indent(int p_idx) const { |
| 2394 | ERR_FAIL_INDEX_V(p_idx, items.size(), 0); |
| 2395 | return items[p_idx].indent; |
| 2396 | } |
| 2397 | |
| 2398 | int PopupMenu::get_item_max_states(int p_idx) const { |
| 2399 | ERR_FAIL_INDEX_V(p_idx, items.size(), -1); |
| 2400 | return items[p_idx].max_states; |
| 2401 | } |
| 2402 | |
| 2403 | int PopupMenu::get_item_state(int p_idx) const { |
| 2404 | ERR_FAIL_INDEX_V(p_idx, items.size(), -1); |
| 2405 | return items[p_idx].state; |
| 2406 | } |
| 2407 | |
| 2408 | void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) { |
| 2409 | if (p_idx < 0) { |
| 2410 | p_idx += get_item_count(); |
| 2411 | } |
| 2412 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2413 | |
| 2414 | if (items[p_idx].separator == p_separator) { |
| 2415 | return; |
| 2416 | } |
| 2417 | |
| 2418 | items.write[p_idx].separator = p_separator; |
| 2419 | items.write[p_idx].accessibility_item_dirty = true; |
| 2420 | |
| 2421 | queue_accessibility_update(); |
| 2422 | control->queue_redraw(); |
| 2423 | } |
| 2424 | |
| 2425 | bool PopupMenu::is_item_separator(int p_idx) const { |
| 2426 | ERR_FAIL_INDEX_V(p_idx, items.size(), false); |
| 2427 | return items[p_idx].separator; |
| 2428 | } |
| 2429 | |
| 2430 | void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) { |
| 2431 | if (p_idx < 0) { |
| 2432 | p_idx += get_item_count(); |
| 2433 | } |
| 2434 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2435 | |
| 2436 | int type = (int)(p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE); |
| 2437 | if (type == items[p_idx].checkable_type) { |
| 2438 | return; |
| 2439 | } |
| 2440 | |
| 2441 | items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE; |
| 2442 | items.write[p_idx].accessibility_item_dirty = true; |
| 2443 | |
| 2444 | if (global_menu.is_valid()) { |
| 2445 | NativeMenu::get_singleton()->set_item_checkable(global_menu, p_idx, p_checkable); |
| 2446 | } |
| 2447 | |
| 2448 | queue_accessibility_update(); |
| 2449 | control->queue_redraw(); |
| 2450 | _menu_changed(); |
| 2451 | } |
| 2452 | |
| 2453 | void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) { |
| 2454 | if (p_idx < 0) { |
| 2455 | p_idx += get_item_count(); |
| 2456 | } |
| 2457 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2458 | |
| 2459 | int type = (int)(p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE); |
| 2460 | if (type == items[p_idx].checkable_type) { |
| 2461 | return; |
| 2462 | } |
| 2463 | |
| 2464 | items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE; |
| 2465 | items.write[p_idx].accessibility_item_dirty = true; |
| 2466 | |
| 2467 | if (global_menu.is_valid()) { |
| 2468 | NativeMenu::get_singleton()->set_item_radio_checkable(global_menu, p_idx, p_radio_checkable); |
| 2469 | } |
| 2470 | |
| 2471 | queue_accessibility_update(); |
| 2472 | control->queue_redraw(); |
| 2473 | _menu_changed(); |
| 2474 | } |
| 2475 | |
| 2476 | void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) { |
| 2477 | if (p_idx < 0) { |
| 2478 | p_idx += get_item_count(); |
| 2479 | } |
| 2480 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2481 | |
| 2482 | if (items[p_idx].tooltip == p_tooltip) { |
| 2483 | return; |
| 2484 | } |
| 2485 | |
| 2486 | items.write[p_idx].tooltip = p_tooltip; |
| 2487 | items.write[p_idx].accessibility_item_dirty = true; |
| 2488 | |
| 2489 | if (global_menu.is_valid()) { |
| 2490 | NativeMenu::get_singleton()->set_item_tooltip(global_menu, p_idx, p_tooltip); |
| 2491 | } |
| 2492 | |
| 2493 | queue_accessibility_update(); |
| 2494 | control->queue_redraw(); |
| 2495 | _menu_changed(); |
| 2496 | } |
| 2497 | |
| 2498 | void PopupMenu::set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global) { |
| 2499 | if (p_idx < 0) { |
| 2500 | p_idx += get_item_count(); |
| 2501 | } |
| 2502 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2503 | |
| 2504 | if (items[p_idx].shortcut == p_shortcut && items[p_idx].shortcut_is_global == p_global && items[p_idx].shortcut.is_valid() == p_shortcut.is_valid()) { |
| 2505 | return; |
| 2506 | } |
| 2507 | |
| 2508 | if (items[p_idx].shortcut.is_valid()) { |
| 2509 | _unref_shortcut(items[p_idx].shortcut); |
| 2510 | } |
| 2511 | items.write[p_idx].shortcut = p_shortcut; |
| 2512 | items.write[p_idx].shortcut_is_global = p_global; |
| 2513 | items.write[p_idx].dirty = true; |
| 2514 | |
| 2515 | if (items[p_idx].shortcut.is_valid()) { |
| 2516 | _ref_shortcut(items[p_idx].shortcut); |
| 2517 | } |
| 2518 | |
| 2519 | if (global_menu.is_valid()) { |
| 2520 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 2521 | nmenu->set_item_accelerator(global_menu, p_idx, Key::NONE); |
| 2522 | if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) { |
| 2523 | Array events = items[p_idx].shortcut->get_events(); |
| 2524 | for (int j = 0; j < events.size(); j++) { |
| 2525 | Ref<InputEventKey> ie = events[j]; |
| 2526 | if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) { |
| 2527 | break; |
| 2528 | } |
| 2529 | } |
| 2530 | if (p_global) { |
| 2531 | nmenu->set_item_key_callback(global_menu, p_idx, callable_mp(this, &PopupMenu::activate_item)); |
| 2532 | } else { |
| 2533 | nmenu->set_item_key_callback(global_menu, p_idx, Callable()); |
| 2534 | } |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | control->queue_redraw(); |
| 2539 | _menu_changed(); |
| 2540 | } |
| 2541 | |
| 2542 | void PopupMenu::set_item_indent(int p_idx, int p_indent) { |
| 2543 | if (p_idx < 0) { |
| 2544 | p_idx += get_item_count(); |
| 2545 | } |
| 2546 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2547 | |
| 2548 | if (items.write[p_idx].indent == p_indent) { |
| 2549 | return; |
| 2550 | } |
| 2551 | items.write[p_idx].indent = p_indent; |
| 2552 | |
| 2553 | if (global_menu.is_valid()) { |
| 2554 | NativeMenu::get_singleton()->set_item_indentation_level(global_menu, p_idx, p_indent); |
| 2555 | } |
| 2556 | |
| 2557 | control->queue_redraw(); |
| 2558 | child_controls_changed(); |
| 2559 | _menu_changed(); |
| 2560 | } |
| 2561 | |
| 2562 | void PopupMenu::set_item_max_states(int p_idx, int p_max_states) { |
| 2563 | if (p_idx < 0) { |
| 2564 | p_idx += get_item_count(); |
| 2565 | } |
| 2566 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2567 | |
| 2568 | if (items[p_idx].max_states == p_max_states) { |
| 2569 | return; |
| 2570 | } |
| 2571 | |
| 2572 | items.write[p_idx].max_states = p_max_states; |
| 2573 | |
| 2574 | if (global_menu.is_valid()) { |
| 2575 | NativeMenu::get_singleton()->set_item_max_states(global_menu, p_idx, p_max_states); |
| 2576 | } |
| 2577 | |
| 2578 | control->queue_redraw(); |
| 2579 | _menu_changed(); |
| 2580 | } |
| 2581 | |
| 2582 | void PopupMenu::set_item_multistate(int p_idx, int p_state) { |
| 2583 | if (p_idx < 0) { |
| 2584 | p_idx += get_item_count(); |
| 2585 | } |
| 2586 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2587 | |
| 2588 | if (items[p_idx].state == p_state) { |
| 2589 | return; |
| 2590 | } |
| 2591 | |
| 2592 | items.write[p_idx].state = p_state; |
| 2593 | items.write[p_idx].accessibility_item_dirty = true; |
| 2594 | |
| 2595 | if (global_menu.is_valid()) { |
| 2596 | NativeMenu::get_singleton()->set_item_state(global_menu, p_idx, p_state); |
| 2597 | } |
| 2598 | |
| 2599 | queue_accessibility_update(); |
| 2600 | control->queue_redraw(); |
| 2601 | _menu_changed(); |
| 2602 | } |
| 2603 | |
| 2604 | void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) { |
| 2605 | if (p_idx < 0) { |
| 2606 | p_idx += get_item_count(); |
| 2607 | } |
| 2608 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2609 | |
| 2610 | if (items[p_idx].shortcut_is_disabled == p_disabled) { |
| 2611 | return; |
| 2612 | } |
| 2613 | |
| 2614 | items.write[p_idx].shortcut_is_disabled = p_disabled; |
| 2615 | |
| 2616 | if (global_menu.is_valid()) { |
| 2617 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 2618 | nmenu->set_item_accelerator(global_menu, p_idx, Key::NONE); |
| 2619 | if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) { |
| 2620 | Array events = items[p_idx].shortcut->get_events(); |
| 2621 | for (int j = 0; j < events.size(); j++) { |
| 2622 | Ref<InputEventKey> ie = events[j]; |
| 2623 | if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) { |
| 2624 | break; |
| 2625 | } |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | control->queue_redraw(); |
| 2631 | _menu_changed(); |
| 2632 | } |
| 2633 | |
| 2634 | void PopupMenu::toggle_item_multistate(int p_idx) { |
| 2635 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2636 | if (0 >= items[p_idx].max_states) { |
| 2637 | return; |
| 2638 | } |
| 2639 | |
| 2640 | ++items.write[p_idx].state; |
| 2641 | if (items.write[p_idx].max_states <= items[p_idx].state) { |
| 2642 | items.write[p_idx].state = 0; |
| 2643 | } |
| 2644 | items.write[p_idx].accessibility_item_dirty = true; |
| 2645 | |
| 2646 | if (global_menu.is_valid()) { |
| 2647 | NativeMenu::get_singleton()->set_item_state(global_menu, p_idx, items[p_idx].state); |
| 2648 | } |
| 2649 | |
| 2650 | queue_accessibility_update(); |
| 2651 | control->queue_redraw(); |
| 2652 | _menu_changed(); |
| 2653 | } |
| 2654 | |
| 2655 | bool PopupMenu::is_item_checkable(int p_idx) const { |
| 2656 | ERR_FAIL_INDEX_V(p_idx, items.size(), false); |
| 2657 | return items[p_idx].checkable_type; |
| 2658 | } |
| 2659 | |
| 2660 | bool PopupMenu::is_item_radio_checkable(int p_idx) const { |
| 2661 | ERR_FAIL_INDEX_V(p_idx, items.size(), false); |
| 2662 | return items[p_idx].checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON; |
| 2663 | } |
| 2664 | |
| 2665 | bool PopupMenu::is_item_shortcut_global(int p_idx) const { |
| 2666 | ERR_FAIL_INDEX_V(p_idx, items.size(), false); |
| 2667 | return items[p_idx].shortcut_is_global; |
| 2668 | } |
| 2669 | |
| 2670 | bool PopupMenu::is_item_shortcut_disabled(int p_idx) const { |
| 2671 | ERR_FAIL_INDEX_V(p_idx, items.size(), false); |
| 2672 | return items[p_idx].shortcut_is_disabled; |
| 2673 | } |
| 2674 | |
| 2675 | void PopupMenu::set_focused_item(int p_idx) { |
| 2676 | if (p_idx != -1) { |
| 2677 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2678 | } |
| 2679 | |
| 2680 | if (mouse_over == p_idx) { |
| 2681 | return; |
| 2682 | } |
| 2683 | |
| 2684 | prev_mouse_over = mouse_over; |
| 2685 | mouse_over = p_idx; |
| 2686 | if (mouse_over != -1) { |
| 2687 | scroll_to_item(mouse_over); |
| 2688 | } |
| 2689 | queue_accessibility_update(); |
| 2690 | control->queue_redraw(); |
| 2691 | } |
| 2692 | |
| 2693 | int PopupMenu::get_focused_item() const { |
| 2694 | return mouse_over; |
| 2695 | } |
| 2696 | |
| 2697 | void PopupMenu::set_item_count(int p_count) { |
| 2698 | ERR_FAIL_COND(p_count < 0); |
| 2699 | int prev_size = items.size(); |
| 2700 | |
| 2701 | if (prev_size == p_count) { |
| 2702 | return; |
| 2703 | } |
| 2704 | |
| 2705 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 2706 | bool is_global = global_menu.is_valid(); |
| 2707 | |
| 2708 | if (is_global && prev_size > p_count) { |
| 2709 | for (int i = prev_size - 1; i >= p_count; i--) { |
| 2710 | nmenu->remove_item(global_menu, i); |
| 2711 | if (items[i].accessibility_item_element.is_valid()) { |
| 2712 | DisplayServer::get_singleton()->accessibility_free_element(items.write[i].accessibility_item_element); |
| 2713 | items.write[i].accessibility_item_element = RID(); |
| 2714 | } |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | items.resize(p_count); |
| 2719 | |
| 2720 | if (prev_size < p_count) { |
| 2721 | for (int i = prev_size; i < p_count; i++) { |
| 2722 | items.write[i].id = i; |
| 2723 | if (is_global) { |
| 2724 | nmenu->add_item(global_menu, String(), callable_mp(this, &PopupMenu::activate_item), Callable(), i); |
| 2725 | } |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | control->queue_redraw(); |
| 2730 | child_controls_changed(); |
| 2731 | notify_property_list_changed(); |
| 2732 | _menu_changed(); |
| 2733 | } |
| 2734 | |
| 2735 | int PopupMenu::get_item_count() const { |
| 2736 | return items.size(); |
| 2737 | } |
| 2738 | |
| 2739 | void PopupMenu::scroll_to_item(int p_idx) { |
| 2740 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2741 | |
| 2742 | // Calculate the position of the item relative to the visible area. |
| 2743 | int item_y = items[p_idx]._ofs_cache; |
| 2744 | int visible_height = scroll_container->get_size().height; |
| 2745 | int relative_y = item_y - scroll_container->get_v_scroll(); |
| 2746 | |
| 2747 | // If item is not fully visible, adjust scroll. |
| 2748 | if (relative_y < 0) { |
| 2749 | scroll_container->set_v_scroll(item_y); |
| 2750 | } else if (relative_y + items[p_idx]._height_cache > visible_height) { |
| 2751 | scroll_container->set_v_scroll(item_y + items[p_idx]._height_cache - visible_height); |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | void PopupMenu::set_prefer_native_menu(bool p_enabled) { |
| 2756 | if (prefer_native != p_enabled) { |
| 2757 | prefer_native = p_enabled; |
| 2758 | if (prefer_native) { |
| 2759 | bind_global_menu(); |
| 2760 | } else { |
| 2761 | unbind_global_menu(); |
| 2762 | } |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | bool PopupMenu::is_prefer_native_menu() const { |
| 2767 | return prefer_native; |
| 2768 | } |
| 2769 | |
| 2770 | bool PopupMenu::is_native_menu() const { |
| 2771 | #ifdef TOOLS_ENABLED |
| 2772 | if (is_part_of_edited_scene()) { |
| 2773 | return false; |
| 2774 | } |
| 2775 | #endif |
| 2776 | |
| 2777 | return global_menu.is_valid(); |
| 2778 | } |
| 2779 | |
| 2780 | bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) { |
| 2781 | ERR_FAIL_COND_V(p_event.is_null(), false); |
| 2782 | Key code = Key::NONE; |
| 2783 | Ref<InputEventKey> k = p_event; |
| 2784 | |
| 2785 | if (k.is_valid()) { |
| 2786 | code = k->get_keycode(); |
| 2787 | if (code == Key::NONE) { |
| 2788 | code = (Key)k->get_unicode(); |
| 2789 | } |
| 2790 | if (k->is_ctrl_pressed()) { |
| 2791 | code |= KeyModifierMask::CTRL; |
| 2792 | } |
| 2793 | if (k->is_alt_pressed()) { |
| 2794 | code |= KeyModifierMask::ALT; |
| 2795 | } |
| 2796 | if (k->is_meta_pressed()) { |
| 2797 | code |= KeyModifierMask::META; |
| 2798 | } |
| 2799 | if (k->is_shift_pressed()) { |
| 2800 | code |= KeyModifierMask::SHIFT; |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | for (int i = 0; i < items.size(); i++) { |
| 2805 | if (is_item_disabled(i) || items[i].shortcut_is_disabled || (!items[i].allow_echo && p_event->is_echo())) { |
| 2806 | continue; |
| 2807 | } |
| 2808 | |
| 2809 | if (items[i].shortcut.is_valid() && items[i].shortcut->matches_event(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) { |
| 2810 | activate_item(i); |
| 2811 | return true; |
| 2812 | } |
| 2813 | |
| 2814 | if (code != Key::NONE && items[i].accel == code) { |
| 2815 | activate_item(i); |
| 2816 | return true; |
| 2817 | } |
| 2818 | |
| 2819 | if (items[i].submenu) { |
| 2820 | if (items[i].submenu->activate_item_by_event(p_event, p_for_global_only)) { |
| 2821 | return true; |
| 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | return false; |
| 2826 | } |
| 2827 | |
| 2828 | void PopupMenu::_about_to_popup() { |
| 2829 | ERR_MAIN_THREAD_GUARD; |
| 2830 | emit_signal(SNAME("about_to_popup")); |
| 2831 | } |
| 2832 | |
| 2833 | void PopupMenu::_about_to_close() { |
| 2834 | ERR_MAIN_THREAD_GUARD; |
| 2835 | emit_signal(SNAME("popup_hide")); |
| 2836 | } |
| 2837 | |
| 2838 | void PopupMenu::activate_item(int p_idx) { |
| 2839 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2840 | ERR_FAIL_COND(items[p_idx].separator); |
| 2841 | int id = items[p_idx].id >= 0 ? items[p_idx].id : p_idx; |
| 2842 | |
| 2843 | //hide all parent PopupMenus |
| 2844 | Node *next = get_parent(); |
| 2845 | PopupMenu *pop = Object::cast_to<PopupMenu>(next); |
| 2846 | while (pop) { |
| 2847 | // We close all parents that are chained together, |
| 2848 | // with hide_on_item_selection enabled |
| 2849 | |
| 2850 | if (items[p_idx].checkable_type) { |
| 2851 | if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) { |
| 2852 | break; |
| 2853 | } |
| 2854 | } else if (0 < items[p_idx].max_states) { |
| 2855 | if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) { |
| 2856 | break; |
| 2857 | } |
| 2858 | } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) { |
| 2859 | break; |
| 2860 | } |
| 2861 | |
| 2862 | pop->hide(); |
| 2863 | next = next->get_parent(); |
| 2864 | pop = Object::cast_to<PopupMenu>(next); |
| 2865 | } |
| 2866 | |
| 2867 | // Hides popup by default; unless otherwise specified |
| 2868 | // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection |
| 2869 | |
| 2870 | bool need_hide = true; |
| 2871 | |
| 2872 | if (items[p_idx].checkable_type) { |
| 2873 | if (!hide_on_checkable_item_selection) { |
| 2874 | need_hide = false; |
| 2875 | } |
| 2876 | } else if (0 < items[p_idx].max_states) { |
| 2877 | if (!hide_on_multistate_item_selection) { |
| 2878 | need_hide = false; |
| 2879 | } |
| 2880 | } else if (!hide_on_item_selection) { |
| 2881 | need_hide = false; |
| 2882 | } |
| 2883 | |
| 2884 | if (need_hide) { |
| 2885 | hide(); |
| 2886 | } |
| 2887 | |
| 2888 | emit_signal(SceneStringName(id_pressed), id); |
| 2889 | emit_signal(SNAME("index_pressed"), p_idx); |
| 2890 | } |
| 2891 | |
| 2892 | void PopupMenu::remove_item(int p_idx) { |
| 2893 | ERR_FAIL_INDEX(p_idx, items.size()); |
| 2894 | |
| 2895 | if (items[p_idx].accessibility_item_element.is_valid()) { |
| 2896 | DisplayServer::get_singleton()->accessibility_free_element(items.write[p_idx].accessibility_item_element); |
| 2897 | items.write[p_idx].accessibility_item_element = RID(); |
| 2898 | } |
| 2899 | if (items[p_idx].shortcut.is_valid()) { |
| 2900 | _unref_shortcut(items[p_idx].shortcut); |
| 2901 | } |
| 2902 | |
| 2903 | items.remove_at(p_idx); |
| 2904 | |
| 2905 | if (global_menu.is_valid()) { |
| 2906 | NativeMenu::get_singleton()->remove_item(global_menu, p_idx); |
| 2907 | } |
| 2908 | |
| 2909 | control->queue_redraw(); |
| 2910 | child_controls_changed(); |
| 2911 | _menu_changed(); |
| 2912 | } |
| 2913 | |
| 2914 | void PopupMenu::add_separator(const String &p_text, int p_id) { |
| 2915 | Item sep; |
| 2916 | sep.separator = true; |
| 2917 | sep.id = p_id; |
| 2918 | sep.accessibility_item_dirty = true; |
| 2919 | if (!p_text.is_empty()) { |
| 2920 | sep.text = p_text; |
| 2921 | sep.xl_text = atr(p_text); |
| 2922 | } |
| 2923 | items.push_back(sep); |
| 2924 | |
| 2925 | if (global_menu.is_valid()) { |
| 2926 | NativeMenu::get_singleton()->add_separator(global_menu); |
| 2927 | } |
| 2928 | |
| 2929 | control->queue_redraw(); |
| 2930 | _menu_changed(); |
| 2931 | } |
| 2932 | |
| 2933 | void PopupMenu::clear(bool p_free_submenus) { |
| 2934 | for (Item &I : items) { |
| 2935 | if (I.accessibility_item_element.is_valid()) { |
| 2936 | DisplayServer::get_singleton()->accessibility_free_element(I.accessibility_item_element); |
| 2937 | I.accessibility_item_element = RID(); |
| 2938 | } |
| 2939 | if (I.shortcut.is_valid()) { |
| 2940 | _unref_shortcut(I.shortcut); |
| 2941 | } |
| 2942 | |
| 2943 | if (p_free_submenus && I.submenu) { |
| 2944 | remove_child(I.submenu); |
| 2945 | I.submenu->queue_free(); |
| 2946 | } |
| 2947 | } |
| 2948 | |
| 2949 | if (global_menu.is_valid()) { |
| 2950 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 2951 | for (int i = items.size() - 1; i >= 0; i--) { |
| 2952 | Item &item = items.write[i]; |
| 2953 | if (item.submenu) { |
| 2954 | item.submenu->unbind_global_menu(); |
| 2955 | item.submenu_bound = false; |
| 2956 | } |
| 2957 | nmenu->remove_item(global_menu, i); |
| 2958 | } |
| 2959 | } |
| 2960 | items.clear(); |
| 2961 | |
| 2962 | prev_mouse_over = -1; |
| 2963 | mouse_over = -1; |
| 2964 | queue_accessibility_update(); |
| 2965 | control->queue_redraw(); |
| 2966 | child_controls_changed(); |
| 2967 | notify_property_list_changed(); |
| 2968 | _menu_changed(); |
| 2969 | } |
| 2970 | |
| 2971 | void PopupMenu::_ref_shortcut(Ref<Shortcut> p_sc) { |
| 2972 | if (!shortcut_refcount.has(p_sc)) { |
| 2973 | shortcut_refcount[p_sc] = 1; |
| 2974 | p_sc->connect_changed(callable_mp(this, &PopupMenu::_shortcut_changed)); |
| 2975 | } else { |
| 2976 | shortcut_refcount[p_sc] += 1; |
| 2977 | } |
| 2978 | } |
| 2979 | |
| 2980 | void PopupMenu::_unref_shortcut(Ref<Shortcut> p_sc) { |
| 2981 | ERR_FAIL_COND(!shortcut_refcount.has(p_sc)); |
| 2982 | shortcut_refcount[p_sc]--; |
| 2983 | if (shortcut_refcount[p_sc] == 0) { |
| 2984 | p_sc->disconnect_changed(callable_mp(this, &PopupMenu::_shortcut_changed)); |
| 2985 | shortcut_refcount.erase(p_sc); |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | void PopupMenu::_shortcut_changed() { |
| 2990 | for (int i = 0; i < items.size(); i++) { |
| 2991 | Item &item = items.write[i]; |
| 2992 | item.dirty = true; |
| 2993 | if (global_menu.is_valid() && !item.separator) { |
| 2994 | NativeMenu *nmenu = NativeMenu::get_singleton(); |
| 2995 | nmenu->set_item_accelerator(global_menu, i, Key::NONE); |
| 2996 | if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) { |
| 2997 | Array events = item.shortcut->get_events(); |
| 2998 | for (const Variant &v : events) { |
| 2999 | Ref<InputEventKey> ie = v; |
| 3000 | if (ie.is_valid() && _set_item_accelerator(i, ie)) { |
| 3001 | break; |
| 3002 | } |
| 3003 | } |
| 3004 | if (item.shortcut_is_global) { |
| 3005 | nmenu->set_item_key_callback(global_menu, i, callable_mp(this, &PopupMenu::activate_item)); |
| 3006 | } else { |
| 3007 | nmenu->set_item_key_callback(global_menu, i, Callable()); |
| 3008 | } |
| 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | control->queue_redraw(); |
| 3013 | } |
| 3014 | |
| 3015 | // Hide on item selection determines whether or not the popup will close after item selection |
| 3016 | void PopupMenu::set_hide_on_item_selection(bool p_enabled) { |
| 3017 | hide_on_item_selection = p_enabled; |
| 3018 | } |
| 3019 | |
| 3020 | bool PopupMenu::is_hide_on_item_selection() const { |
| 3021 | return hide_on_item_selection; |
| 3022 | } |
| 3023 | |
| 3024 | void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) { |
| 3025 | hide_on_checkable_item_selection = p_enabled; |
| 3026 | } |
| 3027 | |
| 3028 | bool PopupMenu::is_hide_on_checkable_item_selection() const { |
| 3029 | return hide_on_checkable_item_selection; |
| 3030 | } |
| 3031 | |
| 3032 | void PopupMenu::set_hide_on_multistate_item_selection(bool p_enabled) { |
| 3033 | hide_on_multistate_item_selection = p_enabled; |
| 3034 | } |
| 3035 | |
| 3036 | bool PopupMenu::is_hide_on_multistate_item_selection() const { |
| 3037 | return hide_on_multistate_item_selection; |
| 3038 | } |
| 3039 | |
| 3040 | void PopupMenu::set_submenu_popup_delay(float p_time) { |
| 3041 | if (p_time <= 0) { |
| 3042 | p_time = 0.01; |
| 3043 | } |
| 3044 | submenu_timer_popup_delay = p_time; |
| 3045 | submenu_timer->set_wait_time(p_time); |
| 3046 | } |
| 3047 | |
| 3048 | float PopupMenu::get_submenu_popup_delay() const { |
| 3049 | return submenu_timer->get_wait_time(); |
| 3050 | } |
| 3051 | |
| 3052 | void PopupMenu::set_allow_search(bool p_allow) { |
| 3053 | allow_search = p_allow; |
| 3054 | } |
| 3055 | |
| 3056 | bool PopupMenu::get_allow_search() const { |
| 3057 | return allow_search; |
| 3058 | } |
| 3059 | |
| 3060 | String PopupMenu::get_tooltip(const Point2 &p_pos) const { |
| 3061 | int over = _get_mouse_over(p_pos); |
| 3062 | if (over < 0 || over >= items.size()) { |
| 3063 | return ""; |
| 3064 | } |
| 3065 | return items[over].tooltip; |
| 3066 | } |
| 3067 | |
| 3068 | #ifdef TOOLS_ENABLED |
| 3069 | PackedStringArray PopupMenu::get_configuration_warnings() const { |
| 3070 | PackedStringArray warnings = Popup::get_configuration_warnings(); |
| 3071 | |
| 3072 | if (!DisplayServer::get_singleton()->is_window_transparency_available() && !GLOBAL_GET_CACHED(bool, "display/window/subwindows/embed_subwindows")) { |
| 3073 | Ref<StyleBoxFlat> sb = theme_cache.panel_style; |
| 3074 | if (sb.is_valid() && (sb->get_shadow_size() > 0 || sb->get_corner_radius(CORNER_TOP_LEFT) > 0 || sb->get_corner_radius(CORNER_TOP_RIGHT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_LEFT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_RIGHT) > 0)) { |
| 3075 | warnings.push_back(RTR("The current theme style has shadows and/or rounded corners for popups, but those won't display correctly if \"display/window/per_pixel_transparency/allowed\" isn't enabled in the Project Settings, nor if it isn't supported.")); |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | return warnings; |
| 3080 | } |
| 3081 | #endif |
| 3082 | |
| 3083 | void PopupMenu::add_autohide_area(const Rect2 &p_area) { |
| 3084 | autohide_areas.push_back(p_area); |
| 3085 | } |
| 3086 | |
| 3087 | void PopupMenu::clear_autohide_areas() { |
| 3088 | autohide_areas.clear(); |
| 3089 | } |
| 3090 | |
| 3091 | bool PopupMenu::_set(const StringName &p_name, const Variant &p_value) { |
| 3092 | if (property_helper.property_set_value(p_name, p_value)) { |
| 3093 | return true; |
| 3094 | } |
| 3095 | |
| 3096 | #ifndef DISABLE_DEPRECATED |
| 3097 | // Compatibility. |
| 3098 | if (p_name == "items") { |
| 3099 | Array arr = p_value; |
| 3100 | ERR_FAIL_COND_V(arr.size() % 10, false); |
| 3101 | clear(); |
| 3102 | |
| 3103 | for (int i = 0; i < arr.size(); i += 10) { |
| 3104 | String text = arr[i + 0]; |
| 3105 | Ref<Texture2D> icon = arr[i + 1]; |
| 3106 | // For compatibility, use false/true for no/checkbox and integers for other values |
| 3107 | bool checkable = arr[i + 2]; |
| 3108 | bool radio_checkable = (int)arr[i + 2] == Item::CHECKABLE_TYPE_RADIO_BUTTON; |
| 3109 | bool checked = arr[i + 3]; |
| 3110 | bool disabled = arr[i + 4]; |
| 3111 | |
| 3112 | int id = arr[i + 5]; |
| 3113 | int accel = arr[i + 6]; |
| 3114 | Variant meta = arr[i + 7]; |
| 3115 | String subm = arr[i + 8]; |
| 3116 | bool sep = arr[i + 9]; |
| 3117 | |
| 3118 | int idx = get_item_count(); |
| 3119 | add_item(text, id); |
| 3120 | set_item_icon(idx, icon); |
| 3121 | if (checkable) { |
| 3122 | if (radio_checkable) { |
| 3123 | set_item_as_radio_checkable(idx, true); |
| 3124 | } else { |
| 3125 | set_item_as_checkable(idx, true); |
| 3126 | } |
| 3127 | } |
| 3128 | set_item_checked(idx, checked); |
| 3129 | set_item_disabled(idx, disabled); |
| 3130 | set_item_id(idx, id); |
| 3131 | set_item_metadata(idx, meta); |
| 3132 | set_item_as_separator(idx, sep); |
| 3133 | set_item_accelerator(idx, (Key)accel); |
| 3134 | set_item_submenu(idx, subm); |
| 3135 | } |
| 3136 | } |
| 3137 | #endif |
| 3138 | return false; |
| 3139 | } |
| 3140 | |
| 3141 | void PopupMenu::_bind_methods() { |
| 3142 | ClassDB::bind_method(D_METHOD("activate_item_by_event", "event", "for_global_only"), &PopupMenu::activate_item_by_event, DEFVAL(false)); |
| 3143 | |
| 3144 | ClassDB::bind_method(D_METHOD("set_prefer_native_menu", "enabled"), &PopupMenu::set_prefer_native_menu); |
| 3145 | ClassDB::bind_method(D_METHOD("is_prefer_native_menu"), &PopupMenu::is_prefer_native_menu); |
| 3146 | ClassDB::bind_method(D_METHOD("is_native_menu"), &PopupMenu::is_native_menu); |
| 3147 | |
| 3148 | ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0)); |
| 3149 | ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0)); |
| 3150 | ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0)); |
| 3151 | ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0)); |
| 3152 | ClassDB::bind_method(D_METHOD("add_radio_check_item", "label", "id", "accel"), &PopupMenu::add_radio_check_item, DEFVAL(-1), DEFVAL(0)); |
| 3153 | ClassDB::bind_method(D_METHOD("add_icon_radio_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_radio_check_item, DEFVAL(-1), DEFVAL(0)); |
| 3154 | |
| 3155 | ClassDB::bind_method(D_METHOD("add_multistate_item", "label", "max_states", "default_state", "id", "accel"), &PopupMenu::add_multistate_item, DEFVAL(0), DEFVAL(-1), DEFVAL(0)); |
| 3156 | |
| 3157 | ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false)); |
| 3158 | ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false)); |
| 3159 | ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false)); |
| 3160 | ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false)); |
| 3161 | ClassDB::bind_method(D_METHOD("add_radio_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_radio_check_shortcut, DEFVAL(-1), DEFVAL(false)); |
| 3162 | ClassDB::bind_method(D_METHOD("add_icon_radio_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_radio_check_shortcut, DEFVAL(-1), DEFVAL(false)); |
| 3163 | |
| 3164 | ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1)); |
| 3165 | ClassDB::bind_method(D_METHOD("add_submenu_node_item", "label", "submenu", "id"), &PopupMenu::add_submenu_node_item, DEFVAL(-1)); |
| 3166 | |
| 3167 | ClassDB::bind_method(D_METHOD("set_item_text", "index", "text"), &PopupMenu::set_item_text); |
| 3168 | ClassDB::bind_method(D_METHOD("set_item_text_direction", "index", "direction"), &PopupMenu::set_item_text_direction); |
| 3169 | ClassDB::bind_method(D_METHOD("set_item_language", "index", "language"), &PopupMenu::set_item_language); |
| 3170 | ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "index", "mode"), &PopupMenu::set_item_auto_translate_mode); |
| 3171 | ClassDB::bind_method(D_METHOD("set_item_icon", "index", "icon"), &PopupMenu::set_item_icon); |
| 3172 | ClassDB::bind_method(D_METHOD("set_item_icon_max_width", "index", "width"), &PopupMenu::set_item_icon_max_width); |
| 3173 | ClassDB::bind_method(D_METHOD("set_item_icon_modulate", "index", "modulate"), &PopupMenu::set_item_icon_modulate); |
| 3174 | ClassDB::bind_method(D_METHOD("set_item_checked", "index", "checked"), &PopupMenu::set_item_checked); |
| 3175 | ClassDB::bind_method(D_METHOD("set_item_id", "index", "id"), &PopupMenu::set_item_id); |
| 3176 | ClassDB::bind_method(D_METHOD("set_item_accelerator", "index", "accel"), &PopupMenu::set_item_accelerator); |
| 3177 | ClassDB::bind_method(D_METHOD("set_item_metadata", "index", "metadata"), &PopupMenu::set_item_metadata); |
| 3178 | ClassDB::bind_method(D_METHOD("set_item_disabled", "index", "disabled"), &PopupMenu::set_item_disabled); |
| 3179 | ClassDB::bind_method(D_METHOD("set_item_submenu", "index", "submenu"), &PopupMenu::set_item_submenu); |
| 3180 | ClassDB::bind_method(D_METHOD("set_item_submenu_node", "index", "submenu"), &PopupMenu::set_item_submenu_node); |
| 3181 | ClassDB::bind_method(D_METHOD("set_item_as_separator", "index", "enable"), &PopupMenu::set_item_as_separator); |
| 3182 | ClassDB::bind_method(D_METHOD("set_item_as_checkable", "index", "enable"), &PopupMenu::set_item_as_checkable); |
| 3183 | ClassDB::bind_method(D_METHOD("set_item_as_radio_checkable", "index", "enable"), &PopupMenu::set_item_as_radio_checkable); |
| 3184 | ClassDB::bind_method(D_METHOD("set_item_tooltip", "index", "tooltip"), &PopupMenu::set_item_tooltip); |
| 3185 | ClassDB::bind_method(D_METHOD("set_item_shortcut", "index", "shortcut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false)); |
| 3186 | ClassDB::bind_method(D_METHOD("set_item_indent", "index", "indent"), &PopupMenu::set_item_indent); |
| 3187 | ClassDB::bind_method(D_METHOD("set_item_multistate", "index", "state"), &PopupMenu::set_item_multistate); |
| 3188 | ClassDB::bind_method(D_METHOD("set_item_multistate_max", "index", "max_states"), &PopupMenu::set_item_max_states); |
| 3189 | ClassDB::bind_method(D_METHOD("set_item_shortcut_disabled", "index", "disabled"), &PopupMenu::set_item_shortcut_disabled); |
| 3190 | |
| 3191 | ClassDB::bind_method(D_METHOD("toggle_item_checked", "index"), &PopupMenu::toggle_item_checked); |
| 3192 | ClassDB::bind_method(D_METHOD("toggle_item_multistate", "index"), &PopupMenu::toggle_item_multistate); |
| 3193 | |
| 3194 | ClassDB::bind_method(D_METHOD("get_item_text", "index"), &PopupMenu::get_item_text); |
| 3195 | ClassDB::bind_method(D_METHOD("get_item_text_direction", "index"), &PopupMenu::get_item_text_direction); |
| 3196 | ClassDB::bind_method(D_METHOD("get_item_language", "index"), &PopupMenu::get_item_language); |
| 3197 | ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "index"), &PopupMenu::get_item_auto_translate_mode); |
| 3198 | ClassDB::bind_method(D_METHOD("get_item_icon", "index"), &PopupMenu::get_item_icon); |
| 3199 | ClassDB::bind_method(D_METHOD("get_item_icon_max_width", "index"), &PopupMenu::get_item_icon_max_width); |
| 3200 | ClassDB::bind_method(D_METHOD("get_item_icon_modulate", "index"), &PopupMenu::get_item_icon_modulate); |
| 3201 | ClassDB::bind_method(D_METHOD("is_item_checked", "index"), &PopupMenu::is_item_checked); |
| 3202 | ClassDB::bind_method(D_METHOD("get_item_id", "index"), &PopupMenu::get_item_id); |
| 3203 | ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index); |
| 3204 | ClassDB::bind_method(D_METHOD("get_item_accelerator", "index"), &PopupMenu::get_item_accelerator); |
| 3205 | ClassDB::bind_method(D_METHOD("get_item_metadata", "index"), &PopupMenu::get_item_metadata); |
| 3206 | ClassDB::bind_method(D_METHOD("is_item_disabled", "index"), &PopupMenu::is_item_disabled); |
| 3207 | ClassDB::bind_method(D_METHOD("get_item_submenu", "index"), &PopupMenu::get_item_submenu); |
| 3208 | ClassDB::bind_method(D_METHOD("get_item_submenu_node", "index"), &PopupMenu::get_item_submenu_node); |
| 3209 | ClassDB::bind_method(D_METHOD("is_item_separator", "index"), &PopupMenu::is_item_separator); |
| 3210 | ClassDB::bind_method(D_METHOD("is_item_checkable", "index"), &PopupMenu::is_item_checkable); |
| 3211 | ClassDB::bind_method(D_METHOD("is_item_radio_checkable", "index"), &PopupMenu::is_item_radio_checkable); |
| 3212 | ClassDB::bind_method(D_METHOD("is_item_shortcut_disabled", "index"), &PopupMenu::is_item_shortcut_disabled); |
| 3213 | ClassDB::bind_method(D_METHOD("get_item_tooltip", "index"), &PopupMenu::get_item_tooltip); |
| 3214 | ClassDB::bind_method(D_METHOD("get_item_shortcut", "index"), &PopupMenu::get_item_shortcut); |
| 3215 | ClassDB::bind_method(D_METHOD("get_item_indent", "index"), &PopupMenu::get_item_indent); |
| 3216 | |
| 3217 | ClassDB::bind_method(D_METHOD("get_item_multistate_max", "index"), &PopupMenu::get_item_max_states); |
| 3218 | ClassDB::bind_method(D_METHOD("get_item_multistate", "index"), &PopupMenu::get_item_state); |
| 3219 | |
| 3220 | ClassDB::bind_method(D_METHOD("set_focused_item", "index"), &PopupMenu::set_focused_item); |
| 3221 | ClassDB::bind_method(D_METHOD("get_focused_item"), &PopupMenu::get_focused_item); |
| 3222 | ClassDB::bind_method(D_METHOD("set_item_count", "count"), &PopupMenu::set_item_count); |
| 3223 | ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count); |
| 3224 | |
| 3225 | ClassDB::bind_method(D_METHOD("scroll_to_item", "index"), &PopupMenu::scroll_to_item); |
| 3226 | |
| 3227 | ClassDB::bind_method(D_METHOD("remove_item", "index"), &PopupMenu::remove_item); |
| 3228 | |
| 3229 | ClassDB::bind_method(D_METHOD("add_separator", "label", "id"), &PopupMenu::add_separator, DEFVAL(String()), DEFVAL(-1)); |
| 3230 | ClassDB::bind_method(D_METHOD("clear", "free_submenus"), &PopupMenu::clear, DEFVAL(false)); |
| 3231 | |
| 3232 | ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection); |
| 3233 | ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection); |
| 3234 | |
| 3235 | ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection); |
| 3236 | ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection); |
| 3237 | |
| 3238 | ClassDB::bind_method(D_METHOD("set_hide_on_state_item_selection", "enable"), &PopupMenu::set_hide_on_multistate_item_selection); |
| 3239 | ClassDB::bind_method(D_METHOD("is_hide_on_state_item_selection"), &PopupMenu::is_hide_on_multistate_item_selection); |
| 3240 | |
| 3241 | ClassDB::bind_method(D_METHOD("set_submenu_popup_delay", "seconds"), &PopupMenu::set_submenu_popup_delay); |
| 3242 | ClassDB::bind_method(D_METHOD("get_submenu_popup_delay"), &PopupMenu::get_submenu_popup_delay); |
| 3243 | |
| 3244 | ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &PopupMenu::set_allow_search); |
| 3245 | ClassDB::bind_method(D_METHOD("get_allow_search"), &PopupMenu::get_allow_search); |
| 3246 | |
| 3247 | ClassDB::bind_method(D_METHOD("is_system_menu"), &PopupMenu::is_system_menu); |
| 3248 | ClassDB::bind_method(D_METHOD("set_system_menu", "system_menu_id"), &PopupMenu::set_system_menu); |
| 3249 | ClassDB::bind_method(D_METHOD("get_system_menu"), &PopupMenu::get_system_menu); |
| 3250 | |
| 3251 | ClassDB::bind_method(D_METHOD("set_shrink_height", "shrink"), &PopupMenu::set_shrink_height); |
| 3252 | ClassDB::bind_method(D_METHOD("get_shrink_height"), &PopupMenu::get_shrink_height); |
| 3253 | |
| 3254 | ClassDB::bind_method(D_METHOD("set_shrink_width", "shrink"), &PopupMenu::set_shrink_width); |
| 3255 | ClassDB::bind_method(D_METHOD("get_shrink_width"), &PopupMenu::get_shrink_width); |
| 3256 | |
| 3257 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection"); |
| 3258 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection"); |
| 3259 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection"); |
| 3260 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "submenu_popup_delay", PROPERTY_HINT_NONE, "suffix:s"), "set_submenu_popup_delay", "get_submenu_popup_delay"); |
| 3261 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search"); |
| 3262 | ADD_PROPERTY(PropertyInfo(Variant::INT, "system_menu_id", PROPERTY_HINT_ENUM, "None:0,Application Menu:2,Window Menu:3,Help Menu:4,Dock:5"), "set_system_menu", "get_system_menu"); |
| 3263 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefer_native_menu"), "set_prefer_native_menu", "is_prefer_native_menu"); |
| 3264 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shrink_height"), "set_shrink_height", "get_shrink_height"); |
| 3265 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shrink_width"), "set_shrink_width", "get_shrink_width"); |
| 3266 | |
| 3267 | ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "item_"); |
| 3268 | |
| 3269 | ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "id"))); |
| 3270 | ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "id"))); |
| 3271 | ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index"))); |
| 3272 | ADD_SIGNAL(MethodInfo("menu_changed")); |
| 3273 | |
| 3274 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, panel_style, "panel"); |
| 3275 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, hover_style, "hover"); |
| 3276 | |
| 3277 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, separator_style, "separator"); |
| 3278 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_left); |
| 3279 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_right); |
| 3280 | |
| 3281 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, v_separation); |
| 3282 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, h_separation); |
| 3283 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, indent); |
| 3284 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_start_padding); |
| 3285 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_end_padding); |
| 3286 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, icon_max_width); |
| 3287 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, gutter_compact); |
| 3288 | |
| 3289 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked); |
| 3290 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked_disabled); |
| 3291 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked); |
| 3292 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked_disabled); |
| 3293 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked); |
| 3294 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked_disabled); |
| 3295 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked); |
| 3296 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked_disabled); |
| 3297 | |
| 3298 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu); |
| 3299 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu_mirrored); |
| 3300 | |
| 3301 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font); |
| 3302 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_size); |
| 3303 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font_separator); |
| 3304 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_separator_size); |
| 3305 | |
| 3306 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_color); |
| 3307 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_hover_color); |
| 3308 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_disabled_color); |
| 3309 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_accelerator_color); |
| 3310 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_outline_size, "outline_size"); |
| 3311 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_outline_color); |
| 3312 | |
| 3313 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_color); |
| 3314 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_separator_outline_size, "separator_outline_size"); |
| 3315 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_outline_color); |
| 3316 | |
| 3317 | Item defaults(true); |
| 3318 | |
| 3319 | base_property_helper.set_prefix("item_"); |
| 3320 | base_property_helper.set_array_length_getter(&PopupMenu::get_item_count); |
| 3321 | base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &PopupMenu::set_item_text, &PopupMenu::get_item_text); |
| 3322 | base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &PopupMenu::set_item_icon, &PopupMenu::get_item_icon); |
| 3323 | base_property_helper.register_property(PropertyInfo(Variant::INT, "checkable", PROPERTY_HINT_ENUM, "No,As checkbox,As radio button"), defaults.checkable_type, &PopupMenu::_set_item_checkable_type, &PopupMenu::_get_item_checkable_type); |
| 3324 | base_property_helper.register_property(PropertyInfo(Variant::BOOL, "checked"), defaults.checked, &PopupMenu::set_item_checked, &PopupMenu::is_item_checked); |
| 3325 | 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, &PopupMenu::set_item_id, &PopupMenu::get_item_id); |
| 3326 | base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &PopupMenu::set_item_disabled, &PopupMenu::is_item_disabled); |
| 3327 | base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator, &PopupMenu::set_item_as_separator, &PopupMenu::is_item_separator); |
| 3328 | PropertyListHelper::register_base_helper(&base_property_helper); |
| 3329 | } |
| 3330 | |
| 3331 | void PopupMenu::_native_popup(const Rect2i &p_rect) { |
| 3332 | Point2i popup_pos = p_rect.position; |
| 3333 | if (is_embedded()) { |
| 3334 | popup_pos = get_embedder()->get_screen_transform().xform(popup_pos); // Note: for embedded windows "screen transform" is transform relative to embedder not the actual screen. |
| 3335 | DisplayServer::WindowID wid = get_window_id(); |
| 3336 | if (wid == DisplayServer::INVALID_WINDOW_ID) { |
| 3337 | wid = DisplayServer::MAIN_WINDOW_ID; |
| 3338 | } |
| 3339 | popup_pos += DisplayServer::get_singleton()->window_get_position(wid); |
| 3340 | } |
| 3341 | float win_scale = get_parent_visible_window()->get_content_scale_factor(); |
| 3342 | NativeMenu::get_singleton()->set_minimum_width(global_menu, p_rect.size.x * win_scale); |
| 3343 | NativeMenu::get_singleton()->popup(global_menu, popup_pos); |
| 3344 | } |
| 3345 | |
| 3346 | String PopupMenu::_atr(int p_idx, const String &p_text) const { |
| 3347 | ERR_FAIL_INDEX_V(p_idx, items.size(), atr(p_text)); |
| 3348 | switch (items[p_idx].auto_translate_mode) { |
| 3349 | case AUTO_TRANSLATE_MODE_INHERIT: { |
| 3350 | return atr(p_text); |
| 3351 | } break; |
| 3352 | case AUTO_TRANSLATE_MODE_ALWAYS: { |
| 3353 | return tr(p_text); |
| 3354 | } break; |
| 3355 | case AUTO_TRANSLATE_MODE_DISABLED: { |
| 3356 | return p_text; |
| 3357 | } break; |
| 3358 | } |
| 3359 | |
| 3360 | ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(items[p_idx].auto_translate_mode)); |
| 3361 | } |
| 3362 | |
| 3363 | void PopupMenu::_popup_base(const Rect2i &p_bounds) { |
| 3364 | bool native = global_menu.is_valid(); |
| 3365 | #ifdef TOOLS_ENABLED |
| 3366 | if (is_part_of_edited_scene()) { |
| 3367 | native = false; |
| 3368 | } |
| 3369 | #endif |
| 3370 | |
| 3371 | if (native) { |
| 3372 | _native_popup(p_bounds != Rect2i() ? p_bounds : Rect2i(get_position(), Size2i())); |
| 3373 | } else { |
| 3374 | if (is_inside_tree()) { |
| 3375 | set_flag(FLAG_POPUP, true); |
| 3376 | set_flag(FLAG_NO_FOCUS, !is_embedded()); |
| 3377 | } |
| 3378 | |
| 3379 | moved = Vector2(); |
| 3380 | popup_time_msec = OS::get_singleton()->get_ticks_msec(); |
| 3381 | |
| 3382 | Popup::_popup_base(p_bounds); |
| 3383 | } |
| 3384 | } |
| 3385 | void PopupMenu::set_shrink_height(bool p_shrink) { |
| 3386 | shrink_height = p_shrink; |
| 3387 | } |
| 3388 | |
| 3389 | bool PopupMenu::get_shrink_height() const { |
| 3390 | return shrink_height; |
| 3391 | } |
| 3392 | |
| 3393 | void PopupMenu::set_shrink_width(bool p_shrink) { |
| 3394 | shrink_width = p_shrink; |
| 3395 | } |
| 3396 | |
| 3397 | bool PopupMenu::get_shrink_width() const { |
| 3398 | return shrink_width; |
| 3399 | } |
| 3400 | |
| 3401 | void PopupMenu::_pre_popup() { |
| 3402 | real_t popup_scale = 1.0; |
| 3403 | bool scale_with_parent = true; |
| 3404 | Node *parent_node = get_parent(); |
| 3405 | |
| 3406 | // Use parent GraphEdit content scale to avoid too tiny or too big menus when using GraphEdit zoom, applied only if menu is a child of GraphElement. |
| 3407 | { |
| 3408 | Node *p = parent_node; |
| 3409 | while (p) { |
| 3410 | GraphElement *gelement = Object::cast_to<GraphElement>(p); |
| 3411 | if (gelement) { |
| 3412 | scale_with_parent = gelement->is_scaling_menus(); |
| 3413 | if (!scale_with_parent) { |
| 3414 | parent_node = nullptr; |
| 3415 | p = gelement->get_parent(); |
| 3416 | while (p) { |
| 3417 | GraphEdit *gedit = Object::cast_to<GraphEdit>(p); |
| 3418 | if (gedit) { |
| 3419 | parent_node = gedit; |
| 3420 | break; |
| 3421 | } |
| 3422 | p = p->get_parent(); |
| 3423 | } |
| 3424 | } |
| 3425 | break; |
| 3426 | } |
| 3427 | p = p->get_parent(); |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | Viewport *vp = get_parent_viewport(); |
| 3432 | if (vp && parent_node) { |
| 3433 | Size2 scale = is_embedded() ? vp->get_popup_base_transform().get_scale() : vp->get_popup_base_transform_native().get_scale(); |
| 3434 | CanvasItem *c = Object::cast_to<CanvasItem>(parent_node); |
| 3435 | if (c) { |
| 3436 | scale *= c->get_global_transform_with_canvas().get_scale(); |
| 3437 | } |
| 3438 | popup_scale = MIN(scale.x, scale.y); |
| 3439 | } |
| 3440 | |
| 3441 | set_content_scale_factor(popup_scale); |
| 3442 | if (is_wrapping_controls()) { |
| 3443 | Size2 minsize = get_contents_minimum_size() * popup_scale; |
| 3444 | Size2 maxsize = get_max_size(); |
| 3445 | if (maxsize.height > 0) { |
| 3446 | minsize.height = MIN(minsize.height, maxsize.height); |
| 3447 | } |
| 3448 | if (maxsize.width > 0) { |
| 3449 | minsize.width = MIN(minsize.width, maxsize.width); |
| 3450 | } |
| 3451 | minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing. |
| 3452 | set_min_size(minsize); // `height` is truncated here by the cast to Size2i for Window.min_size. |
| 3453 | Size2i sz = get_size(); // Shrinkwraps to min size. |
| 3454 | set_size(Vector2i(shrink_width ? 0 : sz.x, shrink_height ? 0 : sz.y)); |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | void PopupMenu::set_visible(bool p_visible) { |
| 3459 | bool native = global_menu.is_valid(); |
| 3460 | #ifdef TOOLS_ENABLED |
| 3461 | if (is_part_of_edited_scene()) { |
| 3462 | native = false; |
| 3463 | } |
| 3464 | #endif |
| 3465 | |
| 3466 | if (native) { |
| 3467 | if (p_visible) { |
| 3468 | _native_popup(Rect2i(get_position(), get_size())); |
| 3469 | } |
| 3470 | } else { |
| 3471 | if (p_visible && is_inside_tree()) { |
| 3472 | set_flag(FLAG_POPUP, true); |
| 3473 | set_flag(FLAG_NO_FOCUS, !is_embedded()); |
| 3474 | } |
| 3475 | |
| 3476 | Popup::set_visible(p_visible); |
| 3477 | } |
| 3478 | } |
| 3479 | |
| 3480 | PopupMenu::PopupMenu() { |
| 3481 | set_flag(FLAG_TRANSPARENT, true); |
| 3482 | // The panel used to draw the panel style. |
| 3483 | panel = memnew(PanelContainer); |
| 3484 | panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 3485 | add_child(panel, false, INTERNAL_MODE_FRONT); |
| 3486 | |
| 3487 | // Scroll Container |
| 3488 | scroll_container = memnew(ScrollContainer); |
| 3489 | scroll_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 3490 | panel->add_child(scroll_container, false, INTERNAL_MODE_FRONT); |
| 3491 | |
| 3492 | // The control which will display the items |
| 3493 | control = memnew(Control); |
| 3494 | control->set_clip_contents(false); |
| 3495 | control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 3496 | control->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 3497 | control->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 3498 | scroll_container->add_child(control, false, INTERNAL_MODE_FRONT); |
| 3499 | control->connect(SceneStringName(draw), callable_mp(this, &PopupMenu::_draw_items)); |
| 3500 | |
| 3501 | submenu_timer = memnew(Timer); |
| 3502 | submenu_timer->set_wait_time(submenu_timer_popup_delay); // Default is 0.2. |
| 3503 | submenu_timer->set_one_shot(true); |
| 3504 | submenu_timer->connect("timeout", callable_mp(this, &PopupMenu::_submenu_timeout)); |
| 3505 | add_child(submenu_timer, false, INTERNAL_MODE_FRONT); |
| 3506 | |
| 3507 | close_suspended_timer = memnew(Timer); |
| 3508 | close_suspended_timer->set_wait_time(CLOSE_SUSPENDED_TIMER_DELAY); |
| 3509 | close_suspended_timer->set_one_shot(true); |
| 3510 | close_suspended_timer->connect("timeout", callable_mp(this, &PopupMenu::_close_suspended_timeout)); |
| 3511 | add_child(close_suspended_timer, false, INTERNAL_MODE_FRONT); |
| 3512 | |
| 3513 | property_helper.setup_for_instance(base_property_helper, this); |
| 3514 | |
| 3515 | #ifdef TOOLS_ENABLED |
| 3516 | ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings)); |
| 3517 | #endif |
| 3518 | } |
| 3519 | |
| 3520 | PopupMenu::~PopupMenu() { |
| 3521 | unbind_global_menu(); |
| 3522 | } |