| 1 | /**************************************************************************/ |
| 2 | /* base_button.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "base_button.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "scene/gui/label.h" |
| 35 | #include "scene/main/timer.h" |
| 36 | #include "scene/main/window.h" |
| 37 | |
| 38 | void BaseButton::_unpress_group() { |
| 39 | if (button_group.is_null()) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (toggle_mode && !button_group->is_allow_unpress()) { |
| 44 | status.pressed = true; |
| 45 | queue_accessibility_update(); |
| 46 | } |
| 47 | |
| 48 | for (BaseButton *E : button_group->buttons) { |
| 49 | if (E == this) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | E->set_pressed(false); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void BaseButton::gui_input(const Ref<InputEvent> &p_event) { |
| 58 | ERR_FAIL_COND(p_event.is_null()); |
| 59 | |
| 60 | if (status.disabled) { // no interaction with disabled button |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | Ref<InputEventMouseButton> mouse_button = p_event; |
| 65 | bool ui_accept = p_event->is_action("ui_accept", true) && !p_event->is_echo(); |
| 66 | |
| 67 | bool button_masked = mouse_button.is_valid() && button_mask.has_flag(mouse_button_to_mask(mouse_button->get_button_index())); |
| 68 | if (button_masked || ui_accept) { |
| 69 | was_mouse_pressed = button_masked; |
| 70 | on_action_event(p_event); |
| 71 | was_mouse_pressed = false; |
| 72 | |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | Ref<InputEventMouseMotion> mouse_motion = p_event; |
| 77 | if (mouse_motion.is_valid()) { |
| 78 | if (status.press_attempt) { |
| 79 | bool last_press_inside = status.pressing_inside; |
| 80 | status.pressing_inside = has_point(mouse_motion->get_position()); |
| 81 | if (last_press_inside != status.pressing_inside) { |
| 82 | queue_redraw(); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void BaseButton::_accessibility_action_click(const Variant &p_data) { |
| 89 | if (toggle_mode) { |
| 90 | status.pressed = !status.pressed; |
| 91 | |
| 92 | if (status.pressed) { |
| 93 | _unpress_group(); |
| 94 | if (button_group.is_valid()) { |
| 95 | button_group->emit_signal(SceneStringName(pressed), this); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | _toggled(status.pressed); |
| 100 | _pressed(); |
| 101 | } else { |
| 102 | _pressed(); |
| 103 | } |
| 104 | queue_accessibility_update(); |
| 105 | queue_redraw(); |
| 106 | } |
| 107 | |
| 108 | void BaseButton::_notification(int p_what) { |
| 109 | switch (p_what) { |
| 110 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 111 | RID ae = get_accessibility_element(); |
| 112 | ERR_FAIL_COND(ae.is_null()); |
| 113 | |
| 114 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON); |
| 115 | |
| 116 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &BaseButton::_accessibility_action_click)); |
| 117 | DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_DISABLED, status.disabled); |
| 118 | if (toggle_mode) { |
| 119 | DisplayServer::get_singleton()->accessibility_update_set_checked(ae, status.pressed); |
| 120 | } |
| 121 | if (button_group.is_valid()) { |
| 122 | for (const BaseButton *btn : button_group->buttons) { |
| 123 | if (btn->is_part_of_edited_scene()) { |
| 124 | continue; |
| 125 | } |
| 126 | DisplayServer::get_singleton()->accessibility_update_add_related_radio_group(ae, btn->get_accessibility_element()); |
| 127 | } |
| 128 | } |
| 129 | if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) { |
| 130 | String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")"; |
| 131 | String tooltip = get_tooltip_text(); |
| 132 | if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) { |
| 133 | text += "\n" + atr(tooltip); |
| 134 | } |
| 135 | DisplayServer::get_singleton()->accessibility_update_set_tooltip(ae, text); |
| 136 | } |
| 137 | } break; |
| 138 | |
| 139 | case NOTIFICATION_MOUSE_ENTER: { |
| 140 | status.hovering = true; |
| 141 | queue_accessibility_update(); |
| 142 | queue_redraw(); |
| 143 | } break; |
| 144 | |
| 145 | case NOTIFICATION_MOUSE_EXIT: { |
| 146 | status.hovering = false; |
| 147 | queue_accessibility_update(); |
| 148 | queue_redraw(); |
| 149 | } break; |
| 150 | |
| 151 | case NOTIFICATION_DRAG_BEGIN: |
| 152 | case NOTIFICATION_SCROLL_BEGIN: { |
| 153 | if (status.press_attempt) { |
| 154 | status.press_attempt = false; |
| 155 | queue_redraw(); |
| 156 | } |
| 157 | } break; |
| 158 | |
| 159 | case NOTIFICATION_FOCUS_ENTER: { |
| 160 | queue_redraw(); |
| 161 | } break; |
| 162 | |
| 163 | case NOTIFICATION_FOCUS_EXIT: { |
| 164 | if (status.press_attempt) { |
| 165 | status.press_attempt = false; |
| 166 | queue_redraw(); |
| 167 | } else if (status.hovering) { |
| 168 | queue_redraw(); |
| 169 | } |
| 170 | |
| 171 | if (status.pressed_down_with_focus) { |
| 172 | status.pressed_down_with_focus = false; |
| 173 | emit_signal(SNAME("button_up")); |
| 174 | } |
| 175 | } break; |
| 176 | |
| 177 | case NOTIFICATION_VISIBILITY_CHANGED: |
| 178 | case NOTIFICATION_EXIT_TREE: { |
| 179 | if (p_what == NOTIFICATION_VISIBILITY_CHANGED && is_visible_in_tree()) { |
| 180 | break; |
| 181 | } |
| 182 | if (!toggle_mode) { |
| 183 | status.pressed = false; |
| 184 | } |
| 185 | status.hovering = false; |
| 186 | status.press_attempt = false; |
| 187 | status.pressing_inside = false; |
| 188 | } break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void BaseButton::_pressed() { |
| 193 | GDVIRTUAL_CALL(_pressed); |
| 194 | pressed(); |
| 195 | emit_signal(SceneStringName(pressed)); |
| 196 | } |
| 197 | |
| 198 | void BaseButton::_toggled(bool p_pressed) { |
| 199 | GDVIRTUAL_CALL(_toggled, p_pressed); |
| 200 | toggled(p_pressed); |
| 201 | emit_signal(SceneStringName(toggled), p_pressed); |
| 202 | } |
| 203 | |
| 204 | void BaseButton::on_action_event(Ref<InputEvent> p_event) { |
| 205 | Ref<InputEventMouseButton> mouse_button = p_event; |
| 206 | |
| 207 | if (p_event->is_pressed() && (mouse_button.is_null() || status.hovering)) { |
| 208 | status.press_attempt = true; |
| 209 | status.pressing_inside = true; |
| 210 | if (!status.pressed_down_with_focus) { |
| 211 | status.pressed_down_with_focus = true; |
| 212 | emit_signal(SNAME("button_down")); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (status.press_attempt && status.pressing_inside) { |
| 217 | if (toggle_mode) { |
| 218 | bool is_pressed = p_event->is_pressed(); |
| 219 | if ((is_pressed && action_mode == ACTION_MODE_BUTTON_PRESS) || (!is_pressed && action_mode == ACTION_MODE_BUTTON_RELEASE)) { |
| 220 | if (action_mode == ACTION_MODE_BUTTON_PRESS) { |
| 221 | status.press_attempt = false; |
| 222 | status.pressing_inside = false; |
| 223 | } |
| 224 | status.pressed = !status.pressed; |
| 225 | _unpress_group(); |
| 226 | if (button_group.is_valid()) { |
| 227 | button_group->emit_signal(SceneStringName(pressed), this); |
| 228 | } |
| 229 | _toggled(status.pressed); |
| 230 | _pressed(); |
| 231 | queue_accessibility_update(); |
| 232 | } |
| 233 | } else { |
| 234 | if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) { |
| 235 | _pressed(); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (!p_event->is_pressed()) { |
| 241 | status.press_attempt = false; |
| 242 | status.pressing_inside = false; |
| 243 | if (status.pressed_down_with_focus) { |
| 244 | status.pressed_down_with_focus = false; |
| 245 | emit_signal(SNAME("button_up")); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | queue_redraw(); |
| 250 | } |
| 251 | |
| 252 | void BaseButton::pressed() { |
| 253 | } |
| 254 | |
| 255 | void BaseButton::toggled(bool p_pressed) { |
| 256 | } |
| 257 | |
| 258 | void BaseButton::set_disabled(bool p_disabled) { |
| 259 | if (status.disabled == p_disabled) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | status.disabled = p_disabled; |
| 264 | if (p_disabled) { |
| 265 | if (!toggle_mode) { |
| 266 | status.pressed = false; |
| 267 | } |
| 268 | status.press_attempt = false; |
| 269 | status.pressing_inside = false; |
| 270 | if (status.pressed_down_with_focus) { |
| 271 | status.pressed_down_with_focus = false; |
| 272 | emit_signal(SNAME("button_up")); |
| 273 | } |
| 274 | } |
| 275 | queue_accessibility_update(); |
| 276 | queue_redraw(); |
| 277 | update_minimum_size(); |
| 278 | } |
| 279 | |
| 280 | bool BaseButton::is_disabled() const { |
| 281 | return status.disabled; |
| 282 | } |
| 283 | |
| 284 | void BaseButton::set_pressed(bool p_pressed) { |
| 285 | bool prev_pressed = status.pressed; |
| 286 | set_pressed_no_signal(p_pressed); |
| 287 | |
| 288 | if (status.pressed == prev_pressed) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | if (p_pressed) { |
| 293 | _unpress_group(); |
| 294 | if (button_group.is_valid()) { |
| 295 | button_group->emit_signal(SceneStringName(pressed), this); |
| 296 | } |
| 297 | } |
| 298 | _toggled(status.pressed); |
| 299 | } |
| 300 | |
| 301 | void BaseButton::set_pressed_no_signal(bool p_pressed) { |
| 302 | if (!toggle_mode) { |
| 303 | return; |
| 304 | } |
| 305 | if (status.pressed == p_pressed) { |
| 306 | return; |
| 307 | } |
| 308 | status.pressed = p_pressed; |
| 309 | queue_accessibility_update(); |
| 310 | queue_redraw(); |
| 311 | } |
| 312 | |
| 313 | bool BaseButton::is_pressing() const { |
| 314 | return status.press_attempt; |
| 315 | } |
| 316 | |
| 317 | bool BaseButton::is_pressed() const { |
| 318 | return toggle_mode ? status.pressed : status.press_attempt; |
| 319 | } |
| 320 | |
| 321 | bool BaseButton::is_hovered() const { |
| 322 | return status.hovering; |
| 323 | } |
| 324 | |
| 325 | BaseButton::DrawMode BaseButton::get_draw_mode() const { |
| 326 | if (status.disabled) { |
| 327 | return DRAW_DISABLED; |
| 328 | } |
| 329 | |
| 330 | if (in_shortcut_feedback) { |
| 331 | return DRAW_HOVER_PRESSED; |
| 332 | } |
| 333 | |
| 334 | if (!status.press_attempt && status.hovering) { |
| 335 | if (status.pressed) { |
| 336 | return DRAW_HOVER_PRESSED; |
| 337 | } |
| 338 | |
| 339 | return DRAW_HOVER; |
| 340 | } else { |
| 341 | // Determine if pressed or not. |
| 342 | bool pressing; |
| 343 | if (status.press_attempt) { |
| 344 | pressing = (status.pressing_inside || keep_pressed_outside); |
| 345 | if (status.pressed) { |
| 346 | pressing = !pressing; |
| 347 | } |
| 348 | } else { |
| 349 | pressing = status.pressed; |
| 350 | } |
| 351 | |
| 352 | if (pressing) { |
| 353 | return DRAW_PRESSED; |
| 354 | } else { |
| 355 | return DRAW_NORMAL; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void BaseButton::set_toggle_mode(bool p_on) { |
| 361 | // Make sure to set 'pressed' to false if we are not in toggle mode |
| 362 | if (!p_on) { |
| 363 | set_pressed(false); |
| 364 | } |
| 365 | queue_accessibility_update(); |
| 366 | |
| 367 | toggle_mode = p_on; |
| 368 | update_configuration_warnings(); |
| 369 | } |
| 370 | |
| 371 | bool BaseButton::is_toggle_mode() const { |
| 372 | return toggle_mode; |
| 373 | } |
| 374 | |
| 375 | void BaseButton::set_shortcut_in_tooltip(bool p_on) { |
| 376 | if (shortcut_in_tooltip != p_on) { |
| 377 | shortcut_in_tooltip = p_on; |
| 378 | queue_accessibility_update(); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | bool BaseButton::is_shortcut_in_tooltip_enabled() const { |
| 383 | return shortcut_in_tooltip; |
| 384 | } |
| 385 | |
| 386 | void BaseButton::set_action_mode(ActionMode p_mode) { |
| 387 | action_mode = p_mode; |
| 388 | } |
| 389 | |
| 390 | BaseButton::ActionMode BaseButton::get_action_mode() const { |
| 391 | return action_mode; |
| 392 | } |
| 393 | |
| 394 | void BaseButton::set_button_mask(BitField<MouseButtonMask> p_mask) { |
| 395 | button_mask = p_mask; |
| 396 | } |
| 397 | |
| 398 | BitField<MouseButtonMask> BaseButton::get_button_mask() const { |
| 399 | return button_mask; |
| 400 | } |
| 401 | |
| 402 | void BaseButton::set_keep_pressed_outside(bool p_on) { |
| 403 | keep_pressed_outside = p_on; |
| 404 | } |
| 405 | |
| 406 | bool BaseButton::is_keep_pressed_outside() const { |
| 407 | return keep_pressed_outside; |
| 408 | } |
| 409 | |
| 410 | void BaseButton::set_shortcut_feedback(bool p_enable) { |
| 411 | shortcut_feedback = p_enable; |
| 412 | } |
| 413 | |
| 414 | bool BaseButton::is_shortcut_feedback() const { |
| 415 | return shortcut_feedback; |
| 416 | } |
| 417 | |
| 418 | void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) { |
| 419 | if (shortcut != p_shortcut) { |
| 420 | shortcut = p_shortcut; |
| 421 | set_process_shortcut_input(shortcut.is_valid()); |
| 422 | queue_accessibility_update(); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | Ref<Shortcut> BaseButton::get_shortcut() const { |
| 427 | return shortcut; |
| 428 | } |
| 429 | |
| 430 | void BaseButton::_shortcut_feedback_timeout() { |
| 431 | in_shortcut_feedback = false; |
| 432 | queue_redraw(); |
| 433 | } |
| 434 | |
| 435 | void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) { |
| 436 | ERR_FAIL_COND(p_event.is_null()); |
| 437 | |
| 438 | if (!is_disabled() && p_event->is_pressed() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) { |
| 439 | if (toggle_mode) { |
| 440 | status.pressed = !status.pressed; |
| 441 | |
| 442 | _unpress_group(); |
| 443 | if (button_group.is_valid()) { |
| 444 | button_group->emit_signal(SceneStringName(pressed), this); |
| 445 | } |
| 446 | |
| 447 | _toggled(status.pressed); |
| 448 | _pressed(); |
| 449 | queue_accessibility_update(); |
| 450 | } else { |
| 451 | _pressed(); |
| 452 | } |
| 453 | queue_redraw(); |
| 454 | accept_event(); |
| 455 | |
| 456 | if (shortcut_feedback && is_inside_tree()) { |
| 457 | if (shortcut_feedback_timer == nullptr) { |
| 458 | shortcut_feedback_timer = memnew(Timer); |
| 459 | shortcut_feedback_timer->set_one_shot(true); |
| 460 | add_child(shortcut_feedback_timer, false, INTERNAL_MODE_BACK); |
| 461 | shortcut_feedback_timer->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/button_shortcut_feedback_highlight_time")); |
| 462 | shortcut_feedback_timer->connect("timeout", callable_mp(this, &BaseButton::_shortcut_feedback_timeout)); |
| 463 | } |
| 464 | |
| 465 | in_shortcut_feedback = true; |
| 466 | shortcut_feedback_timer->start(); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | Control *BaseButton::make_custom_tooltip(const String &p_text) const { |
| 472 | Control *control = Control::make_custom_tooltip(p_text); |
| 473 | if (control) { |
| 474 | return control; |
| 475 | } |
| 476 | if (!shortcut_in_tooltip || shortcut.is_null() || !shortcut->has_valid_event()) { |
| 477 | return nullptr; // Use the default tooltip label. |
| 478 | } |
| 479 | |
| 480 | String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")"; |
| 481 | if (!p_text.is_empty() && shortcut->get_name().nocasecmp_to(p_text) != 0) { |
| 482 | text += "\n" + atr(p_text); |
| 483 | } |
| 484 | |
| 485 | // Make a label similar to the default tooltip label. |
| 486 | // Auto translation is disabled because we already did that manually above. |
| 487 | // |
| 488 | // We can't customize the tooltip text by overriding `get_tooltip()` |
| 489 | // because otherwise user-defined `_make_custom_tooltip()` would receive |
| 490 | // the translated and annotated text. |
| 491 | Label *label = memnew(Label(text)); |
| 492 | label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 493 | label->set_theme_type_variation(SNAME("TooltipLabel")); |
| 494 | |
| 495 | return label; |
| 496 | } |
| 497 | |
| 498 | void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) { |
| 499 | if (button_group.is_valid()) { |
| 500 | button_group->buttons.erase(this); |
| 501 | } |
| 502 | |
| 503 | button_group = p_group; |
| 504 | |
| 505 | if (button_group.is_valid()) { |
| 506 | button_group->buttons.insert(this); |
| 507 | } |
| 508 | |
| 509 | queue_accessibility_update(); |
| 510 | queue_redraw(); //checkbox changes to radio if set a buttongroup |
| 511 | update_configuration_warnings(); |
| 512 | } |
| 513 | |
| 514 | Ref<ButtonGroup> BaseButton::get_button_group() const { |
| 515 | return button_group; |
| 516 | } |
| 517 | |
| 518 | bool BaseButton::_was_pressed_by_mouse() const { |
| 519 | return was_mouse_pressed; |
| 520 | } |
| 521 | |
| 522 | PackedStringArray BaseButton::get_configuration_warnings() const { |
| 523 | PackedStringArray warnings = Control::get_configuration_warnings(); |
| 524 | |
| 525 | if (get_button_group().is_valid() && !is_toggle_mode()) { |
| 526 | warnings.push_back(RTR("ButtonGroup is intended to be used only with buttons that have toggle_mode set to true.")); |
| 527 | } |
| 528 | |
| 529 | return warnings; |
| 530 | } |
| 531 | |
| 532 | void BaseButton::_bind_methods() { |
| 533 | ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed); |
| 534 | ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed); |
| 535 | ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal); |
| 536 | ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered); |
| 537 | ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode); |
| 538 | ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode); |
| 539 | ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip); |
| 540 | ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled); |
| 541 | ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled); |
| 542 | ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled); |
| 543 | ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode); |
| 544 | ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode); |
| 545 | ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask); |
| 546 | ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask); |
| 547 | ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode); |
| 548 | ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside); |
| 549 | ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside); |
| 550 | ClassDB::bind_method(D_METHOD("set_shortcut_feedback", "enabled"), &BaseButton::set_shortcut_feedback); |
| 551 | ClassDB::bind_method(D_METHOD("is_shortcut_feedback"), &BaseButton::is_shortcut_feedback); |
| 552 | |
| 553 | ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut); |
| 554 | ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut); |
| 555 | |
| 556 | ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group); |
| 557 | ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group); |
| 558 | |
| 559 | GDVIRTUAL_BIND(_pressed); |
| 560 | GDVIRTUAL_BIND(_toggled, "toggled_on"); |
| 561 | |
| 562 | ADD_SIGNAL(MethodInfo("pressed")); |
| 563 | ADD_SIGNAL(MethodInfo("button_up")); |
| 564 | ADD_SIGNAL(MethodInfo("button_down")); |
| 565 | ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "toggled_on"))); |
| 566 | |
| 567 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled"); |
| 568 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode"); |
| 569 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed"); |
| 570 | ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode"); |
| 571 | ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask"); |
| 572 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside"); |
| 573 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group"); |
| 574 | |
| 575 | ADD_GROUP("Shortcut", ""); |
| 576 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut"); |
| 577 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_feedback"), "set_shortcut_feedback", "is_shortcut_feedback"); |
| 578 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled"); |
| 579 | |
| 580 | BIND_ENUM_CONSTANT(DRAW_NORMAL); |
| 581 | BIND_ENUM_CONSTANT(DRAW_PRESSED); |
| 582 | BIND_ENUM_CONSTANT(DRAW_HOVER); |
| 583 | BIND_ENUM_CONSTANT(DRAW_DISABLED); |
| 584 | BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED); |
| 585 | |
| 586 | BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS); |
| 587 | BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE); |
| 588 | |
| 589 | GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/button_shortcut_feedback_highlight_time", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), 0.2); |
| 590 | } |
| 591 | |
| 592 | BaseButton::BaseButton() { |
| 593 | set_focus_mode(FOCUS_ALL); |
| 594 | } |
| 595 | |
| 596 | BaseButton::~BaseButton() { |
| 597 | if (button_group.is_valid()) { |
| 598 | button_group->buttons.erase(this); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) { |
| 603 | for (BaseButton *E : buttons) { |
| 604 | r_buttons->push_back(E); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | TypedArray<BaseButton> ButtonGroup::_get_buttons() { |
| 609 | TypedArray<BaseButton> btns; |
| 610 | for (const BaseButton *E : buttons) { |
| 611 | btns.push_back(E); |
| 612 | } |
| 613 | |
| 614 | return btns; |
| 615 | } |
| 616 | |
| 617 | BaseButton *ButtonGroup::get_pressed_button() { |
| 618 | for (BaseButton *E : buttons) { |
| 619 | if (E->is_pressed()) { |
| 620 | return E; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | return nullptr; |
| 625 | } |
| 626 | |
| 627 | void ButtonGroup::set_allow_unpress(bool p_enabled) { |
| 628 | allow_unpress = p_enabled; |
| 629 | } |
| 630 | bool ButtonGroup::is_allow_unpress() { |
| 631 | return allow_unpress; |
| 632 | } |
| 633 | |
| 634 | void ButtonGroup::_bind_methods() { |
| 635 | ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button); |
| 636 | ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons); |
| 637 | ClassDB::bind_method(D_METHOD("set_allow_unpress", "enabled"), &ButtonGroup::set_allow_unpress); |
| 638 | ClassDB::bind_method(D_METHOD("is_allow_unpress"), &ButtonGroup::is_allow_unpress); |
| 639 | |
| 640 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_unpress"), "set_allow_unpress", "is_allow_unpress"); |
| 641 | |
| 642 | ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton"))); |
| 643 | } |
| 644 | |
| 645 | ButtonGroup::ButtonGroup() { |
| 646 | set_local_to_scene(true); |
| 647 | } |