From 67a0da34a2d5a95761c54b3012d0a8f1a79e10a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Wed, 25 Jan 2017 17:06:06 +0100 Subject: [PATCH] Add shape property to TouchScreenButton --- scene/2d/screen_button.cpp | 70 ++++++++++++++++++++++++++++++++++++-- scene/2d/screen_button.h | 11 ++++++ 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/scene/2d/screen_button.cpp b/scene/2d/screen_button.cpp index 6485e465481..68c98907fa9 100644 --- a/scene/2d/screen_button.cpp +++ b/scene/2d/screen_button.cpp @@ -63,6 +63,38 @@ Ref TouchScreenButton::get_bitmask() const{ return bitmask; } +void TouchScreenButton::set_shape(const Ref& p_shape){ + + shape=p_shape; + + if (!is_inside_tree()) + return; + if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) + return; + update(); +} + +Ref TouchScreenButton::get_shape() const{ + + return shape; +} + +void TouchScreenButton::set_shape_centered(bool p_shape_centered) { + + shape_centered=p_shape_centered; + + if (!is_inside_tree()) + return; + if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) + return; + update(); +} + +bool TouchScreenButton::is_shape_centered() const { + + return shape_centered; +} + void TouchScreenButton::_notification(int p_what) { switch(p_what) { @@ -86,6 +118,15 @@ void TouchScreenButton::_notification(int p_what) { draw_texture(texture,Point2()); } + if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) + return; + if (shape.is_valid()) { + Color draw_col=get_tree()->get_debug_collisions_color(); + Vector2 pos=shape_centered ? get_item_rect().size*0.5f : Vector2(); + draw_set_transform_matrix(get_canvas_transform().translated(pos)); + shape->draw(get_canvas_item(),draw_col); + } + } break; case NOTIFICATION_ENTER_TREE: { @@ -243,18 +284,30 @@ void TouchScreenButton::_input(const InputEvent& p_event) { return; //already fingering Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(Point2(p_event.screen_touch.x,p_event.screen_touch.y)); + Rect2 item_rect = get_item_rect(); bool touched=false; + bool check_rect=true; + if (shape.is_valid()) { + + check_rect=false; + Matrix32 xform=shape_centered ? Matrix32().translated(get_item_rect().size*0.5f) : Matrix32(); + touched=shape->collide(xform, unit_rect, Matrix32(0, coord + Vector2(0.5,0.5))); + } + if (bitmask.is_valid()) { - if (Rect2(Point2(),bitmask->get_size()).has_point(coord)) { + check_rect=false; + if (!touched && Rect2(Point2(),bitmask->get_size()).has_point(coord)) { if (bitmask->get_bit(coord)) touched=true; } - } else { + } + + if (!touched && check_rect) { if (!texture.is_null()) - touched=Rect2(Point2(),texture->get_size()).has_point(coord); + touched=item_rect.has_point(coord); } @@ -347,6 +400,12 @@ void TouchScreenButton::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_bitmask","bitmask"),&TouchScreenButton::set_bitmask); ObjectTypeDB::bind_method(_MD("get_bitmask"),&TouchScreenButton::get_bitmask); + ObjectTypeDB::bind_method(_MD("set_shape","shape"),&TouchScreenButton::set_shape); + ObjectTypeDB::bind_method(_MD("get_shape"),&TouchScreenButton::get_shape); + + ObjectTypeDB::bind_method(_MD("set_shape_centered","bool"),&TouchScreenButton::set_shape_centered); + ObjectTypeDB::bind_method(_MD("is_shape_centered"),&TouchScreenButton::is_shape_centered); + ObjectTypeDB::bind_method(_MD("set_action","action"),&TouchScreenButton::set_action); ObjectTypeDB::bind_method(_MD("get_action"),&TouchScreenButton::get_action); @@ -363,6 +422,8 @@ void TouchScreenButton::_bind_methods() { ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture"),_SCS("get_texture")); ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture_pressed"),_SCS("get_texture_pressed")); ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"bitmask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"),_SCS("set_bitmask"),_SCS("get_bitmask")); + ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"shape",PROPERTY_HINT_RESOURCE_TYPE,"Shape2D"),_SCS("set_shape"),_SCS("get_shape")); + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"shape_centered"),_SCS("set_shape_centered"),_SCS("is_shape_centered")); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"passby_press"),_SCS("set_passby_press"),_SCS("is_passby_press_enabled")); ADD_PROPERTY( PropertyInfo(Variant::STRING,"action"),_SCS("set_action"),_SCS("get_action")); ADD_PROPERTY( PropertyInfo(Variant::INT,"visibility_mode",PROPERTY_HINT_ENUM,"Always,TouchScreen Only"),_SCS("set_visibility_mode"),_SCS("get_visibility_mode")); @@ -380,4 +441,7 @@ TouchScreenButton::TouchScreenButton() { action_id=-1; passby_press=false; visibility=VISIBILITY_ALWAYS; + shape_centered=true; + unit_rect=Ref(memnew(RectangleShape2D)); + unit_rect->set_extents(Vector2(0.5,0.5)); } diff --git a/scene/2d/screen_button.h b/scene/2d/screen_button.h index 72f590930a9..bf9ae54a331 100644 --- a/scene/2d/screen_button.h +++ b/scene/2d/screen_button.h @@ -32,6 +32,7 @@ #include "scene/2d/node_2d.h" #include "scene/resources/texture.h" #include "scene/resources/bit_mask.h" +#include "scene/resources/rectangle_shape_2d.h" class TouchScreenButton : public Node2D { @@ -47,6 +48,10 @@ private: Ref texture; Ref texture_pressed; Ref bitmask; + Ref shape; + bool shape_centered; + + Ref unit_rect; StringName action; bool passby_press; @@ -73,6 +78,12 @@ public: void set_bitmask(const Ref& p_bitmask); Ref get_bitmask() const; + void set_shape(const Ref& p_shape); + Ref get_shape() const; + + void set_shape_centered(bool p_shape_centered); + bool is_shape_centered() const; + void set_action(const String& p_action); String get_action() const;