Path: blob/master/scene/2d/physics/touch_screen_button.cpp
9906 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 "scene/main/viewport.h"3334void TouchScreenButton::set_texture_normal(const Ref<Texture2D> &p_texture) {35if (texture_normal == p_texture) {36return;37}38if (texture_normal.is_valid()) {39texture_normal->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));40}41texture_normal = p_texture;42if (texture_normal.is_valid()) {43texture_normal->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);44}45queue_redraw();46}4748Ref<Texture2D> TouchScreenButton::get_texture_normal() const {49return texture_normal;50}5152void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) {53if (texture_pressed == p_texture_pressed) {54return;55}56if (texture_pressed.is_valid()) {57texture_pressed->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));58}59texture_pressed = p_texture_pressed;60if (texture_pressed.is_valid()) {61texture_pressed->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);62}63queue_redraw();64}6566Ref<Texture2D> TouchScreenButton::get_texture_pressed() const {67return texture_pressed;68}6970void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {71bitmask = p_bitmask;72}7374Ref<BitMap> TouchScreenButton::get_bitmask() const {75return bitmask;76}7778void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {79if (shape == p_shape) {80return;81}82if (shape.is_valid()) {83shape->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));84}85shape = p_shape;86if (shape.is_valid()) {87shape->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));88}89queue_redraw();90}9192Ref<Shape2D> TouchScreenButton::get_shape() const {93return shape;94}9596void TouchScreenButton::set_shape_centered(bool p_shape_centered) {97shape_centered = p_shape_centered;98queue_redraw();99}100101bool TouchScreenButton::is_shape_visible() const {102return shape_visible;103}104105void TouchScreenButton::set_shape_visible(bool p_shape_visible) {106shape_visible = p_shape_visible;107queue_redraw();108}109110bool TouchScreenButton::is_shape_centered() const {111return shape_centered;112}113114void TouchScreenButton::_accessibility_action_click(const Variant &p_data) {115_press(0);116_release();117}118119void TouchScreenButton::_notification(int p_what) {120switch (p_what) {121case NOTIFICATION_ACCESSIBILITY_UPDATE: {122RID ae = get_accessibility_element();123ERR_FAIL_COND(ae.is_null());124125Rect2 dst_rect(Point2(), texture_normal.is_valid() ? texture_normal->get_size() : Size2());126127DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);128129DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &TouchScreenButton::_accessibility_action_click));130131DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform());132DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, dst_rect);133} break;134135case NOTIFICATION_DRAW: {136if (!is_inside_tree()) {137return;138}139if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {140return;141}142143if (finger_pressed != -1) {144if (texture_pressed.is_valid()) {145draw_texture(texture_pressed, Point2());146} else if (texture_normal.is_valid()) {147draw_texture(texture_normal, Point2());148}149150} else {151if (texture_normal.is_valid()) {152draw_texture(texture_normal, Point2());153}154}155156if (!shape_visible) {157return;158}159if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {160return;161}162if (shape.is_valid()) {163Color draw_col = get_tree()->get_debug_collisions_color();164165Vector2 pos;166if (shape_centered && texture_normal.is_valid()) {167pos = texture_normal->get_size() * 0.5;168}169170draw_set_transform_matrix(get_canvas_transform().translated_local(pos));171shape->draw(get_canvas_item(), draw_col);172}173} break;174175case NOTIFICATION_ENTER_TREE: {176if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {177return;178}179queue_redraw();180181if (!Engine::get_singleton()->is_editor_hint()) {182set_process_input(is_visible_in_tree());183}184} break;185186case NOTIFICATION_EXIT_TREE: {187if (is_pressed()) {188_release(true);189}190} break;191192case NOTIFICATION_VISIBILITY_CHANGED: {193if (Engine::get_singleton()->is_editor_hint()) {194break;195}196if (is_visible_in_tree()) {197set_process_input(true);198} else {199set_process_input(false);200if (is_pressed()) {201_release();202}203}204} break;205206case NOTIFICATION_SUSPENDED:207case NOTIFICATION_PAUSED: {208if (is_pressed()) {209_release();210}211} break;212}213}214215bool TouchScreenButton::is_pressed() const {216return finger_pressed != -1;217}218219void TouchScreenButton::set_action(const String &p_action) {220action = p_action;221}222223String TouchScreenButton::get_action() const {224return action;225}226227void TouchScreenButton::input(const Ref<InputEvent> &p_event) {228ERR_FAIL_COND(p_event.is_null());229230if (!is_visible_in_tree()) {231return;232}233234const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);235236if (passby_press) {237const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);238239if (st && !st->is_pressed() && finger_pressed == st->get_index()) {240_release();241}242243if ((st && st->is_pressed()) || sd) {244int index = st ? st->get_index() : sd->get_index();245Point2 coord = st ? st->get_position() : sd->get_position();246247if (finger_pressed == -1 || index == finger_pressed) {248if (_is_point_inside(coord)) {249if (finger_pressed == -1) {250_press(index);251}252} else {253if (finger_pressed != -1) {254_release();255}256}257}258}259260} else {261if (st) {262if (st->is_pressed()) {263const bool can_press = finger_pressed == -1;264if (!can_press) {265return; //already fingering266}267268if (_is_point_inside(st->get_position())) {269_press(st->get_index());270}271} else {272if (st->get_index() == finger_pressed) {273_release();274}275}276}277}278}279280bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {281Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);282283bool touched = false;284bool check_rect = true;285286if (shape.is_valid()) {287check_rect = false;288289Vector2 pos;290if (shape_centered && texture_normal.is_valid()) {291pos = texture_normal->get_size() * 0.5;292}293294touched = shape->collide(Transform2D().translated_local(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));295}296297if (bitmask.is_valid()) {298check_rect = false;299if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {300if (bitmask->get_bitv(coord)) {301touched = true;302}303}304}305306if (!touched && check_rect) {307if (texture_normal.is_valid()) {308touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);309}310}311312return touched;313}314315void TouchScreenButton::_press(int p_finger_pressed) {316finger_pressed = p_finger_pressed;317318if (action != StringName()) {319Input::get_singleton()->action_press(action);320Ref<InputEventAction> iea;321iea.instantiate();322iea->set_action(action);323iea->set_pressed(true);324get_viewport()->push_input(iea, true);325}326327emit_signal(SceneStringName(pressed));328queue_redraw();329}330331void TouchScreenButton::_release(bool p_exiting_tree) {332finger_pressed = -1;333334if (action != StringName()) {335Input::get_singleton()->action_release(action);336if (!p_exiting_tree) {337Ref<InputEventAction> iea;338iea.instantiate();339iea->set_action(action);340iea->set_pressed(false);341get_viewport()->push_input(iea, true);342}343}344345if (!p_exiting_tree) {346emit_signal(SNAME("released"));347queue_redraw();348}349}350351#ifdef DEBUG_ENABLED352Rect2 TouchScreenButton::_edit_get_rect() const {353if (texture_normal.is_null()) {354return CanvasItem::_edit_get_rect();355}356357return Rect2(Size2(), texture_normal->get_size());358}359360bool TouchScreenButton::_edit_use_rect() const {361return texture_normal.is_valid();362}363#endif // DEBUG_ENABLED364365Rect2 TouchScreenButton::get_anchorable_rect() const {366if (texture_normal.is_null()) {367return CanvasItem::get_anchorable_rect();368}369370return Rect2(Size2(), texture_normal->get_size());371}372373void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {374visibility = p_mode;375queue_redraw();376}377378TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {379return visibility;380}381382void TouchScreenButton::set_passby_press(bool p_enable) {383passby_press = p_enable;384}385386bool TouchScreenButton::is_passby_press_enabled() const {387return passby_press;388}389390#ifndef DISABLE_DEPRECATED391bool TouchScreenButton::_set(const StringName &p_name, const Variant &p_value) {392if (p_name == CoreStringName(normal)) { // Compatibility with Godot 3.x.393set_texture_normal(p_value);394return true;395} else if (p_name == SceneStringName(pressed)) { // Compatibility with Godot 3.x.396set_texture_pressed(p_value);397return true;398}399return false;400}401#endif // DISABLE_DEPRECATED402403void TouchScreenButton::_bind_methods() {404ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);405ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);406407ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);408ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);409410ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);411ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);412413ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);414ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);415416ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);417ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);418419ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);420ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);421422ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);423ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);424425ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);426ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);427428ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);429ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);430431ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);432433ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");434ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");435ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask");436ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");437ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");438ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");439ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");440ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action", PROPERTY_HINT_INPUT_NAME, "show_builtin,loose_mode"), "set_action", "get_action");441ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");442443ADD_SIGNAL(MethodInfo("pressed"));444ADD_SIGNAL(MethodInfo("released"));445446BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);447BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);448}449450TouchScreenButton::TouchScreenButton() {451unit_rect.instantiate();452unit_rect->set_size(Vector2(1, 1));453}454455456