Browse Source

♻️ extract Controller class (#1236)

pull/1242/head
Yuri Pourre 5 years ago committed by GitHub
parent
commit
e07f5aeacf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      SourceX/controls/controller.h
  2. 42
      SourceX/controls/controller_motion.cpp
  3. 9
      SourceX/controls/controller_motion.h
  4. 16
      SourceX/controls/devices/game_controller.cpp
  5. 16
      SourceX/controls/devices/joystick.cpp
  6. 8
      SourceX/controls/plrctrls.cpp

10
SourceX/controls/controller.h

@ -11,6 +11,16 @@ struct ControllerButtonEvent {
bool up;
};
class Controller {
public:
// Raw axis values.
float leftStickX, leftStickY, rightStickX, rightStickY;
// Axis values scaled to [-1, 1] range and clamped to a deadzone.
float leftStickXUnscaled, leftStickYUnscaled, rightStickXUnscaled, rightStickYUnscaled;
// Whether stick positions have been updated and need rescaling.
bool leftStickNeedsScaling, rightStickNeedsScaling;
};
// NOTE: Not idempotent because of how it handles axis triggers.
// Must be called exactly once per SDL input event.
ControllerButtonEvent ToControllerButtonEvent(const SDL_Event &event);

42
SourceX/controls/controller_motion.cpp

@ -10,6 +10,8 @@
namespace dvl {
Controller controller;
namespace {
void ScaleJoystickAxes(float *x, float *y, float deadzone)
@ -66,7 +68,7 @@ bool SimulateRightStickWithDpad(const SDL_Event &event, ControllerButtonEvent ct
static bool simulating = false;
if (ctrl_event.button == ControllerButton_BUTTON_BACK) {
if (ctrl_event.up && simulating) {
rightStickX = rightStickY = 0;
controller.rightStickX = controller.rightStickY = 0;
simulating = false;
}
return false;
@ -75,31 +77,27 @@ bool SimulateRightStickWithDpad(const SDL_Event &event, ControllerButtonEvent ct
return false;
switch (ctrl_event.button) {
case ControllerButton_BUTTON_DPAD_LEFT:
rightStickX = ctrl_event.up ? 0 : -1;
controller.rightStickX = ctrl_event.up ? 0 : -1;
break;
case ControllerButton_BUTTON_DPAD_RIGHT:
rightStickX = ctrl_event.up ? 0 : 1;
controller.rightStickX = ctrl_event.up ? 0 : 1;
break;
case ControllerButton_BUTTON_DPAD_UP:
rightStickY = ctrl_event.up ? 0 : 1;
controller.rightStickY = ctrl_event.up ? 0 : 1;
break;
case ControllerButton_BUTTON_DPAD_DOWN:
rightStickY = ctrl_event.up ? 0 : -1;
controller.rightStickY = ctrl_event.up ? 0 : -1;
break;
default:
return false;
}
simulating = !(rightStickX == 0 && rightStickY == 0);
simulating = !(controller.rightStickX == 0 && controller.rightStickY == 0);
return true;
}
} // namespace
float leftStickX, leftStickY, rightStickX, rightStickY;
float leftStickXUnscaled, leftStickYUnscaled, rightStickXUnscaled, rightStickYUnscaled;
bool leftStickNeedsScaling, rightStickNeedsScaling;
namespace {
void ScaleJoysticks()
@ -107,18 +105,18 @@ void ScaleJoysticks()
const float rightDeadzone = 0.07;
const float leftDeadzone = 0.07;
if (leftStickNeedsScaling) {
leftStickX = leftStickXUnscaled;
leftStickY = leftStickYUnscaled;
ScaleJoystickAxes(&leftStickX, &leftStickY, leftDeadzone);
leftStickNeedsScaling = false;
if (controller.leftStickNeedsScaling) {
controller.leftStickX = controller.leftStickXUnscaled;
controller.leftStickY = controller.leftStickYUnscaled;
ScaleJoystickAxes(&controller.leftStickX, &controller.leftStickY, leftDeadzone);
controller.leftStickNeedsScaling = false;
}
if (rightStickNeedsScaling) {
rightStickX = rightStickXUnscaled;
rightStickY = rightStickYUnscaled;
ScaleJoystickAxes(&rightStickX, &rightStickY, rightDeadzone);
rightStickNeedsScaling = false;
if (controller.rightStickNeedsScaling) {
controller.rightStickX = controller.rightStickXUnscaled;
controller.rightStickY = controller.rightStickYUnscaled;
ScaleJoystickAxes(&controller.rightStickX, &controller.rightStickY, rightDeadzone);
controller.rightStickNeedsScaling = false;
}
}
@ -148,8 +146,8 @@ bool ProcessControllerMotion(const SDL_Event &event, ControllerButtonEvent ctrl_
AxisDirection GetLeftStickOrDpadDirection(bool allow_dpad)
{
const float stickX = leftStickX;
const float stickY = leftStickY;
const float stickX = controller.leftStickX;
const float stickY = controller.leftStickY;
AxisDirection result { AxisDirectionX_NONE, AxisDirectionY_NONE };

9
SourceX/controls/controller_motion.h

@ -9,14 +9,7 @@
namespace dvl {
// Raw axis values.
extern float leftStickXUnscaled, leftStickYUnscaled, rightStickXUnscaled, rightStickYUnscaled;
// Axis values scaled to [-1, 1] range and clamped to a deadzone.
extern float leftStickX, leftStickY, rightStickX, rightStickY;
// Whether stick positions have been updated and need rescaling.
extern bool leftStickNeedsScaling, rightStickNeedsScaling;
extern struct Controller controller;
// Updates motion state for mouse and joystick sticks.
bool ProcessControllerMotion(const SDL_Event &event, ControllerButtonEvent ctrl_event);

16
SourceX/controls/devices/game_controller.cpp

@ -130,20 +130,20 @@ bool GameController::ProcessAxisMotion(const SDL_Event &event)
return false;
switch (event.caxis.axis) {
case SDL_CONTROLLER_AXIS_LEFTX:
leftStickXUnscaled = event.caxis.value;
leftStickNeedsScaling = true;
controller.leftStickXUnscaled = event.caxis.value;
controller.leftStickNeedsScaling = true;
break;
case SDL_CONTROLLER_AXIS_LEFTY:
leftStickYUnscaled = -event.caxis.value;
leftStickNeedsScaling = true;
controller.leftStickYUnscaled = -event.caxis.value;
controller.leftStickNeedsScaling = true;
break;
case SDL_CONTROLLER_AXIS_RIGHTX:
rightStickXUnscaled = event.caxis.value;
rightStickNeedsScaling = true;
controller.rightStickXUnscaled = event.caxis.value;
controller.rightStickNeedsScaling = true;
break;
case SDL_CONTROLLER_AXIS_RIGHTY:
rightStickYUnscaled = -event.caxis.value;
rightStickNeedsScaling = true;
controller.rightStickYUnscaled = -event.caxis.value;
controller.rightStickNeedsScaling = true;
break;
default:
return false;

16
SourceX/controls/devices/joystick.cpp

@ -219,26 +219,26 @@ bool Joystick::ProcessAxisMotion(const SDL_Event &event)
switch (event.jaxis.axis) {
#ifdef JOY_AXIS_LEFTX
case JOY_AXIS_LEFTX:
leftStickXUnscaled = event.jaxis.value;
leftStickNeedsScaling = true;
controller.leftStickXUnscaled = event.jaxis.value;
controller.leftStickNeedsScaling = true;
break;
#endif
#ifdef JOY_AXIS_LEFTY
case JOY_AXIS_LEFTY:
leftStickYUnscaled = -event.jaxis.value;
leftStickNeedsScaling = true;
controller.leftStickYUnscaled = -event.jaxis.value;
controller.leftStickNeedsScaling = true;
break;
#endif
#ifdef JOY_AXIS_RIGHTX
case JOY_AXIS_RIGHTX:
rightStickXUnscaled = event.jaxis.value;
rightStickNeedsScaling = true;
controller.rightStickXUnscaled = event.jaxis.value;
controller.rightStickNeedsScaling = true;
break;
#endif
#ifdef JOY_AXIS_RIGHTY
case JOY_AXIS_RIGHTY:
rightStickYUnscaled = -event.jaxis.value;
rightStickNeedsScaling = true;
controller.rightStickYUnscaled = -event.jaxis.value;
controller.rightStickNeedsScaling = true;
break;
#endif
default:

8
SourceX/controls/plrctrls.cpp

@ -898,8 +898,8 @@ struct RightStickAccumulator {
{
const Uint32 tc = SDL_GetTicks();
const int dtc = tc - lastTc;
hiresDX += rightStickX * dtc;
hiresDY += rightStickY * dtc;
hiresDX += controller.rightStickX * dtc;
hiresDY += controller.rightStickY * dtc;
const int dx = hiresDX / slowdown;
const int dy = hiresDY / slowdown;
*x += dx;
@ -977,14 +977,14 @@ bool IsAutomapActive()
bool IsMovingMouseCursorWithController()
{
return rightStickX != 0 || rightStickY != 0;
return controller.rightStickX != 0 || controller.rightStickY != 0;
}
void HandleRightStickMotion()
{
static RightStickAccumulator acc;
// deadzone is handled in ScaleJoystickAxes() already
if (rightStickX == 0 && rightStickY == 0) {
if (controller.rightStickX == 0 && controller.rightStickY == 0) {
acc.clear();
return;
}

Loading…
Cancel
Save