DDraceNetwork Docs
chat.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_CLIENT_COMPONENTS_CHAT_H
4#define GAME_CLIENT_COMPONENTS_CHAT_H
5#include <vector>
6
7#include <engine/console.h>
11
14#include <game/client/render.h>
15#include <game/client/skin.h>
17
18class CChat : public CComponent
19{
20 static constexpr float CHAT_HEIGHT_FULL = 200.0f;
21 static constexpr float CHAT_HEIGHT_MIN = 50.0f;
22 static constexpr float CHAT_FONTSIZE_WIDTH_RATIO = 2.5f;
23
24 enum
25 {
27 MAX_LINE_LENGTH = 256
28 };
29
31 struct CLine
32 {
33 int64_t m_Time;
34 float m_aYOffset[2];
37 bool m_Team;
40 char m_aName[64];
44 std::optional<ColorRGBA> m_CustomColor;
45
48
49 char m_aSkinName[std::size(g_Config.m_ClPlayerSkin)];
52
54
56 };
57
60
63
64 enum
65 {
66 // client IDs for special messages
69
73
78 };
79
80 int m_Mode;
81 bool m_Show;
89 {
91 int Score;
92 };
95
96 struct CCommand
97 {
101
102 CCommand() = default;
103 CCommand(const char *pName, const char *pParams, const char *pHelpText)
104 {
105 str_copy(m_aName, pName);
106 str_copy(m_aParams, pParams);
107 str_copy(m_aHelpText, pHelpText);
108 }
109
110 bool operator<(const CCommand &Other) const { return str_comp(m_aName, Other.m_aName) < 0; }
111 bool operator<=(const CCommand &Other) const { return str_comp(m_aName, Other.m_aName) <= 0; }
112 bool operator==(const CCommand &Other) const { return str_comp(m_aName, Other.m_aName) == 0; }
113 };
114
115 std::vector<CCommand> m_vCommands;
117
119 {
121 char m_aText[1];
122 };
131
133
134 static void ConSay(IConsole::IResult *pResult, void *pUserData);
135 static void ConSayTeam(IConsole::IResult *pResult, void *pUserData);
136 static void ConChat(IConsole::IResult *pResult, void *pUserData);
137 static void ConShowChat(IConsole::IResult *pResult, void *pUserData);
138 static void ConEcho(IConsole::IResult *pResult, void *pUserData);
139 static void ConClearChat(IConsole::IResult *pResult, void *pUserData);
140
141 static void ConchainChatOld(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
142 static void ConchainChatFontSize(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
143 static void ConchainChatWidth(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
144
145 bool LineShouldHighlight(const char *pLine, const char *pName);
146 void StoreSave(const char *pText);
147
148public:
149 CChat();
150 int Sizeof() const override { return sizeof(*this); }
151
152 static constexpr float MESSAGE_TEE_PADDING_RIGHT = 0.5f;
153
154 bool IsActive() const { return m_Mode != MODE_NONE; }
155 void AddLine(int ClientId, int Team, const char *pLine);
156 void EnableMode(int Team);
157 void DisableMode();
158 void RegisterCommand(const char *pName, const char *pParams, const char *pHelpText);
159 void UnregisterCommand(const char *pName);
160 void Echo(const char *pString);
161
162 void OnWindowResize() override;
163 void OnConsoleInit() override;
164 void OnStateChange(int NewState, int OldState) override;
165 void OnRefreshSkins() override;
166 void OnRender() override;
167 void OnPrepareLines(float y);
168 void Reset();
169 void OnRelease() override;
170 void OnMessage(int MsgType, void *pRawMsg) override;
171 bool OnInput(const IInput::CEvent &Event) override;
172 void OnInit() override;
173
174 void RebuildChat();
175 void ClearLines();
176
177 void EnsureCoherentFontSize() const;
178 void EnsureCoherentWidth() const;
179
180 float FontSize() const { return g_Config.m_ClChatFontSize / 10.0f; }
181 float MessagePaddingX() const { return FontSize() * (5 / 6.f); }
182 float MessagePaddingY() const { return FontSize() * (1 / 6.f); }
183 float MessageTeeSize() const { return FontSize() * (7 / 6.f); }
184 float MessageRounding() const { return FontSize() * (1 / 2.f); }
185
186 // ----- send functions -----
187
188 // Sends a chat message to the server.
189 //
190 // @param Team MODE_ALL=0 MODE_TEAM=1
191 // @param pLine the chat message
192 void SendChat(int Team, const char *pLine);
193
194 // Sends a chat message to the server.
195 //
196 // It uses a queue with a maximum of 3 entries
197 // that ensures there is a minimum delay of one second
198 // between sent messages.
199 //
200 // It uses team or public chat depending on m_Mode.
201 void SendChatQueued(const char *pLine);
202};
203#endif
Definition: chat.h:19
void OnRender() override
Definition: chat.cpp:1145
void AddLine(int ClientId, int Team, const char *pLine)
Definition: chat.cpp:639
void Echo(const char *pString)
Definition: chat.cpp:218
int m_PlaceholderOffset
Definition: chat.h:85
void RegisterCommand(const char *pName, const char *pParams, const char *pHelpText)
Definition: chat.cpp:70
CStaticRingBuffer< CHistoryEntry, 64 *1024, CRingBufferBase::FLAG_RECYCLE > m_History
Definition: chat.h:124
int m_CurrentLine
Definition: chat.h:62
int64_t m_LastChatSend
Definition: chat.h:126
CChat()
Definition: chat.cpp:25
float MessageTeeSize() const
Definition: chat.h:183
bool m_PrevShowChat
Definition: chat.h:59
CLineInputBuffered< MAX_LINE_LENGTH > m_Input
Definition: chat.h:30
float MessagePaddingX() const
Definition: chat.h:181
static void ConchainChatWidth(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
Definition: chat.cpp:210
@ CHAT_CLIENT
Definition: chat.h:76
@ CHAT_HIGHLIGHT
Definition: chat.h:75
@ SERVER_MSG
Definition: chat.h:68
@ CHAT_NUM
Definition: chat.h:77
@ CHAT_SERVER
Definition: chat.h:74
@ CLIENT_MSG
Definition: chat.h:67
@ MODE_TEAM
Definition: chat.h:72
@ MODE_NONE
Definition: chat.h:70
@ MODE_ALL
Definition: chat.h:71
void EnableMode(int Team)
Definition: chat.cpp:507
void OnPrepareLines(float y)
Definition: chat.cpp:914
CRateablePlayer m_aPlayerCompletionList[MAX_CLIENTS]
Definition: chat.h:93
void SendChat(int Team, const char *pLine)
Definition: chat.cpp:1334
bool m_EditingNewLine
Definition: chat.h:130
CLine m_aLines[MAX_LINES]
Definition: chat.h:61
void OnRefreshSkins() override
Definition: chat.cpp:899
void OnWindowResize() override
Definition: chat.cpp:115
bool m_PrevScoreBoardShowed
Definition: chat.h:58
static constexpr float CHAT_HEIGHT_FULL
Definition: chat.h:20
int m_Mode
Definition: chat.h:80
static void ConchainChatOld(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
Definition: chat.cpp:196
char m_aCurrentInputText[MAX_LINE_LENGTH]
Definition: chat.h:129
float MessagePaddingY() const
Definition: chat.h:182
int m_CompletionChosen
Definition: chat.h:83
float FontSize() const
Definition: chat.h:180
bool m_ServerSupportsCommandInfo
Definition: chat.h:132
void Reset()
Definition: chat.cpp:120
int m_PlaceholderLength
Definition: chat.h:86
static char ms_aDisplayText[MAX_LINE_LENGTH]
Definition: chat.h:87
void RebuildChat()
Definition: chat.cpp:86
int Sizeof() const override
Definition: chat.h:150
void UnregisterCommand(const char *pName)
Definition: chat.cpp:81
bool m_Show
Definition: chat.h:81
static constexpr float CHAT_FONTSIZE_WIDTH_RATIO
Definition: chat.h:22
bool OnInput(const IInput::CEvent &Event) override
Definition: chat.cpp:241
static void ConSayTeam(IConsole::IResult *pResult, void *pUserData)
Definition: chat.cpp:162
static void ConSay(IConsole::IResult *pResult, void *pUserData)
Definition: chat.cpp:157
std::vector< CCommand > m_vCommands
Definition: chat.h:115
void OnMessage(int MsgType, void *pRawMsg) override
Definition: chat.cpp:535
bool LineShouldHighlight(const char *pLine, const char *pName)
Definition: chat.cpp:562
void EnsureCoherentFontSize() const
Definition: chat.cpp:1312
char m_aCompletionBuffer[MAX_LINE_LENGTH]
Definition: chat.h:84
void OnStateChange(int NewState, int OldState) override
Definition: chat.cpp:151
static void ConShowChat(IConsole::IResult *pResult, void *pUserData)
Definition: chat.cpp:181
static void ConClearChat(IConsole::IResult *pResult, void *pUserData)
Definition: chat.cpp:191
bool m_CommandsNeedSorting
Definition: chat.h:116
int64_t m_aLastSoundPlayed[CHAT_NUM]
Definition: chat.h:127
void SendChatQueued(const char *pLine)
Definition: chat.cpp:1359
static void ConchainChatFontSize(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
Definition: chat.cpp:202
void EnsureCoherentWidth() const
Definition: chat.cpp:1322
bool m_IsInputCensored
Definition: chat.h:128
void OnRelease() override
Definition: chat.cpp:146
int m_PlayerCompletionListLength
Definition: chat.h:94
CHistoryEntry * m_pHistoryEntry
Definition: chat.h:123
bool m_CompletionUsed
Definition: chat.h:82
void StoreSave(const char *pText)
Definition: chat.cpp:587
bool IsActive() const
Definition: chat.h:154
void DisableMode()
Definition: chat.cpp:526
@ MAX_LINES
Definition: chat.h:26
@ MAX_LINE_LENGTH
Definition: chat.h:27
void OnInit() override
Definition: chat.cpp:233
float MessageRounding() const
Definition: chat.h:184
static void ConChat(IConsole::IResult *pResult, void *pUserData)
Definition: chat.cpp:167
static constexpr float MESSAGE_TEE_PADDING_RIGHT
Definition: chat.h:152
static void ConEcho(IConsole::IResult *pResult, void *pUserData)
Definition: chat.cpp:186
int m_PendingChatCounter
Definition: chat.h:125
static constexpr float CHAT_HEIGHT_MIN
Definition: chat.h:21
void ClearLines()
Definition: chat.cpp:98
void OnConsoleInit() override
Definition: chat.cpp:223
Definition: component.h:20
Definition: ringbuffer.h:78
Definition: render.h:34
Definition: console.h:45
void(* FCommandCallback)(IResult *pResult, void *pUserData)
Definition: console.h:92
@ TEMPCMD_HELP_LENGTH
Definition: console.h:34
@ TEMPCMD_PARAMS_LENGTH
Definition: console.h:35
@ TEMPCMD_NAME_LENGTH
Definition: console.h:33
Definition: input.h:24
CConfig g_Config
Definition: config.cpp:12
@ MAX_CLIENTS
Definition: protocol.h:88
int str_comp(const char *a, const char *b)
Definition: system.cpp:2933
int str_copy(char *dst, const char *src, int dst_size)
Definition: system.cpp:2720
Definition: chat.h:97
char m_aParams[IConsole::TEMPCMD_PARAMS_LENGTH]
Definition: chat.h:99
CCommand(const char *pName, const char *pParams, const char *pHelpText)
Definition: chat.h:103
bool operator==(const CCommand &Other) const
Definition: chat.h:112
CCommand()=default
bool operator<=(const CCommand &Other) const
Definition: chat.h:111
char m_aHelpText[IConsole::TEMPCMD_HELP_LENGTH]
Definition: chat.h:100
char m_aName[IConsole::TEMPCMD_NAME_LENGTH]
Definition: chat.h:98
bool operator<(const CCommand &Other) const
Definition: chat.h:110
Definition: chat.h:119
char m_aText[1]
Definition: chat.h:121
int m_Team
Definition: chat.h:120
Definition: chat.h:32
bool m_Friend
Definition: chat.h:42
int64_t m_Time
Definition: chat.h:33
bool m_Team
Definition: chat.h:37
int m_TeamNumber
Definition: chat.h:36
int m_TimesRepeated
Definition: chat.h:55
float m_aYOffset[2]
Definition: chat.h:34
int m_ClientId
Definition: chat.h:35
int m_QuadContainerIndex
Definition: chat.h:47
bool m_Whisper
Definition: chat.h:38
char m_aName[64]
Definition: chat.h:40
std::optional< ColorRGBA > m_CustomColor
Definition: chat.h:44
bool m_Highlighted
Definition: chat.h:43
int m_NameColor
Definition: chat.h:39
char m_aSkinName[std::size(g_Config.m_ClPlayerSkin)]
Definition: chat.h:49
float m_TextYOffset
Definition: chat.h:53
STextContainerIndex m_TextContainerIndex
Definition: chat.h:46
bool m_HasRenderTee
Definition: chat.h:50
char m_aText[MAX_LINE_LENGTH]
Definition: chat.h:41
CTeeRenderInfo m_TeeRenderInfo
Definition: chat.h:51
Definition: chat.h:89
int ClientId
Definition: chat.h:90
int Score
Definition: chat.h:91
Definition: textrender.h:293