2023-01-05 20:25:55 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* base_button.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "base_button.h"
|
2017-08-28 03:07:15 +08:00
|
|
|
|
2023-01-13 20:59:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/keyboard.h"
|
2020-03-04 09:51:12 +08:00
|
|
|
#include "scene/main/window.h"
|
2014-12-03 04:26:56 +08:00
|
|
|
#include "scene/scene_string_names.h"
|
2017-01-10 12:04:31 +08:00
|
|
|
|
|
|
|
void BaseButton::_unpress_group() {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!button_group.is_valid()) {
|
2017-01-10 12:04:31 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2017-09-27 07:40:00 +08:00
|
|
|
if (toggle_mode) {
|
|
|
|
status.pressed = true;
|
|
|
|
}
|
2017-04-10 12:57:24 +08:00
|
|
|
|
2022-05-19 07:43:40 +08:00
|
|
|
for (BaseButton *E : button_group->buttons) {
|
|
|
|
if (E == this) {
|
2017-01-10 12:04:31 +08:00
|
|
|
continue;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2022-05-19 07:43:40 +08:00
|
|
|
E->set_pressed(false);
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 23:37:22 +08:00
|
|
|
void BaseButton::gui_input(const Ref<InputEvent> &p_event) {
|
2021-04-05 14:52:21 +08:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (status.disabled) { // no interaction with disabled button
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2019-05-21 16:26:08 +08:00
|
|
|
Ref<InputEventMouseButton> mouse_button = p_event;
|
2022-09-24 16:01:02 +08:00
|
|
|
bool ui_accept = p_event->is_action("ui_accept", true) && !p_event->is_echo();
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2023-01-08 07:55:54 +08:00
|
|
|
bool button_masked = mouse_button.is_valid() && button_mask.has_flag(mouse_button_to_mask(mouse_button->get_button_index()));
|
2019-05-21 16:26:08 +08:00
|
|
|
if (button_masked || ui_accept) {
|
2022-08-20 01:19:39 +08:00
|
|
|
was_mouse_pressed = button_masked;
|
2019-02-03 09:24:05 +08:00
|
|
|
on_action_event(p_event);
|
2022-08-28 00:32:45 +08:00
|
|
|
was_mouse_pressed = false;
|
|
|
|
|
2019-05-21 16:26:08 +08:00
|
|
|
return;
|
2017-05-20 23:38:03 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2019-05-21 16:26:08 +08:00
|
|
|
Ref<InputEventMouseMotion> mouse_motion = p_event;
|
|
|
|
if (mouse_motion.is_valid()) {
|
|
|
|
if (status.press_attempt) {
|
2017-05-20 23:38:03 +08:00
|
|
|
bool last_press_inside = status.pressing_inside;
|
2019-05-21 16:26:08 +08:00
|
|
|
status.pressing_inside = has_point(mouse_motion->get_position());
|
|
|
|
if (last_press_inside != status.pressing_inside) {
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::_notification(int p_what) {
|
2022-02-16 01:06:48 +08:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_MOUSE_ENTER: {
|
|
|
|
status.hovering = true;
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2022-02-16 01:06:48 +08:00
|
|
|
} break;
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2022-02-16 01:06:48 +08:00
|
|
|
case NOTIFICATION_MOUSE_EXIT: {
|
|
|
|
status.hovering = false;
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2022-02-16 01:06:48 +08:00
|
|
|
} break;
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2022-02-16 01:06:48 +08:00
|
|
|
case NOTIFICATION_DRAG_BEGIN:
|
|
|
|
case NOTIFICATION_SCROLL_BEGIN: {
|
|
|
|
if (status.press_attempt) {
|
|
|
|
status.press_attempt = false;
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2022-02-16 01:06:48 +08:00
|
|
|
}
|
|
|
|
} break;
|
2016-06-24 05:20:39 +08:00
|
|
|
|
2022-02-16 01:06:48 +08:00
|
|
|
case NOTIFICATION_FOCUS_ENTER: {
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2022-02-16 01:06:48 +08:00
|
|
|
} break;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2022-02-16 01:06:48 +08:00
|
|
|
case NOTIFICATION_FOCUS_EXIT: {
|
|
|
|
if (status.press_attempt) {
|
|
|
|
status.press_attempt = false;
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2022-02-16 01:06:48 +08:00
|
|
|
} else if (status.hovering) {
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2022-02-16 01:06:48 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED:
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED && is_visible_in_tree()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!toggle_mode) {
|
|
|
|
status.pressed = false;
|
|
|
|
}
|
|
|
|
status.hovering = false;
|
|
|
|
status.press_attempt = false;
|
|
|
|
status.pressing_inside = false;
|
|
|
|
} break;
|
2015-09-26 09:06:42 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 05:28:34 +08:00
|
|
|
void BaseButton::_pressed() {
|
2021-08-22 09:52:44 +08:00
|
|
|
GDVIRTUAL_CALL(_pressed);
|
2019-05-29 05:28:34 +08:00
|
|
|
pressed();
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("pressed"));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 05:28:34 +08:00
|
|
|
void BaseButton::_toggled(bool p_pressed) {
|
2021-08-22 09:52:44 +08:00
|
|
|
GDVIRTUAL_CALL(_toggled, p_pressed);
|
2019-05-29 05:28:34 +08:00
|
|
|
toggled(p_pressed);
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("toggled"), p_pressed);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:24:05 +08:00
|
|
|
void BaseButton::on_action_event(Ref<InputEvent> p_event) {
|
|
|
|
if (p_event->is_pressed()) {
|
|
|
|
status.press_attempt = true;
|
|
|
|
status.pressing_inside = true;
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("button_down"));
|
2019-02-03 09:24:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status.press_attempt && status.pressing_inside) {
|
|
|
|
if (toggle_mode) {
|
2021-08-11 18:21:31 +08:00
|
|
|
bool is_pressed = p_event->is_pressed();
|
|
|
|
if ((is_pressed && action_mode == ACTION_MODE_BUTTON_PRESS) || (!is_pressed && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
|
2019-02-03 09:24:05 +08:00
|
|
|
if (action_mode == ACTION_MODE_BUTTON_PRESS) {
|
|
|
|
status.press_attempt = false;
|
|
|
|
status.pressing_inside = false;
|
|
|
|
}
|
|
|
|
status.pressed = !status.pressed;
|
|
|
|
_unpress_group();
|
2021-05-06 18:16:27 +08:00
|
|
|
if (button_group.is_valid()) {
|
2021-07-18 05:22:52 +08:00
|
|
|
button_group->emit_signal(SNAME("pressed"), this);
|
2021-05-06 18:16:27 +08:00
|
|
|
}
|
2019-02-03 09:24:05 +08:00
|
|
|
_toggled(status.pressed);
|
|
|
|
_pressed();
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-09 07:01:37 +08:00
|
|
|
if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
|
2019-02-03 09:24:05 +08:00
|
|
|
_pressed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:21:52 +08:00
|
|
|
if (!p_event->is_pressed()) {
|
|
|
|
Ref<InputEventMouseButton> mouse_button = p_event;
|
|
|
|
if (mouse_button.is_valid()) {
|
|
|
|
if (!has_point(mouse_button->get_position())) {
|
|
|
|
status.hovering = false;
|
|
|
|
}
|
|
|
|
}
|
2019-02-03 09:24:05 +08:00
|
|
|
status.press_attempt = false;
|
2020-01-03 19:21:52 +08:00
|
|
|
status.pressing_inside = false;
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("button_up"));
|
2019-02-03 09:24:05 +08:00
|
|
|
}
|
|
|
|
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2019-02-03 09:24:05 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 05:28:34 +08:00
|
|
|
void BaseButton::pressed() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::toggled(bool p_pressed) {
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void BaseButton::set_disabled(bool p_disabled) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (status.disabled == p_disabled) {
|
2017-11-07 15:58:35 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
status.disabled = p_disabled;
|
2018-09-14 15:21:58 +08:00
|
|
|
if (p_disabled) {
|
|
|
|
if (!toggle_mode) {
|
|
|
|
status.pressed = false;
|
|
|
|
}
|
|
|
|
status.press_attempt = false;
|
|
|
|
status.pressing_inside = false;
|
|
|
|
}
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2015-03-02 08:07:50 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
bool BaseButton::is_disabled() const {
|
|
|
|
return status.disabled;
|
2015-03-02 08:07:50 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
void BaseButton::set_pressed(bool p_pressed) {
|
2022-11-01 06:12:53 +08:00
|
|
|
bool prev_pressed = status.pressed;
|
|
|
|
set_pressed_no_signal(p_pressed);
|
|
|
|
|
|
|
|
if (status.pressed == prev_pressed) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-01-10 12:04:31 +08:00
|
|
|
|
|
|
|
if (p_pressed) {
|
|
|
|
_unpress_group();
|
2021-05-06 18:16:27 +08:00
|
|
|
if (button_group.is_valid()) {
|
2021-07-18 05:22:52 +08:00
|
|
|
button_group->emit_signal(SNAME("pressed"), this);
|
2021-05-06 18:16:27 +08:00
|
|
|
}
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
2019-06-12 22:31:01 +08:00
|
|
|
_toggled(status.pressed);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2021-05-06 20:33:44 +08:00
|
|
|
void BaseButton::set_pressed_no_signal(bool p_pressed) {
|
|
|
|
if (!toggle_mode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (status.pressed == p_pressed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
status.pressed = p_pressed;
|
|
|
|
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2021-05-06 20:33:44 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
bool BaseButton::is_pressing() const {
|
|
|
|
return status.press_attempt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::is_pressed() const {
|
|
|
|
return toggle_mode ? status.pressed : status.press_attempt;
|
|
|
|
}
|
|
|
|
|
2014-06-30 09:41:02 +08:00
|
|
|
bool BaseButton::is_hovered() const {
|
|
|
|
return status.hovering;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
BaseButton::DrawMode BaseButton::get_draw_mode() const {
|
|
|
|
if (status.disabled) {
|
|
|
|
return DRAW_DISABLED;
|
2022-09-28 22:20:29 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2023-01-13 20:59:38 +08:00
|
|
|
if (in_shortcut_feedback) {
|
|
|
|
return DRAW_HOVER_PRESSED;
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:20:41 +08:00
|
|
|
if (!status.press_attempt && status.hovering) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (status.pressed) {
|
2018-09-24 07:56:30 +08:00
|
|
|
return DRAW_HOVER_PRESSED;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return DRAW_HOVER;
|
|
|
|
} else {
|
2022-09-28 22:20:29 +08:00
|
|
|
// Determine if pressed or not.
|
2014-02-10 09:10:30 +08:00
|
|
|
bool pressing;
|
|
|
|
if (status.press_attempt) {
|
2019-01-29 15:19:48 +08:00
|
|
|
pressing = (status.pressing_inside || keep_pressed_outside);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (status.pressed) {
|
2014-02-10 09:10:30 +08:00
|
|
|
pressing = !pressing;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
|
|
|
pressing = status.pressed;
|
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2023-01-13 20:59:38 +08:00
|
|
|
if (pressing) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return DRAW_PRESSED;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2014-02-10 09:10:30 +08:00
|
|
|
return DRAW_NORMAL;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::set_toggle_mode(bool p_on) {
|
2019-10-19 00:38:56 +08:00
|
|
|
// Make sure to set 'pressed' to false if we are not in toggle mode
|
|
|
|
if (!p_on) {
|
|
|
|
set_pressed(false);
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
toggle_mode = p_on;
|
2022-12-20 14:08:42 +08:00
|
|
|
update_configuration_warnings();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::is_toggle_mode() const {
|
|
|
|
return toggle_mode;
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:30:59 +08:00
|
|
|
void BaseButton::set_shortcut_in_tooltip(bool p_on) {
|
|
|
|
shortcut_in_tooltip = p_on;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::is_shortcut_in_tooltip_enabled() const {
|
|
|
|
return shortcut_in_tooltip;
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:30 +08:00
|
|
|
void BaseButton::set_action_mode(ActionMode p_mode) {
|
|
|
|
action_mode = p_mode;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:30 +08:00
|
|
|
BaseButton::ActionMode BaseButton::get_action_mode() const {
|
|
|
|
return action_mode;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2023-01-08 07:55:54 +08:00
|
|
|
void BaseButton::set_button_mask(BitField<MouseButtonMask> p_mask) {
|
2018-03-12 01:19:18 +08:00
|
|
|
button_mask = p_mask;
|
|
|
|
}
|
|
|
|
|
2023-01-08 07:55:54 +08:00
|
|
|
BitField<MouseButtonMask> BaseButton::get_button_mask() const {
|
2018-03-12 01:19:18 +08:00
|
|
|
return button_mask;
|
|
|
|
}
|
|
|
|
|
2019-01-29 15:19:48 +08:00
|
|
|
void BaseButton::set_keep_pressed_outside(bool p_on) {
|
|
|
|
keep_pressed_outside = p_on;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::is_keep_pressed_outside() const {
|
|
|
|
return keep_pressed_outside;
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:59:38 +08:00
|
|
|
void BaseButton::set_shortcut_feedback(bool p_enable) {
|
|
|
|
shortcut_feedback = p_enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::is_shortcut_feedback() const {
|
|
|
|
return shortcut_feedback;
|
|
|
|
}
|
|
|
|
|
2020-09-10 03:53:24 +08:00
|
|
|
void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) {
|
2016-06-05 08:31:29 +08:00
|
|
|
shortcut = p_shortcut;
|
2022-01-11 21:59:52 +08:00
|
|
|
set_process_shortcut_input(shortcut.is_valid());
|
2016-06-05 08:31:29 +08:00
|
|
|
}
|
|
|
|
|
2020-09-10 03:53:24 +08:00
|
|
|
Ref<Shortcut> BaseButton::get_shortcut() const {
|
2016-06-05 08:31:29 +08:00
|
|
|
return shortcut;
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:59:38 +08:00
|
|
|
void BaseButton::_shortcut_feedback_timeout() {
|
|
|
|
in_shortcut_feedback = false;
|
|
|
|
queue_redraw();
|
|
|
|
}
|
|
|
|
|
2022-01-11 21:59:52 +08:00
|
|
|
void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
|
2021-04-05 14:52:21 +08:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2023-01-13 20:59:38 +08:00
|
|
|
if (!is_disabled() && p_event->is_pressed() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
|
|
|
|
if (toggle_mode) {
|
|
|
|
status.pressed = !status.pressed;
|
|
|
|
|
|
|
|
if (status.pressed) {
|
|
|
|
_unpress_group();
|
|
|
|
if (button_group.is_valid()) {
|
|
|
|
button_group->emit_signal(SNAME("pressed"), this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_toggled(status.pressed);
|
|
|
|
_pressed();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
_pressed();
|
|
|
|
}
|
|
|
|
queue_redraw();
|
2020-09-16 08:44:38 +08:00
|
|
|
accept_event();
|
2023-01-13 20:59:38 +08:00
|
|
|
|
|
|
|
if (shortcut_feedback) {
|
|
|
|
if (shortcut_feedback_timer == nullptr) {
|
|
|
|
shortcut_feedback_timer = memnew(Timer);
|
2023-01-21 17:54:45 +08:00
|
|
|
shortcut_feedback_timer->set_one_shot(true);
|
2023-01-13 20:59:38 +08:00
|
|
|
add_child(shortcut_feedback_timer);
|
|
|
|
shortcut_feedback_timer->set_wait_time(GLOBAL_GET("gui/timers/button_shortcut_feedback_highlight_time"));
|
|
|
|
shortcut_feedback_timer->connect("timeout", callable_mp(this, &BaseButton::_shortcut_feedback_timeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
in_shortcut_feedback = true;
|
|
|
|
shortcut_feedback_timer->start();
|
|
|
|
}
|
2016-06-05 08:31:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String BaseButton::get_tooltip(const Point2 &p_pos) const {
|
|
|
|
String tooltip = Control::get_tooltip(p_pos);
|
2021-08-03 23:27:45 +08:00
|
|
|
if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
|
2017-12-22 03:17:30 +08:00
|
|
|
String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
|
2021-12-09 17:42:46 +08:00
|
|
|
if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
|
2021-12-21 01:08:37 +08:00
|
|
|
text += "\n" + atr(tooltip);
|
2016-06-12 03:51:00 +08:00
|
|
|
}
|
2017-12-22 03:17:30 +08:00
|
|
|
tooltip = text;
|
2016-06-12 03:51:00 +08:00
|
|
|
}
|
2016-06-05 08:31:29 +08:00
|
|
|
return tooltip;
|
|
|
|
}
|
2014-11-02 22:31:01 +08:00
|
|
|
|
2017-01-10 12:04:31 +08:00
|
|
|
void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
|
|
|
|
if (button_group.is_valid()) {
|
|
|
|
button_group->buttons.erase(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
button_group = p_group;
|
|
|
|
|
|
|
|
if (button_group.is_valid()) {
|
|
|
|
button_group->buttons.insert(this);
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw(); //checkbox changes to radio if set a buttongroup
|
2022-12-20 14:08:42 +08:00
|
|
|
update_configuration_warnings();
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<ButtonGroup> BaseButton::get_button_group() const {
|
|
|
|
return button_group;
|
|
|
|
}
|
|
|
|
|
2022-08-20 01:19:39 +08:00
|
|
|
bool BaseButton::_was_pressed_by_mouse() const {
|
|
|
|
return was_mouse_pressed;
|
|
|
|
}
|
|
|
|
|
2022-12-20 14:08:42 +08:00
|
|
|
PackedStringArray BaseButton::get_configuration_warnings() const {
|
|
|
|
PackedStringArray warnings = Control::get_configuration_warnings();
|
|
|
|
|
|
|
|
if (get_button_group().is_valid() && !is_toggle_mode()) {
|
|
|
|
warnings.push_back(RTR("ButtonGroup is intended to be used only with buttons that have toggle_mode set to true."));
|
|
|
|
}
|
|
|
|
|
|
|
|
return warnings;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void BaseButton::_bind_methods() {
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
|
2021-05-06 20:33:44 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
|
2018-11-19 21:30:59 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
|
2018-03-12 01:19:18 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
|
2019-01-29 15:19:48 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
|
2023-01-13 20:59:38 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_shortcut_feedback", "enabled"), &BaseButton::set_shortcut_feedback);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_shortcut_feedback"), &BaseButton::is_shortcut_feedback);
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2017-08-09 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2017-08-09 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2021-08-22 09:52:44 +08:00
|
|
|
GDVIRTUAL_BIND(_pressed);
|
|
|
|
GDVIRTUAL_BIND(_toggled, "button_pressed");
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
ADD_SIGNAL(MethodInfo("pressed"));
|
2016-06-18 07:21:14 +08:00
|
|
|
ADD_SIGNAL(MethodInfo("button_up"));
|
|
|
|
ADD_SIGNAL(MethodInfo("button_down"));
|
2018-01-09 23:39:43 +08:00
|
|
|
ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
|
2022-01-28 22:35:25 +08:00
|
|
|
|
2018-11-08 22:30:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
|
2017-02-12 08:11:37 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
|
2022-01-24 04:33:58 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed");
|
2018-11-08 22:30:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
|
2019-01-29 15:19:48 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
|
2023-01-25 22:39:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
|
|
|
|
|
|
|
|
ADD_GROUP("Shortcut", "");
|
2020-09-10 03:53:24 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
|
2022-07-23 07:19:48 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_feedback"), "set_shortcut_feedback", "is_shortcut_feedback");
|
2023-01-25 22:39:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2017-08-20 23:45:01 +08:00
|
|
|
BIND_ENUM_CONSTANT(DRAW_NORMAL);
|
|
|
|
BIND_ENUM_CONSTANT(DRAW_PRESSED);
|
|
|
|
BIND_ENUM_CONSTANT(DRAW_HOVER);
|
|
|
|
BIND_ENUM_CONSTANT(DRAW_DISABLED);
|
2018-09-24 07:56:30 +08:00
|
|
|
BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2017-08-20 23:45:01 +08:00
|
|
|
BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
|
|
|
|
BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
|
2023-01-13 20:59:38 +08:00
|
|
|
|
|
|
|
GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/button_shortcut_feedback_highlight_time", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), 0.2);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BaseButton::BaseButton() {
|
|
|
|
set_focus_mode(FOCUS_ALL);
|
2018-01-06 05:06:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BaseButton::~BaseButton() {
|
2017-01-10 12:04:31 +08:00
|
|
|
if (button_group.is_valid()) {
|
|
|
|
button_group->buttons.erase(this);
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 12:04:31 +08:00
|
|
|
void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
|
2022-05-19 07:43:40 +08:00
|
|
|
for (BaseButton *E : buttons) {
|
|
|
|
r_buttons->push_back(E);
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:41:48 +08:00
|
|
|
TypedArray<BaseButton> ButtonGroup::_get_buttons() {
|
|
|
|
TypedArray<BaseButton> btns;
|
2022-05-19 07:43:40 +08:00
|
|
|
for (const BaseButton *E : buttons) {
|
|
|
|
btns.push_back(E);
|
2018-12-14 13:39:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return btns;
|
|
|
|
}
|
|
|
|
|
2017-01-10 12:04:31 +08:00
|
|
|
BaseButton *ButtonGroup::get_pressed_button() {
|
2022-05-19 07:43:40 +08:00
|
|
|
for (BaseButton *E : buttons) {
|
|
|
|
if (E->is_pressed()) {
|
|
|
|
return E;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
return nullptr;
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonGroup::_bind_methods() {
|
2017-08-09 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
|
2018-12-14 13:39:31 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
|
2022-01-28 22:35:25 +08:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton")));
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ButtonGroup::ButtonGroup() {
|
|
|
|
set_local_to_scene(true);
|
|
|
|
}
|