This commit is contained in:
nastys
2019-07-30 13:48:28 +02:00
parent 17b6c02aee
commit d1349cfebe
157 changed files with 0 additions and 0 deletions
@@ -0,0 +1,52 @@
#pragma once
namespace TLAC::Input
{
enum Direction
{
DIR_UP,
DIR_RIGHT,
DIR_DOWN,
DIR_LEFT
};
enum Ds4Button : int
{
DS4_SQUARE = 0,
DS4_CROSS = 1,
DS4_CIRCLE = 2,
DS4_TRIANGLE = 3,
DS4_L1 = 4,
DS4_R1 = 5,
DS4_L_TRIGGER = 6,
DS4_R_TRIGGER = 7,
DS4_SHARE = 8,
DS4_OPTIONS = 9,
DS4_L3 = 10,
DS4_R3 = 11,
DS4_PS = 12,
DS4_TOUCH = 13,
DS4_DPAD_UP,
DS4_DPAD_RIGHT,
DS4_DPAD_DOWN,
DS4_DPAD_LEFT,
DS4_L_STICK_UP,
DS4_L_STICK_RIGHT,
DS4_L_STICK_DOWN,
DS4_L_STICK_LEFT,
DS4_R_STICK_UP,
DS4_R_STICK_RIGHT,
DS4_R_STICK_DOWN,
DS4_R_STICK_LEFT,
DS4_BUTTON_MAX,
};
}
@@ -0,0 +1,14 @@
#include "Ds4State.h"
namespace TLAC::Input
{
Joystick::Joystick() : XAxis(0.0f), YAxis(0.0f)
{
return;
};
Joystick::Joystick(float xAxis, float yAxis) : XAxis(xAxis), YAxis(yAxis)
{
return;
};
}
@@ -0,0 +1,39 @@
#pragma once
#include "../DirectInput.h"
#include "Ds4Button.h"
namespace TLAC::Input
{
struct Joystick
{
FLOAT XAxis, YAxis;
Joystick();
Joystick(float xAxis, float yAxis);
};
struct Dpad
{
BOOL IsDown;
FLOAT Angle;
Joystick Stick;
};
struct Trigger
{
FLOAT Axis;
};
struct Ds4State
{
DIJOYSTATE2 DI_JoyState;
BYTE Buttons[DS4_BUTTON_MAX];
Dpad Dpad;
Joystick LeftStick;
Joystick RightStick;
Trigger LeftTrigger;
Trigger RightTrigger;
};
}
@@ -0,0 +1,169 @@
#include "DualShock4.h"
#include "../../../Utilities/Math.h"
#include "../../../framework.h"
#include <stdio.h>
namespace TLAC::Input
{
DualShock4* DualShock4::instance;
DualShock4::DualShock4()
{
}
DualShock4::~DualShock4()
{
DI_Dispose();
}
bool DualShock4::TryInitializeInstance()
{
if (InstanceInitialized())
return true;
if (!DirectInputInitialized())
return false;
DualShock4 *dualShock4 = new DualShock4();
bool success = dualShock4->Initialize();
instance = success ? dualShock4 : nullptr;
if (!success)
delete dualShock4;
return success;
}
bool DualShock4::Initialize()
{
HRESULT result = NULL;
const size_t guidCount = sizeof(GUID_Ds4) / sizeof(GUID);
for (size_t i = 0; i < guidCount; i++)
{
result = DI_CreateDevice(GUID_Ds4[i]);
if (!FAILED(result))
break;
else if (i == guidCount - 1)
return false;
}
if (FAILED(result = DI_SetDataFormat(&c_dfDIJoystick2)))
return false;
result = DI_Acquire();
return true;
}
bool DualShock4::PollInput()
{
lastState = currentState;
HRESULT result = NULL;
result = DI_Poll();
result = DI_GetDeviceState(sizeof(DIJOYSTATE2), &currentState.DI_JoyState);
if (result != DI_OK)
return false;
UpdateInternalDs4State(currentState);
for (int button = 0; button < DS4_BUTTON_MAX; button++)
currentState.Buttons[button] = GetButtonState(currentState, (Ds4Button)button);
return true;
}
void DualShock4::UpdateInternalDs4State(Ds4State &state)
{
if (state.Dpad.IsDown = state.DI_JoyState.rgdwPOV[0] != -1)
{
state.Dpad.Angle = (state.DI_JoyState.rgdwPOV[0] / 100.0f);
auto direction = Utilities::GetDirection(state.Dpad.Angle);
state.Dpad.Stick = { direction.Y, -direction.X };
}
else
{
state.Dpad.Angle = 0;
state.Dpad.Stick = Joystick();
}
state.LeftStick = NormalizeStick(state.DI_JoyState.lX, state.DI_JoyState.lY);
state.RightStick = NormalizeStick(state.DI_JoyState.lZ, state.DI_JoyState.lRz);
state.LeftTrigger = { NormalizeTrigger(state.DI_JoyState.lRx) };
state.RightTrigger = { NormalizeTrigger(state.DI_JoyState.lRy) };
}
bool DualShock4::IsDown(Ds4Button button)
{
return currentState.Buttons[button];
}
bool DualShock4::IsUp(Ds4Button button)
{
return !IsDown(button);
}
bool DualShock4::IsTapped(Ds4Button button)
{
return IsDown(button) && WasUp(button);
}
bool DualShock4::IsReleased(Ds4Button button)
{
return IsUp(button) && WasDown(button);
}
bool DualShock4::WasDown(Ds4Button button)
{
return lastState.Buttons[button];
}
bool DualShock4::WasUp(Ds4Button button)
{
return !WasDown(button);
}
bool DualShock4::MatchesDirection(Joystick joystick, Direction directionEnum, float threshold)
{
switch (directionEnum)
{
case TLAC::Input::DIR_UP:
return joystick.YAxis <= -threshold;
case TLAC::Input::DIR_RIGHT:
return joystick.XAxis >= +threshold;
case TLAC::Input::DIR_DOWN:
return joystick.YAxis >= +threshold;
case TLAC::Input::DIR_LEFT:
return joystick.XAxis <= -threshold;
default:
return false;
}
}
bool DualShock4::GetButtonState(Ds4State &state, Ds4Button button)
{
if (button >= DS4_SQUARE && button <= DS4_TOUCH)
return state.DI_JoyState.rgbButtons[button];
if (button >= DS4_DPAD_UP && button <= DS4_DPAD_LEFT)
return state.Dpad.IsDown ? MatchesDirection(state.Dpad.Stick, (Direction)(button - DS4_DPAD_UP), dpadThreshold) : false;
if (button >= DS4_L_STICK_UP && button <= DS4_L_STICK_LEFT)
return MatchesDirection(state.LeftStick, (Direction)(button - DS4_L_STICK_UP), joystickThreshold);
if (button >= DS4_R_STICK_UP && button <= DS4_R_STICK_LEFT)
return MatchesDirection(state.RightStick, (Direction)(button - DS4_R_STICK_UP), joystickThreshold);
return false;
}
}
@@ -0,0 +1,62 @@
#pragma once
#include "../Controller.h"
#include "../../IInputDevice.h"
#include "Ds4State.h"
namespace TLAC::Input
{
// DualShock 4 Wireless Controller Product GUIDs:
const GUID GUID_Ds4[2] =
{
// First Generation: {05C4054C-0000-0000-0000-504944564944}
{ 0x05C4054C, 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } },
// Second Generation: {09CC054C-0000-0000-0000-504944564944}
{ 0x09CC054C, 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } },
};
class DualShock4 : public Controller, public IInputDevice
{
public:
DualShock4();
~DualShock4();
static bool TryInitializeInstance();
bool Initialize();
bool PollInput() override;
bool IsDown(Ds4Button button);
bool IsUp(Ds4Button button);
bool IsTapped(Ds4Button button);
bool IsReleased(Ds4Button button);
bool WasDown(Ds4Button button);
bool WasUp(Ds4Button button);
inline Joystick GetLeftStick() { return currentState.LeftStick; };
inline Joystick GetRightStick() { return currentState.RightStick; };
inline Joystick GetDpad() { return currentState.Dpad.Stick; };
static inline bool InstanceInitialized() { return instance != nullptr; };
static inline DualShock4* GetInstance() { return instance; };
static inline void DeleteInstance() { delete instance; instance = nullptr; };
private:
static DualShock4* instance;
Ds4State lastState;
Ds4State currentState;
const float triggerThreshold = 0.5f;
const float joystickThreshold = 0.5f;
const float dpadThreshold = 0.5f;
inline float NormalizeTrigger(long value) { return (float)value / USHRT_MAX; };
inline float NormalizeStick(long value) { return (float)value / USHRT_MAX * 2.0f - 1.0f; };
inline Joystick NormalizeStick(long x, long y) { return Joystick(NormalizeStick(x), NormalizeStick(y)); };
void UpdateInternalDs4State(Ds4State &state);
bool MatchesDirection(Joystick joystick, Direction directionEnum, float threshold);
bool GetButtonState(Ds4State &state, Ds4Button button);
};
}