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_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
// The data is accumulated in SDL itself and will be processed
// in `_mouse_info`.
break;
case SDL_MOUSEWHEEL:
handleMouseWheelEvent(&(e.wheel));
handleMouseEvent(&e);
break;
case SDL_FINGERDOWN:
case SDL_FINGERMOTION:
case SDL_FINGERUP:
handleTouchFingerEvent(&(e.tfinger));
handleTouchEvent(&e);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:

View File

@ -1,5 +1,12 @@
#include "dinput.h"
enum InputType {
INPUT_TYPE_MOUSE,
INPUT_TYPE_TOUCH,
} InputType;
static int gLastInputType = INPUT_TYPE_MOUSE;
static int gTouchMouseLastX = 0;
static int gTouchMouseLastY = 0;
static int gTouchMouseDeltaX = 0;
@ -62,7 +69,7 @@ bool mouseDeviceUnacquire()
// 0x4E053C
bool mouseDeviceGetData(MouseData* mouseState)
{
#if __ANDROID__
if (gLastInputType == INPUT_TYPE_TOUCH) {
mouseState->x = gTouchMouseDeltaX;
mouseState->y = gTouchMouseDeltaY;
mouseState->buttons[0] = 0;
@ -95,16 +102,16 @@ bool mouseDeviceGetData(MouseData* mouseState)
}
}
}
#else
} else {
Uint32 buttons = SDL_GetRelativeMouseState(&(mouseState->x), &(mouseState->y));
mouseState->buttons[0] = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
mouseState->buttons[1] = (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
mouseState->wheelX = gMouseWheelDeltaX;
mouseState->wheelY = gMouseWheelDeltaY;
#endif
gMouseWheelDeltaX = 0;
gMouseWheelDeltaY = 0;
}
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 windowHeight = screenGetHeight();
if (event->type == SDL_FINGERDOWN) {
if (event->tfinger.type == SDL_FINGERDOWN) {
gTouchFingers++;
gTouchMouseLastX = (int)(event->x * windowWidth);
gTouchMouseLastY = (int)(event->y * windowHeight);
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX = 0;
gTouchMouseDeltaY = 0;
if (event->timestamp - gTouchGestureLastTouchDownTimestamp > 250) {
if (event->tfinger.timestamp - gTouchGestureLastTouchDownTimestamp > 250) {
gTouchGestureTaps = 0;
gTouchGestureHandled = false;
}
gTouchGestureLastTouchDownTimestamp = event->timestamp;
} else if (event->type == SDL_FINGERMOTION) {
gTouchGestureLastTouchDownTimestamp = event->tfinger.timestamp;
} else if (event->tfinger.type == SDL_FINGERMOTION) {
int prevX = gTouchMouseLastX;
int prevY = gTouchMouseLastY;
gTouchMouseLastX = (int)(event->x * windowWidth);
gTouchMouseLastY = (int)(event->y * windowHeight);
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX += gTouchMouseLastX - prevX;
gTouchMouseDeltaY += gTouchMouseLastY - prevY;
} else if (event->type == SDL_FINGERUP) {
} else if (event->tfinger.type == SDL_FINGERUP) {
gTouchFingers--;
int prevX = gTouchMouseLastX;
int prevY = gTouchMouseLastY;
gTouchMouseLastX = (int)(event->x * windowWidth);
gTouchMouseLastY = (int)(event->y * windowHeight);
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX += gTouchMouseLastX - prevX;
gTouchMouseDeltaY += gTouchMouseLastY - prevY;
gTouchGestureTaps++;
gTouchGestureLastTouchUpTimestamp = event->timestamp;
gTouchGestureLastTouchUpTimestamp = event->tfinger.timestamp;
}
if (gLastInputType != INPUT_TYPE_TOUCH) {
// Reset mouse data.
SDL_GetRelativeMouseState(NULL, NULL);
gLastInputType = INPUT_TYPE_TOUCH;
}
}
void handleMouseWheelEvent(SDL_MouseWheelEvent* event)
{
gMouseWheelDeltaX += event->x;
gMouseWheelDeltaY += event->y;
}

View File

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