@@ -65,6 +65,7 @@ CInput::CInput(BOOL bExclusive, int deviceForInit)
6565
6666CInput::~CInput (void )
6767{
68+ SDL_SetRelativeMouseMode (SDL_FALSE);
6869#ifdef ENGINE_BUILD
6970 Device.seqFrame .Remove (this );
7071 Device.seqAppDeactivate .Remove (this );
@@ -79,89 +80,67 @@ void CInput::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)
7980 font.OutNext (" *** INPUT: %2.2fms" , pInput->GetStats ().FrameTime .result );
8081}
8182
82- void CInput::SetAllAcquire (BOOL bAcquire)
83- {
84- }
83+ void CInput::SetAllAcquire (BOOL bAcquire) {}
8584
86- void CInput::SetMouseAcquire (BOOL bAcquire)
87- {
88- }
85+ void CInput::SetMouseAcquire (BOOL bAcquire) {}
8986void CInput::SetKBDAcquire (BOOL bAcquire) {}
9087// -----------------------------------------------------------------------
91- void CInput::KeyUpdate (SDL_Event* event)
88+ BOOL b_altF4 = FALSE ;
89+ void CInput::KeyUpdate ()
9290{
91+ if (b_altF4)
92+ return ;
93+
9394 bool b_dik_pause_was_pressed = false ;
9495
95- if (SDL_SCANCODE_PAUSE == event->key .keysym .scancode )
96+ const Uint8* state = SDL_GetKeyboardState (NULL );
97+ #ifndef _EDITOR
98+ bool b_alt_tab = false ;
99+
100+ if (!b_altF4 && state[SDL_SCANCODE_F4] && (state[SDL_SCANCODE_RALT] || state[SDL_SCANCODE_LALT]))
96101 {
97- if (SDL_KEYDOWN == event->key .type )
98- b_dik_pause_was_pressed = true ;
102+ b_altF4 = TRUE ;
103+ Engine.Event .Defer (" KERNEL:disconnect" );
104+ Engine.Event .Defer (" KERNEL:quit" );
105+ SDL_Event ev;
106+ ev.type = SDL_QUIT;
107+ SDL_PushEvent (&ev);
99108 }
109+ #endif
110+ if (b_altF4)
111+ return ;
100112
101113#ifndef _EDITOR
102- bool b_alt_tab = false ;
103-
104114 if (Device.dwPrecacheFrame == 0 )
105115#endif
106116 {
107- const Uint8* state = SDL_GetKeyboardState (NULL );
108-
109- if (SDL_KEYDOWN == event->key .type )
110- cbStack.back ()->IR_OnKeyboardPress (event->key .keysym .scancode );
111- else if (SDL_KEYUP == event->key .type )
117+ for (u32 i = 0 ; i < COUNT_KB_BUTTONS; i++)
112118 {
113- cbStack.back ()->IR_OnKeyboardRelease (event->key .keysym .scancode );
119+ if (state[i])
120+ cbStack.back ()->IR_OnKeyboardPress (i);
121+ else
122+ {
123+ cbStack.back ()->IR_OnKeyboardRelease (i);
114124#ifndef _EDITOR
115- if (SDL_SCANCODE_TAB == event->key .keysym .scancode && KMOD_ALT == SDL_GetModState ())
116- b_alt_tab = true ;
125+ if (SDL_SCANCODE_TAB == state[i] &&
126+ (iGetAsyncKeyState (SDL_SCANCODE_LALT) || iGetAsyncKeyState (SDL_SCANCODE_RALT)))
127+ b_alt_tab = true ;
117128#endif
129+ }
118130 }
119131
120132 for (u32 i = 0 ; i < COUNT_KB_BUTTONS; i++)
121- if (state[i])
133+ if (KBState[i] && state[i])
122134 cbStack.back ()->IR_OnKeyboardHold (i);
135+
136+ for (u32 idx = 0 ; idx < COUNT_KB_BUTTONS; idx++)
137+ KBState[idx] = state[idx];
123138 }
124139
125140#ifndef _EDITOR
126141 if (b_alt_tab)
127142 SDL_MinimizeWindow (Device.m_sdlWnd );
128143#endif
129- /*
130- #ifndef _EDITOR
131- //update xinput if exist
132- for( DWORD iUserIndex=0; iUserIndex<DXUT_MAX_CONTROLLERS; iUserIndex++ )
133- {
134- DXUTGetGamepadState( iUserIndex, &g_GamePads[iUserIndex], true, false );
135-
136- if( !g_GamePads[iUserIndex].bConnected )
137- continue; // unplugged?
138-
139- bool new_b, old_b;
140- new_b = !!(g_GamePads[iUserIndex].wPressedButtons & XINPUT_GAMEPAD_A);
141- old_b = !!(g_GamePads[iUserIndex].wLastButtons & XINPUT_GAMEPAD_A);
142-
143- if(new_b != old_b)
144- {
145- if(old_b)
146- cbStack.back()->IR_OnMousePress(0);
147- else
148- cbStack.back()->IR_OnMouseRelease(0);
149- }
150- int dx,dy;
151- dx = iFloor(g_GamePads[iUserIndex].fThumbRX*6);
152- dy = iFloor(g_GamePads[iUserIndex].fThumbRY*6);
153- if(dx || dy)
154- cbStack.back()->IR_OnMouseMove ( dx, dy );
155- }
156-
157- if(Device.fTimeGlobal > stop_vibration_time)
158- {
159- stop_vibration_time = flt_max;
160- set_vibration (0, 0);
161- }
162- //xinput
163- #endif
164- */
165144}
166145
167146bool CInput::get_key_name (int dik, LPSTR dest_str, int dest_sz)
@@ -209,46 +188,71 @@ void CInput::ClipCursor(bool clip)
209188 }
210189}
211190
212- void CInput::MouseUpdate (SDL_Event * event)
191+ void CInput::MouseUpdate (SDL_Event* event)
213192{
214- DWORD dwElements = MOUSEBUFFERSIZE;
215-
216193#ifndef _EDITOR
217194 if (Device.dwPrecacheFrame )
218195 return ;
219196#endif
220197 BOOL mouse_prev[COUNT_MOUSE_BUTTONS];
221198
222- mouse_prev[0 ] = mouseState[0 ];
223- mouse_prev[1 ] = mouseState[1 ];
224- mouse_prev[2 ] = mouseState[2 ];
225- mouse_prev[3 ] = mouseState[3 ];
226- mouse_prev[4 ] = mouseState[4 ];
227- mouse_prev[5 ] = mouseState[5 ];
228- mouse_prev[6 ] = mouseState[6 ];
229- mouse_prev[7 ] = mouseState[7 ];
230-
231199 offs[0 ] = offs[1 ] = offs[2 ] = 0 ;
232200
233201 switch (event->type )
234202 {
235203 case SDL_MOUSEMOTION:
204+ {
205+ offs[0 ] += event->motion .xrel ;
206+ offs[1 ] += event->motion .yrel ;
236207 timeStamp[0 ] = event->motion .timestamp ;
237- cbStack.back ()->IR_OnMouseMove (event->motion .xrel , event->motion .yrel );
238- break ;
239- case SDL_MOUSEBUTTONUP:
240- cbStack.back ()->IR_OnMouseRelease (event->button .button );
208+ timeStamp[1 ] = event->motion .timestamp ;
209+ if (offs[0 ] || offs[1 ])
210+ cbStack.back ()->IR_OnMouseMove (offs[0 ], offs[1 ]);
211+ }
212+ break ;
213+ case SDL_MOUSEBUTTONUP:
214+ mouseState[event->button .button ] = FALSE ;
215+ cbStack.back ()->IR_OnKeyboardRelease (SDL_NUM_SCANCODES + event->button .button );
241216 break ;
242217 case SDL_MOUSEBUTTONDOWN:
243- cbStack.back ()->IR_OnMousePress (event->button .button );
218+ mouseState[event->button .button ] = TRUE ;
219+ cbStack.back ()->IR_OnKeyboardPress (SDL_NUM_SCANCODES + event->button .button );
244220 break ;
245221 case SDL_MOUSEWHEEL:
222+ offs[2 ] += event->wheel .direction ;
246223 timeStamp[2 ] = event->wheel .timestamp ;
247- cbStack.back ()->IR_OnMouseWheel (event->wheel .direction );
224+ if (offs[2 ])
225+ cbStack.back ()->IR_OnMouseWheel (offs[2 ]);
226+ break ;
227+ default :
228+ if (timeStamp[1 ] && ((dwCurTime - timeStamp[1 ]) >= 25 ))
229+ cbStack.back ()->IR_OnMouseStop (1 , timeStamp[1 ] = 0 );
230+ if (timeStamp[0 ] && ((dwCurTime - timeStamp[0 ]) >= 25 ))
231+ cbStack.back ()->IR_OnMouseStop (0 , timeStamp[0 ] = 0 );
248232 break ;
249- default : break ;
250233 }
251234
235+ auto isButtonOnHold = [&](int i) {
236+ if (mouseState[i] && mouse_prev[i])
237+ cbStack.back ()->IR_OnMouseHold (i);
238+ };
239+
240+ isButtonOnHold (0 );
241+ isButtonOnHold (1 );
242+ isButtonOnHold (2 );
243+ isButtonOnHold (3 );
244+ isButtonOnHold (4 );
245+ isButtonOnHold (5 );
246+ isButtonOnHold (6 );
247+
248+ mouse_prev[0 ] = mouseState[0 ];
249+ mouse_prev[1 ] = mouseState[1 ];
250+ mouse_prev[2 ] = mouseState[2 ];
251+ mouse_prev[3 ] = mouseState[3 ];
252+ mouse_prev[4 ] = mouseState[4 ];
253+ mouse_prev[5 ] = mouseState[5 ];
254+ mouse_prev[6 ] = mouseState[6 ];
255+ mouse_prev[7 ] = mouseState[7 ];
252256}
253257
254258// -------------------------------------------------------
@@ -327,41 +331,29 @@ void CInput::OnFrame(void)
327331
328332 while (SDL_PollEvent (&event))
329333 {
334+ BOOL b_break_cycle = false ;
330335 switch (event.type )
331336 {
332337 case SDL_KEYDOWN:
333- case SDL_KEYUP: KeyUpdate (&event ); continue ;
338+ case SDL_KEYUP: KeyUpdate (); continue ;
334339
335340 case SDL_MOUSEMOTION:
336341 case SDL_MOUSEBUTTONUP:
337342 case SDL_MOUSEBUTTONDOWN:
338343 case SDL_MOUSEWHEEL:
339344 MouseUpdate (&event);
340- // MouseUpdate();
341345 continue ;
342- // case SDL_WINDOWEVENT:
343- // switch (event.window.event)
344- // {
345- // case SDL_WINDOWEVENT_CLOSE:
346- // event.type = SDL_QUIT;
347- // SDL_PushEvent(&event);
348- // continue;
349- // case SDL_WINDOWEVENT_ENTER:
350- // #if SDL_VERSION_ATLEAST(2, 0, 5)
351- // case SDL_WINDOWEVENT_TAKE_FOCUS:
352- // RDEVICE.OnWM_Activate(event.window.data1, event.window.data2);
353- // continue;
354- // #endif
355- // default: SDL_Log("Window %d got unknown event %d", event.window.windowID, event.window.event); continue;
356- // }
357- // continue;
358- case SDL_QUIT:
359- Engine.Event .Defer (" KERNEL:disconnect" );
360- Engine.Event .Defer (" KERNEL:quit" );
346+ case SDL_QUIT: // go to outside event loop
347+ event.type = SDL_QUIT;
348+ SDL_PushEvent (&event);
349+ b_break_cycle = TRUE ;
361350 break ;
362351
363352 default : continue ;
364353 }
354+
355+ if (b_break_cycle)
356+ break ;
365357 }
366358
367359 stats.FrameTime .End ();
@@ -380,9 +372,9 @@ void CInput::unacquire() {}
380372
381373void CInput::acquire (const bool & exclusive)
382374{
383- // pMouse->SetCooperativeLevel(Device.editor() ? Device.editor()->main_handle() : RDEVICE.m_sdlWnd,
375+ // pMouse->SetCooperativeLevel(Device.editor() ? Device.editor()->main_handle() : RDEVICE.m_sdlWnd,
384376 // (exclusive ? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE) | DISCL_FOREGROUND | DISCL_NOWINKEY);
385- // pMouse->Acquire();
377+ // pMouse->Acquire();
386378}
387379
388380void CInput::exclusive_mode (const bool & exclusive)
0 commit comments