Skip to content

Commit 44657a1

Browse files
committed
Support for exclusive input mode
Tweaks for window hit test
1 parent d513f36 commit 44657a1

File tree

11 files changed

+81
-103
lines changed

11 files changed

+81
-103
lines changed

src/xrEngine/Device_Initialize.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Include/editor/ide.hpp"
66
#include "engine_impl.hpp"
7+
#include "xr_input.h"
78
#include "GameFont.h"
89
#include "PerformanceAlert.hpp"
910
#include "xrCore/ModuleLookup.hpp"
@@ -72,15 +73,18 @@ void CRenderDevice::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)
7273

7374
SDL_HitTestResult WindowHitTest(SDL_Window* /*window*/, const SDL_Point* area, void* /*data*/)
7475
{
76+
if (pInput->InputIsGrabbed())
77+
return SDL_HITTEST_NORMAL;
78+
7579
const auto& rect = Device.m_rcWindowClient;
7680

7781
// size of additional interactive area (in pixels)
7882
constexpr int hit = 15;
7983

80-
const bool leftSide = area->x < rect.x + hit;
81-
const bool topSide = area->y < rect.y + hit;
82-
const bool bottomSide = area->y > rect.h - hit;
83-
const bool rightSide = area->x > rect.w - hit;
84+
const bool leftSide = area->x <= rect.x + hit;
85+
const bool topSide = area->y <= rect.y + hit;
86+
const bool bottomSide = area->y >= rect.h - hit;
87+
const bool rightSide = area->x >= rect.w - hit;
8488

8589
if (leftSide && topSide)
8690
return SDL_HITTEST_RESIZE_TOPLEFT;
@@ -106,14 +110,5 @@ SDL_HitTestResult WindowHitTest(SDL_Window* /*window*/, const SDL_Point* area, v
106110
if (leftSide)
107111
return SDL_HITTEST_RESIZE_LEFT;
108112

109-
const int centerX = rect.w / 2;
110-
const int centerY = rect.h / 2;
111-
112-
// Allow drag from any point except window center
113-
// For this case, 'hit' is a size of a square in the center
114-
if ((area->x > centerX + hit || area->x < centerX - hit)
115-
|| (area->y > centerY + hit || area->y < centerY - hit))
116-
return SDL_HITTEST_DRAGGABLE;
117-
118-
return SDL_HITTEST_NORMAL;
113+
return SDL_HITTEST_DRAGGABLE;
119114
}

src/xrEngine/Device_destroy.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ void CRenderDevice::Destroy()
1616
if (!b_is_Ready)
1717
return;
1818
Log("Destroying Direct3D...");
19-
pInput->ClipCursor(false);
2019
GEnv.Render->ValidateHW();
2120
GEnv.DU->OnDeviceDestroy();
2221
b_is_Ready = false;
@@ -51,7 +50,7 @@ void CRenderDevice::Reset(bool precache)
5150
{
5251
const auto dwWidth_before = dwWidth;
5352
const auto dwHeight_before = dwHeight;
54-
pInput->ClipCursor(false);
53+
pInput->GrabInput(false);
5554

5655
const auto tm_start = TimerAsync();
5756

@@ -76,5 +75,5 @@ void CRenderDevice::Reset(bool precache)
7675
seqResolutionChanged.Process();
7776

7877
if (!GEnv.isDedicatedServer)
79-
pInput->ClipCursor(true);
78+
pInput->GrabInput(true);
8079
}

src/xrEngine/XR_IOConsole.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ void CConsole::Hide()
624624
// if ( g_pGameLevel ||
625625
// ( g_pGamePersistent && g_pGamePersistent->m_pMainMenu && g_pGamePersistent->m_pMainMenu->IsActive() ))
626626

627-
if (pInput->get_exclusive_mode())
627+
if (pInput->IsExclusiveMode())
628628
{
629629
SDL_WarpMouseGlobal(m_mouse_pos.x, m_mouse_pos.y); // Replace with SDL_WarpMouseInWindow in case set window-relative coordinates
630630
}

src/xrEngine/device.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ void CRenderDevice::message_loop()
383383
OnWM_Activate(0, event.window.data2);
384384
break;
385385

386+
case SDL_WINDOWEVENT_ENTER:
387+
SDL_ShowCursor(SDL_FALSE);
388+
break;
389+
390+
case SDL_WINDOWEVENT_LEAVE:
391+
SDL_ShowCursor(SDL_TRUE);
392+
break;
393+
386394
case SDL_WINDOWEVENT_CLOSE:
387395
event.type = SDL_QUIT;
388396
}
@@ -425,7 +433,7 @@ void CRenderDevice::Run()
425433
SDL_FlushEvents(SDL_WINDOWEVENT, SDL_SYSWMEVENT);
426434
SDL_ShowWindow(m_sdlWnd);
427435
SDL_RaiseWindow(m_sdlWnd);
428-
pInput->ClipCursor(true);
436+
pInput->GrabInput(true);
429437
message_loop();
430438
seqAppEnd.Process();
431439
// Stop Balance-Thread
@@ -553,9 +561,9 @@ void CRenderDevice::OnWM_Activate(WPARAM wParam, LPARAM /*lParam*/)
553561
const BOOL isWndActive = (fActive != WA_INACTIVE && !fMinimized) ? TRUE : FALSE;
554562

555563
if (!editor() && !GEnv.isDedicatedServer && isWndActive)
556-
pInput->ClipCursor(true);
564+
pInput->GrabInput(true);
557565
else
558-
pInput->ClipCursor(false);
566+
pInput->GrabInput(false);
559567

560568
extern int ps_always_active;
561569
const BOOL isGameActive = ps_always_active || isWndActive;

src/xrEngine/edit_actions.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "edit_actions.h"
1010
#include "line_edit_control.h"
1111
#include "xr_input.h"
12-
#include <locale.h>
1312
#include <codecvt>
1413

1514
namespace text_editor
@@ -76,7 +75,7 @@ void type_pair::on_key_press(line_edit_control* const control)
7675
char c_shift = m_char_shift;
7776

7877
SDL_Event event;
79-
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_TEXTEDITING, SDL_TEXTINPUT))
78+
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_TEXTINPUT, SDL_TEXTINPUT))
8079
{
8180
switch (event.type)
8281
{
@@ -92,10 +91,6 @@ void type_pair::on_key_press(line_edit_control* const control)
9291
}
9392
break;
9493
}
95-
96-
case SDL_TEXTEDITING:
97-
// XXX: use this?
98-
break;
9994
}
10095
}
10196

src/xrEngine/line_edit_control.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,10 @@ void line_edit_control::clamp_cur_pos() { clamp(m_cur_pos, 0, (int)xr_strlen(m_e
720720
void line_edit_control::SwitchKL()
721721
{
722722
#ifdef WINDOWS
723-
if (pInput->get_exclusive_mode())
723+
// XXX: do we even need this?
724+
// Check if SDL_HINT_GRAB_KEYBOARD works
725+
// and enable in case if we will need this
726+
if (false && pInput->IsExclusiveMode())
724727
ActivateKeyboardLayout((HKL)HKL_NEXT, 0);
725728
#endif
726729
}

src/xrEngine/x_ray.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ void CApplication::OnEvent(EVENT E, u64 P1, u64 P2)
159159
{
160160
if (E == eQuit)
161161
{
162-
if (pInput != NULL)
163-
pInput->ClipCursor(false);
162+
if (pInput != nullptr)
163+
pInput->GrabInput(false);
164164

165165
g_SASH.EndBenchmark();
166166

@@ -200,8 +200,8 @@ void CApplication::OnEvent(EVENT E, u64 P1, u64 P2)
200200
}
201201
else if (E == eDisconnect)
202202
{
203-
if (pInput != NULL && TRUE == Engine.Event.Peek("KERNEL:quit"))
204-
pInput->ClipCursor(false);
203+
if (pInput != nullptr && TRUE == Engine.Event.Peek("KERNEL:quit"))
204+
pInput->GrabInput(false);
205205

206206
if (g_pGameLevel)
207207
{

src/xrEngine/xr_input.cpp

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,20 @@ ENGINE_API Flags32 psMouseInvert = {FALSE};
1919

2020
float stop_vibration_time = flt_max;
2121

22-
#define MOUSEBUFFERSIZE 64
23-
#define KEYBOARDBUFFERSIZE 64
24-
25-
static bool g_exclusive = true;
2622
static void on_error_dialog(bool before)
2723
{
28-
if (!pInput || !g_exclusive || Device.editor())
24+
if (!pInput || !pInput->IsExclusiveMode() || Device.editor())
2925
return;
3026

3127
if (before)
32-
{
33-
pInput->ClipCursor(false);
34-
pInput->unacquire();
35-
}
28+
pInput->GrabInput(false);
3629
else
37-
{
38-
pInput->ClipCursor(true);
39-
pInput->acquire(true);
40-
}
30+
pInput->GrabInput(true);
4131
}
4232

43-
CInput::CInput(bool exclusive, int deviceForInit)
33+
CInput::CInput(const bool exclusive)
4434
{
45-
g_exclusive = exclusive;
35+
exclusiveInput = exclusive;
4636

4737
Log("Starting INPUT device...");
4838

@@ -58,22 +48,20 @@ CInput::CInput(bool exclusive, int deviceForInit)
5848

5949
xrDebug::SetDialogHandler(on_error_dialog);
6050

51+
SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
6152
SDL_StopTextInput(); // sanity
6253

63-
#ifdef ENGINE_BUILD
6454
Device.seqAppActivate.Add(this);
6555
Device.seqAppDeactivate.Add(this, REG_PRIORITY_HIGH);
6656
Device.seqFrame.Add(this, REG_PRIORITY_HIGH);
67-
#endif
6857
}
6958

70-
CInput::~CInput(void)
59+
CInput::~CInput()
7160
{
72-
#ifdef ENGINE_BUILD
61+
GrabInput(false);
7362
Device.seqFrame.Remove(this);
7463
Device.seqAppDeactivate.Remove(this);
7564
Device.seqAppActivate.Remove(this);
76-
#endif
7765
}
7866

7967
//-----------------------------------------------------------------------
@@ -83,12 +71,6 @@ void CInput::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)
8371
font.OutNext("*** INPUT: %2.2fms", pInput->GetStats().FrameTime.result);
8472
}
8573

86-
void CInput::SetAllAcquire(bool bAcquire) {}
87-
88-
void CInput::SetMouseAcquire(bool bAcquire) {}
89-
void CInput::SetKBDAcquire(bool bAcquire) {}
90-
91-
9274
void CInput::MouseUpdate()
9375
{
9476
SDL_PumpEvents();
@@ -255,21 +237,37 @@ bool CInput::iGetAsyncBtnState(int btn)
255237
{
256238
return mouseState[btn];
257239
}
258-
void CInput::ClipCursor(bool clip)
240+
241+
void CInput::ClipCursor(const bool clip)
259242
{
260243
if (clip)
261244
{
262-
SDL_SetWindowGrab(Device.m_sdlWnd, SDL_TRUE);
245+
SDL_ShowCursor(SDL_TRUE);
263246
SDL_SetRelativeMouseMode(SDL_TRUE);
264247
}
265248
else
266249
{
267-
SDL_SetWindowGrab(Device.m_sdlWnd, SDL_FALSE);
250+
SDL_ShowCursor(SDL_FALSE);
268251
SDL_SetRelativeMouseMode(SDL_FALSE);
269252
}
270253
}
271254

272-
//-------------------------------------------------------
255+
void CInput::GrabInput(const bool grab)
256+
{
257+
ClipCursor(grab);
258+
259+
if (IsExclusiveMode())
260+
SDL_SetWindowGrab(Device.m_sdlWnd, grab ? SDL_TRUE : SDL_FALSE);
261+
262+
inputGrabbed = grab;
263+
264+
}
265+
266+
bool CInput::InputIsGrabbed() const
267+
{
268+
return inputGrabbed;
269+
}
270+
273271
void CInput::iCapture(IInputReceiver* p)
274272
{
275273
VERIFY(p);
@@ -313,7 +311,6 @@ void CInput::OnAppActivate(void)
313311
if (CurrentIR())
314312
CurrentIR()->IR_OnActivate();
315313

316-
SetAllAcquire(true);
317314
ZeroMemory(mouseState, sizeof(mouseState));
318315
ZeroMemory(keyboardState, sizeof(keyboardState));
319316
ZeroMemory(mouseTimeStamp, sizeof(mouseTimeStamp));
@@ -325,7 +322,6 @@ void CInput::OnAppDeactivate(void)
325322
if (CurrentIR())
326323
CurrentIR()->IR_OnDeactivate();
327324

328-
SetAllAcquire(false);
329325
ZeroMemory(mouseState, sizeof(mouseState));
330326
ZeroMemory(keyboardState, sizeof(keyboardState));
331327
ZeroMemory(mouseTimeStamp, sizeof(mouseTimeStamp));
@@ -352,26 +348,18 @@ IInputReceiver* CInput::CurrentIR()
352348
{
353349
if (cbStack.size())
354350
return cbStack.back();
355-
else
356-
return NULL;
351+
return nullptr;
357352
}
358353

359-
void CInput::unacquire() {}
360-
361-
void CInput::acquire(const bool& exclusive)
354+
void CInput::ExclusiveMode(const bool exclusive)
362355
{
363-
// pMouse->SetCooperativeLevel(Device.editor() ? Device.editor()->main_handle() : RDEVICE.m_sdlWnd,
364-
// (exclusive ? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE) | DISCL_FOREGROUND | DISCL_NOWINKEY);
365-
// pMouse->Acquire();
356+
GrabInput(false);
357+
exclusiveInput = exclusive;
358+
GrabInput(true);
366359
}
367360

368-
void CInput::exclusive_mode(const bool& exclusive)
369-
{
370-
g_exclusive = exclusive;
371-
unacquire();
372-
acquire(exclusive);
373-
}
374-
bool CInput::get_exclusive_mode() { return g_exclusive; }
361+
bool CInput::IsExclusiveMode() const { return exclusiveInput; }
362+
375363
void CInput::feedback(u16 s1, u16 s2, float time)
376364
{
377365
stop_vibration_time = RDEVICE.fTimeGlobal + time;

0 commit comments

Comments
 (0)