Path: blob/master/scene/2d/physics/touch_screen_button.cpp
20851 views
/**************************************************************************/1/* touch_screen_button.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "touch_screen_button.h"3132#include "core/input/input.h"33#include "scene/main/viewport.h"3435void TouchScreenButton::set_texture_normal(const Ref<Texture2D> &p_texture) {36if (texture_normal == p_texture) {37return;38}39if (texture_normal.is_valid()) {40texture_normal->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));41}42texture_normal = p_texture;43if (texture_normal.is_valid()) {44texture_normal->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);45}46queue_redraw();47}4849Ref<Texture2D> TouchScreenButton::get_texture_normal() const {50return texture_normal;51}5253void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) {54if (texture_pressed == p_texture_pressed) {55return;56}57if (texture_pressed.is_valid()) {58texture_pressed->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));59}60texture_pressed = p_texture_pressed;61if (texture_pressed.is_valid()) {62texture_pressed->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);63}64queue_redraw();65}6667Ref<Texture2D> TouchScreenButton::get_texture_pressed() const {68return texture_pressed;69}7071void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {72bitmask = p_bitmask;73}7475Ref<BitMap> TouchScreenButton::get_bitmask() const {76return bitmask;77}7879void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {80if (shape == p_shape) {81return;82}83if (shape.is_valid()) {84shape->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));85}86shape = p_shape;87if (shape.is_valid()) {88shape->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));89}90queue_redraw();91}9293Ref<Shape2D> TouchScreenButton::get_shape() const {94return shape;95}9697void TouchScreenButton::set_shape_centered(bool p_shape_centered) {98shape_centered = p_shape_centered;99queue_redraw();100}101102bool TouchScreenButton::is_shape_visible() const {103return shape_visible;104}105106void TouchScreenButton::set_shape_visible(bool p_shape_visible) {107shape_visible = p_shape_visible;108queue_redraw();109}110111bool TouchScreenButton::is_shape_centered() const {112return shape_centered;113}114115void TouchScreenButton::_accessibility_action_click(const Variant &p_data) {116_press(0);117_release();118}119120void TouchScreenButton::_notification(int p_what) {121switch (p_what) {122case NOTIFICATION_ACCESSIBILITY_UPDATE: {123RID ae = get_accessibility_element();124ERR_FAIL_COND(ae.is_null());125126Rect2 dst_rect(Point2(), texture_normal.is_valid() ? texture_normal->get_size() : Size2());127128DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);129130DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &TouchScreenButton::_accessibility_action_click));131132DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform());133DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, dst_rect);134} break;135136case NOTIFICATION_DRAW: {137if (!is_inside_tree()) {138return;139}140if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {141return;142}143144if (finger_pressed != -1) {145if (texture_pressed.is_valid()) {146draw_texture(texture_pressed, Point2());147} else if (texture_normal.is_valid()) {148draw_texture(texture_normal, Point2());149}150151} else {152if (texture_normal.is_valid()) {153draw_texture(texture_normal, Point2());154}155}156157if (!shape_visible) {158return;159}160if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {161return;162}163if (shape.is_valid()) {164Color draw_col = get_tree()->get_debug_collisions_color();165166Vector2 pos;167if (shape_centered && texture_normal.is_valid()) {168pos = texture_normal->get_size() * 0.5;169}170171draw_set_transform_matrix(get_canvas_transform().translated_local(pos));172shape->draw(get_canvas_item(), draw_col);173}174} break;175176case NOTIFICATION_ENTER_TREE: {177if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {178return;179}180queue_redraw();181182if (!Engine::get_singleton()->is_editor_hint()) {183set_process_input(is_visible_in_tree());184}185} break;186187case NOTIFICATION_EXIT_TREE: {188if (is_pressed()) {189_release(true);190}191} break;192193case NOTIFICATION_VISIBILITY_CHANGED: {194if (Engine::get_singleton()->is_editor_hint()) {195break;196}197if (is_visible_in_tree()) {198set_process_input(true);199} else {200set_process_input(false);201if (is_pressed()) {202_release();203}204}205} break;206207case NOTIFICATION_SUSPENDED:208case NOTIFICATION_PAUSED: {209if (is_pressed()) {210_release();211}212} break;213}214}215216bool TouchScreenButton::is_pressed() const {217return finger_pressed != -1;218}219220void TouchScreenButton::set_action(const String &p_action) {221action = p_action;222}223224String TouchScreenButton::get_action() const {225return action;226}227228void TouchScreenButton::input(const Ref<InputEvent> &p_event) {229ERR_FAIL_COND(p_event.is_null());230231if (!is_visible_in_tree()) {232return;233}234235const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);236237if (passby_press) {238const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);239240if (st && !st->is_pressed() && finger_pressed == st->get_index()) {241_release();242}243244if ((st && st->is_pressed()) || sd) {245int index = st ? st->get_index() : sd->get_index();246Point2 coord = st ? st->get_position() : sd->get_position();247248if (finger_pressed == -1 || index == finger_pressed) {249if (_is_point_inside(coord)) {250if (finger_pressed == -1) {251_press(index);252}253} else {254if (finger_pressed != -1) {255_release();256}257}258}259}260261} else {262if (st) {263if (st->is_pressed()) {264const bool can_press = finger_pressed == -1;265if (!can_press) {266return; //already fingering267}268269if (_is_point_inside(st->get_position())) {270_press(st->get_index());271}272} else {273if (st->get_index() == finger_pressed) {274_release();275}276}277}278}279}280281bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {282Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);283284bool touched = false;285bool check_rect = true;286287if (shape.is_valid()) {288check_rect = false;289290Vector2 pos;291if (shape_centered && texture_normal.is_valid()) {292pos = texture_normal->get_size() * 0.5;293}294295touched = shape->collide(Transform2D().translated_local(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));296}297298if (bitmask.is_valid()) {299check_rect = false;300if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {301if (bitmask->get_bitv(coord)) {302touched = true;303}304}305}306307if (!touched && check_rect) {308if (texture_normal.is_valid()) {309touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);310}311}312313return touched;314}315316void TouchScreenButton::_press(int p_finger_pressed) {317finger_pressed = p_finger_pressed;318319if (action != StringName()) {320Input::get_singleton()->action_press(action);321Ref<InputEventAction> iea;322iea.instantiate();323iea->set_action(action);324iea->set_pressed(true);325get_viewport()->push_input(iea, true);326}327328emit_signal(SceneStringName(pressed));329queue_redraw();330}331332void TouchScreenButton::_release(bool p_exiting_tree) {333finger_pressed = -1;334335if (action != StringName()) {336Input::get_singleton()->action_release(action);337if (!p_exiting_tree) {338Ref<InputEventAction> iea;339iea.instantiate();340iea->set_action(action);341iea->set_pressed(false);342get_viewport()->push_input(iea, true);343}344}345346if (!p_exiting_tree) {347emit_signal(SNAME("released"));348queue_redraw();349}350}351352#ifdef DEBUG_ENABLED353Rect2 TouchScreenButton::_edit_get_rect() const {354if (texture_normal.is_null()) {355return CanvasItem::_edit_get_rect();356}357358return Rect2(Size2(), texture_normal->get_size());359}360361bool TouchScreenButton::_edit_use_rect() const {362return texture_normal.is_valid();363}364#endif // DEBUG_ENABLED365366Rect2 TouchScreenButton::get_anchorable_rect() const {367if (texture_normal.is_null()) {368return CanvasItem::get_anchorable_rect();369}370371return Rect2(Size2(), texture_normal->get_size());372}373374void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {375visibility = p_mode;376queue_redraw();377}378379TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {380return visibility;381}382383void TouchScreenButton::set_passby_press(bool p_enable) {384passby_press = p_enable;385}386387bool TouchScreenButton::is_passby_press_enabled() const {388return passby_press;389}390391#ifndef DISABLE_DEPRECATED392bool TouchScreenButton::_set(const StringName &p_name, const Variant &p_value) {393if (p_name == CoreStringName(normal)) { // Compatibility with Godot 3.x.394set_texture_normal(p_value);395return true;396} else if (p_name == SceneStringName(pressed)) { // Compatibility with Godot 3.x.397set_texture_pressed(p_value);398return true;399}400return false;401}402#endif // DISABLE_DEPRECATED403404void TouchScreenButton::_bind_methods() {405ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);406ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);407408ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);409ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);410411ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);412ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);413414ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);415ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);416417ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);418ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);419420ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);421ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);422423ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);424ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);425426ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);427ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);428429ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);430ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);431432ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);433434ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static()), "set_texture_normal", "get_texture_normal");435ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static()), "set_texture_pressed", "get_texture_pressed");436ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, BitMap::get_class_static()), "set_bitmask", "get_bitmask");437ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, Shape2D::get_class_static()), "set_shape", "get_shape");438ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");439ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");440ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");441ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action", PROPERTY_HINT_INPUT_NAME, "show_builtin,loose_mode"), "set_action", "get_action");442ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");443444ADD_SIGNAL(MethodInfo("pressed"));445ADD_SIGNAL(MethodInfo("released"));446447BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);448BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);449}450451TouchScreenButton::TouchScreenButton() {452unit_rect.instantiate();453unit_rect->set_size(Vector2(1, 1));454}455456457