raze-gles/source/core/inputstate.h
Mitchell Richters c70cc474a0 - change mouse input from int to float and adjust games accordingly.
* Calculate game-side mousex/mousey divisions into the calculations performed in `InputState::GetMouseDelta()`.
* Fix mouse speed when `in_mousesmoothing` is true (wasn't factoring in / 3.f division used in non-true vector.
* Standard mouse forward/side movement speeds in Exhumed & SW with that of other games.
* Remove `strafeyaw` code from Duke/Exhumed/RR as it's not necessary and was leading to situations where the player would continually keep moving sideways even without input.
* Change mouse forward/side velocities to -= current value as is done with controller input and the player's angle/aim velocities.
2020-07-04 18:28:00 +02:00

188 lines
3.4 KiB
C++

#pragma once
#include <stdint.h>
#include "tarray.h"
#include "scancodes.h"
#include "c_bind.h"
#include "c_buttons.h"
#include "d_event.h"
#include "osd.h"
#include "m_joy.h"
#include "gamecvars.h"
typedef uint16_t kb_scancode;
// This encapsulates the entire game-readable input state which previously was spread out across several files.
struct ControlInfo
{
float dx;
float dy;
float dz;
float dyaw;
float dpitch;
float droll;
float mousex;
float mousey;
};
class InputState
{
enum
{
KEYFIFOSIZ = 64,
};
uint8_t KeyStatus[NUM_KEYS];
kb_scancode g_keyFIFO[KEYFIFOSIZ];
char16_t g_keyAsciiFIFO[KEYFIFOSIZ];
uint8_t g_keyFIFOpos;
uint8_t g_keyFIFOend;
uint8_t g_keyAsciiPos;
uint8_t g_keyAsciiEnd;
vec2f_t g_mousePos;
void keySetState(int32_t key, int32_t state);
public:
uint8_t GetKeyStatus(int key)
{
return KeyStatus[key];
}
void ClearKeyStatus(int key)
{
KeyStatus[key] = 0;
}
bool AltPressed()
{
return KeyStatus[sc_LeftAlt] || KeyStatus[sc_RightAlt];
}
bool CtrlPressed()
{
return KeyStatus[sc_LeftControl] || KeyStatus[sc_RightControl];
}
bool WinPressed()
{
return KeyStatus[sc_LeftWin] || KeyStatus[sc_RightWin];
}
bool ShiftPressed()
{
return KeyStatus[sc_LeftShift] || KeyStatus[sc_RightShift];
}
bool EscapePressed()
{
return !!KeyStatus[sc_Escape];
}
void SetBindsEnabled(bool on)
{
}
bool keyBufferWaiting()
{
return (g_keyAsciiPos != g_keyAsciiEnd);
}
int keyBufferFull(void)
{
return ((g_keyAsciiEnd + 1) & (KEYFIFOSIZ - 1)) == g_keyAsciiPos;
}
kb_scancode keyGetScan()
{
if (g_keyFIFOpos == g_keyFIFOend)
return 0;
auto const c = g_keyFIFO[g_keyFIFOpos];
g_keyFIFOpos = ((g_keyFIFOpos + 2) & (KEYFIFOSIZ - 1));
return c;
}
void keyFlushScans(void)
{
memset(&g_keyFIFO, 0, sizeof(g_keyFIFO));
g_keyFIFOpos = g_keyFIFOend = 0;
}
//
// character-based input functions
//
char keyGetChar(void)
{
if (g_keyAsciiPos == g_keyAsciiEnd)
return 0;
char const c = g_keyAsciiFIFO[g_keyAsciiPos];
g_keyAsciiPos = ((g_keyAsciiPos + 1) & (KEYFIFOSIZ - 1));
return c;
}
void keyFlushChars(void)
{
memset(&g_keyAsciiFIFO, 0, sizeof(g_keyAsciiFIFO));
g_keyAsciiPos = g_keyAsciiEnd = 0;
}
inline bool UnboundKeyPressed(int scan)
{
return (GetKeyStatus(scan) != 0 && Bindings.GetBind(scan) == nullptr);
}
void AddEvent(const event_t* ev);
void MouseSetPos(float x, float y)
{
g_mousePos = { x, y };
}
void MouseAddToPos(float x, float y)
{
g_mousePos.x += x;
g_mousePos.y += y;
}
bool gamePadActive()
{
// fixme: This needs to be tracked.
return false;
}
void GetMouseDelta(ControlInfo* info);
void ClearAllInput()
{
memset(KeyStatus, 0, sizeof(KeyStatus));
keyFlushChars();
keyFlushScans();
buttonMap.ResetButtonStates(); // this is important. If all input is cleared, the buttons must be cleared as well.
}
bool CheckAllInput()
{
int res;
do
res = keyGetScan();
while (res > KEY_LASTJOYBUTTON && res < KEY_PAD_LTHUMB_RIGHT); // Controller movement events should not register here.
ClearAllInput();
return res;
}
};
extern InputState inputState;
void CONTROL_GetInput(ControlInfo* info);
int32_t handleevents(void);
#define WIN_IS_PRESSED ( inputState.WinPressed() )
#define ALT_IS_PRESSED ( inputState.AltPressed() )
#define SHIFTS_IS_PRESSED ( inputState.ShiftPressed() )