Proper down and up touch events for single frame touches (#9)

This commit is contained in:
Shane Smiskol 2025-06-23 16:26:49 -07:00 committed by GitHub
parent c0753673e6
commit 7696827099
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -72,6 +72,7 @@ struct finger {
TouchAction action; TouchAction action;
int x; int x;
int y; int y;
bool resetNextFrame;
}; };
struct touch { struct touch {
@ -315,6 +316,7 @@ static int init_touch(const char *dev_path, const char *origin_path) {
platform.touch.fingers[i].x = -1; platform.touch.fingers[i].x = -1;
platform.touch.fingers[i].y = -1; platform.touch.fingers[i].y = -1;
platform.touch.fingers[i].action = TOUCH_ACTION_UP; platform.touch.fingers[i].action = TOUCH_ACTION_UP;
platform.touch.fingers[i].resetNextFrame = false;
CORE.Input.Touch.currentTouchState[0] = 0; CORE.Input.Touch.currentTouchState[0] = 0;
CORE.Input.Touch.previousTouchState[0] = 0; CORE.Input.Touch.previousTouchState[0] = 0;
@ -619,6 +621,11 @@ void PollInputEvents(void) {
for (int i = 0; i < MAX_TOUCH_POINTS; ++i) { for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i]; CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
// caused by single frame down and up events
if (platform.touch.fingers[i].resetNextFrame) {
CORE.Input.Touch.currentTouchState[i] = 0;
platform.touch.fingers[i].resetNextFrame = false;
}
} }
for (int i = 0; i < MAX_MOUSE_BUTTONS; ++i) { for (int i = 0; i < MAX_MOUSE_BUTTONS; ++i) {
@ -628,8 +635,6 @@ void PollInputEvents(void) {
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
CORE.Input.Touch.pointCount = 0; CORE.Input.Touch.pointCount = 0;
int released_detected = 0;
struct input_event event = {0}; struct input_event event = {0};
while (read(platform.touch.fd, &event, sizeof(struct input_event)) == sizeof(struct input_event)) { while (read(platform.touch.fd, &event, sizeof(struct input_event)) == sizeof(struct input_event)) {
if (event.type == SYN_REPORT) { // synchronization frame. Expose completed events back to the library if (event.type == SYN_REPORT) { // synchronization frame. Expose completed events back to the library
@ -649,12 +654,16 @@ void PollInputEvents(void) {
} }
} else if (platform.touch.fingers[i].action == TOUCH_ACTION_UP) { } else if (platform.touch.fingers[i].action == TOUCH_ACTION_UP) {
if (CORE.Input.Touch.currentTouchState[i] == 1) {
released_detected = 1;
}
CORE.Input.Touch.position[i].x = -1; CORE.Input.Touch.position[i].x = -1;
CORE.Input.Touch.position[i].y = -1; CORE.Input.Touch.position[i].y = -1;
CORE.Input.Touch.currentTouchState[i] = 0; // if we received a touch down and up event in the same frame,
// delay up event by one frame so that API user needs no special handling
if (CORE.Input.Touch.previousTouchState[i] == 0) {
CORE.Input.Touch.currentTouchState[i] = 1;
platform.touch.fingers[i].resetNextFrame = true; // mark to be reset next event update loop
} else {
CORE.Input.Touch.currentTouchState[i] = 0;
}
} }
} }
@ -672,12 +681,6 @@ void PollInputEvents(void) {
} }
} }
// hack for now to detect gestures in single frame
if (released_detected) {
CORE.Input.Touch.previousTouchState[0] = 1;
CORE.Input.Touch.currentTouchState[0] = 0;
}
// count how many fingers are left on the screen after processing all events // count how many fingers are left on the screen after processing all events
for (int i = 0; i < MAX_TOUCH_POINTS; ++i) { for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
CORE.Input.Touch.pointCount += platform.touch.fingers[i].action != TOUCH_ACTION_UP; CORE.Input.Touch.pointCount += platform.touch.fingers[i].action != TOUCH_ACTION_UP;