2021-03-11 14:25:00 +00:00
|
|
|
// Copyright 2021, Mateo de Mayo.
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
/*!
|
|
|
|
* @file
|
|
|
|
* @brief Connection between user-generated SDL events and qwerty devices.
|
|
|
|
* @author Mateo de Mayo <mateodemayo@gmail.com>
|
|
|
|
* @ingroup drv_qwerty
|
|
|
|
*/
|
|
|
|
|
2021-03-11 18:04:38 +00:00
|
|
|
// @note: This file will process more than one device, that's
|
|
|
|
// why some comments reference multiple devices
|
|
|
|
|
|
|
|
#include "qwerty_device.h"
|
2021-03-11 15:49:42 +00:00
|
|
|
#include "xrt/xrt_device.h"
|
|
|
|
#include <SDL2/SDL.h>
|
2021-03-11 14:25:00 +00:00
|
|
|
|
2021-03-11 18:14:54 +00:00
|
|
|
// Amount of look_speed units a mouse delta of 1px in screen space will rotate the device
|
|
|
|
#define SENSITIVITY 0.1f
|
|
|
|
|
2021-03-11 18:04:38 +00:00
|
|
|
static void
|
|
|
|
find_qwerty_devices(struct xrt_device **xdevs,
|
|
|
|
size_t num_xdevs,
|
|
|
|
struct xrt_device **xd_hmd)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < num_xdevs; i++) {
|
|
|
|
if (xdevs[i] == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(xdevs[i]->str, QWERTY_HMD_STR) == 0) {
|
|
|
|
*xd_hmd = xdevs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:49:42 +00:00
|
|
|
void
|
|
|
|
qwerty_process_event(struct xrt_device **xdevs, size_t num_xdevs, SDL_Event event)
|
|
|
|
{
|
2021-03-11 18:04:38 +00:00
|
|
|
static struct xrt_device *xd_hmd = NULL;
|
|
|
|
|
|
|
|
// We can cache the devices as they don't get destroyed during runtime
|
|
|
|
static bool cached = false;
|
|
|
|
if (!cached) {
|
|
|
|
find_qwerty_devices(xdevs, num_xdevs, &xd_hmd);
|
|
|
|
cached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool using_qhmd = xd_hmd != NULL;
|
|
|
|
struct qwerty_device *qd_hmd = using_qhmd ? qwerty_device(xd_hmd) : NULL;
|
|
|
|
|
|
|
|
if (!qd_hmd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
// Determine focused device
|
|
|
|
struct qwerty_device *qdev = qd_hmd;
|
|
|
|
|
|
|
|
// WASDQE Movement
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_a) qwerty_press_left(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_a) qwerty_release_left(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_d) qwerty_press_right(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_d) qwerty_release_right(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_w) qwerty_press_forward(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_w) qwerty_release_forward(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_s) qwerty_press_backward(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_s) qwerty_release_backward(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_e) qwerty_press_up(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_e) qwerty_release_up(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_q) qwerty_press_down(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_q) qwerty_release_down(qdev);
|
|
|
|
|
|
|
|
// Arrow keys rotation
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LEFT) qwerty_press_look_left(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_LEFT) qwerty_release_look_left(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RIGHT) qwerty_press_look_right(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_RIGHT) qwerty_release_look_right(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_UP) qwerty_press_look_up(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_UP) qwerty_release_look_up(qdev);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_DOWN) qwerty_press_look_down(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_DOWN) qwerty_release_look_down(qdev);
|
|
|
|
|
2021-03-11 18:14:54 +00:00
|
|
|
// Movement speed
|
|
|
|
if (event.type == SDL_MOUSEWHEEL) qwerty_change_movement_speed(qdev, event.wheel.y);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_KP_PLUS) qwerty_change_movement_speed(qdev, 1);
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_KP_MINUS) qwerty_change_movement_speed(qdev, -1);
|
|
|
|
|
|
|
|
// Sprinting
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LSHIFT) qwerty_press_sprint(qdev);
|
|
|
|
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_LSHIFT) qwerty_release_sprint(qdev);
|
|
|
|
|
|
|
|
// Mouse rotation
|
|
|
|
if (event.type == SDL_MOUSEBUTTONUP && event.button.button == SDL_BUTTON_RIGHT) {
|
|
|
|
SDL_SetRelativeMouseMode(false);
|
|
|
|
}
|
|
|
|
if (event.type == SDL_MOUSEMOTION && event.motion.state & SDL_BUTTON_RMASK) {
|
|
|
|
SDL_SetRelativeMouseMode(true);
|
|
|
|
float yaw = -event.motion.xrel * SENSITIVITY;
|
|
|
|
float pitch = -event.motion.yrel * SENSITIVITY;
|
|
|
|
qwerty_add_look_delta(qdev, yaw, pitch);
|
|
|
|
}
|
2021-03-11 18:04:38 +00:00
|
|
|
// clang-format on
|
2021-03-11 15:49:42 +00:00
|
|
|
}
|