virtualx-engine/scene/gui/base_button.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

529 lines
16 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* base_button.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 02:10:30 +01:00
/* */
/* 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. */
/*************************************************************************/
2014-02-10 02:10:30 +01:00
#include "base_button.h"
2017-08-27 21:07:15 +02:00
#include "core/os/keyboard.h"
#include "scene/main/window.h"
2014-12-02 21:26:56 +01:00
#include "scene/scene_string_names.h"
void BaseButton::_unpress_group() {
if (!button_group.is_valid()) {
return;
}
if (toggle_mode) {
status.pressed = true;
}
for (BaseButton *E : button_group->buttons) {
if (E == this) {
continue;
}
E->set_pressed(false);
}
}
void BaseButton::gui_input(const Ref<InputEvent> &p_event) {
2021-04-05 08:52:21 +02:00
ERR_FAIL_COND(p_event.is_null());
if (status.disabled) { // no interaction with disabled button
2014-02-10 02:10:30 +01:00
return;
}
2016-03-09 00:00:52 +01:00
Ref<InputEventMouseButton> mouse_button = p_event;
bool ui_accept = p_event->is_action("ui_accept") && !p_event->is_echo();
2016-03-09 00:00:52 +01:00
2021-08-13 23:31:57 +02:00
bool button_masked = mouse_button.is_valid() && (mouse_button_to_mask(mouse_button->get_button_index()) & button_mask) != MouseButton::NONE;
if (button_masked || ui_accept) {
was_mouse_pressed = button_masked;
on_action_event(p_event);
was_mouse_pressed = false;
return;
}
2014-02-10 02:10:30 +01:00
Ref<InputEventMouseMotion> mouse_motion = p_event;
if (mouse_motion.is_valid()) {
if (status.press_attempt) {
bool last_press_inside = status.pressing_inside;
status.pressing_inside = has_point(mouse_motion->get_position());
if (last_press_inside != status.pressing_inside) {
queue_redraw();
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
}
void BaseButton::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_MOUSE_ENTER: {
status.hovering = true;
queue_redraw();
} break;
2016-03-09 00:00:52 +01:00
case NOTIFICATION_MOUSE_EXIT: {
status.hovering = false;
queue_redraw();
} break;
case NOTIFICATION_DRAG_BEGIN:
case NOTIFICATION_SCROLL_BEGIN: {
if (status.press_attempt) {
status.press_attempt = false;
queue_redraw();
}
} break;
case NOTIFICATION_FOCUS_ENTER: {
queue_redraw();
} break;
2014-02-10 02:10:30 +01:00
case NOTIFICATION_FOCUS_EXIT: {
if (status.press_attempt) {
status.press_attempt = false;
queue_redraw();
} else if (status.hovering) {
queue_redraw();
}
} 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;
}
2014-02-10 02:10:30 +01:00
}
void BaseButton::_pressed() {
GDVIRTUAL_CALL(_pressed);
pressed();
emit_signal(SNAME("pressed"));
2014-02-10 02:10:30 +01:00
}
void BaseButton::_toggled(bool p_pressed) {
GDVIRTUAL_CALL(_toggled, p_pressed);
toggled(p_pressed);
emit_signal(SNAME("toggled"), p_pressed);
2014-02-10 02:10:30 +01:00
}
void BaseButton::on_action_event(Ref<InputEvent> p_event) {
if (p_event->is_pressed()) {
status.press_attempt = true;
status.pressing_inside = true;
emit_signal(SNAME("button_down"));
}
if (status.press_attempt && status.pressing_inside) {
if (toggle_mode) {
2021-08-11 12:21:31 +02:00
bool is_pressed = p_event->is_pressed();
2021-05-29 15:28:16 +02:00
if (Object::cast_to<InputEventShortcut>(*p_event)) {
2021-08-11 12:21:31 +02:00
is_pressed = false;
2021-05-29 15:28:16 +02:00
}
2021-08-11 12:21:31 +02:00
if ((is_pressed && action_mode == ACTION_MODE_BUTTON_PRESS) || (!is_pressed && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
if (action_mode == ACTION_MODE_BUTTON_PRESS) {
status.press_attempt = false;
status.pressing_inside = false;
}
status.pressed = !status.pressed;
_unpress_group();
2021-05-06 12:16:27 +02:00
if (button_group.is_valid()) {
button_group->emit_signal(SNAME("pressed"), this);
2021-05-06 12:16:27 +02:00
}
_toggled(status.pressed);
_pressed();
}
} else {
if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
_pressed();
}
}
}
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;
}
}
status.press_attempt = false;
status.pressing_inside = false;
emit_signal(SNAME("button_up"));
}
queue_redraw();
}
void BaseButton::pressed() {
}
void BaseButton::toggled(bool p_pressed) {
}
2014-02-10 02:10:30 +01:00
void BaseButton::set_disabled(bool p_disabled) {
if (status.disabled == p_disabled) {
return;
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
status.disabled = p_disabled;
if (p_disabled) {
if (!toggle_mode) {
status.pressed = false;
}
status.press_attempt = false;
status.pressing_inside = false;
}
queue_redraw();
}
2014-02-10 02:10:30 +01:00
bool BaseButton::is_disabled() const {
return status.disabled;
}
2014-02-10 02:10:30 +01:00
void BaseButton::set_pressed(bool p_pressed) {
if (!toggle_mode) {
2014-02-10 02:10:30 +01:00
return;
}
if (status.pressed == p_pressed) {
2014-02-10 02:10:30 +01:00
return;
}
2014-02-10 02:10:30 +01:00
status.pressed = p_pressed;
if (p_pressed) {
_unpress_group();
2021-05-06 12:16:27 +02:00
if (button_group.is_valid()) {
button_group->emit_signal(SNAME("pressed"), this);
2021-05-06 12:16:27 +02:00
}
}
_toggled(status.pressed);
queue_redraw();
2014-02-10 02:10:30 +01:00
}
void BaseButton::set_pressed_no_signal(bool p_pressed) {
if (!toggle_mode) {
return;
}
if (status.pressed == p_pressed) {
return;
}
status.pressed = p_pressed;
queue_redraw();
}
2014-02-10 02:10:30 +01:00
bool BaseButton::is_pressing() const {
return status.press_attempt;
}
bool BaseButton::is_pressed() const {
return toggle_mode ? status.pressed : status.press_attempt;
}
bool BaseButton::is_hovered() const {
return status.hovering;
}
2014-02-10 02:10:30 +01:00
BaseButton::DrawMode BaseButton::get_draw_mode() const {
if (status.disabled) {
return DRAW_DISABLED;
};
2016-03-09 00:00:52 +01:00
if (!status.press_attempt && status.hovering) {
if (status.pressed) {
return DRAW_HOVER_PRESSED;
}
2014-02-10 02:10:30 +01:00
return DRAW_HOVER;
} else {
/* determine if pressed or not */
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
bool pressing;
if (status.press_attempt) {
pressing = (status.pressing_inside || keep_pressed_outside);
if (status.pressed) {
2014-02-10 02:10:30 +01:00
pressing = !pressing;
}
2014-02-10 02:10:30 +01:00
} else {
pressing = status.pressed;
}
2016-03-09 00:00:52 +01:00
if (pressing) {
2014-02-10 02:10:30 +01:00
return DRAW_PRESSED;
} else {
2014-02-10 02:10:30 +01:00
return DRAW_NORMAL;
}
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
return DRAW_NORMAL;
}
void BaseButton::set_toggle_mode(bool p_on) {
// Make sure to set 'pressed' to false if we are not in toggle mode
if (!p_on) {
set_pressed(false);
}
2014-02-10 02:10:30 +01:00
toggle_mode = p_on;
}
bool BaseButton::is_toggle_mode() const {
return toggle_mode;
}
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;
}
void BaseButton::set_action_mode(ActionMode p_mode) {
action_mode = p_mode;
2014-02-10 02:10:30 +01:00
}
BaseButton::ActionMode BaseButton::get_action_mode() const {
return action_mode;
2014-02-10 02:10:30 +01:00
}
2021-08-13 23:31:57 +02:00
void BaseButton::set_button_mask(MouseButton p_mask) {
button_mask = p_mask;
}
2021-08-13 23:31:57 +02:00
MouseButton BaseButton::get_button_mask() const {
return button_mask;
}
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;
}
void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) {
shortcut = p_shortcut;
set_process_shortcut_input(shortcut.is_valid());
}
Ref<Shortcut> BaseButton::get_shortcut() const {
return shortcut;
}
void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
2021-04-05 08:52:21 +02:00
ERR_FAIL_COND(p_event.is_null());
if (!_is_focus_owner_in_shortcut_context()) {
return;
}
if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
on_action_event(p_event);
accept_event();
}
}
String BaseButton::get_tooltip(const Point2 &p_pos) const {
String tooltip = Control::get_tooltip(p_pos);
if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
text += "\n" + atr(tooltip);
}
tooltip = text;
}
return tooltip;
}
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);
}
queue_redraw(); //checkbox changes to radio if set a buttongroup
}
Ref<ButtonGroup> BaseButton::get_button_group() const {
return button_group;
}
void BaseButton::set_shortcut_context(Node *p_node) {
2022-01-25 13:13:00 +01:00
if (p_node != nullptr) {
shortcut_context = p_node->get_instance_id();
} else {
shortcut_context = ObjectID();
}
}
Node *BaseButton::get_shortcut_context() const {
Object *ctx_obj = ObjectDB::get_instance(shortcut_context);
Node *ctx_node = Object::cast_to<Node>(ctx_obj);
return ctx_node;
}
bool BaseButton::_is_focus_owner_in_shortcut_context() const {
if (shortcut_context == ObjectID()) {
// No context, therefore global - always "in" context.
return true;
}
Node *ctx_node = get_shortcut_context();
Control *vp_focus = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
// If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
}
bool BaseButton::_was_pressed_by_mouse() const {
return was_mouse_pressed;
}
2014-02-10 02:10:30 +01:00
void BaseButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
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);
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);
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);
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);
ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
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);
ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
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);
ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &BaseButton::set_shortcut_context);
ClassDB::bind_method(D_METHOD("get_shortcut_context"), &BaseButton::get_shortcut_context);
GDVIRTUAL_BIND(_pressed);
GDVIRTUAL_BIND(_toggled, "button_pressed");
2014-02-10 02:10:30 +01:00
ADD_SIGNAL(MethodInfo("pressed"));
2016-06-18 01:21:14 +02:00
ADD_SIGNAL(MethodInfo("button_up"));
ADD_SIGNAL(MethodInfo("button_down"));
ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed");
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");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_NODE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
BIND_ENUM_CONSTANT(DRAW_NORMAL);
BIND_ENUM_CONSTANT(DRAW_PRESSED);
BIND_ENUM_CONSTANT(DRAW_HOVER);
BIND_ENUM_CONSTANT(DRAW_DISABLED);
BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
2014-02-10 02:10:30 +01:00
}
BaseButton::BaseButton() {
set_focus_mode(FOCUS_ALL);
}
BaseButton::~BaseButton() {
if (button_group.is_valid()) {
button_group->buttons.erase(this);
}
2014-02-10 02:10:30 +01:00
}
void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
for (BaseButton *E : buttons) {
r_buttons->push_back(E);
}
}
TypedArray<BaseButton> ButtonGroup::_get_buttons() {
TypedArray<BaseButton> btns;
for (const BaseButton *E : buttons) {
btns.push_back(E);
}
return btns;
}
BaseButton *ButtonGroup::get_pressed_button() {
for (BaseButton *E : buttons) {
if (E->is_pressed()) {
return E;
}
}
2020-04-02 01:20:12 +02:00
return nullptr;
}
void ButtonGroup::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton")));
}
ButtonGroup::ButtonGroup() {
set_local_to_scene(true);
}