diff options
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 9ffe2d337..a0bcaf444 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -179,6 +179,12 @@ bool g_keys[256] = {}; | |||
| 179 | bool g_focus = false; | 179 | bool g_focus = false; |
| 180 | bool fake_key_down(int vk) { return vk >= 0 && vk < 256 && g_keys[vk]; } | 180 | bool fake_key_down(int vk) { return vk >= 0 && vk < 256 && g_keys[vk]; } |
| 181 | bool fake_focus() { return g_focus; } | 181 | bool fake_focus() { return g_focus; } |
| 182 | int g_cursor_x = 0, g_cursor_y = 0; | ||
| 183 | bool fake_cursor(int* x, int* y) { | ||
| 184 | *x = g_cursor_x; | ||
| 185 | *y = g_cursor_y; | ||
| 186 | return true; | ||
| 187 | } | ||
| 182 | 188 | ||
| 183 | GameInputGamepadState pad_state(IGameInput* input, IGameInputDevice* dev, uint32_t* ui = nullptr) { | 189 | GameInputGamepadState pad_state(IGameInput* input, IGameInputDevice* dev, uint32_t* ui = nullptr) { |
| 184 | GameInputGamepadState st{}; | 190 | GameInputGamepadState st{}; |
| @@ -195,9 +201,99 @@ GameInputGamepadState pad_state(IGameInput* input, IGameInputDevice* dev, uint32 | |||
| 195 | return st; | 201 | return st; |
| 196 | } | 202 | } |
| 197 | 203 | ||
| 204 | bool sticks_at_rest(const GameInputGamepadState& st) { | ||
| 205 | return st.leftThumbstickX == 0 && st.leftThumbstickY == 0 && st.rightThumbstickX == 0 && | ||
| 206 | st.rightThumbstickY == 0 && st.leftTrigger == 0 && st.rightTrigger == 0; | ||
| 207 | } | ||
| 208 | |||
| 209 | // WASD, the mouse and its buttons. Runs in the focused mode with focus and nothing held. | ||
| 210 | void test_keyboard_pad_sticks(IGameInput* input, IGameInputDevice* kb) { | ||
| 211 | rt::gameinput::poll(); | ||
| 212 | check(sticks_at_rest(pad_state(input, kb)), "keyboard pad: sticks and triggers at rest"); | ||
| 213 | |||
| 214 | struct Dir { std::vector<int> keys; float x, y; const char* what; }; | ||
| 215 | const float d = 23170 / 32767.0f; | ||
| 216 | const Dir dirs[] = { | ||
| 217 | {{'W'}, 0, 1, "keyboard pad: W is left stick up"}, | ||
| 218 | {{'S'}, 0, -1, "keyboard pad: S is left stick down"}, | ||
| 219 | {{'A'}, -1, 0, "keyboard pad: A is left stick left"}, | ||
| 220 | {{'D'}, 1, 0, "keyboard pad: D is left stick right"}, | ||
| 221 | {{'W', 'S'}, 0, 0, "keyboard pad: W and S cancel"}, | ||
| 222 | {{'A', 'D'}, 0, 0, "keyboard pad: A and D cancel"}, | ||
| 223 | {{'W', 'S', 'D'}, 1, 0, "keyboard pad: W+S cancel, D still full right"}, | ||
| 224 | {{'W', 'D'}, d, d, "keyboard pad: W+D is up-right on the round gate"}, | ||
| 225 | {{'S', 'A'}, -d, -d, "keyboard pad: S+A is down-left on the round gate"}, | ||
| 226 | }; | ||
| 227 | for (const Dir& e : dirs) { | ||
| 228 | for (int k : e.keys) g_keys[k] = true; | ||
| 229 | rt::gameinput::poll(); | ||
| 230 | const GameInputGamepadState st = pad_state(input, kb); | ||
| 231 | check(approx(st.leftThumbstickX, e.x) && approx(st.leftThumbstickY, e.y) && st.buttons == 0 && | ||
| 232 | st.rightThumbstickX == 0 && st.rightThumbstickY == 0, | ||
| 233 | e.what); | ||
| 234 | for (int k : e.keys) g_keys[k] = false; | ||
| 235 | } | ||
| 236 | rt::gameinput::poll(); | ||
| 237 | check(sticks_at_rest(pad_state(input, kb)), "keyboard pad: left stick centers on release"); | ||
| 238 | |||
| 239 | // 10 px right, 10 px down in one poll: 10 x 1024 axis units each, +Y down on screen is stick down. | ||
| 240 | g_cursor_x += 10; | ||
| 241 | g_cursor_y += 10; | ||
| 242 | rt::gameinput::poll(); | ||
| 243 | GameInputGamepadState st = pad_state(input, kb); | ||
| 244 | check(approx(st.rightThumbstickX, 10240 / 32767.0f) && approx(st.rightThumbstickY, -10240 / 32767.0f) && | ||
| 245 | st.leftThumbstickX == 0, | ||
| 246 | "keyboard pad: mouse delta moves the right stick"); | ||
| 247 | g_cursor_x -= 500; | ||
| 248 | rt::gameinput::poll(); | ||
| 249 | check(approx(pad_state(input, kb).rightThumbstickX, -1), "keyboard pad: a large mouse delta clamps to full"); | ||
| 250 | // The mouse stops: the stick falls back, never past center, to exactly 0. | ||
| 251 | int polls = 0; | ||
| 252 | float last = -1; | ||
| 253 | bool monotonic = true; | ||
| 254 | for (; polls < 200; ++polls) { | ||
| 255 | rt::gameinput::poll(); | ||
| 256 | st = pad_state(input, kb); | ||
| 257 | monotonic &= st.rightThumbstickX >= last && st.rightThumbstickX <= 0; | ||
| 258 | last = st.rightThumbstickX; | ||
| 259 | if (st.rightThumbstickX == 0 && st.rightThumbstickY == 0) break; | ||
| 260 | } | ||
| 261 | check(monotonic && polls < 100 && sticks_at_rest(st), "keyboard pad: right stick decays to 0 when the mouse stops"); | ||
| 262 | |||
| 263 | g_keys[VK_RBUTTON] = true; | ||
| 264 | rt::gameinput::poll(); | ||
| 265 | st = pad_state(input, kb); | ||
| 266 | check(approx(st.leftTrigger, 1) && st.rightTrigger == 0 && st.buttons == 0, | ||
| 267 | "keyboard pad: right mouse button is the left trigger"); | ||
| 268 | g_keys[VK_LBUTTON] = true; | ||
| 269 | rt::gameinput::poll(); | ||
| 270 | st = pad_state(input, kb); | ||
| 271 | check(approx(st.leftTrigger, 1) && approx(st.rightTrigger, 1), "keyboard pad: left mouse button is the right trigger"); | ||
| 272 | g_keys[VK_RBUTTON] = false; | ||
| 273 | rt::gameinput::poll(); | ||
| 274 | st = pad_state(input, kb); | ||
| 275 | check(st.leftTrigger == 0 && approx(st.rightTrigger, 1), "keyboard pad: releasing a mouse button releases its trigger"); | ||
| 276 | |||
| 277 | // Losing focus releases sticks and triggers; the cursor moved meanwhile does not swing the stick on return. | ||
| 278 | g_keys['W'] = g_keys['D'] = g_keys[VK_LBUTTON] = true; | ||
| 279 | g_cursor_x += 20; | ||
| 280 | rt::gameinput::poll(); | ||
| 281 | check(!sticks_at_rest(pad_state(input, kb)), "keyboard pad: sticks and trigger held before focus loss"); | ||
| 282 | g_focus = false; | ||
| 283 | g_cursor_x += 300; | ||
| 284 | rt::gameinput::poll(); | ||
| 285 | check(sticks_at_rest(pad_state(input, kb)), "keyboard pad: losing focus releases sticks and triggers"); | ||
| 286 | g_keys['W'] = g_keys['D'] = g_keys[VK_LBUTTON] = false; | ||
| 287 | g_cursor_x += 300; | ||
| 288 | g_focus = true; | ||
| 289 | rt::gameinput::poll(); | ||
| 290 | check(sticks_at_rest(pad_state(input, kb)), "keyboard pad: no mouse jump when focus returns"); | ||
| 291 | } | ||
| 292 | |||
| 198 | // Runs with no other pad connected and the device callback from main still registered. | 293 | // Runs with no other pad connected and the device callback from main still registered. |
| 199 | void test_keyboard_pad(IGameInput* input) { | 294 | void test_keyboard_pad(IGameInput* input) { |
| 200 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); | 295 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); |
| 296 | rt::gameinput::set_cursor_source(fake_cursor); | ||
| 201 | 297 | ||
| 202 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); | 298 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); |
| 203 | rt::gameinput::reload_config(); | 299 | rt::gameinput::reload_config(); |
| @@ -285,6 +381,11 @@ void test_keyboard_pad(IGameInput* input) { | |||
| 285 | "keyboard pad: LEONIDA_KEYBOARD_PAD=always reads keys without focus"); | 381 | "keyboard pad: LEONIDA_KEYBOARD_PAD=always reads keys without focus"); |
| 286 | g_keys[VK_DOWN] = g_keys[VK_RETURN] = false; | 382 | g_keys[VK_DOWN] = g_keys[VK_RETURN] = false; |
| 287 | 383 | ||
| 384 | set_env("LEONIDA_KEYBOARD_PAD", "1"); | ||
| 385 | rt::gameinput::reload_config(); | ||
| 386 | g_focus = true; | ||
| 387 | test_keyboard_pad_sticks(input, kb); | ||
| 388 | |||
| 288 | // Turning it off removes the pad; turning it on again brings back the same device object. | 389 | // Turning it off removes the pad; turning it on again brings back the same device object. |
| 289 | set_env("LEONIDA_KEYBOARD_PAD", "off"); | 390 | set_env("LEONIDA_KEYBOARD_PAD", "off"); |
| 290 | rt::gameinput::reload_config(); | 391 | rt::gameinput::reload_config(); |
| @@ -303,6 +404,7 @@ void test_keyboard_pad(IGameInput* input) { | |||
| 303 | rt::gameinput::poll(); | 404 | rt::gameinput::poll(); |
| 304 | check(rt::gameinput::connected_count() == 0, "keyboard pad: removed at the end"); | 405 | check(rt::gameinput::connected_count() == 0, "keyboard pad: removed at the end"); |
| 305 | rt::gameinput::set_keyboard_source(nullptr, nullptr); | 406 | rt::gameinput::set_keyboard_source(nullptr, nullptr); |
| 407 | rt::gameinput::set_cursor_source(nullptr); | ||
| 306 | } | 408 | } |
| 307 | 409 | ||
| 308 | bool same_id(const AppLocalDeviceId& a, const AppLocalDeviceId& b) { return std::memcmp(&a, &b, sizeof a) == 0; } | 410 | bool same_id(const AppLocalDeviceId& a, const AppLocalDeviceId& b) { return std::memcmp(&a, &b, sizeof a) == 0; } |