Skip to content

Commit

Permalink
Use SDL kb state for navigational keys
Browse files Browse the repository at this point in the history
  • Loading branch information
foxtacles committed Jun 1, 2024
1 parent 4519075 commit d69cd89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 96 deletions.
50 changes: 23 additions & 27 deletions LEGO1/lego/legoomni/include/legoinputmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "mxpresenter.h"
#include "mxqueue.h"

#include <SDL3/SDL_keyboard.h>
#include <dinput.h>

class LegoCameraController;
Expand Down Expand Up @@ -94,8 +95,6 @@ class LegoInputManager : public MxPresenter {

MxResult Create(HWND p_hwnd);
void Destroy() override;
void CreateAndAcquireKeyboard(HWND p_hwnd);
void ReleaseDX();
MxResult GetJoystickId();
MxResult GetJoystickState(MxU32* p_joystickX, MxU32* p_joystickY, DWORD* p_buttonsState, MxU32* p_povPosition);
void StartAutoDragTimer();
Expand Down Expand Up @@ -132,31 +131,28 @@ class LegoInputManager : public MxPresenter {
// LegoInputManager::`scalar deleting destructor'

private:
MxCriticalSection m_criticalSection; // 0x58
LegoNotifyList* m_keyboardNotifyList; // 0x5c
LegoCameraController* m_camera; // 0x60
LegoWorld* m_world; // 0x64
LegoEventQueue* m_eventQueue; // 0x68
MxS32 m_x; // 0x6c
MxS32 m_y; // 0x70
MxS32 m_unk0x74; // 0x74
UINT m_autoDragTimerID; // 0x78
UINT m_autoDragTime; // 0x7c
MxBool m_unk0x80; // 0x80
MxBool m_unk0x81; // 0x81
LegoControlManager* m_controlManager; // 0x84
MxBool m_unk0x88; // 0x88
IDirectInput* m_directInput; // 0x8c
IDirectInputDevice* m_directInputDevice; // 0x90
MxBool m_kbStateSuccess; // 0x94
MxU8 m_keyboardState[256]; // 0x95
MxBool m_unk0x195; // 0x195
MxS32 m_joyid; // 0x198
MxS32 m_joystickIndex; // 0x19c
JOYCAPS m_joyCaps; // 0x200
MxBool m_useJoystick; // 0x334
MxBool m_unk0x335; // 0x335
MxBool m_unk0x336; // 0x336
MxCriticalSection m_criticalSection; // 0x58
LegoNotifyList* m_keyboardNotifyList; // 0x5c
LegoCameraController* m_camera; // 0x60
LegoWorld* m_world; // 0x64
LegoEventQueue* m_eventQueue; // 0x68
MxS32 m_x; // 0x6c
MxS32 m_y; // 0x70
MxS32 m_unk0x74; // 0x74
UINT m_autoDragTimerID; // 0x78
UINT m_autoDragTime; // 0x7c
MxBool m_unk0x80; // 0x80
MxBool m_unk0x81; // 0x81
LegoControlManager* m_controlManager; // 0x84
MxBool m_unk0x88; // 0x88
const Uint8* m_keyboardState;
MxBool m_unk0x195; // 0x195
MxS32 m_joyid; // 0x198
MxS32 m_joystickIndex; // 0x19c
JOYCAPS m_joyCaps; // 0x200
MxBool m_useJoystick; // 0x334
MxBool m_unk0x335; // 0x335
MxBool m_unk0x336; // 0x336
};

// TEMPLATE: LEGO1 0x10028850
Expand Down
77 changes: 8 additions & 69 deletions LEGO1/lego/legoomni/src/input/legoinputmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ LegoInputManager::LegoInputManager()
m_controlManager = NULL;
m_unk0x81 = FALSE;
m_unk0x88 = FALSE;
m_directInput = NULL;
m_directInputDevice = NULL;
m_kbStateSuccess = FALSE;
m_unk0x195 = 0;
m_joyid = -1;
m_joystickIndex = -1;
Expand Down Expand Up @@ -79,10 +76,9 @@ MxResult LegoInputManager::Create(HWND p_hwnd)
m_eventQueue = new LegoEventQueue;
}

CreateAndAcquireKeyboard(p_hwnd);
GetJoystickId();

if (!m_keyboardNotifyList || !m_eventQueue || !m_directInputDevice) {
if (!m_keyboardNotifyList || !m_eventQueue) {
Destroy();
result = FAILURE;
}
Expand All @@ -93,8 +89,6 @@ MxResult LegoInputManager::Create(HWND p_hwnd)
// FUNCTION: LEGO1 0x1005bfe0
void LegoInputManager::Destroy()
{
ReleaseDX();

if (m_keyboardNotifyList) {
delete m_keyboardNotifyList;
}
Expand All @@ -110,95 +104,40 @@ 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
void LegoInputManager::GetKeyboardState()
{
m_kbStateSuccess = FALSE;

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;
}
}
m_keyboardState = SDL_GetKeyboardState(NULL);
}

// FUNCTION: LEGO1 0x1005c160
MxResult LegoInputManager::GetNavigationKeyStates(MxU32& p_keyFlags)
{
GetKeyboardState();

if (!m_kbStateSuccess) {
return FAILURE;
}

if (g_unk0x100f67b8) {
if (m_keyboardState[DIK_LEFT] & 0x80 && GetAsyncKeyState(VK_LEFT) == 0) {
m_keyboardState[DIK_LEFT] = 0;
}

if (m_keyboardState[DIK_RIGHT] & 0x80 && GetAsyncKeyState(VK_RIGHT) == 0) {
m_keyboardState[DIK_RIGHT] = 0;
}
// [library:input] Figure out if we still need the logic that was here.
}

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;
}

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;
}

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;
}

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;
}

if ((m_keyboardState[DIK_LCONTROL] | m_keyboardState[DIK_RCONTROL]) & 0x80) {
if (m_keyboardState[SDL_SCANCODE_LCTRL] || m_keyboardState[SDL_SCANCODE_RCTRL]) {
keyFlags |= c_bit5;
}

Expand Down

0 comments on commit d69cd89

Please sign in to comment.