Skip to content

Commit 25fc67f

Browse files
committed
Add mouse support on Android
Closes #116
1 parent a41780c commit 25fc67f

File tree

3 files changed

+92
-60
lines changed

3 files changed

+92
-60
lines changed

src/core.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,16 +1342,13 @@ void _GNW95_process_message()
13421342
case SDL_MOUSEMOTION:
13431343
case SDL_MOUSEBUTTONDOWN:
13441344
case SDL_MOUSEBUTTONUP:
1345-
// The data is accumulated in SDL itself and will be processed
1346-
// in `_mouse_info`.
1347-
break;
13481345
case SDL_MOUSEWHEEL:
1349-
handleMouseWheelEvent(&(e.wheel));
1346+
handleMouseEvent(&e);
13501347
break;
13511348
case SDL_FINGERDOWN:
13521349
case SDL_FINGERMOTION:
13531350
case SDL_FINGERUP:
1354-
handleTouchFingerEvent(&(e.tfinger));
1351+
handleTouchEvent(&e);
13551352
break;
13561353
case SDL_KEYDOWN:
13571354
case SDL_KEYUP:

src/dinput.cc

Lines changed: 88 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#include "dinput.h"
22

3+
enum InputType {
4+
INPUT_TYPE_MOUSE,
5+
INPUT_TYPE_TOUCH,
6+
} InputType;
7+
8+
static int gLastInputType = INPUT_TYPE_MOUSE;
9+
310
static int gTouchMouseLastX = 0;
411
static int gTouchMouseLastY = 0;
512
static int gTouchMouseDeltaX = 0;
@@ -62,49 +69,49 @@ bool mouseDeviceUnacquire()
6269
// 0x4E053C
6370
bool mouseDeviceGetData(MouseData* mouseState)
6471
{
65-
#if __ANDROID__
66-
mouseState->x = gTouchMouseDeltaX;
67-
mouseState->y = gTouchMouseDeltaY;
68-
mouseState->buttons[0] = 0;
69-
mouseState->buttons[1] = 0;
70-
mouseState->wheelX = 0;
71-
mouseState->wheelY = 0;
72-
gTouchMouseDeltaX = 0;
73-
gTouchMouseDeltaY = 0;
74-
75-
if (gTouchFingers == 0) {
76-
if (SDL_GetTicks() - gTouchGestureLastTouchUpTimestamp > 150) {
77-
if (!gTouchGestureHandled) {
78-
if (gTouchGestureTaps == 2) {
72+
if (gLastInputType == INPUT_TYPE_TOUCH) {
73+
mouseState->x = gTouchMouseDeltaX;
74+
mouseState->y = gTouchMouseDeltaY;
75+
mouseState->buttons[0] = 0;
76+
mouseState->buttons[1] = 0;
77+
mouseState->wheelX = 0;
78+
mouseState->wheelY = 0;
79+
gTouchMouseDeltaX = 0;
80+
gTouchMouseDeltaY = 0;
81+
82+
if (gTouchFingers == 0) {
83+
if (SDL_GetTicks() - gTouchGestureLastTouchUpTimestamp > 150) {
84+
if (!gTouchGestureHandled) {
85+
if (gTouchGestureTaps == 2) {
86+
mouseState->buttons[0] = 1;
87+
gTouchGestureHandled = true;
88+
} else if (gTouchGestureTaps == 3) {
89+
mouseState->buttons[1] = 1;
90+
gTouchGestureHandled = true;
91+
}
92+
}
93+
}
94+
} else if (gTouchFingers == 1) {
95+
if (SDL_GetTicks() - gTouchGestureLastTouchDownTimestamp > 150) {
96+
if (gTouchGestureTaps == 1) {
7997
mouseState->buttons[0] = 1;
8098
gTouchGestureHandled = true;
81-
} else if (gTouchGestureTaps == 3) {
99+
} else if (gTouchGestureTaps == 2) {
82100
mouseState->buttons[1] = 1;
83101
gTouchGestureHandled = true;
84102
}
85103
}
86104
}
87-
} else if (gTouchFingers == 1) {
88-
if (SDL_GetTicks() - gTouchGestureLastTouchDownTimestamp > 150) {
89-
if (gTouchGestureTaps == 1) {
90-
mouseState->buttons[0] = 1;
91-
gTouchGestureHandled = true;
92-
} else if (gTouchGestureTaps == 2) {
93-
mouseState->buttons[1] = 1;
94-
gTouchGestureHandled = true;
95-
}
96-
}
105+
} else {
106+
Uint32 buttons = SDL_GetRelativeMouseState(&(mouseState->x), &(mouseState->y));
107+
mouseState->buttons[0] = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
108+
mouseState->buttons[1] = (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
109+
mouseState->wheelX = gMouseWheelDeltaX;
110+
mouseState->wheelY = gMouseWheelDeltaY;
111+
112+
gMouseWheelDeltaX = 0;
113+
gMouseWheelDeltaY = 0;
97114
}
98-
#else
99-
Uint32 buttons = SDL_GetRelativeMouseState(&(mouseState->x), &(mouseState->y));
100-
mouseState->buttons[0] = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
101-
mouseState->buttons[1] = (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
102-
mouseState->wheelX = gMouseWheelDeltaX;
103-
mouseState->wheelY = gMouseWheelDeltaY;
104-
#endif
105-
106-
gMouseWheelDeltaX = 0;
107-
gMouseWheelDeltaY = 0;
108115

109116
return true;
110117
}
@@ -156,49 +163,77 @@ void keyboardDeviceFree()
156163
{
157164
}
158165

159-
void handleTouchFingerEvent(SDL_TouchFingerEvent* event)
166+
void handleMouseEvent(SDL_Event* event)
167+
{
168+
// Mouse movement and buttons are accumulated in SDL itself and will be
169+
// processed later in `mouseDeviceGetData` via `SDL_GetRelativeMouseState`.
170+
171+
if (event->type == SDL_MOUSEWHEEL) {
172+
gMouseWheelDeltaX += event->wheel.x;
173+
gMouseWheelDeltaY += event->wheel.y;
174+
}
175+
176+
if (gLastInputType != INPUT_TYPE_MOUSE) {
177+
// Reset touch data.
178+
gTouchMouseLastX = 0;
179+
gTouchMouseLastY = 0;
180+
gTouchMouseDeltaX = 0;
181+
gTouchMouseDeltaY = 0;
182+
183+
gTouchFingers = 0;
184+
gTouchGestureLastTouchDownTimestamp = 0;
185+
gTouchGestureLastTouchUpTimestamp = 0;
186+
gTouchGestureTaps = 0;
187+
gTouchGestureHandled = false;
188+
189+
gLastInputType = INPUT_TYPE_MOUSE;
190+
}
191+
}
192+
193+
void handleTouchEvent(SDL_Event* event)
160194
{
161195
int windowWidth = screenGetWidth();
162196
int windowHeight = screenGetHeight();
163197

164-
if (event->type == SDL_FINGERDOWN) {
198+
if (event->tfinger.type == SDL_FINGERDOWN) {
165199
gTouchFingers++;
166200

167-
gTouchMouseLastX = (int)(event->x * windowWidth);
168-
gTouchMouseLastY = (int)(event->y * windowHeight);
201+
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
202+
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
169203
gTouchMouseDeltaX = 0;
170204
gTouchMouseDeltaY = 0;
171205

172-
if (event->timestamp - gTouchGestureLastTouchDownTimestamp > 250) {
206+
if (event->tfinger.timestamp - gTouchGestureLastTouchDownTimestamp > 250) {
173207
gTouchGestureTaps = 0;
174208
gTouchGestureHandled = false;
175209
}
176210

177-
gTouchGestureLastTouchDownTimestamp = event->timestamp;
178-
} else if (event->type == SDL_FINGERMOTION) {
211+
gTouchGestureLastTouchDownTimestamp = event->tfinger.timestamp;
212+
} else if (event->tfinger.type == SDL_FINGERMOTION) {
179213
int prevX = gTouchMouseLastX;
180214
int prevY = gTouchMouseLastY;
181-
gTouchMouseLastX = (int)(event->x * windowWidth);
182-
gTouchMouseLastY = (int)(event->y * windowHeight);
215+
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
216+
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
183217
gTouchMouseDeltaX += gTouchMouseLastX - prevX;
184218
gTouchMouseDeltaY += gTouchMouseLastY - prevY;
185-
} else if (event->type == SDL_FINGERUP) {
219+
} else if (event->tfinger.type == SDL_FINGERUP) {
186220
gTouchFingers--;
187221

188222
int prevX = gTouchMouseLastX;
189223
int prevY = gTouchMouseLastY;
190-
gTouchMouseLastX = (int)(event->x * windowWidth);
191-
gTouchMouseLastY = (int)(event->y * windowHeight);
224+
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
225+
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
192226
gTouchMouseDeltaX += gTouchMouseLastX - prevX;
193227
gTouchMouseDeltaY += gTouchMouseLastY - prevY;
194228

195229
gTouchGestureTaps++;
196-
gTouchGestureLastTouchUpTimestamp = event->timestamp;
230+
gTouchGestureLastTouchUpTimestamp = event->tfinger.timestamp;
197231
}
198-
}
199232

200-
void handleMouseWheelEvent(SDL_MouseWheelEvent* event)
201-
{
202-
gMouseWheelDeltaX += event->x;
203-
gMouseWheelDeltaY += event->y;
233+
if (gLastInputType != INPUT_TYPE_TOUCH) {
234+
// Reset mouse data.
235+
SDL_GetRelativeMouseState(NULL, NULL);
236+
237+
gLastInputType = INPUT_TYPE_TOUCH;
238+
}
204239
}

src/dinput.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void mouseDeviceFree();
3030
bool keyboardDeviceInit();
3131
void keyboardDeviceFree();
3232

33-
void handleTouchFingerEvent(SDL_TouchFingerEvent* event);
34-
void handleMouseWheelEvent(SDL_MouseWheelEvent* event);
33+
void handleMouseEvent(SDL_Event* event);
34+
void handleTouchEvent(SDL_Event* event);
3535

3636
#endif /* DINPUT_H */

0 commit comments

Comments
 (0)