From 37b4db388a2c2884589e6fdfa712b59787e6247a Mon Sep 17 00:00:00 2001 From: Jakub Date: Sun, 9 Jun 2024 11:06:16 +0200 Subject: [PATCH 1/4] onClientChatboxLayoutChange event --- Client/core/CClientChatboxVariables.h | 38 +++++++++ Client/core/CSettings.cpp | 83 +++++++++++++++---- Client/core/CSettings.h | 7 ++ Client/mods/deathmatch/CClient.cpp | 70 ++++++++++++++++ Client/mods/deathmatch/CClient.h | 2 + Client/mods/deathmatch/logic/CClientGame.cpp | 2 + Client/mods/deathmatch/logic/CClientGame.h | 2 + .../deathmatch/logic/luadefs/CLuaGUIDefs.cpp | 23 +---- .../deathmatch/logic/luadefs/CLuaGUIDefs.h | 1 - Client/sdk/core/CClientBase.h | 2 + 10 files changed, 190 insertions(+), 40 deletions(-) create mode 100644 Client/core/CClientChatboxVariables.h diff --git a/Client/core/CClientChatboxVariables.h b/Client/core/CClientChatboxVariables.h new file mode 100644 index 0000000000..dfcd40e6e2 --- /dev/null +++ b/Client/core/CClientChatboxVariables.h @@ -0,0 +1,38 @@ +/***************************************************************************** + * + * PROJECT: Multi Theft Auto + * LICENSE: See LICENSE in the top level directory + * FILE: core/CClientChatboxVariables.h + * PURPOSE: Header file for client chatbox variables + * + * Multi Theft Auto is available from http://www.multitheftauto.com/ + * + *****************************************************************************/ + +#pragma once + +#include + +#define MAX_CHATBOX_LAYOUT_CVARS 21 + +static const SFixedArray& g_chatboxLayoutCVars = {{"chat_font", + "chat_lines", + "chat_color", + "chat_text_color", + "chat_input_color", + "chat_input_prefix_color", + "chat_input_text_color", + "chat_scale", + "chat_position_offset_x", + "chat_position_offset_y", + "chat_position_horizontal", + "chat_position_vertical", + "chat_text_alignment", + "chat_width", + "chat_css_style_text", + "chat_css_style_background", + "chat_line_life", + "chat_line_fade_out", + "chat_use_cegui", + "text_scale", + "chat_text_outline"}}; diff --git a/Client/core/CSettings.cpp b/Client/core/CSettings.cpp index 847735a891..a51374636f 100644 --- a/Client/core/CSettings.cpp +++ b/Client/core/CSettings.cpp @@ -3619,6 +3619,7 @@ void CSettings::SaveData() } // Chat + this->SetChatLayoutChanged(false); SaveChatColor(Chat::ColorType::BG, "chat_color"); SaveChatColor(Chat::ColorType::TEXT, "chat_text_color"); SaveChatColor(Chat::ColorType::INPUT_BG, "chat_input_color"); @@ -3627,38 +3628,46 @@ void CSettings::SaveData() { if (m_pRadioChatFont[iFont]->GetSelected()) { - CVARS_SET("chat_font", iFont); + SaveChatSetting("chat_font", iFont); break; } } - strVar = m_pChatScaleX->GetText() + " " + m_pChatScaleY->GetText(); - CVARS_SET("chat_scale", strVar); - CVARS_SET("chat_lines", m_pChatLines->GetText()); - CVARS_SET("chat_width", m_pChatWidth->GetText()); - CVARS_SET("chat_css_style_text", m_pChatCssText->GetSelected()); - CVARS_SET("chat_css_style_background", m_pChatCssBackground->GetSelected()); - CVARS_SET("chat_nickcompletion", m_pChatNickCompletion->GetSelected()); - CVARS_SET("chat_text_outline", m_pChatTextBlackOutline->GetSelected()); - CVARS_SET("chat_line_life", GetMilliseconds(m_pChatLineLife)); - CVARS_SET("chat_line_fade_out", GetMilliseconds(m_pChatLineFadeout)); - - CVARS_SET("chat_position_offset_x", m_pChatOffsetX->GetText()); - CVARS_SET("chat_position_offset_y", m_pChatOffsetY->GetText()); + std::stringstream ss; + ss << std::stof(m_pChatScaleX->GetText()) << " " << std::stof(m_pChatScaleY->GetText()); + + SaveChatSetting("chat_scale", ss.str()); + SaveChatSetting("chat_lines", m_pChatLines->GetText()); + SaveChatSetting("chat_width", m_pChatWidth->GetText()); + + SaveChatSetting("chat_css_style_text", m_pChatCssText->GetSelected()); + SaveChatSetting("chat_css_style_background", m_pChatCssBackground->GetSelected()); + SaveChatSetting("chat_nickcompletion", m_pChatNickCompletion->GetSelected()); + SaveChatSetting("chat_text_outline", m_pChatTextBlackOutline->GetSelected()); + SaveChatSetting("chat_line_life", GetMilliseconds(m_pChatLineLife)); + SaveChatSetting("chat_line_fade_out", GetMilliseconds(m_pChatLineFadeout)); + + SaveChatSetting("chat_position_offset_x", m_pChatOffsetX->GetText()); + SaveChatSetting("chat_position_offset_y", m_pChatOffsetY->GetText()); if (CGUIListItem* pSelected = m_pChatHorizontalCombo->GetSelectedItem()) { int iSelected = (int)pSelected->GetData(); - CVARS_SET("chat_position_horizontal", iSelected); + SaveChatSetting("chat_position_horizontal", iSelected); } if (CGUIListItem* pSelected = m_pChatVerticalCombo->GetSelectedItem()) { int iSelected = (int)pSelected->GetData(); - CVARS_SET("chat_position_vertical", iSelected); + SaveChatSetting("chat_position_vertical", iSelected); } if (CGUIListItem* pSelected = m_pChatTextAlignCombo->GetSelectedItem()) { int iSelected = (int)pSelected->GetData(); - CVARS_SET("chat_text_alignment", iSelected); + SaveChatSetting("chat_text_alignment", iSelected); + } + + if (this->GetChatLayoutChanged() && CModManager::GetSingleton().IsLoaded()) + { + CModManager::GetSingletonPtr()->GetCurrentMod()->OnChatboxLayoutChange(); } // Interface @@ -3904,7 +3913,47 @@ void CSettings::SaveChatColor(eChatColorType eType, const char* szCVar) CVARS_GET(szCVar, pOldColor); if (pColor.R != pOldColor.R || pColor.G != pOldColor.G || pColor.B != pOldColor.B || pColor.A != pOldColor.A) + { CVARS_SET(szCVar, pColor); + this->SetChatLayoutChanged(true); + } +} + + +void CSettings::SaveChatSetting(const char* szCVar, int iVal) +{ + // Save value to the CVar if it's different from previous + int iPreviousValue; + CVARS_GET(szCVar, iPreviousValue); + if (iPreviousValue != iVal) + { + CVARS_SET(szCVar, iVal); + this->SetChatLayoutChanged(true); + } +} + +void CSettings::SaveChatSetting(const char* szCVar, std::string strVal) +{ + // Save value to the CVar if it's different from previous + std::string strPreviousValue; + CVARS_GET(szCVar, strPreviousValue); + if (strPreviousValue != strVal) + { + CVARS_SET(szCVar, strVal); + this->SetChatLayoutChanged(true); + } +} + +void CSettings::SaveChatSetting(const char* szCVar, bool bVal) +{ + // Save value to the CVar if it's different from previous + bool bPreviousValue; + CVARS_GET(szCVar, bPreviousValue); + if (bPreviousValue != bVal) + { + CVARS_SET(szCVar, bVal); + this->SetChatLayoutChanged(true); + } } CColor CSettings::GetChatColorValues(eChatColorType eType) diff --git a/Client/core/CSettings.h b/Client/core/CSettings.h index bfeec87c44..64cfbc3518 100644 --- a/Client/core/CSettings.h +++ b/Client/core/CSettings.h @@ -420,11 +420,18 @@ class CSettings void LoadChatColorFromCVar(eChatColorType eType, const char* szCVar); void LoadChatColorFromString(eChatColorType eType, const std::string& strColor); void SaveChatColor(eChatColorType eType, const char* szCVar); + void SaveChatSetting(const char* szCVar, int iVal); + void SaveChatSetting(const char* szCVar, std::string strVal); + void SaveChatSetting(const char* szCVar, bool bVal); CColor GetChatColorValues(eChatColorType eType); void SetChatColorValues(eChatColorType eType, CColor pColor); int GetMilliseconds(CGUIEdit* pEdit); void SetMilliseconds(CGUIEdit* pEdit, int milliseconds); + bool m_bChatLayoutChanged = false; + void SetChatLayoutChanged(bool changed) { this->m_bChatLayoutChanged = changed; }; + bool GetChatLayoutChanged() { return this->m_bChatLayoutChanged; }; + void ResetGTAVolume(); void SetRadioVolume(float fVolume); void SetSFXVolume(float fVolume); diff --git a/Client/mods/deathmatch/CClient.cpp b/Client/mods/deathmatch/CClient.cpp index ab4cb37e7d..cd42722909 100644 --- a/Client/mods/deathmatch/CClient.cpp +++ b/Client/mods/deathmatch/CClient.cpp @@ -13,6 +13,7 @@ #define ALLOC_STATS_MODULE_NAME "client" #include "SharedUtil.hpp" #include +#include <../Client/core/CClientChatboxVariables.h> CCoreInterface* g_pCore = NULL; CLocalizationInterface* g_pLocalization = NULL; @@ -319,6 +320,75 @@ void CClient::OnWindowFocusChange(bool state) g_pClientGame->OnWindowFocusChange(state); } +void CClient::OnChatboxLayoutChange() +{ + if (g_pClientGame) + { + CCVarsInterface* pCVars = g_pCore->GetCVars(); + SString strCVarValue; + std::stringstream ss; + float fNumber, fX, fY; + int iR, iG, iB, iA; + CLuaArguments chatboxItemList; + + for (unsigned int i = 0; i < MAX_CHATBOX_LAYOUT_CVARS; i++) + { + chatboxItemList.PushString(g_chatboxLayoutCVars[i]); + if (g_chatboxLayoutCVars[i] == "chat_color" || g_chatboxLayoutCVars[i] == "chat_text_color" || g_chatboxLayoutCVars[i] == "chat_input_color" || + g_chatboxLayoutCVars[i] == "chat_input_prefix_color" || g_chatboxLayoutCVars[i] == "chat_input_text_color") + { + pCVars->Get(g_chatboxLayoutCVars[i], strCVarValue); + if (strCVarValue.empty()) + continue; + ss.clear(); + ss.str(strCVarValue); + ss >> iR >> iG >> iB >> iA; + + CLuaArguments chatboxColorItem; + chatboxColorItem.PushNumber(1); + chatboxColorItem.PushNumber(iR); + chatboxColorItem.PushNumber(2); + chatboxColorItem.PushNumber(iG); + chatboxColorItem.PushNumber(3); + chatboxColorItem.PushNumber(iB); + chatboxColorItem.PushNumber(4); + chatboxColorItem.PushNumber(iA); + + chatboxItemList.PushTable(&chatboxColorItem); + } + else if (g_chatboxLayoutCVars[i] == "chat_scale") + { + pCVars->Get(g_chatboxLayoutCVars[i], strCVarValue); + if (strCVarValue.empty()) + continue; + ss.clear(); + ss.str(strCVarValue); + ss >> fX >> fY; + + CLuaArguments numberTable; + numberTable.PushNumber(1); + numberTable.PushNumber(fX); + numberTable.PushNumber(2); + numberTable.PushNumber(fY); + + chatboxItemList.PushTable(&numberTable); + } + else + { + pCVars->Get(g_chatboxLayoutCVars[i], fNumber); + if (g_chatboxLayoutCVars[i] == "chat_use_cegui") + chatboxItemList.PushBoolean(fNumber ? true : false); + else + chatboxItemList.PushNumber(fNumber); + } + } + + CLuaArguments Arguments; + Arguments.PushTable(&chatboxItemList); + g_pClientGame->GetRootEntity()->CallEvent("onClientChatboxLayoutChange", Arguments, false); + } +} + 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 49c4a95c7d..0f4ec3fc51 100644 --- a/Client/mods/deathmatch/CClient.h +++ b/Client/mods/deathmatch/CClient.h @@ -34,6 +34,8 @@ class CClient : public CClientBase void OnWindowFocusChange(bool state) override; + void OnChatboxLayoutChange(); + private: struct InitializeArguments { diff --git a/Client/mods/deathmatch/logic/CClientGame.cpp b/Client/mods/deathmatch/logic/CClientGame.cpp index 34946f7141..5d08ed4035 100644 --- a/Client/mods/deathmatch/logic/CClientGame.cpp +++ b/Client/mods/deathmatch/logic/CClientGame.cpp @@ -2797,6 +2797,8 @@ void CClientGame::AddBuiltInEvents() m_Events.AddEvent("onClientWeaponFire", "ped, x, y, z", NULL, false); m_Events.AddEvent("onClientWorldSound", "group, index, x, y, z", nullptr, false); + + m_Events.AddEvent("onClientChatboxLayoutChange", "chatboxLayout", NULL, false); } void CClientGame::DrawFPS() diff --git a/Client/mods/deathmatch/logic/CClientGame.h b/Client/mods/deathmatch/logic/CClientGame.h index 96d82c37a7..6476112dd6 100644 --- a/Client/mods/deathmatch/logic/CClientGame.h +++ b/Client/mods/deathmatch/logic/CClientGame.h @@ -448,6 +448,8 @@ class CClientGame void OnWindowFocusChange(bool state); + void OnChatboxLayoutChange(); + private: // CGUI Callbacks bool OnKeyDown(CGUIKeyEventArgs Args); diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.cpp index b0ea3ec4a8..ab2b774de4 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.cpp @@ -11,28 +11,7 @@ #include "StdInc.h" #include - -static const SFixedArray g_chatboxLayoutCVars = {{"chat_font", - "chat_lines", - "chat_color", - "chat_text_color", - "chat_input_color", - "chat_input_prefix_color", - "chat_input_text_color", - "chat_scale", - "chat_position_offset_x", - "chat_position_offset_y", - "chat_position_horizontal", - "chat_position_vertical", - "chat_text_alignment", - "chat_width", - "chat_css_style_text", - "chat_css_style_background", - "chat_line_life", - "chat_line_fade_out", - "chat_use_cegui", - "text_scale", - "chat_text_outline"}}; +#include <../Client/core/CClientChatboxVariables.h> void CLuaGUIDefs::LoadFunctions() { diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.h index ac778abf95..c1b3857cf3 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.h @@ -12,7 +12,6 @@ #pragma once #include "CLuaDefs.h" -#define MAX_CHATBOX_LAYOUT_CVARS 21 class CLuaGUIDefs : public CLuaDefs { diff --git a/Client/sdk/core/CClientBase.h b/Client/sdk/core/CClientBase.h index 17634dab81..ee6f55426d 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 OnWindowFocusChange(bool state) = 0; + + virtual void OnChatboxLayoutChange() = 0; }; From c95419b43fed8de3dd98f858855428d0a88bf8e9 Mon Sep 17 00:00:00 2001 From: Jakub Date: Sat, 6 Jul 2024 21:19:06 +0200 Subject: [PATCH 2/4] code review fix --- Client/core/CClientChatboxVariables.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/core/CClientChatboxVariables.h b/Client/core/CClientChatboxVariables.h index dfcd40e6e2..ace5278b2f 100644 --- a/Client/core/CClientChatboxVariables.h +++ b/Client/core/CClientChatboxVariables.h @@ -15,7 +15,7 @@ #define MAX_CHATBOX_LAYOUT_CVARS 21 -static const SFixedArray& g_chatboxLayoutCVars = {{"chat_font", +static const SFixedArray& g_chatboxLayoutCVars = {{"chat_font", "chat_lines", "chat_color", "chat_text_color", From d4253d3ec15ad04bf06384054d9c69b52ebe13f4 Mon Sep 17 00:00:00 2001 From: Jakub Starzak Date: Wed, 28 Aug 2024 13:29:07 +0200 Subject: [PATCH 3/4] fix: separate member variables, add const qualifier --- Client/core/CSettings.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Client/core/CSettings.h b/Client/core/CSettings.h index 64cfbc3518..968acbb8fc 100644 --- a/Client/core/CSettings.h +++ b/Client/core/CSettings.h @@ -428,9 +428,8 @@ class CSettings int GetMilliseconds(CGUIEdit* pEdit); void SetMilliseconds(CGUIEdit* pEdit, int milliseconds); - bool m_bChatLayoutChanged = false; - void SetChatLayoutChanged(bool changed) { this->m_bChatLayoutChanged = changed; }; - bool GetChatLayoutChanged() { return this->m_bChatLayoutChanged; }; + void SetChatLayoutChanged(bool changed) { m_bChatLayoutChanged = changed; }; + bool GetChatLayoutChanged() const { return m_bChatLayoutChanged; }; void ResetGTAVolume(); void SetRadioVolume(float fVolume); @@ -472,4 +471,6 @@ class CSettings int m_iMaxAnisotropic; std::list m_pKeyBindSections; + + bool m_bChatLayoutChanged = false; }; From 009d5b83de73a4823d49c242cab938dfe1c23d9e Mon Sep 17 00:00:00 2001 From: Jakub Starzak Date: Wed, 28 Aug 2024 13:33:41 +0200 Subject: [PATCH 4/4] fix: const std::string& --- Client/core/CSettings.cpp | 2 +- Client/core/CSettings.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Client/core/CSettings.cpp b/Client/core/CSettings.cpp index a51374636f..354cb3f694 100644 --- a/Client/core/CSettings.cpp +++ b/Client/core/CSettings.cpp @@ -3932,7 +3932,7 @@ void CSettings::SaveChatSetting(const char* szCVar, int iVal) } } -void CSettings::SaveChatSetting(const char* szCVar, std::string strVal) +void CSettings::SaveChatSetting(const char* szCVar, const std::string& strVal) { // Save value to the CVar if it's different from previous std::string strPreviousValue; diff --git a/Client/core/CSettings.h b/Client/core/CSettings.h index 968acbb8fc..cb92013683 100644 --- a/Client/core/CSettings.h +++ b/Client/core/CSettings.h @@ -421,7 +421,7 @@ class CSettings void LoadChatColorFromString(eChatColorType eType, const std::string& strColor); void SaveChatColor(eChatColorType eType, const char* szCVar); void SaveChatSetting(const char* szCVar, int iVal); - void SaveChatSetting(const char* szCVar, std::string strVal); + void SaveChatSetting(const char* szCVar, const std::string& strVal); void SaveChatSetting(const char* szCVar, bool bVal); CColor GetChatColorValues(eChatColorType eType); void SetChatColorValues(eChatColorType eType, CColor pColor);