Merge pull request #55665 from KoBeWi/you_touched_my_TouchScreeenButton

This commit is contained in:
Rémi Verschelde 2022-01-13 12:23:49 +01:00 committed by GitHub
commit e0f2902a11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 33 deletions

View file

@ -25,16 +25,10 @@
<member name="bitmask" type="BitMap" setter="set_bitmask" getter="get_bitmask">
The button's bitmask.
</member>
<member name="normal" type="Texture2D" setter="set_texture" getter="get_texture">
The button's texture for the normal state.
</member>
<member name="passby_press" type="bool" setter="set_passby_press" getter="is_passby_press_enabled" default="false">
If [code]true[/code], the [signal pressed] and [signal released] signals are emitted whenever a pressed finger goes in and out of the button, even if the pressure started outside the active area of the button.
[b]Note:[/b] This is a "pass-by" (not "bypass") press mode.
</member>
<member name="pressed" type="Texture2D" setter="set_texture_pressed" getter="get_texture_pressed">
The button's texture for the pressed state.
</member>
<member name="shape" type="Shape2D" setter="set_shape" getter="get_shape">
The button's shape.
</member>
@ -44,6 +38,12 @@
<member name="shape_visible" type="bool" setter="set_shape_visible" getter="is_shape_visible" default="true">
If [code]true[/code], the button's shape is visible in the editor.
</member>
<member name="texture_normal" type="Texture2D" setter="set_texture_normal" getter="get_texture_normal">
The button's texture for the normal state.
</member>
<member name="texture_pressed" type="Texture2D" setter="set_texture_pressed" getter="get_texture_pressed">
The button's texture for the pressed state.
</member>
<member name="visibility_mode" type="int" setter="set_visibility_mode" getter="get_visibility_mode" enum="TouchScreenButton.VisibilityMode" default="0">
The button's visibility mode. See [enum VisibilityMode] for possible values.
</member>

View file

@ -32,13 +32,13 @@
#include "scene/main/window.h"
void TouchScreenButton::set_texture(const Ref<Texture2D> &p_texture) {
texture = p_texture;
void TouchScreenButton::set_texture_normal(const Ref<Texture2D> &p_texture) {
texture_normal = p_texture;
update();
}
Ref<Texture2D> TouchScreenButton::get_texture() const {
return texture;
Ref<Texture2D> TouchScreenButton::get_texture_normal() const {
return texture_normal;
}
void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) {
@ -107,13 +107,13 @@ void TouchScreenButton::_notification(int p_what) {
if (finger_pressed != -1) {
if (texture_pressed.is_valid()) {
draw_texture(texture_pressed, Point2());
} else if (texture.is_valid()) {
draw_texture(texture, Point2());
} else if (texture_normal.is_valid()) {
draw_texture(texture_normal, Point2());
}
} else {
if (texture.is_valid()) {
draw_texture(texture, Point2());
if (texture_normal.is_valid()) {
draw_texture(texture_normal, Point2());
}
}
@ -127,8 +127,8 @@ void TouchScreenButton::_notification(int p_what) {
Color draw_col = get_tree()->get_debug_collisions_color();
Vector2 pos;
if (shape_centered && texture.is_valid()) {
pos = texture->get_size() * 0.5;
if (shape_centered && texture_normal.is_valid()) {
pos = texture_normal->get_size() * 0.5;
}
draw_set_transform_matrix(get_canvas_transform().translated(pos));
@ -254,8 +254,8 @@ bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
check_rect = false;
Vector2 pos;
if (shape_centered && texture.is_valid()) {
pos = texture->get_size() * 0.5;
if (shape_centered && texture_normal.is_valid()) {
pos = texture_normal->get_size() * 0.5;
}
touched = shape->collide(Transform2D().translated(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
@ -271,8 +271,8 @@ bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
}
if (!touched && check_rect) {
if (texture.is_valid()) {
touched = Rect2(Size2(), texture->get_size()).has_point(coord);
if (texture_normal.is_valid()) {
touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);
}
}
@ -317,24 +317,24 @@ void TouchScreenButton::_release(bool p_exiting_tree) {
#ifdef TOOLS_ENABLED
Rect2 TouchScreenButton::_edit_get_rect() const {
if (texture.is_null()) {
if (texture_normal.is_null()) {
return CanvasItem::_edit_get_rect();
}
return Rect2(Size2(), texture->get_size());
return Rect2(Size2(), texture_normal->get_size());
}
bool TouchScreenButton::_edit_use_rect() const {
return !texture.is_null();
return !texture_normal.is_null();
}
#endif
Rect2 TouchScreenButton::get_anchorable_rect() const {
if (texture.is_null()) {
if (texture_normal.is_null()) {
return CanvasItem::get_anchorable_rect();
}
return Rect2(Size2(), texture->get_size());
return Rect2(Size2(), texture_normal->get_size());
}
void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
@ -355,10 +355,10 @@ bool TouchScreenButton::is_passby_press_enabled() const {
}
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);
ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);
ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);
ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture_pressed"), &TouchScreenButton::set_texture_pressed);
ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);
ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
@ -384,8 +384,8 @@ void TouchScreenButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
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, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_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");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");

View file

@ -46,7 +46,7 @@ public:
};
private:
Ref<Texture2D> texture;
Ref<Texture2D> texture_normal;
Ref<Texture2D> texture_pressed;
Ref<BitMap> bitmask;
Ref<Shape2D> shape;
@ -78,8 +78,8 @@ public:
virtual bool _edit_use_rect() const override;
#endif
void set_texture(const Ref<Texture2D> &p_texture);
Ref<Texture2D> get_texture() const;
void set_texture_normal(const Ref<Texture2D> &p_texture);
Ref<Texture2D> get_texture_normal() const;
void set_texture_pressed(const Ref<Texture2D> &p_texture_pressed);
Ref<Texture2D> get_texture_pressed() const;