Merge pull request #9178 from RandomShaper/fix-touchbutton-2.1
Fix/improve TouchScreenButton (2.1)
This commit is contained in:
commit
89621fee5d
2 changed files with 41 additions and 48 deletions
|
@ -204,6 +204,8 @@ void TouchScreenButton::_input(const InputEvent &p_event) {
|
|||
if (p_event.device != 0)
|
||||
return;
|
||||
|
||||
ERR_FAIL_COND(!is_visible());
|
||||
|
||||
if (passby_press) {
|
||||
|
||||
if (p_event.type == InputEvent::SCREEN_TOUCH && !p_event.screen_touch.pressed && finger_pressed == p_event.screen_touch.index) {
|
||||
|
@ -215,23 +217,7 @@ void TouchScreenButton::_input(const InputEvent &p_event) {
|
|||
|
||||
if (finger_pressed == -1 || p_event.screen_touch.index == finger_pressed) {
|
||||
|
||||
Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(Point2(p_event.screen_touch.x, p_event.screen_touch.y));
|
||||
|
||||
bool touched = false;
|
||||
if (bitmask.is_valid()) {
|
||||
|
||||
if (Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
|
||||
|
||||
if (bitmask->get_bit(coord))
|
||||
touched = true;
|
||||
}
|
||||
} else {
|
||||
|
||||
if (texture.is_valid())
|
||||
touched = Rect2(Point2(), texture->get_size()).has_point(coord);
|
||||
}
|
||||
|
||||
if (touched) {
|
||||
if (_is_touch_inside(p_event.screen_touch)) {
|
||||
if (finger_pressed == -1) {
|
||||
_press(p_event.screen_touch.index);
|
||||
}
|
||||
|
@ -249,22 +235,35 @@ void TouchScreenButton::_input(const InputEvent &p_event) {
|
|||
|
||||
if (p_event.screen_touch.pressed) {
|
||||
|
||||
if (!is_visible())
|
||||
return;
|
||||
|
||||
const bool can_press = finger_pressed == -1;
|
||||
if (!can_press)
|
||||
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();
|
||||
if (_is_touch_inside(p_event.screen_touch)) {
|
||||
_press(p_event.screen_touch.index);
|
||||
}
|
||||
} else {
|
||||
if (p_event.screen_touch.index == finger_pressed) {
|
||||
_release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TouchScreenButton::_is_touch_inside(const InputEventScreenTouch &p_touch) {
|
||||
|
||||
Point2 coord = get_global_transform_with_canvas().affine_inverse().xform(Point2(p_touch.x, p_touch.y));
|
||||
|
||||
bool touched = false;
|
||||
bool check_rect = true;
|
||||
|
||||
Rect2 item_rect = get_item_rect();
|
||||
|
||||
if (shape.is_valid()) {
|
||||
|
||||
check_rect = false;
|
||||
Matrix32 xform = shape_centered ? Matrix32().translated(get_item_rect().size * 0.5f) : Matrix32();
|
||||
Matrix32 xform = shape_centered ? Matrix32().translated(item_rect.size * 0.5f) : Matrix32();
|
||||
touched = shape->collide(xform, unit_rect, Matrix32(0, coord + Vector2(0.5, 0.5)));
|
||||
}
|
||||
|
||||
|
@ -279,20 +278,12 @@ void TouchScreenButton::_input(const InputEvent &p_event) {
|
|||
}
|
||||
|
||||
if (!touched && check_rect) {
|
||||
if (!texture.is_null())
|
||||
|
||||
if (texture.is_valid())
|
||||
touched = item_rect.has_point(coord);
|
||||
}
|
||||
|
||||
if (touched) {
|
||||
_press(p_event.screen_touch.index);
|
||||
}
|
||||
} else {
|
||||
if (p_event.screen_touch.index == finger_pressed) {
|
||||
_release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return touched;
|
||||
}
|
||||
|
||||
void TouchScreenButton::_press(int p_finger_pressed) {
|
||||
|
|
|
@ -64,6 +64,8 @@ private:
|
|||
|
||||
void _input(const InputEvent &p_Event);
|
||||
|
||||
bool _is_touch_inside(const InputEventScreenTouch &p_touch);
|
||||
|
||||
void _press(int p_finger_pressed);
|
||||
void _release(bool p_exiting_tree = false);
|
||||
|
||||
|
|
Loading…
Reference in a new issue