Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Fix gestures for PLATFORM_DESKTOP_SDL #3499

Merged
merged 3 commits into from Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/platforms/rcore_desktop_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,15 @@ void PollInputEvents(void)
// so, if mouse is not moved it returns a (0, 0) position... this behaviour should be reviewed!
//for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.position[i] = (Vector2){ 0, 0 };

// Map touch position to mouse position for convenience
// WARNING: If the target desktop device supports touch screen, this behavious should be reviewed!
// https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch
// https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages
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
// NOTE: Android supports up to 260 keys
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
Expand Down Expand Up @@ -1077,10 +1086,16 @@ void PollInputEvents(void)
case SDL_MOUSEBUTTONDOWN:
{
CORE.Input.Mouse.currentButtonState[event.button.button - 1] = 1;

touchAction = 1;
gestureUpdate = true;
} break;
case SDL_MOUSEBUTTONUP:
{
CORE.Input.Mouse.currentButtonState[event.button.button - 1] = 0;

touchAction = 0;
gestureUpdate = true;
} break;
case SDL_MOUSEWHEEL:
{
Expand All @@ -1100,6 +1115,10 @@ void PollInputEvents(void)
CORE.Input.Mouse.currentPosition.x = (float)event.motion.x;
CORE.Input.Mouse.currentPosition.y = (float)event.motion.y;
}

CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
touchAction = 2;
gestureUpdate = true;
} break;

// Check gamepad events
Expand All @@ -1122,11 +1141,38 @@ void PollInputEvents(void)
} 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
if (touchAction == 2) gestureEvent.position[0] = CORE.Input.Touch.position[0];
else 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
}
//-----------------------------------------------------------------------------
}


//----------------------------------------------------------------------------------
// Module Internal Functions Definition
//----------------------------------------------------------------------------------
Expand Down