From 39581cf672507e43ac3376c00221fd440f658afc Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Sat, 21 Jul 2018 20:04:37 +0500 Subject: [PATCH] Mouse works as good as it was before --- src/xrEngine/xr_input.cpp | 154 +++++++++++++++++++++++++------------- src/xrEngine/xr_input.h | 24 +++--- src/xrGame/MainMenu.cpp | 3 +- 3 files changed, 116 insertions(+), 65 deletions(-) diff --git a/src/xrEngine/xr_input.cpp b/src/xrEngine/xr_input.cpp index 382e0727398..6604ae796aa 100644 --- a/src/xrEngine/xr_input.cpp +++ b/src/xrEngine/xr_input.cpp @@ -37,12 +37,14 @@ static void on_error_dialog(bool before) pInput->acquire(true); } -CInput::CInput(BOOL bExclusive, int deviceForInit) +CInput::CInput(bool exclusive, int deviceForInit) { - g_exclusive = !!bExclusive; + g_exclusive = exclusive; Log("Starting INPUT device..."); + MouseDelta = 25; + ZeroMemory(mouseState, sizeof(mouseState)); ZeroMemory(KBState, sizeof(KBState)); ZeroMemory(timeStamp, sizeof(timeStamp)); @@ -80,11 +82,88 @@ void CInput::DumpStatistics(IGameFont& font, IPerformanceAlert* alert) font.OutNext("*** INPUT: %2.2fms", pInput->GetStats().FrameTime.result); } -void CInput::SetAllAcquire(BOOL bAcquire) {} +void CInput::SetAllAcquire(bool bAcquire) {} + +void CInput::SetMouseAcquire(bool bAcquire) {} +void CInput::SetKBDAcquire(bool bAcquire) {} + + +void CInput::MouseUpdate() +{ + SDL_Event event; + + bool mouse_prev[COUNT_MOUSE_BUTTONS]; + + mouse_prev[0] = mouseState[0]; + mouse_prev[1] = mouseState[1]; + mouse_prev[2] = mouseState[2]; + mouse_prev[3] = mouseState[3]; + mouse_prev[4] = mouseState[4]; + mouse_prev[5] = mouseState[5]; + mouse_prev[6] = mouseState[6]; + mouse_prev[7] = mouseState[7]; + + bool mouseMoved = false; + offs[0] = offs[1] = offs[2] = 0; + while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEWHEEL)) + { + switch (event.type) + { + case SDL_MOUSEMOTION: + mouseMoved = true; + timeStamp[0] = dwCurTime + event.motion.timestamp; + timeStamp[1] = dwCurTime + event.motion.timestamp; + offs[0] += event.motion.xrel; + offs[1] += event.motion.yrel; + break; + case SDL_MOUSEBUTTONUP: + mouseState[event.button.button - 1] = false; + cbStack.back()->IR_OnMouseRelease(event.button.button - 1); + break; + case SDL_MOUSEBUTTONDOWN: + mouseState[event.button.button - 1] = true; + cbStack.back()->IR_OnMousePress(event.button.button - 1); + break; + case SDL_MOUSEWHEEL: + timeStamp[2] = dwCurTime + event.wheel.timestamp; + timeStamp[3] = dwCurTime + event.wheel.timestamp; + offs[2] += event.wheel.y; + offs[3] += event.wheel.x; + break; + } + } + + auto isButtonOnHold = [&](int i) + { + if (mouseState[i] && mouse_prev[i]) + cbStack.back()->IR_OnMouseHold(i); + }; + + isButtonOnHold(0); + isButtonOnHold(1); + isButtonOnHold(2); + isButtonOnHold(3); + isButtonOnHold(4); + isButtonOnHold(5); + isButtonOnHold(6); + isButtonOnHold(7); + + if (mouseMoved) + { + if (offs[0] || offs[1]) + cbStack.back()->IR_OnMouseMove(offs[0], offs[1]); + if (offs[2]) + cbStack.back()->IR_OnMouseWheel(offs[2]); + } + else + { + if (timeStamp[1] && dwCurTime - timeStamp[1] >= MouseDelta) + cbStack.back()->IR_OnMouseStop(0, timeStamp[1] = 0); + if (timeStamp[0] && dwCurTime - timeStamp[0] >= MouseDelta) + cbStack.back()->IR_OnMouseStop(0, timeStamp[0] = 0); + } +} -void CInput::SetMouseAcquire(BOOL bAcquire) {} -void CInput::SetKBDAcquire(BOOL bAcquire) {} -//----------------------------------------------------------------------- BOOL b_altF4 = FALSE; void CInput::KeyUpdate() { @@ -141,23 +220,24 @@ bool CInput::get_key_name(int dik, LPSTR dest_str, int dest_sz) #define MOUSE_1 (SDL_NUM_SCANCODES + SDL_BUTTON_LEFT) #define MOUSE_8 (SDL_NUM_SCANCODES + 8) -BOOL CInput::iGetAsyncKeyState(int dik) +bool CInput::iGetAsyncKeyState(int dik) { if (dik < COUNT_KB_BUTTONS) - return !!KBState[dik]; - else if (dik >= MOUSE_1 && dik <= MOUSE_8) + return KBState[dik]; + + if (dik >= MOUSE_1 && dik <= MOUSE_8) { - int mk = dik - MOUSE_1; + const int mk = dik - MOUSE_1; return iGetAsyncBtnState(mk); } - else - return FALSE; // unknown key ??? + + // unknown key ??? + return false; } -BOOL CInput::iGetAsyncBtnState(int btn) +bool CInput::iGetAsyncBtnState(int btn) { - if (btn <= COUNT_MOUSE_BUTTONS) - return !!mouseState[btn + 1]; + return mouseState[btn]; } void CInput::ClipCursor(bool clip) { @@ -247,7 +327,12 @@ void CInput::OnFrame(void) stats.FrameTime.Begin(); dwCurTime = RDEVICE.TimerAsync_MMT(); - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_MOUSEWHEEL)) + if (Device.dwPrecacheFrame == 0) + { + MouseUpdate(); + } + + while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_KEYMAPCHANGED)) { switch (event.type) { @@ -271,43 +356,6 @@ void CInput::OnFrame(void) } } break; - case SDL_MOUSEMOTION: -#ifndef _EDITOR - if (Device.dwPrecacheFrame == 0) -#endif - { - timeStamp[0] = event.motion.timestamp; - timeStamp[1] = event.motion.timestamp; - cbStack.back()->IR_OnMouseMove(event.motion.xrel, event.motion.yrel); - } - break; - case SDL_MOUSEBUTTONUP: -#ifndef _EDITOR - if (Device.dwPrecacheFrame == 0) -#endif - { - mouseState[event.button.button] = FALSE; - cbStack.back()->IR_OnKeyboardRelease(SDL_NUM_SCANCODES + event.button.button); - } - break; - case SDL_MOUSEBUTTONDOWN: -#ifndef _EDITOR - if (Device.dwPrecacheFrame == 0) -#endif - { - mouseState[event.button.button] = TRUE; - cbStack.back()->IR_OnKeyboardPress(SDL_NUM_SCANCODES + event.button.button); - } - break; - case SDL_MOUSEWHEEL: -#ifndef _EDITOR - if (Device.dwPrecacheFrame == 0) -#endif - { - timeStamp[2] = event.wheel.timestamp; - cbStack.back()->IR_OnMouseWheel(event.wheel.y); - } - break; } } diff --git a/src/xrEngine/xr_input.h b/src/xrEngine/xr_input.h index 6bdb039ac69..17746b33f13 100644 --- a/src/xrEngine/xr_input.h +++ b/src/xrEngine/xr_input.h @@ -24,8 +24,8 @@ class ENGINE_API CInput enum { COUNT_MOUSE_BUTTONS = 8, - COUNT_MOUSE_AXIS = 3, - COUNT_KB_BUTTONS = SDL_SCANCODE_MODE + COUNT_MOUSE_AXIS = 4, + COUNT_KB_BUTTONS = SDL_NUM_SCANCODES }; struct InputStatistics @@ -41,35 +41,37 @@ class ENGINE_API CInput u32 timeStamp[COUNT_MOUSE_AXIS]; u32 timeSave[COUNT_MOUSE_AXIS]; + int offs[COUNT_MOUSE_AXIS]; - BOOL mouseState[COUNT_MOUSE_BUTTONS]; - //---------------------- - BOOL KBState[COUNT_KB_BUTTONS]; + bool mouseState[COUNT_MOUSE_BUTTONS]; + bool KBState[COUNT_KB_BUTTONS]; xr_vector cbStack; + void MouseUpdate(); void KeyUpdate(); InputStatistics stats; public: u32 dwCurTime; + u32 MouseDelta; const InputStatistics& GetStats() const { return stats; } void DumpStatistics(class IGameFont& font, class IPerformanceAlert* alert); - void SetAllAcquire(BOOL bAcquire = TRUE); - void SetMouseAcquire(BOOL bAcquire); - void SetKBDAcquire(BOOL bAcquire); + void SetAllAcquire(bool bAcquire = true); + void SetMouseAcquire(bool bAcquire); + void SetKBDAcquire(bool bAcquire); void iCapture(IInputReceiver* pc); void iRelease(IInputReceiver* pc); - BOOL iGetAsyncKeyState(int dik); - BOOL iGetAsyncBtnState(int btn); + bool iGetAsyncKeyState(int dik); + bool iGetAsyncBtnState(int btn); void iGetLastMouseDelta(Ivector2& p) { p.set(offs[0], offs[1]); } void ClipCursor(bool clip); - CInput(BOOL bExclusive = true, int deviceForInit = default_key); + CInput(bool exclusive = true, int deviceForInit = default_key); ~CInput(); virtual void OnFrame(void); diff --git a/src/xrGame/MainMenu.cpp b/src/xrGame/MainMenu.cpp index 89ff84a41a5..7103fb7c581 100644 --- a/src/xrGame/MainMenu.cpp +++ b/src/xrGame/MainMenu.cpp @@ -298,8 +298,9 @@ bool CMainMenu::ReloadUI() bool CMainMenu::IsActive() { return !!m_Flags.test(flActive); } bool CMainMenu::CanSkipSceneRendering() { return IsActive() && !m_Flags.test(flGameSaveScreenshot); } + // IInputReceiver -static int mouse_button_2_key[] = {SDL_NUM_SCANCODES, MOUSE_1, MOUSE_2, MOUSE_3}; +static int mouse_button_2_key[] = { MOUSE_1, MOUSE_2, MOUSE_3, MOUSE_4, MOUSE_5, MOUSE_6, MOUSE_7, MOUSE_8 }; void CMainMenu::IR_OnMousePress(int btn) { if (!IsActive())