2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* base_button.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "base_button.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "button_group.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "os/keyboard.h"
|
|
|
|
#include "print_string.h"
|
2016-06-28 01:14:59 +02:00
|
|
|
#include "scene/main/viewport.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "scene/scene_string_names.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void BaseButton::_input_event(InputEvent p_event) {
|
|
|
|
|
|
|
|
if (status.disabled) // no interaction with disabled button
|
|
|
|
return;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (p_event.type) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
case InputEvent::MOUSE_BUTTON: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const InputEventMouseButton &b = p_event.mouse_button;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (status.disabled || b.button_index != 1)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (status.pressing_button)
|
|
|
|
break;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (status.click_on_press) {
|
|
|
|
|
|
|
|
if (b.pressed) {
|
|
|
|
|
2016-06-18 01:21:14 +02:00
|
|
|
emit_signal("button_down");
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!toggle_mode) { //mouse press attempt
|
2014-11-12 15:23:23 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = true;
|
|
|
|
status.pressing_inside = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
pressed();
|
2014-12-02 21:26:56 +01:00
|
|
|
if (get_script_instance()) {
|
|
|
|
Variant::CallError ce;
|
2017-03-19 00:36:26 +01:00
|
|
|
get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
|
2014-12-02 21:26:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
emit_signal("pressed");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.pressed = !status.pressed;
|
2014-02-10 02:10:30 +01:00
|
|
|
pressed();
|
2014-12-02 21:26:56 +01:00
|
|
|
if (get_script_instance()) {
|
|
|
|
Variant::CallError ce;
|
2017-03-19 00:36:26 +01:00
|
|
|
get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
|
2014-12-02 21:26:56 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
emit_signal("pressed");
|
|
|
|
|
|
|
|
toggled(status.pressed);
|
2017-03-19 00:36:26 +01:00
|
|
|
emit_signal("toggled", status.pressed);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
} else {
|
|
|
|
|
2016-06-18 01:21:14 +02:00
|
|
|
emit_signal("button_up");
|
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
if (status.press_attempt && status.pressing_inside) {
|
2017-03-19 00:36:26 +01:00
|
|
|
// released();
|
2014-11-12 15:23:23 +01:00
|
|
|
emit_signal("released");
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2014-10-24 16:33:18 +02:00
|
|
|
update();
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b.pressed) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = true;
|
|
|
|
status.pressing_inside = true;
|
2016-06-18 01:21:14 +02:00
|
|
|
emit_signal("button_down");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-06-18 01:21:14 +02:00
|
|
|
emit_signal("button_up");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (status.press_attempt && status.pressing_inside) {
|
2014-11-12 15:23:23 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!toggle_mode) { //mouse press attempt
|
2014-11-12 15:23:23 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
pressed();
|
2014-12-02 21:26:56 +01:00
|
|
|
if (get_script_instance()) {
|
|
|
|
Variant::CallError ce;
|
2017-03-19 00:36:26 +01:00
|
|
|
get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
|
2014-12-02 21:26:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
emit_signal("pressed");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2014-11-12 15:23:23 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.pressed = !status.pressed;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
pressed();
|
|
|
|
emit_signal("pressed");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
toggled(status.pressed);
|
2017-03-19 00:36:26 +01:00
|
|
|
emit_signal("toggled", status.pressed);
|
2014-12-02 21:26:56 +01:00
|
|
|
if (get_script_instance()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
|
2014-12-02 21:26:56 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-12 15:23:23 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
update();
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case InputEvent::MOUSE_MOTION: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (status.press_attempt && status.pressing_button == 0) {
|
|
|
|
bool last_press_inside = status.pressing_inside;
|
|
|
|
status.pressing_inside = has_point(Point2(p_event.mouse_motion.x, p_event.mouse_motion.y));
|
|
|
|
if (last_press_inside != status.pressing_inside)
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
} break;
|
2015-05-29 23:37:23 +02:00
|
|
|
case InputEvent::ACTION:
|
2014-02-10 02:10:30 +01:00
|
|
|
case InputEvent::JOYSTICK_BUTTON:
|
|
|
|
case InputEvent::KEY: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_event.is_echo()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.disabled) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (status.press_attempt && status.pressing_button == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_event.is_action("ui_accept")) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_event.is_pressed()) {
|
|
|
|
|
|
|
|
status.pressing_button++;
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = true;
|
|
|
|
status.pressing_inside = true;
|
2016-06-18 01:21:14 +02:00
|
|
|
emit_signal("button_down");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else if (status.press_attempt) {
|
|
|
|
|
|
|
|
if (status.pressing_button)
|
|
|
|
status.pressing_button--;
|
|
|
|
|
|
|
|
if (status.pressing_button)
|
|
|
|
break;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = false;
|
|
|
|
status.pressing_inside = false;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-06-18 01:21:14 +02:00
|
|
|
emit_signal("button_up");
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!toggle_mode) { //mouse press attempt
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
pressed();
|
2016-03-09 00:00:52 +01:00
|
|
|
emit_signal("pressed");
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.pressed = !status.pressed;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
pressed();
|
|
|
|
emit_signal("pressed");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
toggled(status.pressed);
|
2014-12-02 21:26:56 +01:00
|
|
|
if (get_script_instance()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
|
2014-12-02 21:26:56 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
emit_signal("toggled", status.pressed);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
accept_event();
|
2016-03-09 00:00:52 +01:00
|
|
|
update();
|
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) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_MOUSE_ENTER) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.hovering = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_MOUSE_EXIT) {
|
|
|
|
status.hovering = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
2016-03-09 00:00:52 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_DRAG_BEGIN) {
|
2016-06-23 23:20:39 +02:00
|
|
|
|
|
|
|
if (status.press_attempt) {
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = false;
|
|
|
|
status.pressing_button = 0;
|
2016-06-23 23:20:39 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_FOCUS_ENTER) {
|
|
|
|
|
|
|
|
status.hovering = true;
|
2016-10-07 15:33:28 +02:00
|
|
|
update();
|
|
|
|
}
|
2016-06-23 23:20:39 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_FOCUS_EXIT) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (status.pressing_button && status.press_attempt) {
|
2017-03-19 00:36:26 +01:00
|
|
|
status.press_attempt = false;
|
|
|
|
status.pressing_button = 0;
|
|
|
|
status.hovering = false;
|
2016-10-07 15:33:28 +02:00
|
|
|
update();
|
|
|
|
} else if (status.hovering) {
|
2017-03-19 00:36:26 +01:00
|
|
|
status.hovering = false;
|
2016-06-23 23:20:39 +02:00
|
|
|
update();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
CanvasItem *ci = this;
|
|
|
|
while (ci) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ButtonGroup *bg = ci->cast_to<ButtonGroup>();
|
|
|
|
if (bg) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
group = bg;
|
2014-02-10 02:10:30 +01:00
|
|
|
group->_add_button(this);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ci = ci->get_parent_item();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (group)
|
|
|
|
group->_remove_button(this);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible()) {
|
2015-09-26 03:06:42 +02:00
|
|
|
|
|
|
|
if (!toggle_mode) {
|
|
|
|
status.pressed = false;
|
|
|
|
}
|
|
|
|
status.hovering = false;
|
|
|
|
status.press_attempt = false;
|
|
|
|
status.pressing_inside = false;
|
|
|
|
status.pressing_button = 0;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::pressed() {
|
|
|
|
|
|
|
|
if (get_script_instance())
|
|
|
|
get_script_instance()->call("pressed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::toggled(bool p_pressed) {
|
|
|
|
|
|
|
|
if (get_script_instance())
|
2017-03-19 00:36:26 +01:00
|
|
|
get_script_instance()->call("toggled", p_pressed);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::set_disabled(bool p_disabled) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
status.disabled = p_disabled;
|
|
|
|
update();
|
|
|
|
_change_notify("disabled");
|
|
|
|
if (p_disabled)
|
|
|
|
set_focus_mode(FOCUS_NONE);
|
|
|
|
else
|
2016-04-12 18:25:17 +02:00
|
|
|
set_focus_mode(enabled_focus_mode);
|
2015-03-02 01:07:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
bool BaseButton::is_disabled() const {
|
|
|
|
|
|
|
|
return status.disabled;
|
2015-03-02 01:07:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void BaseButton::set_pressed(bool p_pressed) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!toggle_mode)
|
|
|
|
return;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (status.pressed == p_pressed)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
_change_notify("pressed");
|
2017-03-19 00:36:26 +01:00
|
|
|
status.pressed = p_pressed;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool BaseButton::is_pressing() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return status.press_attempt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::is_pressed() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return toggle_mode ? status.pressed : status.press_attempt;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-06-30 03:41:02 +02:00
|
|
|
bool BaseButton::is_hovered() const {
|
|
|
|
|
|
|
|
return status.hovering;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
BaseButton::DrawMode BaseButton::get_draw_mode() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (status.disabled) {
|
|
|
|
return DRAW_DISABLED;
|
|
|
|
};
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//print_line("press attempt: "+itos(status.press_attempt)+" hover: "+itos(status.hovering)+" pressed: "+itos(status.pressed));
|
2017-03-19 00:36:26 +01:00
|
|
|
if (status.press_attempt == false && status.hovering && !status.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) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
pressing = status.pressing_inside;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (status.pressed)
|
2017-03-19 00:36:26 +01:00
|
|
|
pressing = !pressing;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
pressing = status.pressed;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
if (pressing)
|
2014-02-10 02:10:30 +01:00
|
|
|
return DRAW_PRESSED;
|
2016-03-09 00:00:52 +01:00
|
|
|
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) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
toggle_mode = p_on;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::is_toggle_mode() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return toggle_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::set_click_on_press(bool p_click_on_press) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
status.click_on_press = p_click_on_press;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseButton::get_click_on_press() const {
|
|
|
|
|
|
|
|
return status.click_on_press;
|
|
|
|
}
|
|
|
|
|
2016-04-12 18:25:17 +02:00
|
|
|
void BaseButton::set_enabled_focus_mode(FocusMode p_mode) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-04-12 18:25:17 +02:00
|
|
|
enabled_focus_mode = p_mode;
|
|
|
|
if (!status.disabled) {
|
2017-03-19 00:36:26 +01:00
|
|
|
set_focus_mode(p_mode);
|
2016-04-12 18:25:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Control::FocusMode BaseButton::get_enabled_focus_mode() const {
|
|
|
|
|
|
|
|
return enabled_focus_mode;
|
|
|
|
}
|
2014-11-02 15:31:01 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void BaseButton::set_shortcut(const Ref<ShortCut> &p_shortcut) {
|
2016-06-05 02:31:29 +02:00
|
|
|
|
|
|
|
if (shortcut.is_null() == p_shortcut.is_null())
|
|
|
|
return;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
shortcut = p_shortcut;
|
2016-06-05 02:31:29 +02:00
|
|
|
set_process_unhandled_input(shortcut.is_valid());
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Ref<ShortCut> BaseButton::get_shortcut() const {
|
2016-06-05 02:31:29 +02:00
|
|
|
return shortcut;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseButton::_unhandled_input(InputEvent p_event) {
|
|
|
|
|
2016-07-15 21:22:01 +02:00
|
|
|
if (!is_disabled() && is_visible() && p_event.is_pressed() && !p_event.is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) {
|
2016-06-28 01:14:59 +02:00
|
|
|
|
|
|
|
if (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this))
|
|
|
|
return; //ignore because of modal window
|
|
|
|
|
2016-06-05 02:31:29 +02:00
|
|
|
if (is_toggle_mode()) {
|
|
|
|
set_pressed(!is_pressed());
|
2017-03-19 00:36:26 +01:00
|
|
|
emit_signal("toggled", is_pressed());
|
2016-06-05 02:31:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_signal("pressed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String BaseButton::get_tooltip(const Point2 &p_pos) const {
|
2016-06-05 02:31:29 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String tooltip = Control::get_tooltip(p_pos);
|
2016-06-11 21:51:00 +02:00
|
|
|
if (shortcut.is_valid() && shortcut->is_valid()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
if (tooltip.find("$sc") != -1) {
|
|
|
|
tooltip = tooltip.replace_first("$sc", "(" + shortcut->get_as_text() + ")");
|
2016-06-11 21:51:00 +02:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
tooltip += " (" + shortcut->get_as_text() + ")";
|
2016-06-11 21:51:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 02:31:29 +02:00
|
|
|
return tooltip;
|
|
|
|
}
|
2014-11-02 15:31:01 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void BaseButton::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_input_event"), &BaseButton::_input_event);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_unhandled_input"), &BaseButton::_unhandled_input);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_pressed", "pressed"), &BaseButton::set_pressed);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_pressed"), &BaseButton::is_pressed);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_hovered"), &BaseButton::is_hovered);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_toggle_mode"), &BaseButton::is_toggle_mode);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_disabled", "disabled"), &BaseButton::set_disabled);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_disabled"), &BaseButton::is_disabled);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_click_on_press", "enable"), &BaseButton::set_click_on_press);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_click_on_press"), &BaseButton::get_click_on_press);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_draw_mode"), &BaseButton::get_draw_mode);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_shortcut"), &BaseButton::get_shortcut);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-12-02 21:26:56 +01:00
|
|
|
BIND_VMETHOD(MethodInfo("_pressed"));
|
2017-03-19 00:36:26 +01:00
|
|
|
BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "pressed")));
|
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("pressed"));
|
|
|
|
ADD_SIGNAL(MethodInfo("released"));
|
|
|
|
ADD_SIGNAL(MethodInfo("button_up"));
|
|
|
|
ADD_SIGNAL(MethodInfo("button_down"));
|
|
|
|
ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "pressed")));
|
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "disabled"), _SCS("set_disabled"), _SCS("is_disabled"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), _SCS("set_toggle_mode"), _SCS("is_toggle_mode"));
|
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "is_pressed"), _SCS("set_pressed"), _SCS("is_pressed"));
|
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "click_on_press"), _SCS("set_click_on_press"), _SCS("get_click_on_press"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), _SCS("set_enabled_focus_mode"), _SCS("get_enabled_focus_mode"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), _SCS("set_shortcut"), _SCS("get_shortcut"));
|
|
|
|
|
|
|
|
BIND_CONSTANT(DRAW_NORMAL);
|
|
|
|
BIND_CONSTANT(DRAW_PRESSED);
|
|
|
|
BIND_CONSTANT(DRAW_HOVER);
|
|
|
|
BIND_CONSTANT(DRAW_DISABLED);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BaseButton::BaseButton() {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
toggle_mode = false;
|
|
|
|
status.pressed = false;
|
|
|
|
status.press_attempt = false;
|
|
|
|
status.hovering = false;
|
|
|
|
status.pressing_inside = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
status.disabled = false;
|
2017-03-19 00:36:26 +01:00
|
|
|
status.click_on_press = false;
|
|
|
|
status.pressing_button = 0;
|
|
|
|
set_focus_mode(FOCUS_ALL);
|
2016-04-12 18:25:17 +02:00
|
|
|
enabled_focus_mode = FOCUS_ALL;
|
2017-03-19 00:36:26 +01:00
|
|
|
group = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
BaseButton::~BaseButton() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|