Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onClientChatboxLayoutChange event #3448

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Client/core/CClientChatboxVariables.h
Original file line number Diff line number Diff line change
@@ -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 <SharedUtil.Misc.h>

#define MAX_CHATBOX_LAYOUT_CVARS 21

static const SFixedArray<const char*, MAX_CHATBOX_LAYOUT_CVARS>& 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"}};
83 changes: 66 additions & 17 deletions Client/core/CSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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());
Comment on lines +3636 to +3637
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SString constructor can do printf formatting.


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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions Client/core/CSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
70 changes: 70 additions & 0 deletions Client/mods/deathmatch/CClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define ALLOC_STATS_MODULE_NAME "client"
#include "SharedUtil.hpp"
#include <core/CClientCommands.h>
#include <../Client/core/CClientChatboxVariables.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header belongs in Client/sdk/core if you want to interface between core and deathmatch.


CCoreInterface* g_pCore = NULL;
CLocalizationInterface* g_pLocalization = NULL;
Expand Down Expand Up @@ -305,6 +306,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++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use an array of struct for g_chatboxLayoutCVars, and store the type of value next to the cvar name. For example:

enum class DummyType {
    STRING,
    BOOLEAN,
    FLOAT1,
    FLOAT2,
    FLOAT4,
};

struct Dummy {
    const char* name;
    DummyType   type;
};

{
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]"
Expand Down
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/CClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class CClient : public CClientBase

void OnWindowFocusChange(bool state) override;

void OnChatboxLayoutChange();

private:
struct InitializeArguments
{
Expand Down
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/CClientGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,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()
Expand Down
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/CClientGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ class CClientGame

void OnWindowFocusChange(bool state);

void OnChatboxLayoutChange();

private:
// CGUI Callbacks
bool OnKeyDown(CGUIKeyEventArgs Args);
Expand Down
23 changes: 1 addition & 22 deletions Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,7 @@

#include "StdInc.h"
#include <lua/CLuaFunctionParser.h>

static const SFixedArray<const char*, MAX_CHATBOX_LAYOUT_CVARS> 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()
{
Expand Down
1 change: 0 additions & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#pragma once
#include "CLuaDefs.h"

#define MAX_CHATBOX_LAYOUT_CVARS 21

class CLuaGUIDefs : public CLuaDefs
{
Expand Down
2 changes: 2 additions & 0 deletions Client/sdk/core/CClientBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ class CClientBase
virtual void GetPlayerNames(std::vector<SString>& vPlayerNames) = 0;

virtual void OnWindowFocusChange(bool state) = 0;

virtual void OnChatboxLayoutChange() = 0;
};
Loading