|
1 | 1 | #include "dinput.h"
|
2 | 2 |
|
| 3 | +enum InputType { |
| 4 | + INPUT_TYPE_MOUSE, |
| 5 | + INPUT_TYPE_TOUCH, |
| 6 | +} InputType; |
| 7 | + |
| 8 | +static int gLastInputType = INPUT_TYPE_MOUSE; |
| 9 | + |
3 | 10 | static int gTouchMouseLastX = 0;
|
4 | 11 | static int gTouchMouseLastY = 0;
|
5 | 12 | static int gTouchMouseDeltaX = 0;
|
@@ -62,49 +69,49 @@ bool mouseDeviceUnacquire()
|
62 | 69 | // 0x4E053C
|
63 | 70 | bool mouseDeviceGetData(MouseData* mouseState)
|
64 | 71 | {
|
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) { |
79 | 97 | mouseState->buttons[0] = 1;
|
80 | 98 | gTouchGestureHandled = true;
|
81 |
| - } else if (gTouchGestureTaps == 3) { |
| 99 | + } else if (gTouchGestureTaps == 2) { |
82 | 100 | mouseState->buttons[1] = 1;
|
83 | 101 | gTouchGestureHandled = true;
|
84 | 102 | }
|
85 | 103 | }
|
86 | 104 | }
|
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; |
97 | 114 | }
|
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; |
108 | 115 |
|
109 | 116 | return true;
|
110 | 117 | }
|
@@ -156,49 +163,77 @@ void keyboardDeviceFree()
|
156 | 163 | {
|
157 | 164 | }
|
158 | 165 |
|
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) |
160 | 194 | {
|
161 | 195 | int windowWidth = screenGetWidth();
|
162 | 196 | int windowHeight = screenGetHeight();
|
163 | 197 |
|
164 |
| - if (event->type == SDL_FINGERDOWN) { |
| 198 | + if (event->tfinger.type == SDL_FINGERDOWN) { |
165 | 199 | gTouchFingers++;
|
166 | 200 |
|
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); |
169 | 203 | gTouchMouseDeltaX = 0;
|
170 | 204 | gTouchMouseDeltaY = 0;
|
171 | 205 |
|
172 |
| - if (event->timestamp - gTouchGestureLastTouchDownTimestamp > 250) { |
| 206 | + if (event->tfinger.timestamp - gTouchGestureLastTouchDownTimestamp > 250) { |
173 | 207 | gTouchGestureTaps = 0;
|
174 | 208 | gTouchGestureHandled = false;
|
175 | 209 | }
|
176 | 210 |
|
177 |
| - gTouchGestureLastTouchDownTimestamp = event->timestamp; |
178 |
| - } else if (event->type == SDL_FINGERMOTION) { |
| 211 | + gTouchGestureLastTouchDownTimestamp = event->tfinger.timestamp; |
| 212 | + } else if (event->tfinger.type == SDL_FINGERMOTION) { |
179 | 213 | int prevX = gTouchMouseLastX;
|
180 | 214 | 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); |
183 | 217 | gTouchMouseDeltaX += gTouchMouseLastX - prevX;
|
184 | 218 | gTouchMouseDeltaY += gTouchMouseLastY - prevY;
|
185 |
| - } else if (event->type == SDL_FINGERUP) { |
| 219 | + } else if (event->tfinger.type == SDL_FINGERUP) { |
186 | 220 | gTouchFingers--;
|
187 | 221 |
|
188 | 222 | int prevX = gTouchMouseLastX;
|
189 | 223 | 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); |
192 | 226 | gTouchMouseDeltaX += gTouchMouseLastX - prevX;
|
193 | 227 | gTouchMouseDeltaY += gTouchMouseLastY - prevY;
|
194 | 228 |
|
195 | 229 | gTouchGestureTaps++;
|
196 |
| - gTouchGestureLastTouchUpTimestamp = event->timestamp; |
| 230 | + gTouchGestureLastTouchUpTimestamp = event->tfinger.timestamp; |
197 | 231 | }
|
198 |
| -} |
199 | 232 |
|
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 | + } |
204 | 239 | }
|
0 commit comments