Clean up
This commit is contained in:
@@ -0,0 +1,266 @@
|
||||
#include <windows.h>
|
||||
#include "Xinput.h"
|
||||
#include "../../Constants.h"
|
||||
|
||||
namespace TLAC::Input
|
||||
{
|
||||
Xinput* Xinput::instance;
|
||||
|
||||
Xinput::Xinput()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Xinput* Xinput::GetInstance()
|
||||
{
|
||||
if (instance == nullptr)
|
||||
{
|
||||
instance = new Xinput();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
void Xinput::SetTapStates(BYTE keycode, float elapsed)
|
||||
{
|
||||
KeyDoubleTapStates[keycode] = IsTapped(keycode) ? KeyDoubleTapWatches[keycode].Restart() <= DOUBLE_TAP_THRESHOLD : false;
|
||||
|
||||
bool isDown = currentState.KeyStates[keycode];
|
||||
bool isTapped = IsTapped(keycode);
|
||||
|
||||
keyIntervalTapStates[keycode] = isTapped;
|
||||
|
||||
if (isTapped)
|
||||
{
|
||||
keyIntervalTapTimes[keycode] = 0;
|
||||
keyIntervalInitials[keycode] = true;
|
||||
}
|
||||
else if (isDown)
|
||||
{
|
||||
float threshold = keyIntervalInitials[keycode] ? INTERVAL_TAP_DELAY_THRESHOLD : INTERVAL_TAP_THRESHOLD;
|
||||
|
||||
bool intervalTapped = (keyIntervalTapTimes[keycode] += elapsed) > threshold;
|
||||
keyIntervalTapStates[keycode] = intervalTapped;
|
||||
|
||||
if (intervalTapped)
|
||||
{
|
||||
keyIntervalTapTimes[keycode] = 0;
|
||||
keyIntervalInitials[keycode] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Xinput::PollInput()
|
||||
{
|
||||
lastState = currentState;
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
float elapsed = keyIntervalWatch.Restart();
|
||||
|
||||
if (XInputGetState(0, &state) == ERROR_SUCCESS)
|
||||
{
|
||||
BYTE i = XINPUT_A;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_A)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_DOWN;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_B;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_B)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_RIGHT;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_X;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_X)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_LEFT;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_Y;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_Y)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_UP;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_LS;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_RS;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_LSB;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_RSB;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_START;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_START)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_BACK;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_LT;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.bLeftTrigger > 230)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
i = XINPUT_RT;
|
||||
{
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.bRightTrigger > 230)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
//float normLX = fmaxf(-1, (float)state.Gamepad.sThumbLX / 32767);
|
||||
i = XINPUT_LRIGHT;
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.sThumbLX > 10000)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
|
||||
i = XINPUT_LLEFT;
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.sThumbLX < -10000)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
|
||||
{
|
||||
//float normLX = fmaxf(-1, (float)state.Gamepad.sThumbRX / 32767);
|
||||
i = XINPUT_RRIGHT;
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.sThumbRX > 10000)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
|
||||
i = XINPUT_RLEFT;
|
||||
currentState.KeyStates[i] = false;
|
||||
if (state.Gamepad.sThumbRX < -10000)
|
||||
currentState.KeyStates[i] = true;
|
||||
SetTapStates(i, elapsed);
|
||||
}
|
||||
}
|
||||
else {
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
ZeroMemory(¤tState, sizeof(currentState));
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
bool Xinput::IsDown(BYTE keycode)
|
||||
{
|
||||
return currentState.IsDown(keycode);
|
||||
}
|
||||
|
||||
bool Xinput::IsUp(BYTE keycode)
|
||||
{
|
||||
return !IsDown(keycode);
|
||||
}
|
||||
|
||||
bool Xinput::IsTapped(BYTE keycode)
|
||||
{
|
||||
return IsDown(keycode) && WasUp(keycode);
|
||||
}
|
||||
|
||||
bool Xinput::IsDoubleTapped(BYTE keycode)
|
||||
{
|
||||
return KeyDoubleTapStates[keycode];
|
||||
}
|
||||
|
||||
bool Xinput::IsReleased(BYTE keycode)
|
||||
{
|
||||
return IsUp(keycode) && WasDown(keycode);
|
||||
}
|
||||
|
||||
inline bool Xinput::WasDown(BYTE keycode)
|
||||
{
|
||||
return lastState.IsDown(keycode);
|
||||
}
|
||||
|
||||
inline bool Xinput::WasUp(BYTE keycode)
|
||||
{
|
||||
return !WasDown(keycode);
|
||||
}
|
||||
|
||||
bool Xinput::IsIntervalTapped(BYTE keycode)
|
||||
{
|
||||
return keyIntervalTapStates[keycode];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
#include "../IInputDevice.h"
|
||||
#include <xinput.h>
|
||||
#include "../../Utilities/Stopwatch.h"
|
||||
#include "XinputState.h"
|
||||
|
||||
using Stopwatch = TLAC::Utilities::Stopwatch;
|
||||
|
||||
namespace TLAC::Input
|
||||
{
|
||||
class Xinput : public IInputDevice
|
||||
{
|
||||
const float DOUBLE_TAP_THRESHOLD = 200.0f;
|
||||
const float INTERVAL_TAP_DELAY_THRESHOLD = 500.0f;
|
||||
const float INTERVAL_TAP_THRESHOLD = 75.0f;
|
||||
|
||||
public:
|
||||
static Xinput* GetInstance();
|
||||
|
||||
bool PollInput() override;
|
||||
bool IsDown(BYTE keycode);
|
||||
bool IsUp(BYTE keycode);
|
||||
bool IsTapped(BYTE keycode);
|
||||
bool IsReleased(BYTE keycode);
|
||||
bool IsDoubleTapped(BYTE keycode);
|
||||
bool IsIntervalTapped(BYTE keycode);
|
||||
|
||||
bool WasDown(BYTE keycode);
|
||||
bool WasUp(BYTE keycode);
|
||||
|
||||
private:
|
||||
Xinput();
|
||||
XinputState lastState;
|
||||
XinputState currentState;
|
||||
|
||||
XINPUT_STATE state;
|
||||
|
||||
BYTE KeyDoubleTapStates[0xFF];
|
||||
Utilities::Stopwatch KeyDoubleTapWatches[0xFF];
|
||||
|
||||
Stopwatch keyIntervalWatch;
|
||||
|
||||
BOOL keyIntervalInitials[0xFF];
|
||||
BYTE keyIntervalTapStates[0xFF];
|
||||
FLOAT keyIntervalTapTimes[0xFF];
|
||||
|
||||
static Xinput* instance;
|
||||
|
||||
void SetTapStates(BYTE keycode, float elapsed);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "XinputState.h"
|
||||
|
||||
namespace TLAC::Input
|
||||
{
|
||||
bool XinputState::IsDown(BYTE keycode)
|
||||
{
|
||||
return KeyStates[keycode];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
|
||||
namespace TLAC::Input
|
||||
{
|
||||
struct XinputState
|
||||
{
|
||||
bool KeyStates[0x8F];
|
||||
|
||||
bool IsDown(BYTE keycode);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user