From 491189b8719753eed58e47a8859513630da2c24c Mon Sep 17 00:00:00 2001 From: LopSided <40902730+Lpsd@users.noreply.github.com> Date: Wed, 22 Sep 2021 16:09:55 +0100 Subject: [PATCH 01/10] Add isMTAWindowFocused() and onClientMTAFocusChange event --- Client/core/CMessageLoopHook.cpp | 30 ++++++++++++++----- Client/mods/deathmatch/CClient.cpp | 5 ++++ Client/mods/deathmatch/CClient.h | 2 ++ Client/mods/deathmatch/logic/CClientGame.cpp | 15 ++++++++++ Client/mods/deathmatch/logic/CClientGame.h | 5 ++++ .../logic/luadefs/CLuaClientDefs.cpp | 8 ++++- .../deathmatch/logic/luadefs/CLuaClientDefs.h | 1 + Client/sdk/core/CClientBase.h | 2 ++ 8 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Client/core/CMessageLoopHook.cpp b/Client/core/CMessageLoopHook.cpp index 564563e0df0..2014f2463e9 100644 --- a/Client/core/CMessageLoopHook.cpp +++ b/Client/core/CMessageLoopHook.cpp @@ -124,15 +124,31 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w // Alternate alt-tab system if (pThis && hwnd == pThis->GetHookedWindowHandle()) { - if (uMsg == WM_ACTIVATE && LOWORD(wParam) == WA_ACTIVE) + if (uMsg == WM_ACTIVATE) { - GetVideoModeManager()->OnGainFocus(); - } - if (uMsg == WM_ACTIVATE && LOWORD(wParam) == WA_INACTIVE) - { - GetVideoModeManager()->OnLoseFocus(); - g_pCore->GetKeyBinds()->OnLoseFocus(); + CModManager* pModManager = CModManager::GetSingletonPtr(); + + if (pModManager && pModManager->IsLoaded()) + { + CClientBase* pBase = pModManager->GetCurrentMod(); + + if (pBase) + { + pBase->OnWindowFocusChange(LOWORD(wParam) == WA_ACTIVE); + } + } + + if (LOWORD(wParam) == WA_ACTIVE) + { + GetVideoModeManager()->OnGainFocus(); + } + else if (LOWORD(wParam) == WA_INACTIVE) + { + GetVideoModeManager()->OnLoseFocus(); + g_pCore->GetKeyBinds()->OnLoseFocus(); + } } + if (uMsg == WM_PAINT) { GetVideoModeManager()->OnPaint(); diff --git a/Client/mods/deathmatch/CClient.cpp b/Client/mods/deathmatch/CClient.cpp index 92c412a765c..dfc5274255b 100644 --- a/Client/mods/deathmatch/CClient.cpp +++ b/Client/mods/deathmatch/CClient.cpp @@ -321,6 +321,11 @@ void CClient::TriggerDiscordJoin(SString strSecret) g_pClientGame->TriggerDiscordJoin(strSecret); } +void CClient::OnWindowFocusChange(bool state) +{ + g_pClientGame->OnWindowFocusChange(state); +} + CClient::InitializeArguments CClient::ExtractInitializeArguments(const char* arguments) { // Format: "nickname [password]" diff --git a/Client/mods/deathmatch/CClient.h b/Client/mods/deathmatch/CClient.h index 0f9090c1ead..a36b554f3b3 100644 --- a/Client/mods/deathmatch/CClient.h +++ b/Client/mods/deathmatch/CClient.h @@ -34,6 +34,8 @@ class CClient : public CClientBase void TriggerDiscordJoin(SString strSecret); + void OnWindowFocusChange(bool state); + private: struct InitializeArguments { diff --git a/Client/mods/deathmatch/logic/CClientGame.cpp b/Client/mods/deathmatch/logic/CClientGame.cpp index 8871c130c7b..1abe37b5867 100644 --- a/Client/mods/deathmatch/logic/CClientGame.cpp +++ b/Client/mods/deathmatch/logic/CClientGame.cpp @@ -92,6 +92,8 @@ CClientGame::CClientGame(bool bLocalPlay) : m_ServerInfo(new CServerInfo()) m_lastWeaponSlot = WEAPONSLOT_MAX; // last stored weapon slot, for weapon slot syncing to server (sets to invalid value) ResetAmmoInClip(); + m_bWasFocused = g_pCore->IsFocused(); + m_bCursorEventsEnabled = false; m_bInitiallyFadedOut = true; @@ -2563,6 +2565,7 @@ void CClientGame::AddBuiltInEvents() m_Events.AddEvent("onClientRender", "", NULL, false); m_Events.AddEvent("onClientMinimize", "", NULL, false); m_Events.AddEvent("onClientRestore", "", NULL, false); + m_Events.AddEvent("onClientMTAFocusChange", "", NULL, false); // Cursor events m_Events.AddEvent("onClientClick", "button, state, screenX, screenY, worldX, worldY, worldZ, gui_clicked", NULL, false); @@ -6543,6 +6546,18 @@ void CClientGame::TriggerDiscordJoin(SString strSecret) g_pNet->DeallocateNetBitStream(pBitStream); } +void CClientGame::OnWindowFocusChange(bool state) +{ + if (state == m_bWasFocused) + return; + + m_bWasFocused = state; + + CLuaArguments Arguments; + Arguments.PushBoolean(state); + m_pRootEntity->CallEvent("onClientMTAFocusChange", Arguments, false); +} + void CClientGame::InsertIFPPointerToMap(const unsigned int u32BlockNameHash, const std::shared_ptr& pIFP) { m_mapOfIfpPointers[u32BlockNameHash] = pIFP; diff --git a/Client/mods/deathmatch/logic/CClientGame.h b/Client/mods/deathmatch/logic/CClientGame.h index 7633479508e..1d3880d69e1 100644 --- a/Client/mods/deathmatch/logic/CClientGame.h +++ b/Client/mods/deathmatch/logic/CClientGame.h @@ -255,6 +255,8 @@ class CClientGame void EnablePacketRecorder(const char* szFilename); void InitVoice(bool bEnabled, unsigned int uiServerSampleRate, unsigned char ucQuality, unsigned int uiBitrate); + bool IsWindowFocused() const { return m_bWasFocused; } + // Accessors CVoiceRecorder* GetVoiceRecorder() { return m_pVoiceRecorder; }; @@ -440,6 +442,8 @@ class CClientGame void TriggerDiscordJoin(SString strSecret); + void OnWindowFocusChange(bool state); + private: // CGUI Callbacks bool OnKeyDown(CGUIKeyEventArgs Args); @@ -772,6 +776,7 @@ class CClientGame bool m_bBeingDeleted; // To enable speedy disconnect bool m_bWasMinimized; + bool m_bWasFocused; // Cache for speeding up collision processing public: diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.cpp index 2cc0dcf6bd1..6179e2f1d55 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.cpp @@ -22,7 +22,8 @@ void CLuaClientDefs::LoadFunctions() {"showChat", ArgumentParserWarn}, {"isChatVisible", ArgumentParserWarn}, {"isChatInputBlocked", ArgumentParser}, - {"clearDebugBox", ArgumentParser} + {"clearDebugBox", ArgumentParser}, + {"isMTAWindowFocused", ArgumentParser}, }; for (const auto& [name, func] : functions) @@ -70,3 +71,8 @@ bool CLuaClientDefs::ClearDebug() g_pCore->DebugClear(); return true; } + +bool CLuaClientDefs::IsMTAWindowFocused() +{ + return m_pClientGame->IsWindowFocused(); +} diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.h index b708aca6660..845547b9ee6 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaClientDefs.h @@ -26,4 +26,5 @@ class CLuaClientDefs : public CLuaDefs static bool IsChatVisible(); static bool IsChatInputBlocked(); static bool ClearDebug(); + static bool IsMTAWindowFocused(); }; diff --git a/Client/sdk/core/CClientBase.h b/Client/sdk/core/CClientBase.h index 3acfcd8face..1c352ffc7d6 100644 --- a/Client/sdk/core/CClientBase.h +++ b/Client/sdk/core/CClientBase.h @@ -34,4 +34,6 @@ class CClientBase virtual void GetPlayerNames(std::vector& vPlayerNames) = 0; virtual void TriggerDiscordJoin(SString strSecret) = 0; + + virtual void OnWindowFocusChange(bool state) = 0; }; From 54fe33eb9404e6e4fa70adb92fa81147c53d233e Mon Sep 17 00:00:00 2001 From: LopSided <40902730+Lpsd@users.noreply.github.com> Date: Wed, 22 Sep 2021 16:42:13 +0100 Subject: [PATCH 02/10] Fix issue with click focusing --- Client/core/CMessageLoopHook.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Client/core/CMessageLoopHook.cpp b/Client/core/CMessageLoopHook.cpp index 2014f2463e9..dc2b5b3d271 100644 --- a/Client/core/CMessageLoopHook.cpp +++ b/Client/core/CMessageLoopHook.cpp @@ -124,7 +124,7 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w // Alternate alt-tab system if (pThis && hwnd == pThis->GetHookedWindowHandle()) { - if (uMsg == WM_ACTIVATE) + if (uMsg == WM_ACTIVATE || uMsg == WM_USERCHANGED) { CModManager* pModManager = CModManager::GetSingletonPtr(); @@ -134,10 +134,16 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w if (pBase) { - pBase->OnWindowFocusChange(LOWORD(wParam) == WA_ACTIVE); + WORD state = LOWORD(wParam); + bool focus = (state == WA_CLICKACTIVE) || (state == WA_ACTIVE); + + pBase->OnWindowFocusChange(focus); } } + } + if (uMsg == WM_ACTIVATE) + { if (LOWORD(wParam) == WA_ACTIVE) { GetVideoModeManager()->OnGainFocus(); From 9b055f2d1b8129ee254d29f53cd2ab3d8017491d Mon Sep 17 00:00:00 2001 From: LopSided <40902730+Lpsd@users.noreply.github.com> Date: Wed, 22 Sep 2021 16:43:55 +0100 Subject: [PATCH 03/10] Remove unnecessary condition --- Client/core/CMessageLoopHook.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/core/CMessageLoopHook.cpp b/Client/core/CMessageLoopHook.cpp index dc2b5b3d271..8e24dccb03e 100644 --- a/Client/core/CMessageLoopHook.cpp +++ b/Client/core/CMessageLoopHook.cpp @@ -124,7 +124,7 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w // Alternate alt-tab system if (pThis && hwnd == pThis->GetHookedWindowHandle()) { - if (uMsg == WM_ACTIVATE || uMsg == WM_USERCHANGED) + if (uMsg == WM_ACTIVATE) { CModManager* pModManager = CModManager::GetSingletonPtr(); From 99d7da8af02986c444a81fb14dc380a6836d31f7 Mon Sep 17 00:00:00 2001 From: LopSided <40902730+Lpsd@users.noreply.github.com> Date: Wed, 22 Sep 2021 16:50:47 +0100 Subject: [PATCH 04/10] Add args to AddEvent def --- Client/mods/deathmatch/logic/CClientGame.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/mods/deathmatch/logic/CClientGame.cpp b/Client/mods/deathmatch/logic/CClientGame.cpp index 1abe37b5867..b232ac19071 100644 --- a/Client/mods/deathmatch/logic/CClientGame.cpp +++ b/Client/mods/deathmatch/logic/CClientGame.cpp @@ -2565,7 +2565,7 @@ void CClientGame::AddBuiltInEvents() m_Events.AddEvent("onClientRender", "", NULL, false); m_Events.AddEvent("onClientMinimize", "", NULL, false); m_Events.AddEvent("onClientRestore", "", NULL, false); - m_Events.AddEvent("onClientMTAFocusChange", "", NULL, false); + m_Events.AddEvent("onClientMTAFocusChange", "focused", NULL, false); // Cursor events m_Events.AddEvent("onClientClick", "button, state, screenX, screenY, worldX, worldY, worldZ, gui_clicked", NULL, false); From 02ea5dc4d07d8e1b812f88fff971dd106f59bbec Mon Sep 17 00:00:00 2001 From: LopSided <40902730+Lpsd@users.noreply.github.com> Date: Wed, 22 Sep 2021 17:11:09 +0100 Subject: [PATCH 05/10] Merge condition --- Client/core/CMessageLoopHook.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/Client/core/CMessageLoopHook.cpp b/Client/core/CMessageLoopHook.cpp index 8e24dccb03e..808f3e5834a 100644 --- a/Client/core/CMessageLoopHook.cpp +++ b/Client/core/CMessageLoopHook.cpp @@ -140,10 +140,7 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w pBase->OnWindowFocusChange(focus); } } - } - if (uMsg == WM_ACTIVATE) - { if (LOWORD(wParam) == WA_ACTIVE) { GetVideoModeManager()->OnGainFocus(); From c2fb64ca03ed51a300550915c40a29d9021e4704 Mon Sep 17 00:00:00 2001 From: Lpsd <40902730+Lpsd@users.noreply.github.com> Date: Sun, 11 Sep 2022 17:03:00 +0100 Subject: [PATCH 06/10] Update code style & variables scope --- Client/core/CMessageLoopHook.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Client/core/CMessageLoopHook.cpp b/Client/core/CMessageLoopHook.cpp index 808f3e5834a..24469d6fd0c 100644 --- a/Client/core/CMessageLoopHook.cpp +++ b/Client/core/CMessageLoopHook.cpp @@ -127,6 +127,7 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w if (uMsg == WM_ACTIVATE) { CModManager* pModManager = CModManager::GetSingletonPtr(); + WORD wState = LOWORD(wParam); if (pModManager && pModManager->IsLoaded()) { @@ -134,18 +135,16 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w if (pBase) { - WORD state = LOWORD(wParam); - bool focus = (state == WA_CLICKACTIVE) || (state == WA_ACTIVE); - - pBase->OnWindowFocusChange(focus); + bool bFocus = (wState == WA_CLICKACTIVE) || (wState == WA_ACTIVE); + pBase->OnWindowFocusChange(bFocus); } } - if (LOWORD(wParam) == WA_ACTIVE) + if (wState == WA_ACTIVE) { GetVideoModeManager()->OnGainFocus(); } - else if (LOWORD(wParam) == WA_INACTIVE) + else if (wState == WA_INACTIVE) { GetVideoModeManager()->OnLoseFocus(); g_pCore->GetKeyBinds()->OnLoseFocus(); From 6f3c8464238b33d524cb42aa81507d6f585995da Mon Sep 17 00:00:00 2001 From: Lpsd <40902730+Lpsd@users.noreply.github.com> Date: Sun, 11 Sep 2022 17:09:18 +0100 Subject: [PATCH 07/10] Use switch case --- Client/core/CMessageLoopHook.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Client/core/CMessageLoopHook.cpp b/Client/core/CMessageLoopHook.cpp index 24469d6fd0c..2351d98a160 100644 --- a/Client/core/CMessageLoopHook.cpp +++ b/Client/core/CMessageLoopHook.cpp @@ -140,14 +140,18 @@ LRESULT CALLBACK CMessageLoopHook::ProcessMessage(HWND hwnd, UINT uMsg, WPARAM w } } - if (wState == WA_ACTIVE) + switch (wState) { - GetVideoModeManager()->OnGainFocus(); - } - else if (wState == WA_INACTIVE) - { - GetVideoModeManager()->OnLoseFocus(); - g_pCore->GetKeyBinds()->OnLoseFocus(); + case WA_ACTIVE: + GetVideoModeManager()->OnGainFocus(); + break; + + case WA_INACTIVE: + { + GetVideoModeManager()->OnLoseFocus(); + g_pCore->GetKeyBinds()->OnLoseFocus(); + break; + } } } From c7842bb090fc00aec4f7c3c62a6d2b6f51b20e7d Mon Sep 17 00:00:00 2001 From: Lpsd <40902730+Lpsd@users.noreply.github.com> Date: Sun, 11 Sep 2022 17:11:10 +0100 Subject: [PATCH 08/10] Change variable name --- Client/mods/deathmatch/logic/CClientGame.cpp | 4 ++-- Client/mods/deathmatch/logic/CClientGame.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Client/mods/deathmatch/logic/CClientGame.cpp b/Client/mods/deathmatch/logic/CClientGame.cpp index b42c75d6c5e..52a302f712a 100644 --- a/Client/mods/deathmatch/logic/CClientGame.cpp +++ b/Client/mods/deathmatch/logic/CClientGame.cpp @@ -6553,10 +6553,10 @@ void CClientGame::RestreamWorld() void CClientGame::OnWindowFocusChange(bool state) { - if (state == m_bWasFocused) + if (state == m_bFocused) return; - m_bWasFocused = state; + m_bFocused = state; CLuaArguments Arguments; Arguments.PushBoolean(state); diff --git a/Client/mods/deathmatch/logic/CClientGame.h b/Client/mods/deathmatch/logic/CClientGame.h index 334595c85f2..ef2468303d1 100644 --- a/Client/mods/deathmatch/logic/CClientGame.h +++ b/Client/mods/deathmatch/logic/CClientGame.h @@ -256,7 +256,7 @@ class CClientGame void EnablePacketRecorder(const char* szFilename); void InitVoice(bool bEnabled, unsigned int uiServerSampleRate, unsigned char ucQuality, unsigned int uiBitrate); - bool IsWindowFocused() const { return m_bWasFocused; } + bool IsWindowFocused() const { return m_bFocused; } // Accessors @@ -777,7 +777,7 @@ class CClientGame bool m_bBeingDeleted; // To enable speedy disconnect bool m_bWasMinimized; - bool m_bWasFocused; + bool m_bFocused; // Cache for speeding up collision processing public: From 1294db2d7416d03354367bb2626ec3e6e8f8d88d Mon Sep 17 00:00:00 2001 From: Lpsd <40902730+Lpsd@users.noreply.github.com> Date: Sun, 11 Sep 2022 17:32:51 +0100 Subject: [PATCH 09/10] Update CClientGame.cpp --- Client/mods/deathmatch/logic/CClientGame.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/mods/deathmatch/logic/CClientGame.cpp b/Client/mods/deathmatch/logic/CClientGame.cpp index 52a302f712a..1e5a6e9fb37 100644 --- a/Client/mods/deathmatch/logic/CClientGame.cpp +++ b/Client/mods/deathmatch/logic/CClientGame.cpp @@ -92,7 +92,7 @@ CClientGame::CClientGame(bool bLocalPlay) : m_ServerInfo(new CServerInfo()) m_lastWeaponSlot = WEAPONSLOT_MAX; // last stored weapon slot, for weapon slot syncing to server (sets to invalid value) ResetAmmoInClip(); - m_bWasFocused = g_pCore->IsFocused(); + m_bFocused = g_pCore->IsFocused(); m_bCursorEventsEnabled = false; m_bInitiallyFadedOut = true; From 523f315c63602a56ed1150b3639f213050a20872 Mon Sep 17 00:00:00 2001 From: Marek Kulik Date: Sun, 11 Sep 2022 18:46:57 +0200 Subject: [PATCH 10/10] Update Client/mods/deathmatch/CClient.h --- Client/mods/deathmatch/CClient.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/mods/deathmatch/CClient.h b/Client/mods/deathmatch/CClient.h index 6fa9059aa72..49c4a95c7d3 100644 --- a/Client/mods/deathmatch/CClient.h +++ b/Client/mods/deathmatch/CClient.h @@ -32,7 +32,7 @@ class CClient : public CClientBase bool HandleException(CExceptionInformation* pExceptionInformation); void GetPlayerNames(std::vector& vPlayerNames); - void OnWindowFocusChange(bool state); + void OnWindowFocusChange(bool state) override; private: struct InitializeArguments