Merge pull request #7641 from RandomShaper/touch-button-shape
Touch button shape (2.1)
This commit is contained in:
commit
4a73e74ccb
|
@ -63,6 +63,38 @@ Ref<BitMap> TouchScreenButton::get_bitmask() const{
|
||||||
return bitmask;
|
return bitmask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TouchScreenButton::set_shape(const Ref<Shape2D>& 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<Shape2D> 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) {
|
void TouchScreenButton::_notification(int p_what) {
|
||||||
|
|
||||||
switch(p_what) {
|
switch(p_what) {
|
||||||
|
@ -86,6 +118,15 @@ void TouchScreenButton::_notification(int p_what) {
|
||||||
draw_texture(texture,Point2());
|
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;
|
} break;
|
||||||
case NOTIFICATION_ENTER_TREE: {
|
case NOTIFICATION_ENTER_TREE: {
|
||||||
|
|
||||||
|
@ -243,18 +284,30 @@ void TouchScreenButton::_input(const InputEvent& p_event) {
|
||||||
return; //already fingering
|
return; //already fingering
|
||||||
|
|
||||||
Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(Point2(p_event.screen_touch.x,p_event.screen_touch.y));
|
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 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 (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))
|
if (bitmask->get_bit(coord))
|
||||||
touched=true;
|
touched=true;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
if (!touched && check_rect) {
|
||||||
if (!texture.is_null())
|
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("set_bitmask","bitmask"),&TouchScreenButton::set_bitmask);
|
||||||
ObjectTypeDB::bind_method(_MD("get_bitmask"),&TouchScreenButton::get_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("set_action","action"),&TouchScreenButton::set_action);
|
||||||
ObjectTypeDB::bind_method(_MD("get_action"),&TouchScreenButton::get_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,"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,"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,"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::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::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"));
|
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;
|
action_id=-1;
|
||||||
passby_press=false;
|
passby_press=false;
|
||||||
visibility=VISIBILITY_ALWAYS;
|
visibility=VISIBILITY_ALWAYS;
|
||||||
|
shape_centered=true;
|
||||||
|
unit_rect=Ref<RectangleShape2D>(memnew(RectangleShape2D));
|
||||||
|
unit_rect->set_extents(Vector2(0.5,0.5));
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
#include "scene/2d/node_2d.h"
|
#include "scene/2d/node_2d.h"
|
||||||
#include "scene/resources/texture.h"
|
#include "scene/resources/texture.h"
|
||||||
#include "scene/resources/bit_mask.h"
|
#include "scene/resources/bit_mask.h"
|
||||||
|
#include "scene/resources/rectangle_shape_2d.h"
|
||||||
|
|
||||||
class TouchScreenButton : public Node2D {
|
class TouchScreenButton : public Node2D {
|
||||||
|
|
||||||
|
@ -47,6 +48,10 @@ private:
|
||||||
Ref<Texture> texture;
|
Ref<Texture> texture;
|
||||||
Ref<Texture> texture_pressed;
|
Ref<Texture> texture_pressed;
|
||||||
Ref<BitMap> bitmask;
|
Ref<BitMap> bitmask;
|
||||||
|
Ref<Shape2D> shape;
|
||||||
|
bool shape_centered;
|
||||||
|
|
||||||
|
Ref<RectangleShape2D> unit_rect;
|
||||||
|
|
||||||
StringName action;
|
StringName action;
|
||||||
bool passby_press;
|
bool passby_press;
|
||||||
|
@ -73,6 +78,12 @@ public:
|
||||||
void set_bitmask(const Ref<BitMap>& p_bitmask);
|
void set_bitmask(const Ref<BitMap>& p_bitmask);
|
||||||
Ref<BitMap> get_bitmask() const;
|
Ref<BitMap> get_bitmask() const;
|
||||||
|
|
||||||
|
void set_shape(const Ref<Shape2D>& p_shape);
|
||||||
|
Ref<Shape2D> get_shape() const;
|
||||||
|
|
||||||
|
void set_shape_centered(bool p_shape_centered);
|
||||||
|
bool is_shape_centered() const;
|
||||||
|
|
||||||
void set_action(const String& p_action);
|
void set_action(const String& p_action);
|
||||||
String get_action() const;
|
String get_action() const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue