Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tweak(conhost): more 'intuitive' behavior with F8 + NUI
As stated in a forum topic (https://forum.cfx.re/t/4802344/4), it can be
confusing that you can't actually click 'behind' the empty space when F8
is opened.

This changeset makes the console host (+ its GUI stuff) use dear ImGui's
WantCaptureMouse and WantCaptureKeyboard flags if there's NUI focus.

Additionally, MouseDrawCursor (and nui::SetHideCursor) get used so that
there's no 'double' cursor in the main menu with these.
  • Loading branch information
blattersturm committed Mar 24, 2023
1 parent 671c631 commit 6d37422
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
1 change: 1 addition & 0 deletions code/components/conhost-v2/component.lua
Expand Up @@ -7,6 +7,7 @@ return function()
"rage:input",
"rage:graphics",
"rage:nutsnbolts",
"nui:core",
}
end

Expand Down
77 changes: 69 additions & 8 deletions code/components/conhost-v2/src/ConsoleHostImpl.cpp
Expand Up @@ -21,6 +21,12 @@
#include <rapidjson/document.h>
#include <rapidjson/writer.h>

#if __has_include("CefOverlay.h")
#include "CefOverlay.h"

#define WITH_NUI 1
#endif

#ifndef IS_LAUNCHER
#include <DrawCommands.h>
#include <grcTexture.h>
Expand Down Expand Up @@ -70,6 +76,46 @@ extern bool g_cursorFlag;
int g_scrollTop;
int g_bufferHeight;

bool ConsoleHasAnything()
{
return g_consoleFlag || g_cursorFlag;
}

bool ConsoleHasMouse()
{
if (ConsoleHasAnything())
{
#if WITH_NUI
if (nui::HasFocus())
{
return ImGui::GetIO().WantCaptureMouse;
}
#endif

return true;
}

return false;
}

bool ConsoleHasKeyboard()
{
// g_cursorFlag is cursor-only (for when we want to use the dear ImGui cursor for other purposes)
if (g_consoleFlag)
{
#if WITH_NUI
if (nui::HasFocus())
{
return ImGui::GetIO().WantCaptureKeyboard;
}
#endif

return true;
}

return false;
}

#ifndef IS_LAUNCHER
static uint32_t g_pointSamplerState;
static rage::grcTexture* g_fontTexture;
Expand Down Expand Up @@ -359,7 +405,11 @@ void OnConsoleFrameDraw(int width, int height, bool usedSharedD3D11)

HandleFxDKInput(io);

io.MouseDrawCursor = g_consoleFlag || g_cursorFlag;
io.MouseDrawCursor = ConsoleHasMouse();

#ifdef WITH_NUI
nui::SetHideCursor(io.MouseDrawCursor);
#endif

{
io.DisplaySize = ImVec2(width, height);
Expand Down Expand Up @@ -468,7 +518,12 @@ static void OnConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam,

ConHost::OnShouldDrawGui(&shouldDrawGui);

if ((!g_consoleFlag && !g_cursorFlag) || !pass)
if (!pass)
{
return;
}

if (!ConsoleHasAnything())
{
return;
}
Expand All @@ -485,12 +540,18 @@ static void OnConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam,

if (msg == WM_INPUT || (msg >= WM_MOUSEFIRST && msg <= WM_MOUSELAST))
{
pass = false;
if (ConsoleHasMouse())
{
pass = false;
}
}

if (g_consoleFlag && ((msg >= WM_KEYFIRST && msg <= WM_KEYLAST) || msg == WM_CHAR))
if ((msg >= WM_KEYFIRST && msg <= WM_KEYLAST) || msg == WM_CHAR)
{
pass = false;
if (ConsoleHasKeyboard())
{
pass = false;
}
}

if (!pass)
Expand Down Expand Up @@ -719,7 +780,7 @@ static HookFunction initFunction([]()

InputHook::QueryInputTarget.Connect([](std::vector<InputTarget*>& targets)
{
if (!g_consoleFlag && !g_cursorFlag)
if (!ConsoleHasAnything())
{
return true;
}
Expand Down Expand Up @@ -808,7 +869,7 @@ static HookFunction initFunction([]()

InputHook::QueryMayLockCursor.Connect([](int& may)
{
if (g_consoleFlag || g_cursorFlag)
if (ConsoleHasAnything())
{
may = 0;
}
Expand Down Expand Up @@ -864,7 +925,7 @@ static decltype(&ReleaseCapture) g_origReleaseCapture;

static void WINAPI ReleaseCaptureStub()
{
if (g_consoleFlag || g_cursorFlag)
if (ConsoleHasAnything())
{
return;
}
Expand Down

0 comments on commit 6d37422

Please sign in to comment.