From 3ea355bf389a14a4b913a9faee114e5d8352eca3 Mon Sep 17 00:00:00 2001 From: aixxe Date: Tue, 20 Dec 2016 20:39:21 +0000 Subject: [PATCH] Properly handle input while menu is visible. * INSERT toggles the menu. Signed-off-by: aixxe --- include/cstrike/Interfaces/IInputInternal.h | 4 ++-- src/GUI/Components.cpp | 14 ++++++++++++ src/GUI/GUI.cpp | 12 ++++++++++ src/GUI/GUI.h | 13 +++++++++++ src/Hooks/Hooks.h | 1 + src/Hooks/PumpWindowsMessageLoop.cpp | 4 ++++ src/Hooks/SetKeyCodeState.cpp | 10 ++++++--- src/Hooks/ShowPixels.cpp | 25 ++++++++++++++++----- 8 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 src/GUI/Components.cpp create mode 100644 src/GUI/GUI.cpp create mode 100644 src/GUI/GUI.h diff --git a/include/cstrike/Interfaces/IInputInternal.h b/include/cstrike/Interfaces/IInputInternal.h index e59fb95..9f073cf 100644 --- a/include/cstrike/Interfaces/IInputInternal.h +++ b/include/cstrike/Interfaces/IInputInternal.h @@ -11,8 +11,8 @@ enum MouseCodeState_t { class IInputInternal { public: - void SetKeyCodeState(KeyCode code, bool pressed) { - GetVirtualFunction(this, 83)(this, code, pressed); + void SetKeyCodeState(KeyCode code, bool down) { + GetVirtualFunction(this, 83)(this, code, down); } void SetMouseCodeState(MouseCode code, MouseCodeState_t state) { diff --git a/src/GUI/Components.cpp b/src/GUI/Components.cpp new file mode 100644 index 0000000..9fecc07 --- /dev/null +++ b/src/GUI/Components.cpp @@ -0,0 +1,14 @@ +#include "GUI.h" + +void GUI::DrawFramerateCounter() { + ImGui::SetNextWindowPos(ImVec2(10, 10)); + ImGui::Begin("FPS", nullptr, ImVec2(0, 0), 0.3f, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize); + ImGui::Text("FPS: %.2f", ImGui::GetIO().Framerate); + ImGui::End(); +} + +void GUI::DrawConfigurationWindow() { + ImGui::Begin("cstrike-basehook-linux", nullptr); + ImGui::Text("Hello, world!"); + ImGui::End(); +} diff --git a/src/GUI/GUI.cpp b/src/GUI/GUI.cpp new file mode 100644 index 0000000..fd077bf --- /dev/null +++ b/src/GUI/GUI.cpp @@ -0,0 +1,12 @@ +#include "GUI.h" + +bool GUI::IsVisible = false; + +void GUI::Render() { + // Draw various global components. + GUI::DrawFramerateCounter(); + + // Draw the actual configuration window. + if (GUI::IsVisible) + GUI::DrawConfigurationWindow(); +} \ No newline at end of file diff --git a/src/GUI/GUI.h b/src/GUI/GUI.h new file mode 100644 index 0000000..8e98d8d --- /dev/null +++ b/src/GUI/GUI.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +namespace GUI { + extern bool IsVisible; + + void DrawFramerateCounter(); + void DrawConfigurationWindow(); + + void Render(); +} \ No newline at end of file diff --git a/src/Hooks/Hooks.h b/src/Hooks/Hooks.h index dc7ebbc..36ffe5b 100644 --- a/src/Hooks/Hooks.h +++ b/src/Hooks/Hooks.h @@ -1,6 +1,7 @@ #pragma once #include "../Basehook.h" +#include "../GUI/GUI.h" namespace Hooks { // IBaseClientDLL diff --git a/src/Hooks/PumpWindowsMessageLoop.cpp b/src/Hooks/PumpWindowsMessageLoop.cpp index ad723aa..a940509 100644 --- a/src/Hooks/PumpWindowsMessageLoop.cpp +++ b/src/Hooks/PumpWindowsMessageLoop.cpp @@ -6,6 +6,10 @@ void Hooks::PumpWindowsMessageLoop(ILauncherMgr* thisptr) { // Get the original function and store it statically. static PumpWindowsMessageLoop_t oPumpWindowsMessageLoop = sdl_hook->GetOriginalFunction(15); + // Block game input while the GUI is visible. + if (GUI::IsVisible) + return; + // Call original 'ILauncherMgr::PumpWindowsMessageLoop'. oPumpWindowsMessageLoop(thisptr); } \ No newline at end of file diff --git a/src/Hooks/SetKeyCodeState.cpp b/src/Hooks/SetKeyCodeState.cpp index 0b32be3..2f085e6 100644 --- a/src/Hooks/SetKeyCodeState.cpp +++ b/src/Hooks/SetKeyCodeState.cpp @@ -2,13 +2,17 @@ typedef void (*SetKeyCodeState_t) (IInputInternal*, KeyCode, bool); -void Hooks::SetKeyCodeState(IInputInternal* thisptr, KeyCode code, bool pressed) { +void Hooks::SetKeyCodeState(IInputInternal* thisptr, KeyCode code, bool down) { // Get the original function and store it statically. static SetKeyCodeState_t oSetKeyCodeState = inputinternal_hook->GetOriginalFunction(83); // Print to console every time a key is pressed or released. - cvar->ConsoleColorPrintf(Color(150, 150, 255, 255), "IInputInternal::SetKeyCodeState - code: %i, pressed: %i\n", code, pressed); + cvar->ConsoleColorPrintf(Color(150, 150, 255, 255), "IInputInternal::SetKeyCodeState - code: %i, down: %i\n", code, down); + + // Check if we should open the menu. + if (code == KeyCode::KEY_INSERT && down == false) + GUI::IsVisible = true; // Call original 'IInputInternal::SetKeyCodeState'. - oSetKeyCodeState(thisptr, code, pressed); + oSetKeyCodeState(thisptr, code, down); } \ No newline at end of file diff --git a/src/Hooks/ShowPixels.cpp b/src/Hooks/ShowPixels.cpp index f73042e..a19bc14 100644 --- a/src/Hooks/ShowPixels.cpp +++ b/src/Hooks/ShowPixels.cpp @@ -1,7 +1,5 @@ #include "Hooks.h" - -#include -#include +#include "../GUI/GUI.h" typedef void (*ShowPixels_t) (ILauncherMgr*, CShowPixelsParams*); @@ -30,8 +28,25 @@ void Hooks::ShowPixels(ILauncherMgr* thisptr, CShowPixelsParams* params) { // Start ImGui rendering. ImGui_ImplSdl_NewFrame(window); - // Draw some test stuff. - ImGui::Text("Hello, world!"); + // Enable or disable the ImGui cursor depending on the GUI visibility. + ImGui::GetIO().MouseDrawCursor = GUI::IsVisible; + ImGui::GetIO().WantCaptureMouse = GUI::IsVisible; + ImGui::GetIO().WantCaptureKeyboard = GUI::IsVisible; + + // Handle incoming input while the menu is active. + if (GUI::IsVisible) { + SDL_Event event = {}; + + while (SDL_PollEvent(&event)) { + if (event.key.keysym.sym == SDLK_INSERT && event.type == SDL_KEYDOWN) + GUI::IsVisible = !GUI::IsVisible; + + ImGui_ImplSdl_ProcessEvent(&event); + } + } + + // Draw GUI components. + GUI::Render(); // Finish ImGui rendering. ImGui::Render();