Skip to content

Commit

Permalink
Support for exclusive input mode
Browse files Browse the repository at this point in the history
Tweaks for window hit test
  • Loading branch information
Xottab-DUTY committed Jul 27, 2018
1 parent d513f36 commit 44657a1
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 103 deletions.
23 changes: 9 additions & 14 deletions src/xrEngine/Device_Initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Include/editor/ide.hpp"
#include "engine_impl.hpp"
#include "xr_input.h"
#include "GameFont.h"
#include "PerformanceAlert.hpp"
#include "xrCore/ModuleLookup.hpp"
Expand Down Expand Up @@ -72,15 +73,18 @@ void CRenderDevice::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)

SDL_HitTestResult WindowHitTest(SDL_Window* /*window*/, const SDL_Point* area, void* /*data*/)
{
if (pInput->InputIsGrabbed())
return SDL_HITTEST_NORMAL;

const auto& rect = Device.m_rcWindowClient;

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

const bool leftSide = area->x < rect.x + hit;
const bool topSide = area->y < rect.y + hit;
const bool bottomSide = area->y > rect.h - hit;
const bool rightSide = area->x > rect.w - hit;
const bool leftSide = area->x <= rect.x + hit;
const bool topSide = area->y <= rect.y + hit;
const bool bottomSide = area->y >= rect.h - hit;
const bool rightSide = area->x >= rect.w - hit;

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

const int centerX = rect.w / 2;
const int centerY = rect.h / 2;

// Allow drag from any point except window center
// For this case, 'hit' is a size of a square in the center
if ((area->x > centerX + hit || area->x < centerX - hit)
|| (area->y > centerY + hit || area->y < centerY - hit))
return SDL_HITTEST_DRAGGABLE;

return SDL_HITTEST_NORMAL;
return SDL_HITTEST_DRAGGABLE;
}
5 changes: 2 additions & 3 deletions src/xrEngine/Device_destroy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ void CRenderDevice::Destroy()
if (!b_is_Ready)
return;
Log("Destroying Direct3D...");
pInput->ClipCursor(false);
GEnv.Render->ValidateHW();
GEnv.DU->OnDeviceDestroy();
b_is_Ready = false;
Expand Down Expand Up @@ -51,7 +50,7 @@ void CRenderDevice::Reset(bool precache)
{
const auto dwWidth_before = dwWidth;
const auto dwHeight_before = dwHeight;
pInput->ClipCursor(false);
pInput->GrabInput(false);

const auto tm_start = TimerAsync();

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

if (!GEnv.isDedicatedServer)
pInput->ClipCursor(true);
pInput->GrabInput(true);
}
2 changes: 1 addition & 1 deletion src/xrEngine/XR_IOConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ void CConsole::Hide()
// if ( g_pGameLevel ||
// ( g_pGamePersistent && g_pGamePersistent->m_pMainMenu && g_pGamePersistent->m_pMainMenu->IsActive() ))

if (pInput->get_exclusive_mode())
if (pInput->IsExclusiveMode())
{
SDL_WarpMouseGlobal(m_mouse_pos.x, m_mouse_pos.y); // Replace with SDL_WarpMouseInWindow in case set window-relative coordinates
}
Expand Down
14 changes: 11 additions & 3 deletions src/xrEngine/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,14 @@ void CRenderDevice::message_loop()
OnWM_Activate(0, event.window.data2);
break;

case SDL_WINDOWEVENT_ENTER:
SDL_ShowCursor(SDL_FALSE);
break;

case SDL_WINDOWEVENT_LEAVE:
SDL_ShowCursor(SDL_TRUE);
break;

case SDL_WINDOWEVENT_CLOSE:
event.type = SDL_QUIT;
}
Expand Down Expand Up @@ -425,7 +433,7 @@ void CRenderDevice::Run()
SDL_FlushEvents(SDL_WINDOWEVENT, SDL_SYSWMEVENT);
SDL_ShowWindow(m_sdlWnd);
SDL_RaiseWindow(m_sdlWnd);
pInput->ClipCursor(true);
pInput->GrabInput(true);
message_loop();
seqAppEnd.Process();
// Stop Balance-Thread
Expand Down Expand Up @@ -553,9 +561,9 @@ void CRenderDevice::OnWM_Activate(WPARAM wParam, LPARAM /*lParam*/)
const BOOL isWndActive = (fActive != WA_INACTIVE && !fMinimized) ? TRUE : FALSE;

if (!editor() && !GEnv.isDedicatedServer && isWndActive)
pInput->ClipCursor(true);
pInput->GrabInput(true);
else
pInput->ClipCursor(false);
pInput->GrabInput(false);

extern int ps_always_active;
const BOOL isGameActive = ps_always_active || isWndActive;
Expand Down
7 changes: 1 addition & 6 deletions src/xrEngine/edit_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "edit_actions.h"
#include "line_edit_control.h"
#include "xr_input.h"
#include <locale.h>
#include <codecvt>

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

SDL_Event event;
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_TEXTEDITING, SDL_TEXTINPUT))
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_TEXTINPUT, SDL_TEXTINPUT))
{
switch (event.type)
{
Expand All @@ -92,10 +91,6 @@ void type_pair::on_key_press(line_edit_control* const control)
}
break;
}

case SDL_TEXTEDITING:
// XXX: use this?
break;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/xrEngine/line_edit_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,10 @@ void line_edit_control::clamp_cur_pos() { clamp(m_cur_pos, 0, (int)xr_strlen(m_e
void line_edit_control::SwitchKL()
{
#ifdef WINDOWS
if (pInput->get_exclusive_mode())
// XXX: do we even need this?
// Check if SDL_HINT_GRAB_KEYBOARD works
// and enable in case if we will need this
if (false && pInput->IsExclusiveMode())
ActivateKeyboardLayout((HKL)HKL_NEXT, 0);
#endif
}
Expand Down
8 changes: 4 additions & 4 deletions src/xrEngine/x_ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ void CApplication::OnEvent(EVENT E, u64 P1, u64 P2)
{
if (E == eQuit)
{
if (pInput != NULL)
pInput->ClipCursor(false);
if (pInput != nullptr)
pInput->GrabInput(false);

g_SASH.EndBenchmark();

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

if (g_pGameLevel)
{
Expand Down
82 changes: 35 additions & 47 deletions src/xrEngine/xr_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,20 @@ ENGINE_API Flags32 psMouseInvert = {FALSE};

float stop_vibration_time = flt_max;

#define MOUSEBUFFERSIZE 64
#define KEYBOARDBUFFERSIZE 64

static bool g_exclusive = true;
static void on_error_dialog(bool before)
{
if (!pInput || !g_exclusive || Device.editor())
if (!pInput || !pInput->IsExclusiveMode() || Device.editor())
return;

if (before)
{
pInput->ClipCursor(false);
pInput->unacquire();
}
pInput->GrabInput(false);
else
{
pInput->ClipCursor(true);
pInput->acquire(true);
}
pInput->GrabInput(true);
}

CInput::CInput(bool exclusive, int deviceForInit)
CInput::CInput(const bool exclusive)
{
g_exclusive = exclusive;
exclusiveInput = exclusive;

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

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

xrDebug::SetDialogHandler(on_error_dialog);

SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
SDL_StopTextInput(); // sanity

#ifdef ENGINE_BUILD
Device.seqAppActivate.Add(this);
Device.seqAppDeactivate.Add(this, REG_PRIORITY_HIGH);
Device.seqFrame.Add(this, REG_PRIORITY_HIGH);
#endif
}

CInput::~CInput(void)
CInput::~CInput()
{
#ifdef ENGINE_BUILD
GrabInput(false);
Device.seqFrame.Remove(this);
Device.seqAppDeactivate.Remove(this);
Device.seqAppActivate.Remove(this);
#endif
}

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

void CInput::SetAllAcquire(bool bAcquire) {}

void CInput::SetMouseAcquire(bool bAcquire) {}
void CInput::SetKBDAcquire(bool bAcquire) {}


void CInput::MouseUpdate()
{
SDL_PumpEvents();
Expand Down Expand Up @@ -255,21 +237,37 @@ bool CInput::iGetAsyncBtnState(int btn)
{
return mouseState[btn];
}
void CInput::ClipCursor(bool clip)

void CInput::ClipCursor(const bool clip)
{
if (clip)
{
SDL_SetWindowGrab(Device.m_sdlWnd, SDL_TRUE);
SDL_ShowCursor(SDL_TRUE);
SDL_SetRelativeMouseMode(SDL_TRUE);
}
else
{
SDL_SetWindowGrab(Device.m_sdlWnd, SDL_FALSE);
SDL_ShowCursor(SDL_FALSE);
SDL_SetRelativeMouseMode(SDL_FALSE);
}
}

//-------------------------------------------------------
void CInput::GrabInput(const bool grab)
{
ClipCursor(grab);

if (IsExclusiveMode())
SDL_SetWindowGrab(Device.m_sdlWnd, grab ? SDL_TRUE : SDL_FALSE);

inputGrabbed = grab;

}

bool CInput::InputIsGrabbed() const
{
return inputGrabbed;
}

void CInput::iCapture(IInputReceiver* p)
{
VERIFY(p);
Expand Down Expand Up @@ -313,7 +311,6 @@ void CInput::OnAppActivate(void)
if (CurrentIR())
CurrentIR()->IR_OnActivate();

SetAllAcquire(true);
ZeroMemory(mouseState, sizeof(mouseState));
ZeroMemory(keyboardState, sizeof(keyboardState));
ZeroMemory(mouseTimeStamp, sizeof(mouseTimeStamp));
Expand All @@ -325,7 +322,6 @@ void CInput::OnAppDeactivate(void)
if (CurrentIR())
CurrentIR()->IR_OnDeactivate();

SetAllAcquire(false);
ZeroMemory(mouseState, sizeof(mouseState));
ZeroMemory(keyboardState, sizeof(keyboardState));
ZeroMemory(mouseTimeStamp, sizeof(mouseTimeStamp));
Expand All @@ -352,26 +348,18 @@ IInputReceiver* CInput::CurrentIR()
{
if (cbStack.size())
return cbStack.back();
else
return NULL;
return nullptr;
}

void CInput::unacquire() {}

void CInput::acquire(const bool& exclusive)
void CInput::ExclusiveMode(const bool exclusive)
{
// pMouse->SetCooperativeLevel(Device.editor() ? Device.editor()->main_handle() : RDEVICE.m_sdlWnd,
// (exclusive ? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE) | DISCL_FOREGROUND | DISCL_NOWINKEY);
// pMouse->Acquire();
GrabInput(false);
exclusiveInput = exclusive;
GrabInput(true);
}

void CInput::exclusive_mode(const bool& exclusive)
{
g_exclusive = exclusive;
unacquire();
acquire(exclusive);
}
bool CInput::get_exclusive_mode() { return g_exclusive; }
bool CInput::IsExclusiveMode() const { return exclusiveInput; }

void CInput::feedback(u16 s1, u16 s2, float time)
{
stop_vibration_time = RDEVICE.fTimeGlobal + time;
Expand Down
Loading

0 comments on commit 44657a1

Please sign in to comment.