Skip to content

Commit be4712d

Browse files
committed
Mouse works as good as it was before
1 parent cbe6db8 commit be4712d

File tree

3 files changed

+116
-65
lines changed

3 files changed

+116
-65
lines changed

src/xrEngine/xr_input.cpp

Lines changed: 101 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ static void on_error_dialog(bool before)
3737
pInput->acquire(true);
3838
}
3939

40-
CInput::CInput(BOOL bExclusive, int deviceForInit)
40+
CInput::CInput(bool exclusive, int deviceForInit)
4141
{
42-
g_exclusive = !!bExclusive;
42+
g_exclusive = exclusive;
4343

4444
Log("Starting INPUT device...");
4545

46+
MouseDelta = 25;
47+
4648
ZeroMemory(mouseState, sizeof(mouseState));
4749
ZeroMemory(KBState, sizeof(KBState));
4850
ZeroMemory(timeStamp, sizeof(timeStamp));
@@ -80,11 +82,88 @@ void CInput::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)
8082
font.OutNext("*** INPUT: %2.2fms", pInput->GetStats().FrameTime.result);
8183
}
8284

83-
void CInput::SetAllAcquire(BOOL bAcquire) {}
85+
void CInput::SetAllAcquire(bool bAcquire) {}
86+
87+
void CInput::SetMouseAcquire(bool bAcquire) {}
88+
void CInput::SetKBDAcquire(bool bAcquire) {}
89+
90+
91+
void CInput::MouseUpdate()
92+
{
93+
SDL_Event event;
94+
95+
bool mouse_prev[COUNT_MOUSE_BUTTONS];
96+
97+
mouse_prev[0] = mouseState[0];
98+
mouse_prev[1] = mouseState[1];
99+
mouse_prev[2] = mouseState[2];
100+
mouse_prev[3] = mouseState[3];
101+
mouse_prev[4] = mouseState[4];
102+
mouse_prev[5] = mouseState[5];
103+
mouse_prev[6] = mouseState[6];
104+
mouse_prev[7] = mouseState[7];
105+
106+
bool mouseMoved = false;
107+
offs[0] = offs[1] = offs[2] = 0;
108+
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEWHEEL))
109+
{
110+
switch (event.type)
111+
{
112+
case SDL_MOUSEMOTION:
113+
mouseMoved = true;
114+
timeStamp[0] = dwCurTime + event.motion.timestamp;
115+
timeStamp[1] = dwCurTime + event.motion.timestamp;
116+
offs[0] += event.motion.xrel;
117+
offs[1] += event.motion.yrel;
118+
break;
119+
case SDL_MOUSEBUTTONUP:
120+
mouseState[event.button.button - 1] = false;
121+
cbStack.back()->IR_OnMouseRelease(event.button.button - 1);
122+
break;
123+
case SDL_MOUSEBUTTONDOWN:
124+
mouseState[event.button.button - 1] = true;
125+
cbStack.back()->IR_OnMousePress(event.button.button - 1);
126+
break;
127+
case SDL_MOUSEWHEEL:
128+
timeStamp[2] = dwCurTime + event.wheel.timestamp;
129+
timeStamp[3] = dwCurTime + event.wheel.timestamp;
130+
offs[2] += event.wheel.y;
131+
offs[3] += event.wheel.x;
132+
break;
133+
}
134+
}
135+
136+
auto isButtonOnHold = [&](int i)
137+
{
138+
if (mouseState[i] && mouse_prev[i])
139+
cbStack.back()->IR_OnMouseHold(i);
140+
};
141+
142+
isButtonOnHold(0);
143+
isButtonOnHold(1);
144+
isButtonOnHold(2);
145+
isButtonOnHold(3);
146+
isButtonOnHold(4);
147+
isButtonOnHold(5);
148+
isButtonOnHold(6);
149+
isButtonOnHold(7);
150+
151+
if (mouseMoved)
152+
{
153+
if (offs[0] || offs[1])
154+
cbStack.back()->IR_OnMouseMove(offs[0], offs[1]);
155+
if (offs[2])
156+
cbStack.back()->IR_OnMouseWheel(offs[2]);
157+
}
158+
else
159+
{
160+
if (timeStamp[1] && dwCurTime - timeStamp[1] >= MouseDelta)
161+
cbStack.back()->IR_OnMouseStop(0, timeStamp[1] = 0);
162+
if (timeStamp[0] && dwCurTime - timeStamp[0] >= MouseDelta)
163+
cbStack.back()->IR_OnMouseStop(0, timeStamp[0] = 0);
164+
}
165+
}
84166

85-
void CInput::SetMouseAcquire(BOOL bAcquire) {}
86-
void CInput::SetKBDAcquire(BOOL bAcquire) {}
87-
//-----------------------------------------------------------------------
88167
BOOL b_altF4 = FALSE;
89168
void CInput::KeyUpdate()
90169
{
@@ -141,23 +220,24 @@ bool CInput::get_key_name(int dik, LPSTR dest_str, int dest_sz)
141220
#define MOUSE_1 (SDL_NUM_SCANCODES + SDL_BUTTON_LEFT)
142221
#define MOUSE_8 (SDL_NUM_SCANCODES + 8)
143222

144-
BOOL CInput::iGetAsyncKeyState(int dik)
223+
bool CInput::iGetAsyncKeyState(int dik)
145224
{
146225
if (dik < COUNT_KB_BUTTONS)
147-
return !!KBState[dik];
148-
else if (dik >= MOUSE_1 && dik <= MOUSE_8)
226+
return KBState[dik];
227+
228+
if (dik >= MOUSE_1 && dik <= MOUSE_8)
149229
{
150-
int mk = dik - MOUSE_1;
230+
const int mk = dik - MOUSE_1;
151231
return iGetAsyncBtnState(mk);
152232
}
153-
else
154-
return FALSE; // unknown key ???
233+
234+
// unknown key ???
235+
return false;
155236
}
156237

157-
BOOL CInput::iGetAsyncBtnState(int btn)
238+
bool CInput::iGetAsyncBtnState(int btn)
158239
{
159-
if (btn <= COUNT_MOUSE_BUTTONS)
160-
return !!mouseState[btn + 1];
240+
return mouseState[btn];
161241
}
162242
void CInput::ClipCursor(bool clip)
163243
{
@@ -247,7 +327,12 @@ void CInput::OnFrame(void)
247327
stats.FrameTime.Begin();
248328
dwCurTime = RDEVICE.TimerAsync_MMT();
249329

250-
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_MOUSEWHEEL))
330+
if (Device.dwPrecacheFrame == 0)
331+
{
332+
MouseUpdate();
333+
}
334+
335+
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_KEYMAPCHANGED))
251336
{
252337
switch (event.type)
253338
{
@@ -271,43 +356,6 @@ void CInput::OnFrame(void)
271356
}
272357
}
273358
break;
274-
case SDL_MOUSEMOTION:
275-
#ifndef _EDITOR
276-
if (Device.dwPrecacheFrame == 0)
277-
#endif
278-
{
279-
timeStamp[0] = event.motion.timestamp;
280-
timeStamp[1] = event.motion.timestamp;
281-
cbStack.back()->IR_OnMouseMove(event.motion.xrel, event.motion.yrel);
282-
}
283-
break;
284-
case SDL_MOUSEBUTTONUP:
285-
#ifndef _EDITOR
286-
if (Device.dwPrecacheFrame == 0)
287-
#endif
288-
{
289-
mouseState[event.button.button] = FALSE;
290-
cbStack.back()->IR_OnKeyboardRelease(SDL_NUM_SCANCODES + event.button.button);
291-
}
292-
break;
293-
case SDL_MOUSEBUTTONDOWN:
294-
#ifndef _EDITOR
295-
if (Device.dwPrecacheFrame == 0)
296-
#endif
297-
{
298-
mouseState[event.button.button] = TRUE;
299-
cbStack.back()->IR_OnKeyboardPress(SDL_NUM_SCANCODES + event.button.button);
300-
}
301-
break;
302-
case SDL_MOUSEWHEEL:
303-
#ifndef _EDITOR
304-
if (Device.dwPrecacheFrame == 0)
305-
#endif
306-
{
307-
timeStamp[2] = event.wheel.timestamp;
308-
cbStack.back()->IR_OnMouseWheel(event.wheel.y);
309-
}
310-
break;
311359
}
312360
}
313361

src/xrEngine/xr_input.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class ENGINE_API CInput
2424
enum
2525
{
2626
COUNT_MOUSE_BUTTONS = 8,
27-
COUNT_MOUSE_AXIS = 3,
28-
COUNT_KB_BUTTONS = SDL_SCANCODE_MODE
27+
COUNT_MOUSE_AXIS = 4,
28+
COUNT_KB_BUTTONS = SDL_NUM_SCANCODES
2929
};
3030

3131
struct InputStatistics
@@ -41,35 +41,37 @@ class ENGINE_API CInput
4141

4242
u32 timeStamp[COUNT_MOUSE_AXIS];
4343
u32 timeSave[COUNT_MOUSE_AXIS];
44+
4445
int offs[COUNT_MOUSE_AXIS];
45-
BOOL mouseState[COUNT_MOUSE_BUTTONS];
4646

47-
//----------------------
48-
BOOL KBState[COUNT_KB_BUTTONS];
47+
bool mouseState[COUNT_MOUSE_BUTTONS];
48+
bool KBState[COUNT_KB_BUTTONS];
4949

5050
xr_vector<IInputReceiver*> cbStack;
5151

52+
void MouseUpdate();
5253
void KeyUpdate();
5354

5455
InputStatistics stats;
5556

5657
public:
5758
u32 dwCurTime;
59+
u32 MouseDelta;
5860

5961
const InputStatistics& GetStats() const { return stats; }
6062
void DumpStatistics(class IGameFont& font, class IPerformanceAlert* alert);
61-
void SetAllAcquire(BOOL bAcquire = TRUE);
62-
void SetMouseAcquire(BOOL bAcquire);
63-
void SetKBDAcquire(BOOL bAcquire);
63+
void SetAllAcquire(bool bAcquire = true);
64+
void SetMouseAcquire(bool bAcquire);
65+
void SetKBDAcquire(bool bAcquire);
6466

6567
void iCapture(IInputReceiver* pc);
6668
void iRelease(IInputReceiver* pc);
67-
BOOL iGetAsyncKeyState(int dik);
68-
BOOL iGetAsyncBtnState(int btn);
69+
bool iGetAsyncKeyState(int dik);
70+
bool iGetAsyncBtnState(int btn);
6971
void iGetLastMouseDelta(Ivector2& p) { p.set(offs[0], offs[1]); }
7072
void ClipCursor(bool clip);
7173

72-
CInput(BOOL bExclusive = true, int deviceForInit = default_key);
74+
CInput(bool exclusive = true, int deviceForInit = default_key);
7375
~CInput();
7476

7577
virtual void OnFrame(void);

src/xrGame/MainMenu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ bool CMainMenu::ReloadUI()
300300

301301
bool CMainMenu::IsActive() { return !!m_Flags.test(flActive); }
302302
bool CMainMenu::CanSkipSceneRendering() { return IsActive() && !m_Flags.test(flGameSaveScreenshot); }
303+
303304
// IInputReceiver
304-
static int mouse_button_2_key[] = {SDL_NUM_SCANCODES, MOUSE_1, MOUSE_2, MOUSE_3};
305+
static int mouse_button_2_key[] = { MOUSE_1, MOUSE_2, MOUSE_3, MOUSE_4, MOUSE_5, MOUSE_6, MOUSE_7, MOUSE_8 };
305306
void CMainMenu::IR_OnMousePress(int btn)
306307
{
307308
if (!IsActive())

0 commit comments

Comments
 (0)