Add mouse support on Android

Closes #116
This commit is contained in:
Alexander Batalov 2022-08-19 13:36:14 +03:00
parent a41780caa2
commit 25fc67fec1
3 changed files with 92 additions and 60 deletions

View File

@ -1342,16 +1342,13 @@ void _GNW95_process_message()
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
// The data is accumulated in SDL itself and will be processed
// in `_mouse_info`.
break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
handleMouseWheelEvent(&(e.wheel)); handleMouseEvent(&e);
break; break;
case SDL_FINGERDOWN: case SDL_FINGERDOWN:
case SDL_FINGERMOTION: case SDL_FINGERMOTION:
case SDL_FINGERUP: case SDL_FINGERUP:
handleTouchFingerEvent(&(e.tfinger)); handleTouchEvent(&e);
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
case SDL_KEYUP: case SDL_KEYUP:

View File

@ -1,5 +1,12 @@
#include "dinput.h" #include "dinput.h"
enum InputType {
INPUT_TYPE_MOUSE,
INPUT_TYPE_TOUCH,
} InputType;
static int gLastInputType = INPUT_TYPE_MOUSE;
static int gTouchMouseLastX = 0; static int gTouchMouseLastX = 0;
static int gTouchMouseLastY = 0; static int gTouchMouseLastY = 0;
static int gTouchMouseDeltaX = 0; static int gTouchMouseDeltaX = 0;
@ -62,7 +69,7 @@ bool mouseDeviceUnacquire()
// 0x4E053C // 0x4E053C
bool mouseDeviceGetData(MouseData* mouseState) bool mouseDeviceGetData(MouseData* mouseState)
{ {
#if __ANDROID__ if (gLastInputType == INPUT_TYPE_TOUCH) {
mouseState->x = gTouchMouseDeltaX; mouseState->x = gTouchMouseDeltaX;
mouseState->y = gTouchMouseDeltaY; mouseState->y = gTouchMouseDeltaY;
mouseState->buttons[0] = 0; mouseState->buttons[0] = 0;
@ -95,16 +102,16 @@ bool mouseDeviceGetData(MouseData* mouseState)
} }
} }
} }
#else } else {
Uint32 buttons = SDL_GetRelativeMouseState(&(mouseState->x), &(mouseState->y)); Uint32 buttons = SDL_GetRelativeMouseState(&(mouseState->x), &(mouseState->y));
mouseState->buttons[0] = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; mouseState->buttons[0] = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
mouseState->buttons[1] = (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0; mouseState->buttons[1] = (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
mouseState->wheelX = gMouseWheelDeltaX; mouseState->wheelX = gMouseWheelDeltaX;
mouseState->wheelY = gMouseWheelDeltaY; mouseState->wheelY = gMouseWheelDeltaY;
#endif
gMouseWheelDeltaX = 0; gMouseWheelDeltaX = 0;
gMouseWheelDeltaY = 0; gMouseWheelDeltaY = 0;
}
return true; return true;
} }
@ -156,49 +163,77 @@ void keyboardDeviceFree()
{ {
} }
void handleTouchFingerEvent(SDL_TouchFingerEvent* event) void handleMouseEvent(SDL_Event* event)
{
// Mouse movement and buttons are accumulated in SDL itself and will be
// processed later in `mouseDeviceGetData` via `SDL_GetRelativeMouseState`.
if (event->type == SDL_MOUSEWHEEL) {
gMouseWheelDeltaX += event->wheel.x;
gMouseWheelDeltaY += event->wheel.y;
}
if (gLastInputType != INPUT_TYPE_MOUSE) {
// Reset touch data.
gTouchMouseLastX = 0;
gTouchMouseLastY = 0;
gTouchMouseDeltaX = 0;
gTouchMouseDeltaY = 0;
gTouchFingers = 0;
gTouchGestureLastTouchDownTimestamp = 0;
gTouchGestureLastTouchUpTimestamp = 0;
gTouchGestureTaps = 0;
gTouchGestureHandled = false;
gLastInputType = INPUT_TYPE_MOUSE;
}
}
void handleTouchEvent(SDL_Event* event)
{ {
int windowWidth = screenGetWidth(); int windowWidth = screenGetWidth();
int windowHeight = screenGetHeight(); int windowHeight = screenGetHeight();
if (event->type == SDL_FINGERDOWN) { if (event->tfinger.type == SDL_FINGERDOWN) {
gTouchFingers++; gTouchFingers++;
gTouchMouseLastX = (int)(event->x * windowWidth); gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->y * windowHeight); gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX = 0; gTouchMouseDeltaX = 0;
gTouchMouseDeltaY = 0; gTouchMouseDeltaY = 0;
if (event->timestamp - gTouchGestureLastTouchDownTimestamp > 250) { if (event->tfinger.timestamp - gTouchGestureLastTouchDownTimestamp > 250) {
gTouchGestureTaps = 0; gTouchGestureTaps = 0;
gTouchGestureHandled = false; gTouchGestureHandled = false;
} }
gTouchGestureLastTouchDownTimestamp = event->timestamp; gTouchGestureLastTouchDownTimestamp = event->tfinger.timestamp;
} else if (event->type == SDL_FINGERMOTION) { } else if (event->tfinger.type == SDL_FINGERMOTION) {
int prevX = gTouchMouseLastX; int prevX = gTouchMouseLastX;
int prevY = gTouchMouseLastY; int prevY = gTouchMouseLastY;
gTouchMouseLastX = (int)(event->x * windowWidth); gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->y * windowHeight); gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX += gTouchMouseLastX - prevX; gTouchMouseDeltaX += gTouchMouseLastX - prevX;
gTouchMouseDeltaY += gTouchMouseLastY - prevY; gTouchMouseDeltaY += gTouchMouseLastY - prevY;
} else if (event->type == SDL_FINGERUP) { } else if (event->tfinger.type == SDL_FINGERUP) {
gTouchFingers--; gTouchFingers--;
int prevX = gTouchMouseLastX; int prevX = gTouchMouseLastX;
int prevY = gTouchMouseLastY; int prevY = gTouchMouseLastY;
gTouchMouseLastX = (int)(event->x * windowWidth); gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->y * windowHeight); gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX += gTouchMouseLastX - prevX; gTouchMouseDeltaX += gTouchMouseLastX - prevX;
gTouchMouseDeltaY += gTouchMouseLastY - prevY; gTouchMouseDeltaY += gTouchMouseLastY - prevY;
gTouchGestureTaps++; gTouchGestureTaps++;
gTouchGestureLastTouchUpTimestamp = event->timestamp; gTouchGestureLastTouchUpTimestamp = event->tfinger.timestamp;
}
} }
void handleMouseWheelEvent(SDL_MouseWheelEvent* event) if (gLastInputType != INPUT_TYPE_TOUCH) {
{ // Reset mouse data.
gMouseWheelDeltaX += event->x; SDL_GetRelativeMouseState(NULL, NULL);
gMouseWheelDeltaY += event->y;
gLastInputType = INPUT_TYPE_TOUCH;
}
} }

View File

@ -30,7 +30,7 @@ void mouseDeviceFree();
bool keyboardDeviceInit(); bool keyboardDeviceInit();
void keyboardDeviceFree(); void keyboardDeviceFree();
void handleTouchFingerEvent(SDL_TouchFingerEvent* event); void handleMouseEvent(SDL_Event* event);
void handleMouseWheelEvent(SDL_MouseWheelEvent* event); void handleTouchEvent(SDL_Event* event);
#endif /* DINPUT_H */ #endif /* DINPUT_H */