Skip to content

Commit 82801ab

Browse files
Lpsdpatrikjuvonenbotder
authored
Add set/getChatboxCharacterLimit and increase max chat message length to 255 (#2215)
* Initial commit * Add max input char limit and max input line values as const members to CChat * Add GetChatboxMaxCharacterLimit to CCore (for CChat) * Update CCoreInterface virtual method ordering * Increase MAX_CHAT_LENGTH and make MAX_CHATECHO_LENGTH "dynamic" * Fix Packet_ChatEcho to use MAX_CHATECHO_LENGTH instead of MAX_OUTPUTCHATBOX_LENGTH Why was it using MAX_OUTPUTCHATBOX_LENGTH all this time? lmao * Make setChatboxCharacterLimit reset on passing -1, instead of none Otherwise you could pass anything other than a number to setChatBoxCharacterLimit, and you wouldn't know anything was wrong, yet the chatbox input limit would reset * Remove extra func definition added by accident * Apply suggestions from code review * Apply constexpr suggestion from botder's code review Suggestion at #2215 (comment) Co-authored-by: patrikjuvonen <22572159+patrikjuvonen@users.noreply.github.com> Co-authored-by: Marek Kulik <me@botder.com>
1 parent 9f8e6cd commit 82801ab

File tree

10 files changed

+110
-12
lines changed

10 files changed

+110
-12
lines changed

Client/core/CChat.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ CChat::CChat(CGUI* pManager, const CVector2D& vecPosition)
6363
m_ePositionVertical = Chat::Position::Vertical::TOP;
6464
m_eTextAlign = Chat::Text::Align::LEFT;
6565
m_iSelectedInputHistoryEntry = -1;
66+
m_iCharacterLimit = m_iDefaultCharacterLimit;
6667

6768
// Background area
6869
m_pBackground = m_pManager->CreateStaticImage();
@@ -714,7 +715,7 @@ bool CChat::CharacterKeyHandler(CGUIKeyEventArgs KeyboardArgs)
714715
{
715716
// Check size if it's ok, then output
716717
SString strOutput = strCurrentInput.replace(iFound, std::string::npos, strPlayerName);
717-
if (MbUTF8ToUTF16(strOutput).size() < CHAT_MAX_CHAT_LENGTH)
718+
if (MbUTF8ToUTF16(strOutput).size() < m_iCharacterLimit)
718719
{
719720
bSuccess = true;
720721
m_strLastPlayerNamePart = strPlayerNamePart;
@@ -745,7 +746,7 @@ bool CChat::CharacterKeyHandler(CGUIKeyEventArgs KeyboardArgs)
745746
m_strLastPlayerName.clear();
746747

747748
// If we haven't exceeded the maximum number of characters per chat message, append the char to the message and update the input control
748-
if (MbUTF8ToUTF16(m_strInputText).size() < CHAT_MAX_CHAT_LENGTH)
749+
if (MbUTF8ToUTF16(m_strInputText).size() < m_iCharacterLimit)
749750
{
750751
if (KeyboardArgs.codepoint >= 32)
751752
{
@@ -865,7 +866,7 @@ void CChat::UpdateGUI()
865866
m_pBackground->SetSize(m_vecBackgroundSize);
866867

867868
// Make sure there is enough room for all the lines
868-
uint uiMaxNumLines = g_pCore->GetGraphics()->GetViewportHeight() / std::max(1.f, CChat::GetFontHeight(m_vecScale.fY)) - 3;
869+
uint uiMaxNumLines = g_pCore->GetGraphics()->GetViewportHeight() / std::max(1.f, CChat::GetFontHeight(m_vecScale.fY)) - m_iMaxInputLines;
869870
if (m_uiNumLines > uiMaxNumLines)
870871
SetNumLines(uiMaxNumLines);
871872

@@ -967,7 +968,7 @@ void CChat::SetInputText(const char* szText)
967968

968969
CChatLine* pLine = NULL;
969970

970-
while (szRemainingText && m_InputLine.m_ExtraLines.size() < 3)
971+
while (szRemainingText && m_InputLine.m_ExtraLines.size() < m_iMaxInputLines)
971972
{
972973
m_InputLine.m_ExtraLines.resize(m_InputLine.m_ExtraLines.size() + 1);
973974
CChatLine& line = *(m_InputLine.m_ExtraLines.end() - 1);
@@ -1077,6 +1078,11 @@ void CChat::DrawTextString(const char* szText, CRect2D DrawArea, float fZ, CRect
10771078
}
10781079
}
10791080

1081+
void CChat::SetCharacterLimit(int charLimit)
1082+
{
1083+
m_iCharacterLimit = charLimit;
1084+
}
1085+
10801086
CChatLine::CChatLine()
10811087
{
10821088
m_bActive = false;

Client/core/CChat.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class CChatLineSection;
1919
#define CHAT_WIDTH 320 // Chatbox default width
2020
#define CHAT_TEXT_COLOR CColor(235, 221, 178) // Chatbox default text color
2121
#define CHAT_MAX_LINES 100 // Chatbox maximum chat lines
22-
#define CHAT_MAX_CHAT_LENGTH 96 // Chatbox maximum chat message length
2322
#define CHAT_BUFFER 1024 // Chatbox buffer size
2423
#define CHAT_INPUT_HISTORY_LENGTH 128 // Chatbox input history length
2524

@@ -203,6 +202,11 @@ class CChat
203202
void SetChatFont(eChatFont Font);
204203
void OnModLoad();
205204

205+
void SetCharacterLimit(int charLimit);
206+
int GetCharacterLimit() const { return m_iCharacterLimit; }
207+
constexpr int GetDefaultCharacterLimit() const { return m_iDefaultCharacterLimit; }
208+
constexpr int GetMaxCharacterLimit() const { return m_iMaxCharacterLimit; }
209+
206210
private:
207211
void LoadCVars();
208212

@@ -292,4 +296,9 @@ class CChat
292296
CTickCount m_lastRenderTargetCreationFail;
293297

294298
bool m_bNickCompletion;
299+
300+
int m_iCharacterLimit;
301+
static inline constexpr int m_iDefaultCharacterLimit = 96;
302+
static inline constexpr int m_iMaxCharacterLimit = 255;
303+
static inline constexpr int m_iMaxInputLines = 5;
295304
};

Client/core/CCore.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,47 @@ bool CCore::IsChatInputEnabled()
504504
return false;
505505
}
506506

507+
bool CCore::SetChatboxCharacterLimit(int charLimit)
508+
{
509+
CChat* pChat = m_pLocalGUI->GetChat();
510+
511+
if (!pChat)
512+
return false;
513+
514+
pChat->SetCharacterLimit(charLimit);
515+
return true;
516+
}
517+
518+
void CCore::ResetChatboxCharacterLimit()
519+
{
520+
CChat* pChat = m_pLocalGUI->GetChat();
521+
522+
if (!pChat)
523+
return;
524+
525+
pChat->SetCharacterLimit(pChat->GetDefaultCharacterLimit());
526+
}
527+
528+
int CCore::GetChatboxCharacterLimit()
529+
{
530+
CChat* pChat = m_pLocalGUI->GetChat();
531+
532+
if (!pChat)
533+
return 0;
534+
535+
return pChat->GetCharacterLimit();
536+
}
537+
538+
int CCore::GetChatboxMaxCharacterLimit()
539+
{
540+
CChat* pChat = m_pLocalGUI->GetChat();
541+
542+
if (!pChat)
543+
return 0;
544+
545+
return pChat->GetMaxCharacterLimit();
546+
}
547+
507548
bool CCore::IsSettingsVisible()
508549
{
509550
if (m_pLocalGUI)
@@ -1280,6 +1321,9 @@ void CCore::OnModUnload()
12801321

12811322
// Destroy tray icon
12821323
m_pTrayIcon->DestroyTrayIcon();
1324+
1325+
// Reset chatbox character limit
1326+
ResetChatboxCharacterLimit();
12831327
}
12841328

12851329
void CCore::RegisterCommands()

Client/core/CCore.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
125125
bool IsChatInputEnabled();
126126
bool ClearChat();
127127
void OnGameTimerUpdate();
128+
bool SetChatboxCharacterLimit(int charLimit);
129+
void ResetChatboxCharacterLimit();
130+
int GetChatboxCharacterLimit();
131+
int GetChatboxMaxCharacterLimit();
128132

129133
// Screenshots
130134
void TakeScreenShot();

Client/mods/deathmatch/logic/CPacketHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ void CPacketHandler::Packet_ChatEcho(NetBitStreamInterface& bitStream)
13741374
szMessage[iNumberOfBytesUsed] = 0;
13751375
// actual limits enforced on the remote client, this is the maximum a string can be to be printed.
13761376
if (MbUTF8ToUTF16(szMessage).size() <=
1377-
MAX_OUTPUTCHATBOX_LENGTH + 6) // Extra 6 characters to fix #7125 (Teamsay + long name + long message = too long message)
1377+
MAX_CHATECHO_LENGTH + 6) // Extra 6 characters to fix #7125 (Teamsay + long name + long message = too long message)
13781378
{
13791379
// Strip it for bad characters
13801380
StripControlCodes(szMessage, ' ');

Client/mods/deathmatch/logic/Config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ class CVaPassNext
5252
#define va_pass(valist) CVaPassNext(valist).svapassnext
5353
/*** va_pass() (passing of ... variable length arguments ***/
5454

55-
// Min and max number of characters in chat messages
55+
// Min and max number of characters in chat messages (the message itself, not including player nick)
5656
#define MIN_CHAT_LENGTH 1
57-
#define MAX_CHAT_LENGTH 96
57+
#define MAX_CHAT_LENGTH 255
5858

5959
// Min and max number of characters in a console command
6060
#define MIN_COMMAND_LENGTH 1
6161
#define MAX_COMMAND_LENGTH 255
6262

6363
// Min and max number of characters in chat echos
6464
#define MIN_CHATECHO_LENGTH 1
65-
#define MAX_CHATECHO_LENGTH 128
65+
#define MAX_CHATECHO_LENGTH (MAX_CHAT_LENGTH + MAX_PLAYER_NICK_LENGTH + 2) // +2 is for ": " between player nick and the message
6666

6767
// Min and max number of characters in outputChatBox from the server
6868
#define MIN_OUTPUTCHATBOX_LENGTH 1

Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13+
#include <lua/CLuaFunctionParser.h>
1314

1415
static const SFixedArray<const char*, MAX_CHATBOX_LAYOUT_CVARS> g_chatboxLayoutCVars = {{"chat_font",
1516
"chat_lines",
@@ -49,6 +50,9 @@ void CLuaGUIDefs::LoadFunctions()
4950
{"isMTAWindowActive", GUIIsMTAWindowActive},
5051
{"isTransferBoxActive", GUIIsTransferBoxActive},
5152

53+
{"setChatboxCharacterLimit", ArgumentParser<GUISetChatboxCharacterLimit>},
54+
{"getChatboxCharacterLimit", ArgumentParser<GUIGetChatboxCharacterLimit>},
55+
5256
{"guiCreateWindow", GUICreateWindow},
5357
{"guiCreateLabel", GUICreateLabel},
5458
{"guiCreateButton", GUICreateButton},
@@ -239,6 +243,8 @@ void CLuaGUIDefs::AddGuiElementClass(lua_State* luaVM)
239243
lua_classfunction(luaVM, "isInputEnabled", "guiGetInputEnabled");
240244
lua_classfunction(luaVM, "getInputMode", "guiGetInputMode");
241245
lua_classfunction(luaVM, "getCursorType", "guiGetCursorType");
246+
lua_classfunction(luaVM, "setChatboxCharacterLimit", "setChatboxCharacterLimit");
247+
lua_classfunction(luaVM, "getChatboxCharacterLimit", "getChatboxCharacterLimit");
242248

243249
lua_classfunction(luaVM, "getScreenSize", "guiGetScreenSize");
244250
lua_classfunction(luaVM, "getProperties", "guiGetProperties");
@@ -280,6 +286,7 @@ void CLuaGUIDefs::AddGuiElementClass(lua_State* luaVM)
280286
lua_classvariable(luaVM, "text", "guiSetText", "guiGetText");
281287
lua_classvariable(luaVM, "size", "guiSetSize", "guiGetSize");
282288
lua_classvariable(luaVM, "position", "guiSetPosition", "guiGetPosition");
289+
lua_classvariable(luaVM, "chatboxCharacterLimit", "setChatboxCharacterLimit", "getChatboxCharacterLimit");
283290

284291
lua_registerclass(luaVM, "GuiElement", "Element");
285292
}
@@ -4058,3 +4065,24 @@ int CLuaGUIDefs::GUIGetCursorType(lua_State* luaVM)
40584065
lua_pushstring(luaVM, EnumToString(eType));
40594066
return 1;
40604067
}
4068+
4069+
bool CLuaGUIDefs::GUISetChatboxCharacterLimit(int charLimit)
4070+
{
4071+
if (charLimit == -1)
4072+
{
4073+
g_pCore->ResetChatboxCharacterLimit();
4074+
return true;
4075+
}
4076+
4077+
int maxCharLimit = g_pCore->GetChatboxMaxCharacterLimit();
4078+
4079+
if (charLimit < 0 || charLimit > maxCharLimit)
4080+
throw std::invalid_argument(SString("Character limit must be %s than, or equal to %i (got: %i)", (charLimit < 0) ? "greater" : "less", (charLimit < 0) ? 0 : maxCharLimit, charLimit));
4081+
4082+
return g_pCore->SetChatboxCharacterLimit(charLimit);
4083+
}
4084+
4085+
int CLuaGUIDefs::GUIGetChatboxCharacterLimit()
4086+
{
4087+
return g_pCore->GetChatboxCharacterLimit();
4088+
}

Client/mods/deathmatch/logic/luadefs/CLuaGUIDefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ class CLuaGUIDefs : public CLuaDefs
159159
LUA_DECLARE(GUIComboBoxIsOpen);
160160
LUA_DECLARE(GUIGetCursorType);
161161

162+
static bool GUISetChatboxCharacterLimit(int charLimit);
163+
static int GUIGetChatboxCharacterLimit();
164+
162165
private:
163166
static void AddGuiElementClass(lua_State* luaVM);
164167
static void AddGuiFontClass(lua_State* luaVM);

Client/sdk/core/CCoreInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ class CCoreInterface
175175
virtual void OnGameTimerUpdate() = 0;
176176

177177
virtual bool IsChatInputBlocked() = 0;
178+
virtual bool SetChatboxCharacterLimit(int charLimit) = 0;
179+
virtual void ResetChatboxCharacterLimit() = 0;
180+
virtual int GetChatboxCharacterLimit() = 0;
181+
virtual int GetChatboxMaxCharacterLimit() = 0;
178182
};
179183

180184
class CClientTime

Server/mods/deathmatch/Config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
// Min number of characters in passwords
3030
#define MIN_PASSWORD_LENGTH 1
3131

32-
// Min and max number of characters in chat messages
32+
// Min and max number of characters in chat messages (the message itself, not including player nick)
3333
#define MIN_CHAT_LENGTH 1
34-
#define MAX_CHAT_LENGTH 96
34+
#define MAX_CHAT_LENGTH 255
3535

3636
// Min and max number of characters in a console command
3737
#define MIN_COMMAND_LENGTH 1
3838
#define MAX_COMMAND_LENGTH 255
3939

4040
// Min and max number of characters in chat echos
4141
#define MIN_CHATECHO_LENGTH 1
42-
#define MAX_CHATECHO_LENGTH 128
42+
#define MAX_CHATECHO_LENGTH (MAX_CHAT_LENGTH + MAX_PLAYER_NICK_LENGTH + 2) // +2 is for ": " between player nick and the message
4343

4444
// Min and max number of characters in outputChatBox from the server
4545
#define MIN_OUTPUTCHATBOX_LENGTH 1

0 commit comments

Comments
 (0)