Use SDL kb state for navigational keys

This commit is contained in:
Christian Semmler 2024-05-31 23:05:47 -04:00
parent 451907599f
commit d69cd89d69
2 changed files with 31 additions and 96 deletions

View File

@ -7,6 +7,7 @@
#include "mxpresenter.h" #include "mxpresenter.h"
#include "mxqueue.h" #include "mxqueue.h"
#include <SDL3/SDL_keyboard.h>
#include <dinput.h> #include <dinput.h>
class LegoCameraController; class LegoCameraController;
@ -94,8 +95,6 @@ class LegoInputManager : public MxPresenter {
MxResult Create(HWND p_hwnd); MxResult Create(HWND p_hwnd);
void Destroy() override; void Destroy() override;
void CreateAndAcquireKeyboard(HWND p_hwnd);
void ReleaseDX();
MxResult GetJoystickId(); MxResult GetJoystickId();
MxResult GetJoystickState(MxU32* p_joystickX, MxU32* p_joystickY, DWORD* p_buttonsState, MxU32* p_povPosition); MxResult GetJoystickState(MxU32* p_joystickX, MxU32* p_joystickY, DWORD* p_buttonsState, MxU32* p_povPosition);
void StartAutoDragTimer(); void StartAutoDragTimer();
@ -146,10 +145,7 @@ class LegoInputManager : public MxPresenter {
MxBool m_unk0x81; // 0x81 MxBool m_unk0x81; // 0x81
LegoControlManager* m_controlManager; // 0x84 LegoControlManager* m_controlManager; // 0x84
MxBool m_unk0x88; // 0x88 MxBool m_unk0x88; // 0x88
IDirectInput* m_directInput; // 0x8c const Uint8* m_keyboardState;
IDirectInputDevice* m_directInputDevice; // 0x90
MxBool m_kbStateSuccess; // 0x94
MxU8 m_keyboardState[256]; // 0x95
MxBool m_unk0x195; // 0x195 MxBool m_unk0x195; // 0x195
MxS32 m_joyid; // 0x198 MxS32 m_joyid; // 0x198
MxS32 m_joystickIndex; // 0x19c MxS32 m_joystickIndex; // 0x19c

View File

@ -38,9 +38,6 @@ LegoInputManager::LegoInputManager()
m_controlManager = NULL; m_controlManager = NULL;
m_unk0x81 = FALSE; m_unk0x81 = FALSE;
m_unk0x88 = FALSE; m_unk0x88 = FALSE;
m_directInput = NULL;
m_directInputDevice = NULL;
m_kbStateSuccess = FALSE;
m_unk0x195 = 0; m_unk0x195 = 0;
m_joyid = -1; m_joyid = -1;
m_joystickIndex = -1; m_joystickIndex = -1;
@ -79,10 +76,9 @@ MxResult LegoInputManager::Create(HWND p_hwnd)
m_eventQueue = new LegoEventQueue; m_eventQueue = new LegoEventQueue;
} }
CreateAndAcquireKeyboard(p_hwnd);
GetJoystickId(); GetJoystickId();
if (!m_keyboardNotifyList || !m_eventQueue || !m_directInputDevice) { if (!m_keyboardNotifyList || !m_eventQueue) {
Destroy(); Destroy();
result = FAILURE; result = FAILURE;
} }
@ -93,8 +89,6 @@ MxResult LegoInputManager::Create(HWND p_hwnd)
// FUNCTION: LEGO1 0x1005bfe0 // FUNCTION: LEGO1 0x1005bfe0
void LegoInputManager::Destroy() void LegoInputManager::Destroy()
{ {
ReleaseDX();
if (m_keyboardNotifyList) { if (m_keyboardNotifyList) {
delete m_keyboardNotifyList; delete m_keyboardNotifyList;
} }
@ -110,55 +104,10 @@ void LegoInputManager::Destroy()
} }
} }
// FUNCTION: LEGO1 0x1005c030
void LegoInputManager::CreateAndAcquireKeyboard(HWND p_hwnd)
{
HINSTANCE hinstance = (HINSTANCE) GetWindowLong(p_hwnd, GWL_HINSTANCE);
HRESULT hresult = DirectInputCreate(hinstance, 0x500, &m_directInput, NULL); // 0x500 for DX5
if (hresult == DI_OK) {
HRESULT createdeviceresult = m_directInput->CreateDevice(GUID_SysKeyboard, &m_directInputDevice, NULL);
if (createdeviceresult == DI_OK) {
m_directInputDevice->SetCooperativeLevel(p_hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
m_directInputDevice->SetDataFormat(&c_dfDIKeyboard);
m_directInputDevice->Acquire();
}
}
}
// FUNCTION: LEGO1 0x1005c0a0
void LegoInputManager::ReleaseDX()
{
if (m_directInputDevice != NULL) {
m_directInputDevice->Unacquire();
m_directInputDevice->Release();
m_directInputDevice = NULL;
}
if (m_directInput != NULL) {
m_directInput->Release();
m_directInput = NULL;
}
}
// FUNCTION: LEGO1 0x1005c0f0 // FUNCTION: LEGO1 0x1005c0f0
void LegoInputManager::GetKeyboardState() void LegoInputManager::GetKeyboardState()
{ {
m_kbStateSuccess = FALSE; m_keyboardState = SDL_GetKeyboardState(NULL);
if (m_directInputDevice) {
HRESULT hr = m_directInputDevice->GetDeviceState(sizeOfArray(m_keyboardState), &m_keyboardState);
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
if (m_directInputDevice->Acquire() == S_OK) {
hr = m_directInputDevice->GetDeviceState(sizeOfArray(m_keyboardState), &m_keyboardState);
}
}
if (hr == S_OK) {
m_kbStateSuccess = TRUE;
}
}
} }
// FUNCTION: LEGO1 0x1005c160 // FUNCTION: LEGO1 0x1005c160
@ -166,39 +115,29 @@ MxResult LegoInputManager::GetNavigationKeyStates(MxU32& p_keyFlags)
{ {
GetKeyboardState(); GetKeyboardState();
if (!m_kbStateSuccess) {
return FAILURE;
}
if (g_unk0x100f67b8) { if (g_unk0x100f67b8) {
if (m_keyboardState[DIK_LEFT] & 0x80 && GetAsyncKeyState(VK_LEFT) == 0) { // [library:input] Figure out if we still need the logic that was here.
m_keyboardState[DIK_LEFT] = 0;
}
if (m_keyboardState[DIK_RIGHT] & 0x80 && GetAsyncKeyState(VK_RIGHT) == 0) {
m_keyboardState[DIK_RIGHT] = 0;
}
} }
MxU32 keyFlags = 0; MxU32 keyFlags = 0;
if ((m_keyboardState[DIK_NUMPAD8] | m_keyboardState[DIK_UP]) & 0x80) { if (m_keyboardState[SDL_SCANCODE_KP_8] || m_keyboardState[SDL_SCANCODE_UP]) {
keyFlags |= c_up; keyFlags |= c_up;
} }
if ((m_keyboardState[DIK_NUMPAD2] | m_keyboardState[DIK_DOWN]) & 0x80) { if ((m_keyboardState[SDL_SCANCODE_KP_2] || m_keyboardState[SDL_SCANCODE_DOWN])) {
keyFlags |= c_down; keyFlags |= c_down;
} }
if ((m_keyboardState[DIK_NUMPAD4] | m_keyboardState[DIK_LEFT]) & 0x80) { if ((m_keyboardState[SDL_SCANCODE_KP_4] || m_keyboardState[SDL_SCANCODE_LEFT])) {
keyFlags |= c_left; keyFlags |= c_left;
} }
if ((m_keyboardState[DIK_NUMPAD6] | m_keyboardState[DIK_RIGHT]) & 0x80) { if ((m_keyboardState[SDL_SCANCODE_KP_6] || m_keyboardState[SDL_SCANCODE_RIGHT])) {
keyFlags |= c_right; keyFlags |= c_right;
} }
if ((m_keyboardState[DIK_LCONTROL] | m_keyboardState[DIK_RCONTROL]) & 0x80) { if (m_keyboardState[SDL_SCANCODE_LCTRL] || m_keyboardState[SDL_SCANCODE_RCTRL]) {
keyFlags |= c_bit5; keyFlags |= c_bit5;
} }