Skip to content

Commit

Permalink
add warlist chat binds
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrc6 committed Dec 23, 2024
1 parent bb8b9a7 commit 7280439
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 60 deletions.
98 changes: 93 additions & 5 deletions src/game/client/components/tclient/bindchat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ void CBindchat::ConRemoveBindchat(IConsole::IResult *pResult, void *pUserData)
pThis->RemoveBind(aName);
}


void CBindchat::ConRemoveBindchatAll(IConsole::IResult *pResult, void *pUserData)
{
CBindchat *pThis = static_cast<CBindchat *>(pUserData);
Expand Down Expand Up @@ -84,11 +83,44 @@ void CBindchat::AddBind(const char *pName, const char *pCommand)
m_vBinds.push_back(Bind);
}

void CBindchat::AddBindDefault(const char *pName, const char *pCommand)
{
if((pName[0] == '\0' && pCommand[0] == '\0') || m_vBinds.size() >= BINDCHAT_MAX_BINDS)
return;

// If a bind for this command is found then don't add this
for(auto It = m_vBinds.begin(); It != m_vBinds.end(); ++It)
{
if(str_comp(It->m_aCommand, pCommand) == 0)
return;
}

CBind Bind;
Bind.m_Default = true;
str_copy(Bind.m_aName, pName);
str_copy(Bind.m_aCommand, pCommand);
m_vBinds.push_back(Bind);
}

void CBindchat::RemoveBindCommand(const char *pCommand)
{
if(pCommand[0] == '\0')
return;
for(auto It = m_vBinds.begin(); It != m_vBinds.end(); ++It)
{
if(str_comp(It->m_aCommand, pCommand) == 0)
{
m_vBinds.erase(It);
return;
}
}
}

void CBindchat::RemoveBind(const char *pName)
{
if(pName[0] == '\0')
return;
for (auto It = m_vBinds.begin(); It != m_vBinds.end(); ++It)
for(auto It = m_vBinds.begin(); It != m_vBinds.end(); ++It)
{
if(str_comp(It->m_aName, pName) == 0)
{
Expand All @@ -111,6 +143,18 @@ void CBindchat::RemoveAllBinds()
m_vBinds.clear();
}

int CBindchat::GetBindNoDefault(const char *pCommand)
{
if(pCommand[0] == '\0')
return -1;
for(auto It = m_vBinds.begin(); It != m_vBinds.end(); ++It)
{
if(str_comp_nocase(It->m_aCommand, pCommand) == 0 && !It->m_Default)
return &*It - m_vBinds.data();
}
return -1;
}

int CBindchat::GetBind(const char *pCommand)
{
if(pCommand[0] == '\0')
Expand Down Expand Up @@ -141,6 +185,25 @@ void CBindchat::OnConsoleInit()
Console()->Register("unbindchat", "s[name] r[command]", CFGFLAG_CLIENT, ConRemoveBindchat, this, "Remove a chat bind");
Console()->Register("unbindchatall", "", CFGFLAG_CLIENT, ConRemoveBindchatAll, this, "Removes all chat binds");
Console()->Register("bindchatdefaults", "", CFGFLAG_CLIENT, ConBindchatDefaults, this, "Adds default chat binds");

m_pStorage = Kernel()->RequestInterface<IStorage>();
IOHANDLE File = m_pStorage->OpenFile(BINDCHAT_FILE, IOFLAG_READ, IStorage::TYPE_ALL);
if(File)
{
io_close(File);
Console()->ExecuteFile(BINDCHAT_FILE);
}

// Default Binds
// Default Binds
AddBindDefault(".war", "war_name_index 1");
AddBindDefault(".warclan", "war_clan_index 1");
AddBindDefault(".team", "war_name_index 2");
AddBindDefault(".teamclan", "war_clan_index 2");
AddBindDefault(".delwar", "remove_war_name_index 1");
AddBindDefault(".delwarclan", "remove_war_clan_index 1");
AddBindDefault(".delteam", "remove_war_name_index 2");
AddBindDefault(".delteamclan", "remove_war_clan_index 2");
}

void CBindchat::ExecuteBind(int Bind, const char *pArgs)
Expand Down Expand Up @@ -176,9 +239,10 @@ bool CBindchat::ChatDoBinds(const char *pText)
return false;
}

bool CBindchat::ChatDoAutocomplete(bool ShiftPressed) {
bool CBindchat::ChatDoAutocomplete(bool ShiftPressed)
{
CChat &Chat = GameClient()->m_Chat;

if(m_vBinds.size() == 0)
return false;
if(*Chat.m_aCompletionBuffer == '\0')
Expand Down Expand Up @@ -233,12 +297,28 @@ bool CBindchat::ChatDoAutocomplete(bool ShiftPressed) {
return pCompletionBind != nullptr;
}

void CBindchat::WriteLine(const char *pLine)
{
if(!m_BindchatFile || io_write(m_BindchatFile, pLine, str_length(pLine)) != static_cast<unsigned>(str_length(pLine)) || !io_write_newline(m_BindchatFile))
return;
}
void CBindchat::ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserData)
{
CBindchat *pThis = (CBindchat *)pUserData;
bool Failed = false;
pThis->m_BindchatFile = pThis->m_pStorage->OpenFile(BINDCHAT_FILE, IOFLAG_WRITE, IStorage::TYPE_SAVE);
if(!pThis->m_BindchatFile)
{
dbg_msg("config", "ERROR: opening %s failed", BINDCHAT_FILE);
return;
}

for(CBind &Bind : pThis->m_vBinds)
{
// Default binds do not need to be saved because they will get added on launch
if(Bind.m_Default)
continue;

char aBuf[BINDCHAT_MAX_CMD * 2] = "";
char *pEnd = aBuf + sizeof(aBuf);
char *pDst;
Expand All @@ -251,6 +331,14 @@ void CBindchat::ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserDa
pDst = aBuf + str_length(aBuf);
str_escape(&pDst, Bind.m_aCommand, pEnd);
str_append(aBuf, "\"");
pConfigManager->WriteLine(aBuf);
pThis->WriteLine(aBuf);
}

if(io_sync(pThis->m_BindchatFile) != 0)
Failed = true;
if(io_close(pThis->m_BindchatFile) != 0)
Failed = true;
pThis->m_BindchatFile = {};
if(Failed)
dbg_msg("config", "ERROR: writing to %s failed", BINDCHAT_FILE);
}
13 changes: 12 additions & 1 deletion src/game/client/components/tclient/bindchat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#ifndef GAME_CLIENT_COMPONENTS_BINDCHAT_H
#define GAME_CLIENT_COMPONENTS_BINDCHAT_H
#include <game/client/component.h>

#define BINDCHAT_FILE "tclient_chatbinds.cfg"

class IConfigManager;

enum
Expand All @@ -23,13 +26,17 @@ class CBindchat : public CComponent

void ExecuteBind(int Bind, const char *pArgs);

void WriteLine(const char *pLine);
class IStorage *m_pStorage = nullptr;
IOHANDLE m_BindchatFile = nullptr;

public:
class CBind
{
public:
char m_aName[BINDCHAT_MAX_NAME];
char m_aCommand[BINDCHAT_MAX_CMD];

bool m_Default = false;
bool operator==(const CBind &Other) const
{
return str_comp(m_aName, Other.m_aName) == 0 && str_comp(m_aCommand, Other.m_aCommand) == 0;
Expand All @@ -44,10 +51,14 @@ class CBindchat : public CComponent
virtual void OnConsoleInit() override;

void AddBind(const char *pName, const char *pCommand);
void AddBindDefault(const char *pName, const char *pCommand);

void RemoveBindCommand(const char *pCommand);
void RemoveBind(const char *pName);
void RemoveBind(int Index);
void RemoveAllBinds();

int GetBindNoDefault(const char *pCommand);
int GetBind(const char *pCommand);
CBind *Get(int Index);

Expand Down
61 changes: 50 additions & 11 deletions src/game/client/components/tclient/menus_tclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ void CMenus::OpenTClientDiscord()
enum
{
TCLIENT_TAB_SETTINGS = 0,
TCLIENT_TAB_BINDWHEEL = 1,
TCLIENT_TAB_BINDCHAT = 2,
TCLIENT_TAB_WARLIST = 3,
TCLIENT_TAB_INFO = 4,
NUMBER_OF_TCLIENT_TABS = 5
TCLIENT_TAB_BINDWHEEL,
TCLIENT_TAB_BINDCHAT,
TCLIENT_TAB_WARLIST,
TCLIENT_TAB_INFO,
NUMBER_OF_TCLIENT_TABS
};

typedef struct
Expand Down Expand Up @@ -133,11 +133,10 @@ bool CMenus::DoSliderWithScaledValue(const void *pId, int *pOption, const CUIRec
return false;
}


bool CMenus::DoEditBoxWithLabel(CLineInput *LineInput, const CUIRect *pRect, const char *pLabel, const char *pDefault, char *pBuf, size_t BufSize)
{
CUIRect Button, Label;
pRect->VSplitLeft(100.0f, &Label, &Button);
pRect->VSplitLeft(175.0f, &Label, &Button);
Ui()->DoLabel(&Label, pLabel, FontSize, TEXTALIGN_ML);
LineInput->SetBuffer(pBuf, BufSize);
LineInput->SetEmptyText(pDefault);
Expand Down Expand Up @@ -278,7 +277,7 @@ void CMenus::RenderSettingsTClient(CUIRect MainView)
Ui()->DoLabel(&Label, Localize("Input"), HeadlineFontSize, TEXTALIGN_ML);
Column.HSplitTop(MarginSmall, nullptr, &Column);

DoButton_CheckBoxAutoVMarginAndSet(&g_Config.m_ClFastInput, Localize("Fast Inputs (-20ms input delay)"), &g_Config.m_ClFastInput, &Column, LineSize);
DoButton_CheckBoxAutoVMarginAndSet(&g_Config.m_ClFastInput, Localize("Fast Inputs (-20ms visual delay)"), &g_Config.m_ClFastInput, &Column, LineSize);

Column.HSplitTop(MarginSmall, nullptr, &Column);
if(g_Config.m_ClFastInput)
Expand Down Expand Up @@ -594,7 +593,9 @@ void CMenus::RenderSettingsTClient(CUIRect MainView)
if(s_CurCustomTab == TCLIENT_TAB_BINDCHAT)
{
MainView.HSplitTop(MarginBetweenSections, nullptr, &MainView);
Column = MainView;
MainView.VSplitMid(&LeftView, &RightView, Margin);

Column = LeftView;

Column.HSplitTop(HeadlineHeight, &Label, &Column);
Ui()->DoLabel(&Label, Localize("Kaomoji"), HeadlineFontSize, TEXTALIGN_ML);
Expand All @@ -604,11 +605,11 @@ void CMenus::RenderSettingsTClient(CUIRect MainView)
{
Column.HSplitTop(LineSize, &Button, &Column);
char *BindCommand;
int BindIndex = GameClient()->m_Bindchat.GetBind(pCommand);
int BindIndex = GameClient()->m_Bindchat.GetBindNoDefault(pCommand);
bool BindNew = BindIndex == -1;
static char s_aBindCommandTemp[BINDCHAT_MAX_CMD] = "";
if(BindNew)
{
static char s_aBindCommandTemp[BINDCHAT_MAX_CMD] = "";
BindCommand = s_aBindCommandTemp;
BindNew = true; // Make a new bind, as we arent editing one
}
Expand Down Expand Up @@ -640,6 +641,44 @@ void CMenus::RenderSettingsTClient(CUIRect MainView)
static CLineInput s_KaomojiUnflip;
DoBindchat(s_KaomojiUnflip, Localize("Unflip:"), "!unflip", "say ┬─┬ノ( º _ ºノ)");

Column = RightView;

Column.HSplitTop(HeadlineHeight, &Label, &Column);
Ui()->DoLabel(&Label, Localize("Warlist"), HeadlineFontSize, TEXTALIGN_ML);
Column.HSplitTop(MarginSmall, nullptr, &Column);


static CLineInput s_Warlist1, s_Warlist2, s_Warlist3, s_Warlist4, s_Warlist5, s_Warlist6, s_Warlist7, s_Warlist8;
char aBuf[128];
char aGroup1Name[MAX_WARLIST_TYPE_LENGTH]; // enemy by default
char aGroup2Name[MAX_WARLIST_TYPE_LENGTH]; // team by default
str_copy(aGroup1Name, GameClient()->m_WarList.m_WarTypes[1]->m_aWarName);
str_copy(aGroup2Name, GameClient()->m_WarList.m_WarTypes[2]->m_aWarName);
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Add %s name:", aGroup1Name);
DoBindchat(s_Warlist1, aBuf, ".war", "war_name_index 1");
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Add %s clan:", aGroup1Name);
DoBindchat(s_Warlist2, aBuf, ".warclan", "war_clan_index 1");
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Add %s name:", aGroup2Name);
DoBindchat(s_Warlist3, aBuf, ".team", "war_name_index 2");
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Add %s clan:", aGroup2Name);
DoBindchat(s_Warlist4, aBuf, ".teamclan", "war_clan_index 2");
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Remove %s name:", aGroup1Name);
DoBindchat(s_Warlist5, aBuf, ".delwar", "remove_war_name_index 1");
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Remove %s clan:", aGroup1Name);
DoBindchat(s_Warlist6, aBuf, ".delwarclan", "remove_war_clan_index 1");
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Remove %s name:", aGroup2Name);
DoBindchat(s_Warlist7, aBuf, ".delteam", "remove_war_name_index 2");
Column.HSplitTop(MarginSmall, nullptr, &Column);
str_format(aBuf, sizeof(aBuf), "Remove %s clan:", aGroup2Name);
DoBindchat(s_Warlist8, aBuf, ".delteamclan", "remove_war_clan_index 2");

}

if(s_CurCustomTab == TCLIENT_TAB_BINDWHEEL)
Expand Down
Loading

0 comments on commit 7280439

Please sign in to comment.