Review the gesture handling for SDL

This commit is contained in:
ubkp 2023-11-01 17:44:02 -03:00
parent 668e298c66
commit dfdd4b9e0e

View File

@ -970,6 +970,9 @@ void PollInputEvents(void)
// https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages // https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition; CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
int touchAction = -1; // 0-TOUCH_ACTION_UP, 1-TOUCH_ACTION_DOWN, 2-TOUCH_ACTION_MOVE
bool gestureUpdate = false; // Flag to note gestures require to update
// Register previous keys states // Register previous keys states
// NOTE: Android supports up to 260 keys // NOTE: Android supports up to 260 keys
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
@ -1084,59 +1087,15 @@ void PollInputEvents(void)
{ {
CORE.Input.Mouse.currentButtonState[event.button.button - 1] = 1; CORE.Input.Mouse.currentButtonState[event.button.button - 1] = 1;
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) touchAction = 1;
// Process mouse events as touches to be able to use mouse-gestures gestureUpdate = true;
GestureEvent gestureEvent = { 0 };
// Register touch actions
if ((CORE.Input.Mouse.currentButtonState[event.button.button - 1] == 1) && (CORE.Input.Mouse.previousButtonState[event.button.button - 1] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
else if ((CORE.Input.Mouse.currentButtonState[event.button.button - 1] == 0) && (CORE.Input.Mouse.previousButtonState[event.button.button - 1] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = GetMousePosition();
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent);
#endif
} break; } break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
{ {
CORE.Input.Mouse.currentButtonState[event.button.button - 1] = 0; CORE.Input.Mouse.currentButtonState[event.button.button - 1] = 0;
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) touchAction = 0;
// Process mouse events as touches to be able to use mouse-gestures gestureUpdate = true;
GestureEvent gestureEvent = { 0 };
// Register touch actions
if ((CORE.Input.Mouse.currentButtonState[event.button.button - 1] == 1) && (CORE.Input.Mouse.previousButtonState[event.button.button - 1] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
else if ((CORE.Input.Mouse.currentButtonState[event.button.button - 1] == 0) && (CORE.Input.Mouse.previousButtonState[event.button.button - 1] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = GetMousePosition();
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent);
#endif
} break; } break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
{ {
@ -1157,30 +1116,9 @@ void PollInputEvents(void)
CORE.Input.Mouse.currentPosition.y = (float)event.motion.y; CORE.Input.Mouse.currentPosition.y = (float)event.motion.y;
} }
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition; CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
touchAction = 2;
// Process mouse events as touches to be able to use mouse-gestures gestureUpdate = true;
GestureEvent gestureEvent = { 0 };
gestureEvent.touchAction = TOUCH_ACTION_MOVE;
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = CORE.Input.Touch.position[0];
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent);
#endif
} break; } break;
// Check gamepad events // Check gamepad events
@ -1203,6 +1141,33 @@ void PollInputEvents(void)
} break; } break;
default: break; default: break;
} }
#if defined(SUPPORT_GESTURES_SYSTEM)
if (gestureUpdate)
{
// Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 };
// Register touch actions
gestureEvent.touchAction = touchAction;
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = GetMousePosition();
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent);
}
#endif
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
} }