@@ -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- // -----------------------------------------------------------------------
88167BOOL b_altF4 = FALSE ;
89168void 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}
162242void 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
0 commit comments