Fix/improve TouchScreenButton
- Refactor touch acceptance logic so the same is used whether passby is enabled or not. - Remove the check for visibility during input handling as it should never fail; instead using now an ERR_FAIL_COND() just in case since we have been checking for that so far. Fixes #9159.
This commit is contained in:
parent
6ae8cbb850
commit
693ebbb807
|
@ -204,6 +204,8 @@ void TouchScreenButton::_input(const InputEvent &p_event) {
|
||||||
if (p_event.device != 0)
|
if (p_event.device != 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
ERR_FAIL_COND(!is_visible());
|
||||||
|
|
||||||
if (passby_press) {
|
if (passby_press) {
|
||||||
|
|
||||||
if (p_event.type == InputEvent::SCREEN_TOUCH && !p_event.screen_touch.pressed && finger_pressed == p_event.screen_touch.index) {
|
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) {
|
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));
|
if (_is_touch_inside(p_event.screen_touch)) {
|
||||||
|
|
||||||
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 (finger_pressed == -1) {
|
if (finger_pressed == -1) {
|
||||||
_press(p_event.screen_touch.index);
|
_press(p_event.screen_touch.index);
|
||||||
}
|
}
|
||||||
|
@ -249,22 +235,35 @@ void TouchScreenButton::_input(const InputEvent &p_event) {
|
||||||
|
|
||||||
if (p_event.screen_touch.pressed) {
|
if (p_event.screen_touch.pressed) {
|
||||||
|
|
||||||
if (!is_visible())
|
|
||||||
return;
|
|
||||||
|
|
||||||
const bool can_press = finger_pressed == -1;
|
const bool can_press = finger_pressed == -1;
|
||||||
if (!can_press)
|
if (!can_press)
|
||||||
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));
|
if (_is_touch_inside(p_event.screen_touch)) {
|
||||||
Rect2 item_rect = get_item_rect();
|
_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 touched = false;
|
||||||
bool check_rect = true;
|
bool check_rect = true;
|
||||||
|
|
||||||
|
Rect2 item_rect = get_item_rect();
|
||||||
|
|
||||||
if (shape.is_valid()) {
|
if (shape.is_valid()) {
|
||||||
|
|
||||||
check_rect = false;
|
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)));
|
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 (!touched && check_rect) {
|
||||||
if (!texture.is_null())
|
|
||||||
|
if (texture.is_valid())
|
||||||
touched = item_rect.has_point(coord);
|
touched = item_rect.has_point(coord);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (touched) {
|
return touched;
|
||||||
_press(p_event.screen_touch.index);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (p_event.screen_touch.index == finger_pressed) {
|
|
||||||
_release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TouchScreenButton::_press(int p_finger_pressed) {
|
void TouchScreenButton::_press(int p_finger_pressed) {
|
||||||
|
|
|
@ -64,6 +64,8 @@ private:
|
||||||
|
|
||||||
void _input(const InputEvent &p_Event);
|
void _input(const InputEvent &p_Event);
|
||||||
|
|
||||||
|
bool _is_touch_inside(const InputEventScreenTouch &p_touch);
|
||||||
|
|
||||||
void _press(int p_finger_pressed);
|
void _press(int p_finger_pressed);
|
||||||
void _release(bool p_exiting_tree = false);
|
void _release(bool p_exiting_tree = false);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue