diff options
Diffstat (limited to 'scene/2d/touch_screen_button.cpp')
-rw-r--r-- | scene/2d/touch_screen_button.cpp | 158 |
1 files changed, 69 insertions, 89 deletions
diff --git a/scene/2d/touch_screen_button.cpp b/scene/2d/touch_screen_button.cpp index 2cb979a0e0..8bd7b696f2 100644 --- a/scene/2d/touch_screen_button.cpp +++ b/scene/2d/touch_screen_button.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,195 +30,188 @@ #include "touch_screen_button.h" -#include "core/input/input_filter.h" -#include "core/input/input_map.h" -#include "core/os/os.h" #include "scene/main/window.h" -# -void TouchScreenButton::set_texture(const Ref<Texture2D> &p_texture) { +void TouchScreenButton::set_texture(const Ref<Texture2D> &p_texture) { texture = p_texture; update(); } Ref<Texture2D> TouchScreenButton::get_texture() const { - return texture; } void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) { - texture_pressed = p_texture_pressed; update(); } Ref<Texture2D> TouchScreenButton::get_texture_pressed() const { - return texture_pressed; } void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) { - bitmask = p_bitmask; } Ref<BitMap> TouchScreenButton::get_bitmask() const { - return bitmask; } void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) { - - if (shape.is_valid()) + if (shape.is_valid()) { shape->disconnect("changed", callable_mp((CanvasItem *)this, &CanvasItem::update)); + } shape = p_shape; - if (shape.is_valid()) + if (shape.is_valid()) { shape->connect("changed", callable_mp((CanvasItem *)this, &CanvasItem::update)); + } update(); } Ref<Shape2D> TouchScreenButton::get_shape() const { - return shape; } void TouchScreenButton::set_shape_centered(bool p_shape_centered) { - shape_centered = p_shape_centered; update(); } bool TouchScreenButton::is_shape_visible() const { - return shape_visible; } void TouchScreenButton::set_shape_visible(bool p_shape_visible) { - shape_visible = p_shape_visible; update(); } bool TouchScreenButton::is_shape_centered() const { - return shape_centered; } void TouchScreenButton::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_DRAW: { - - if (!is_inside_tree()) + if (!is_inside_tree()) { return; - 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) + } + 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) { return; + } if (finger_pressed != -1) { - - if (texture_pressed.is_valid()) + if (texture_pressed.is_valid()) { draw_texture(texture_pressed, Point2()); - else if (texture.is_valid()) + } else if (texture.is_valid()) { draw_texture(texture, Point2()); + } } else { - if (texture.is_valid()) + if (texture.is_valid()) { draw_texture(texture, Point2()); + } } - if (!shape_visible) + if (!shape_visible) { return; - if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) + } + if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) { return; + } if (shape.is_valid()) { Color draw_col = get_tree()->get_debug_collisions_color(); - Vector2 size = texture.is_null() ? shape->get_rect().size : texture->get_size(); - Vector2 pos = shape_centered ? size * 0.5f : Vector2(); + Vector2 pos; + if (shape_centered && texture.is_valid()) { + pos = texture->get_size() * 0.5; + } + draw_set_transform_matrix(get_canvas_transform().translated(pos)); shape->draw(get_canvas_item(), draw_col); } } break; case NOTIFICATION_ENTER_TREE: { - - 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) + 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) { return; + } update(); - if (!Engine::get_singleton()->is_editor_hint()) + if (!Engine::get_singleton()->is_editor_hint()) { set_process_input(is_visible_in_tree()); + } } break; case NOTIFICATION_EXIT_TREE: { - if (is_pressed()) + if (is_pressed()) { _release(true); + } } break; case NOTIFICATION_VISIBILITY_CHANGED: { - if (Engine::get_singleton()->is_editor_hint()) + if (Engine::get_singleton()->is_editor_hint()) { break; + } if (is_visible_in_tree()) { set_process_input(true); } else { set_process_input(false); - if (is_pressed()) + if (is_pressed()) { _release(); + } } } break; case NOTIFICATION_PAUSED: { - if (is_pressed()) + if (is_pressed()) { _release(); + } } break; } } bool TouchScreenButton::is_pressed() const { - return finger_pressed != -1; } void TouchScreenButton::set_action(const String &p_action) { - action = p_action; } String TouchScreenButton::get_action() const { - return action; } -void TouchScreenButton::_input(const Ref<InputEvent> &p_event) { +void TouchScreenButton::input(const Ref<InputEvent> &p_event) { + ERR_FAIL_COND(p_event.is_null()); - if (!get_tree()) + if (!get_tree()) { return; + } - if (p_event->get_device() != 0) + if (p_event->get_device() != 0) { return; + } ERR_FAIL_COND(!is_visible_in_tree()); const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event); if (passby_press) { - const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event); if (st && !st->is_pressed() && finger_pressed == st->get_index()) { - _release(); } if ((st && st->is_pressed()) || sd) { - int index = st ? st->get_index() : sd->get_index(); Point2 coord = st ? st->get_position() : sd->get_position(); if (finger_pressed == -1 || index == finger_pressed) { - if (_is_point_inside(coord)) { if (finger_pressed == -1) { _press(index); @@ -232,14 +225,12 @@ void TouchScreenButton::_input(const Ref<InputEvent> &p_event) { } } else { - if (st) { - if (st->is_pressed()) { - const bool can_press = finger_pressed == -1; - if (!can_press) + if (!can_press) { return; //already fingering + } if (_is_point_inside(st->get_position())) { _press(st->get_index()); @@ -260,75 +251,75 @@ bool TouchScreenButton::_is_point_inside(const Point2 &p_point) { bool check_rect = true; if (shape.is_valid()) { - check_rect = false; - Transform2D xform = shape_centered ? Transform2D().translated(shape->get_rect().size * 0.5f) : Transform2D(); - touched = shape->collide(xform, unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5))); + + Vector2 pos; + if (shape_centered && texture.is_valid()) { + pos = texture->get_size() * 0.5; + } + + touched = shape->collide(Transform2D().translated(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5))); } if (bitmask.is_valid()) { - check_rect = false; if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) { - - if (bitmask->get_bit(coord)) + if (bitmask->get_bit(coord)) { touched = true; + } } } if (!touched && check_rect) { - if (texture.is_valid()) + if (texture.is_valid()) { touched = Rect2(Size2(), texture->get_size()).has_point(coord); + } } return touched; } void TouchScreenButton::_press(int p_finger_pressed) { - finger_pressed = p_finger_pressed; if (action != StringName()) { - - InputFilter::get_singleton()->action_press(action); + Input::get_singleton()->action_press(action); Ref<InputEventAction> iea; - iea.instance(); + iea.instantiate(); iea->set_action(action); iea->set_pressed(true); - get_viewport()->input(iea, true); + get_viewport()->push_input(iea, true); } - emit_signal("pressed"); + emit_signal(SNAME("pressed")); update(); } void TouchScreenButton::_release(bool p_exiting_tree) { - finger_pressed = -1; if (action != StringName()) { - - InputFilter::get_singleton()->action_release(action); + Input::get_singleton()->action_release(action); if (!p_exiting_tree) { - Ref<InputEventAction> iea; - iea.instance(); + iea.instantiate(); iea->set_action(action); iea->set_pressed(false); - get_viewport()->input(iea, true); + get_viewport()->push_input(iea, true); } } if (!p_exiting_tree) { - emit_signal("released"); + emit_signal(SNAME("released")); update(); } } #ifdef TOOLS_ENABLED Rect2 TouchScreenButton::_edit_get_rect() const { - if (texture.is_null()) + if (texture.is_null()) { return CanvasItem::_edit_get_rect(); + } return Rect2(Size2(), texture->get_size()); } @@ -339,8 +330,9 @@ bool TouchScreenButton::_edit_use_rect() const { #endif Rect2 TouchScreenButton::get_anchorable_rect() const { - if (texture.is_null()) + if (texture.is_null()) { return CanvasItem::get_anchorable_rect(); + } return Rect2(Size2(), texture->get_size()); } @@ -351,22 +343,18 @@ void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) { } TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const { - return visibility; } void TouchScreenButton::set_passby_press(bool p_enable) { - passby_press = p_enable; } bool TouchScreenButton::is_passby_press_enabled() const { - return passby_press; } void TouchScreenButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TouchScreenButton::set_texture); ClassDB::bind_method(D_METHOD("get_texture"), &TouchScreenButton::get_texture); @@ -396,8 +384,6 @@ void TouchScreenButton::_bind_methods() { ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed); - ClassDB::bind_method(D_METHOD("_input"), &TouchScreenButton::_input); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask"); @@ -416,12 +402,6 @@ void TouchScreenButton::_bind_methods() { } TouchScreenButton::TouchScreenButton() { - - finger_pressed = -1; - passby_press = false; - visibility = VISIBILITY_ALWAYS; - shape_centered = true; - shape_visible = true; unit_rect = Ref<RectangleShape2D>(memnew(RectangleShape2D)); - unit_rect->set_extents(Vector2(0.5, 0.5)); + unit_rect->set_size(Vector2(1, 1)); } |