2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-02-12 17:18:13 +01:00
|
|
|
/* touch_screen_button.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2019-02-12 17:18:13 +01:00
|
|
|
#include "touch_screen_button.h"
|
|
|
|
|
2020-03-04 02:51:12 +01:00
|
|
|
#include "scene/main/window.h"
|
2021-08-13 01:05:59 +02:00
|
|
|
|
2021-12-06 15:46:05 +01:00
|
|
|
void TouchScreenButton::set_texture_normal(const Ref<Texture2D> &p_texture) {
|
|
|
|
texture_normal = p_texture;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-12-06 15:46:05 +01:00
|
|
|
Ref<Texture2D> TouchScreenButton::get_texture_normal() const {
|
|
|
|
return texture_normal;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) {
|
2017-03-05 16:44:50 +01:00
|
|
|
texture_pressed = p_texture_pressed;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Texture2D> TouchScreenButton::get_texture_pressed() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return texture_pressed;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {
|
|
|
|
bitmask = p_bitmask;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<BitMap> TouchScreenButton::get_bitmask() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return bitmask;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (shape.is_valid()) {
|
2020-02-21 23:26:13 +01:00
|
|
|
shape->disconnect("changed", callable_mp((CanvasItem *)this, &CanvasItem::update));
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-03 14:23:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
shape = p_shape;
|
2017-01-25 17:06:06 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (shape.is_valid()) {
|
2020-02-21 23:26:13 +01:00
|
|
|
shape->connect("changed", callable_mp((CanvasItem *)this, &CanvasItem::update));
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-03 14:23:52 +02:00
|
|
|
|
2017-01-25 17:06:06 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<Shape2D> TouchScreenButton::get_shape() const {
|
2017-01-25 17:06:06 +01:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreenButton::set_shape_centered(bool p_shape_centered) {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_centered = p_shape_centered;
|
2017-04-03 14:23:52 +02:00
|
|
|
update();
|
|
|
|
}
|
2017-01-25 17:06:06 +01:00
|
|
|
|
2017-04-03 14:23:52 +02:00
|
|
|
bool TouchScreenButton::is_shape_visible() const {
|
|
|
|
return shape_visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreenButton::set_shape_visible(bool p_shape_visible) {
|
|
|
|
shape_visible = p_shape_visible;
|
2017-01-25 17:06:06 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TouchScreenButton::is_shape_centered() const {
|
|
|
|
return shape_centered;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void TouchScreenButton::_notification(int p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_DRAW: {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id())) && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (finger_pressed != -1) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (texture_pressed.is_valid()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
draw_texture(texture_pressed, Point2());
|
2021-12-06 15:46:05 +01:00
|
|
|
} else if (texture_normal.is_valid()) {
|
|
|
|
draw_texture(texture_normal, Point2());
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
2021-12-06 15:46:05 +01:00
|
|
|
if (texture_normal.is_valid()) {
|
|
|
|
draw_texture(texture_normal, Point2());
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!shape_visible) {
|
2017-04-03 14:23:52 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
|
2017-01-25 17:06:06 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-25 17:06:06 +01:00
|
|
|
if (shape.is_valid()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Color draw_col = get_tree()->get_debug_collisions_color();
|
2019-10-21 23:37:07 +02:00
|
|
|
|
2020-12-27 21:02:01 +01:00
|
|
|
Vector2 pos;
|
2021-12-06 15:46:05 +01:00
|
|
|
if (shape_centered && texture_normal.is_valid()) {
|
|
|
|
pos = texture_normal->get_size() * 0.5;
|
2020-12-27 21:02:01 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 17:06:06 +01:00
|
|
|
draw_set_transform_matrix(get_canvas_transform().translated(pos));
|
2017-03-05 16:44:50 +01:00
|
|
|
shape->draw(get_canvas_item(), draw_col);
|
2017-01-25 17:06:06 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id())) && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
2014-07-13 02:48:02 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
2017-03-09 01:41:28 +01:00
|
|
|
set_process_input(is_visible_in_tree());
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2015-12-27 23:06:30 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_pressed()) {
|
2017-02-04 16:57:11 +01:00
|
|
|
_release(true);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-12-27 23:06:30 +01:00
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2017-03-09 01:41:28 +01:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2017-03-09 01:41:28 +01:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-09 01:41:28 +01:00
|
|
|
if (is_visible_in_tree()) {
|
|
|
|
set_process_input(true);
|
|
|
|
} else {
|
|
|
|
set_process_input(false);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_pressed()) {
|
2017-03-09 01:41:28 +01:00
|
|
|
_release();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-09 01:41:28 +01:00
|
|
|
}
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2017-02-04 16:57:11 +01:00
|
|
|
case NOTIFICATION_PAUSED: {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_pressed()) {
|
2017-03-09 01:41:28 +01:00
|
|
|
_release();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-09 01:41:28 +01:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool TouchScreenButton::is_pressed() const {
|
|
|
|
return finger_pressed != -1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TouchScreenButton::set_action(const String &p_action) {
|
|
|
|
action = p_action;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String TouchScreenButton::get_action() const {
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:37:22 +02:00
|
|
|
void TouchScreenButton::input(const Ref<InputEvent> &p_event) {
|
2021-04-05 08:52:21 +02:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!get_tree()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_event->get_device() != 0) {
|
2014-08-14 15:31:38 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-06-14 17:08:35 +02:00
|
|
|
ERR_FAIL_COND(!is_visible_in_tree());
|
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);
|
2017-06-14 17:08:35 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (passby_press) {
|
2017-08-24 22:58:51 +02:00
|
|
|
const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);
|
2017-05-20 17:38:03 +02:00
|
|
|
|
2017-06-14 17:08:35 +02:00
|
|
|
if (st && !st->is_pressed() && finger_pressed == st->get_index()) {
|
2017-02-04 16:57:11 +01:00
|
|
|
_release();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-06-14 17:08:35 +02:00
|
|
|
if ((st && st->is_pressed()) || sd) {
|
|
|
|
int index = st ? st->get_index() : sd->get_index();
|
|
|
|
Point2 coord = st ? st->get_position() : sd->get_position();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (finger_pressed == -1 || index == finger_pressed) {
|
2017-06-14 17:08:35 +02:00
|
|
|
if (_is_point_inside(coord)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (finger_pressed == -1) {
|
2017-05-20 17:38:03 +02:00
|
|
|
_press(index);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (finger_pressed != -1) {
|
2017-02-04 16:57:11 +01:00
|
|
|
_release();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-06-14 17:08:35 +02:00
|
|
|
if (st) {
|
2017-05-20 17:38:03 +02:00
|
|
|
if (st->is_pressed()) {
|
2017-03-09 01:41:28 +01:00
|
|
|
const bool can_press = finger_pressed == -1;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!can_press) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return; //already fingering
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-06-14 17:08:35 +02:00
|
|
|
if (_is_point_inside(st->get_position())) {
|
2017-05-20 17:38:03 +02:00
|
|
|
_press(st->get_index());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-20 17:38:03 +02:00
|
|
|
if (st->get_index() == finger_pressed) {
|
2017-02-04 16:57:11 +01:00
|
|
|
_release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-06-14 17:08:35 +02:00
|
|
|
bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
|
|
|
|
Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);
|
|
|
|
|
|
|
|
bool touched = false;
|
|
|
|
bool check_rect = true;
|
|
|
|
|
|
|
|
if (shape.is_valid()) {
|
|
|
|
check_rect = false;
|
2020-05-07 16:04:59 +02:00
|
|
|
|
2020-12-27 21:02:01 +01:00
|
|
|
Vector2 pos;
|
2021-12-06 15:46:05 +01:00
|
|
|
if (shape_centered && texture_normal.is_valid()) {
|
|
|
|
pos = texture_normal->get_size() * 0.5;
|
2020-12-27 21:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
touched = shape->collide(Transform2D().translated(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
|
2017-06-14 17:08:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bitmask.is_valid()) {
|
|
|
|
check_rect = false;
|
|
|
|
if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (bitmask->get_bit(coord)) {
|
2017-06-14 17:08:35 +02:00
|
|
|
touched = true;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-06-14 17:08:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!touched && check_rect) {
|
2021-12-06 15:46:05 +01:00
|
|
|
if (texture_normal.is_valid()) {
|
|
|
|
touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-06-14 17:08:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return touched;
|
|
|
|
}
|
|
|
|
|
2017-02-04 16:57:11 +01:00
|
|
|
void TouchScreenButton::_press(int p_finger_pressed) {
|
2017-03-05 16:44:50 +01:00
|
|
|
finger_pressed = p_finger_pressed;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (action != StringName()) {
|
2020-04-28 15:19:37 +02:00
|
|
|
Input::get_singleton()->action_press(action);
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventAction> iea;
|
2021-06-18 00:03:09 +02:00
|
|
|
iea.instantiate();
|
2017-05-20 17:38:03 +02:00
|
|
|
iea->set_action(action);
|
|
|
|
iea->set_pressed(true);
|
2021-08-22 17:37:22 +02:00
|
|
|
get_viewport()->push_input(iea, true);
|
2017-02-04 16:57:11 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("pressed"));
|
2017-02-04 16:57:11 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreenButton::_release(bool p_exiting_tree) {
|
2017-03-05 16:44:50 +01:00
|
|
|
finger_pressed = -1;
|
2017-02-04 16:57:11 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (action != StringName()) {
|
2020-04-28 15:19:37 +02:00
|
|
|
Input::get_singleton()->action_release(action);
|
2017-02-22 01:36:31 +01:00
|
|
|
if (!p_exiting_tree) {
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventAction> iea;
|
2021-06-18 00:03:09 +02:00
|
|
|
iea.instantiate();
|
2017-05-20 17:38:03 +02:00
|
|
|
iea->set_action(action);
|
|
|
|
iea->set_pressed(false);
|
2021-08-22 17:37:22 +02:00
|
|
|
get_viewport()->push_input(iea, true);
|
2017-02-22 01:36:31 +01:00
|
|
|
}
|
2017-02-04 16:57:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!p_exiting_tree) {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("released"));
|
2017-02-04 16:57:11 +01:00
|
|
|
update();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 23:37:07 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-11-15 23:03:25 +01:00
|
|
|
Rect2 TouchScreenButton::_edit_get_rect() const {
|
2021-12-06 15:46:05 +01:00
|
|
|
if (texture_normal.is_null()) {
|
2019-11-08 10:54:36 +01:00
|
|
|
return CanvasItem::_edit_get_rect();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-12-06 15:46:05 +01:00
|
|
|
return Rect2(Size2(), texture_normal->get_size());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 21:35:41 +01:00
|
|
|
bool TouchScreenButton::_edit_use_rect() const {
|
2021-12-06 15:46:05 +01:00
|
|
|
return !texture_normal.is_null();
|
2018-05-05 16:59:00 +02:00
|
|
|
}
|
2019-10-21 23:37:07 +02:00
|
|
|
#endif
|
2018-05-05 16:59:00 +02:00
|
|
|
|
|
|
|
Rect2 TouchScreenButton::get_anchorable_rect() const {
|
2021-12-06 15:46:05 +01:00
|
|
|
if (texture_normal.is_null()) {
|
2018-05-05 16:59:00 +02:00
|
|
|
return CanvasItem::get_anchorable_rect();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-05 16:59:00 +02:00
|
|
|
|
2021-12-06 15:46:05 +01:00
|
|
|
return Rect2(Size2(), texture_normal->get_size());
|
2018-03-08 21:35:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
|
2017-03-05 16:44:50 +01:00
|
|
|
visibility = p_mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
|
|
|
|
return visibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreenButton::set_passby_press(bool p_enable) {
|
2017-03-05 16:44:50 +01:00
|
|
|
passby_press = p_enable;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool TouchScreenButton::is_passby_press_enabled() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return passby_press;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreenButton::_bind_methods() {
|
2021-12-06 15:46:05 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);
|
2017-01-25 17:06:06 +01:00
|
|
|
|
2021-12-06 15:46:05 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
|
2017-01-25 17:06:06 +01:00
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-04-03 14:23:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-12-06 15:46:05 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");
|
2017-04-03 14:23:52 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");
|
2020-02-20 22:58:05 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action"), "set_action", "get_action");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("pressed"));
|
|
|
|
ADD_SIGNAL(MethodInfo("released"));
|
2017-09-12 21:09:06 +02:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);
|
|
|
|
BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TouchScreenButton::TouchScreenButton() {
|
2017-03-05 16:44:50 +01:00
|
|
|
unit_rect = Ref<RectangleShape2D>(memnew(RectangleShape2D));
|
2020-12-07 20:58:47 +01:00
|
|
|
unit_rect->set_size(Vector2(1, 1));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|