1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <engine/engine.h>
#include <engine/graphics.h>
#include <engine/textrender.h>
#include <engine/keys.h>
#include <engine/shared/config.h>
#include <game/generated/protocol.h>
#include <game/generated/client_data.h>
#include <game/client/gameclient.h>
#include <game/client/components/scoreboard.h>
#include <game/client/components/sounds.h>
#include <game/localization.h>
#include "chat.h"
CChat::CChat()
{
OnReset();
}
void CChat::OnReset()
{
for(int i = 0; i < MAX_LINES; i++)
{
m_aLines[i].m_Time = 0;
m_aLines[i].m_aText[0] = 0;
m_aLines[i].m_aName[0] = 0;
}
m_Show = false;
m_InputUpdate = false;
m_ChatStringOffset = 0;
m_CompletionChosen = -1;
m_aCompletionBuffer[0] = 0;
m_PlaceholderOffset = 0;
m_PlaceholderLength = 0;
m_pHistoryEntry = 0x0;
m_PendingChatCounter = 0;
m_LastChatSend = 0;
for(int i = 0; i < CHAT_NUM; ++i)
m_aLastSoundPlayed[i] = 0;
}
void CChat::OnRelease()
{
m_Show = false;
}
void CChat::OnStateChange(int NewState, int OldState)
{
if(OldState <= IClient::STATE_CONNECTING)
{
m_Mode = MODE_NONE;
for(int i = 0; i < MAX_LINES; i++)
m_aLines[i].m_Time = 0;
m_CurrentLine = 0;
}
}
void CChat::ConSay(IConsole::IResult *pResult, void *pUserData)
{
((CChat*)pUserData)->Say(0, pResult->GetString(0));
}
void CChat::ConSayTeam(IConsole::IResult *pResult, void *pUserData)
{
((CChat*)pUserData)->Say(1, pResult->GetString(0));
}
void CChat::ConChat(IConsole::IResult *pResult, void *pUserData)
{
const char *pMode = pResult->GetString(0);
if(str_comp(pMode, "all") == 0)
((CChat*)pUserData)->EnableMode(0);
else if(str_comp(pMode, "team") == 0)
((CChat*)pUserData)->EnableMode(1);
else
((CChat*)pUserData)->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", "expected all or team as mode");
}
void CChat::ConShowChat(IConsole::IResult *pResult, void *pUserData)
{
((CChat *)pUserData)->m_Show = pResult->GetInteger(0) != 0;
}
void CChat::OnConsoleInit()
{
Console()->Register("say", "r", CFGFLAG_CLIENT, ConSay, this, "Say in chat");
Console()->Register("say_team", "r", CFGFLAG_CLIENT, ConSayTeam, this, "Say in team chat");
Console()->Register("chat", "s", CFGFLAG_CLIENT, ConChat, this, "Enable chat with all/team mode");
Console()->Register("+show_chat", "", CFGFLAG_CLIENT, ConShowChat, this, "Show chat");
}
bool CChat::OnInput(IInput::CEvent Event)
{
if(m_Mode == MODE_NONE)
return false;
if(Event.m_Flags&IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)
{
m_Mode = MODE_NONE;
m_pClient->OnRelease();
}
else if(Event.m_Flags&IInput::FLAG_PRESS && (Event.m_Key == KEY_RETURN || Event.m_Key == KEY_KP_ENTER))
{
if(m_Input.GetString()[0])
{
if(m_LastChatSend+time_freq() < time_get())
Say(m_Mode == MODE_ALL ? 0 : 1, m_Input.GetString());
else
++m_PendingChatCounter;
CHistoryEntry *pEntry = m_History.Allocate(sizeof(CHistoryEntry)+m_Input.GetLength());
pEntry->m_Team = m_Mode == MODE_ALL ? 0 : 1;
mem_copy(pEntry->m_aText, m_Input.GetString(), m_Input.GetLength()+1);
}
m_pHistoryEntry = 0x0;
m_Mode = MODE_NONE;
m_pClient->OnRelease();
}
if(Event.m_Flags&IInput::FLAG_PRESS && Event.m_Key == KEY_TAB)
{
// fill the completion buffer
if(m_CompletionChosen < 0)
{
const char *pCursor = m_Input.GetString()+m_Input.GetCursorOffset();
for(int Count = 0; Count < m_Input.GetCursorOffset() && *(pCursor-1) != ' '; --pCursor, ++Count);
m_PlaceholderOffset = pCursor-m_Input.GetString();
for(m_PlaceholderLength = 0; *pCursor && *pCursor != ' '; ++pCursor)
++m_PlaceholderLength;
str_copy(m_aCompletionBuffer, m_Input.GetString()+m_PlaceholderOffset, min(static_cast<int>(sizeof(m_aCompletionBuffer)), m_PlaceholderLength+1));
}
// find next possible name
const char *pCompletionString = 0;
m_CompletionChosen = (m_CompletionChosen+1)%(2*MAX_CLIENTS);
for(int i = 0; i < 2*MAX_CLIENTS; ++i)
{
int SearchType = ((m_CompletionChosen+i)%(2*MAX_CLIENTS))/MAX_CLIENTS;
int Index = (m_CompletionChosen+i)%MAX_CLIENTS;
if(!m_pClient->m_Snap.m_paPlayerInfos[Index])
continue;
bool Found = false;
if(SearchType == 1)
{
if(str_comp_nocase_num(m_pClient->m_aClients[Index].m_aName, m_aCompletionBuffer, str_length(m_aCompletionBuffer)) &&
str_find_nocase(m_pClient->m_aClients[Index].m_aName, m_aCompletionBuffer))
Found = true;
}
else if(!str_comp_nocase_num(m_pClient->m_aClients[Index].m_aName, m_aCompletionBuffer, str_length(m_aCompletionBuffer)))
Found = true;
if(Found)
{
pCompletionString = m_pClient->m_aClients[Index].m_aName;
m_CompletionChosen = Index+SearchType*MAX_CLIENTS;
break;
}
}
// insert the name
if(pCompletionString)
{
char aBuf[256];
// add part before the name
str_copy(aBuf, m_Input.GetString(), min(static_cast<int>(sizeof(aBuf)), m_PlaceholderOffset+1));
// add the name
str_append(aBuf, pCompletionString, sizeof(aBuf));
// add seperator
const char *pSeparator = "";
if(*(m_Input.GetString()+m_PlaceholderOffset+m_PlaceholderLength) != ' ')
pSeparator = m_PlaceholderOffset == 0 ? ": " : " ";
else if(m_PlaceholderOffset == 0)
pSeparator = ":";
if(*pSeparator)
str_append(aBuf, pSeparator, sizeof(aBuf));
// add part after the name
str_append(aBuf, m_Input.GetString()+m_PlaceholderOffset+m_PlaceholderLength, sizeof(aBuf));
m_PlaceholderLength = str_length(pSeparator)+str_length(pCompletionString);
m_OldChatStringLength = m_Input.GetLength();
m_Input.Set(aBuf);
m_Input.SetCursorOffset(m_PlaceholderOffset+m_PlaceholderLength);
m_InputUpdate = true;
}
}
else
{
// reset name completion process
if(Event.m_Flags&IInput::FLAG_PRESS && Event.m_Key != KEY_TAB)
m_CompletionChosen = -1;
m_OldChatStringLength = m_Input.GetLength();
m_Input.ProcessInput(Event);
m_InputUpdate = true;
}
if(Event.m_Flags&IInput::FLAG_PRESS && Event.m_Key == KEY_UP)
{
if(m_pHistoryEntry)
{
CHistoryEntry *pTest = m_History.Prev(m_pHistoryEntry);
if(pTest)
m_pHistoryEntry = pTest;
}
else
m_pHistoryEntry = m_History.Last();
if(m_pHistoryEntry)
m_Input.Set(m_pHistoryEntry->m_aText);
}
else if (Event.m_Flags&IInput::FLAG_PRESS && Event.m_Key == KEY_DOWN)
{
if(m_pHistoryEntry)
m_pHistoryEntry = m_History.Next(m_pHistoryEntry);
if (m_pHistoryEntry)
m_Input.Set(m_pHistoryEntry->m_aText);
else
m_Input.Clear();
}
return true;
}
void CChat::EnableMode(int Team)
{
if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
return;
if(m_Mode == MODE_NONE)
{
if(Team)
m_Mode = MODE_TEAM;
else
m_Mode = MODE_ALL;
m_Input.Clear();
Input()->ClearEvents();
m_CompletionChosen = -1;
}
}
void CChat::OnMessage(int MsgType, void *pRawMsg)
{
if(MsgType == NETMSGTYPE_SV_CHAT)
{
CNetMsg_Sv_Chat *pMsg = (CNetMsg_Sv_Chat *)pRawMsg;
AddLine(pMsg->m_ClientID, pMsg->m_Team, pMsg->m_pMessage);
}
}
void CChat::AddLine(int ClientID, int Team, const char *pLine)
{
if(ClientID != -1 && (m_pClient->m_aClients[ClientID].m_aName[0] == '\0' || // unknown client
m_pClient->m_aClients[ClientID].m_ChatIgnore))
return;
bool Highlighted = false;
char *p = const_cast<char*>(pLine);
while(*p)
{
Highlighted = false;
pLine = p;
// find line seperator and strip multiline
while(*p)
{
if(*p++ == '\n')
{
*(p-1) = 0;
break;
}
}
m_CurrentLine = (m_CurrentLine+1)%MAX_LINES;
m_aLines[m_CurrentLine].m_Time = time_get();
m_aLines[m_CurrentLine].m_YOffset[0] = -1.0f;
m_aLines[m_CurrentLine].m_YOffset[1] = -1.0f;
m_aLines[m_CurrentLine].m_ClientID = ClientID;
m_aLines[m_CurrentLine].m_Team = Team;
m_aLines[m_CurrentLine].m_NameColor = -2;
// check for highlighted name
const char *pHL = str_find_nocase(pLine, m_pClient->m_aClients[m_pClient->m_Snap.m_LocalClientID].m_aName);
if(pHL)
{
int Length = str_length(m_pClient->m_aClients[m_pClient->m_Snap.m_LocalClientID].m_aName);
if((pLine == pHL || pHL[-1] == ' ') && (pHL[Length] == 0 || pHL[Length] == ' ' || (pHL[Length] == ':' && pHL[Length+1] == ' ')))
Highlighted = true;
}
m_aLines[m_CurrentLine].m_Highlighted = Highlighted;
if(ClientID == -1) // server message
{
str_copy(m_aLines[m_CurrentLine].m_aName, "*** ", sizeof(m_aLines[m_CurrentLine].m_aName));
str_format(m_aLines[m_CurrentLine].m_aText, sizeof(m_aLines[m_CurrentLine].m_aText), "%s", pLine);
}
else
{
if(m_pClient->m_aClients[ClientID].m_Team == TEAM_SPECTATORS)
m_aLines[m_CurrentLine].m_NameColor = TEAM_SPECTATORS;
if(m_pClient->m_Snap.m_pGameInfoObj && m_pClient->m_Snap.m_pGameInfoObj->m_GameFlags&GAMEFLAG_TEAMS)
{
if(m_pClient->m_aClients[ClientID].m_Team == TEAM_RED)
m_aLines[m_CurrentLine].m_NameColor = TEAM_RED;
else if(m_pClient->m_aClients[ClientID].m_Team == TEAM_BLUE)
m_aLines[m_CurrentLine].m_NameColor = TEAM_BLUE;
}
str_copy(m_aLines[m_CurrentLine].m_aName, m_pClient->m_aClients[ClientID].m_aName, sizeof(m_aLines[m_CurrentLine].m_aName));
str_format(m_aLines[m_CurrentLine].m_aText, sizeof(m_aLines[m_CurrentLine].m_aText), ": %s", pLine);
}
char aBuf[1024];
str_format(aBuf, sizeof(aBuf), "%s%s", m_aLines[m_CurrentLine].m_aName, m_aLines[m_CurrentLine].m_aText);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_aLines[m_CurrentLine].m_Team?"teamchat":"chat", aBuf);
}
// play sound
int64 Now = time_get();
if(ClientID == -1)
{
if(Now-m_aLastSoundPlayed[CHAT_SERVER] >= time_freq()*3/10)
{
m_pClient->m_pSounds->Play(CSounds::CHN_GUI, SOUND_CHAT_SERVER, 0);
m_aLastSoundPlayed[CHAT_SERVER] = Now;
}
}
else if(Highlighted)
{
if(Now-m_aLastSoundPlayed[CHAT_HIGHLIGHT] >= time_freq()*3/10)
{
m_pClient->m_pSounds->Play(CSounds::CHN_GUI, SOUND_CHAT_CLIENT, 0);
m_aLastSoundPlayed[CHAT_HIGHLIGHT] = Now;
}
}
else
{
if(Now-m_aLastSoundPlayed[CHAT_CLIENT] >= time_freq()*3/10)
{
m_pClient->m_pSounds->Play(CSounds::CHN_GUI, SOUND_CHAT_HIGHLIGHT, 0);
m_aLastSoundPlayed[CHAT_CLIENT] = Now;
}
}
}
void CChat::OnRender()
{
// send pending chat messages
if(m_PendingChatCounter > 0 && m_LastChatSend+time_freq() < time_get())
{
CHistoryEntry *pEntry = m_History.Last();
for(int i = m_PendingChatCounter-1; pEntry; --i, pEntry = m_History.Prev(pEntry))
{
if(i == 0)
{
Say(pEntry->m_Team, pEntry->m_aText);
break;
}
}
--m_PendingChatCounter;
}
float Width = 300.0f*Graphics()->ScreenAspect();
Graphics()->MapScreen(0.0f, 0.0f, Width, 300.0f);
float x = 5.0f;
float y = 300.0f-20.0f;
if(m_Mode != MODE_NONE)
{
// render chat input
CTextCursor Cursor;
TextRender()->SetCursor(&Cursor, x, y, 8.0f, TEXTFLAG_RENDER);
Cursor.m_LineWidth = Width-190.0f;
Cursor.m_MaxLines = 2;
if(m_Mode == MODE_ALL)
TextRender()->TextEx(&Cursor, Localize("All"), -1);
else if(m_Mode == MODE_TEAM)
TextRender()->TextEx(&Cursor, Localize("Team"), -1);
else
TextRender()->TextEx(&Cursor, Localize("Chat"), -1);
TextRender()->TextEx(&Cursor, ": ", -1);
// check if the visible text has to be moved
if(m_InputUpdate)
{
if(m_ChatStringOffset > 0 && m_Input.GetLength() < m_OldChatStringLength)
m_ChatStringOffset = max(0, m_ChatStringOffset-(m_OldChatStringLength-m_Input.GetLength()));
if(m_ChatStringOffset > m_Input.GetCursorOffset())
m_ChatStringOffset -= m_ChatStringOffset-m_Input.GetCursorOffset();
else
{
CTextCursor Temp = Cursor;
Temp.m_Flags = 0;
TextRender()->TextEx(&Temp, m_Input.GetString()+m_ChatStringOffset, m_Input.GetCursorOffset()-m_ChatStringOffset);
TextRender()->TextEx(&Temp, "|", -1);
while(Temp.m_LineCount > 2)
{
++m_ChatStringOffset;
Temp = Cursor;
Temp.m_Flags = 0;
TextRender()->TextEx(&Temp, m_Input.GetString()+m_ChatStringOffset, m_Input.GetCursorOffset()-m_ChatStringOffset);
TextRender()->TextEx(&Temp, "|", -1);
}
}
m_InputUpdate = false;
}
TextRender()->TextEx(&Cursor, m_Input.GetString()+m_ChatStringOffset, m_Input.GetCursorOffset()-m_ChatStringOffset);
static float MarkerOffset = TextRender()->TextWidth(0, 8.0f, "|", -1)/3;
CTextCursor Marker = Cursor;
Marker.m_X -= MarkerOffset;
TextRender()->TextEx(&Marker, "|", -1);
TextRender()->TextEx(&Cursor, m_Input.GetString()+m_Input.GetCursorOffset(), -1);
}
y -= 8.0f;
int64 Now = time_get();
float LineWidth = m_pClient->m_pScoreboard->Active() ? 90.0f : 200.0f;
float HeightLimit = m_pClient->m_pScoreboard->Active() ? 230.0f : m_Show ? 50.0f : 200.0f;
float Begin = x;
float FontSize = 6.0f;
CTextCursor Cursor;
int OffsetType = m_pClient->m_pScoreboard->Active() ? 1 : 0;
for(int i = 0; i < MAX_LINES; i++)
{
int r = ((m_CurrentLine-i)+MAX_LINES)%MAX_LINES;
if(Now > m_aLines[r].m_Time+16*time_freq() && !m_Show)
break;
// get the y offset (calculate it if we haven't done that yet)
if(m_aLines[r].m_YOffset[OffsetType] < 0.0f)
{
TextRender()->SetCursor(&Cursor, Begin, 0.0f, FontSize, 0);
Cursor.m_LineWidth = LineWidth;
TextRender()->TextEx(&Cursor, m_aLines[r].m_aName, -1);
TextRender()->TextEx(&Cursor, m_aLines[r].m_aText, -1);
m_aLines[r].m_YOffset[OffsetType] = Cursor.m_Y + Cursor.m_FontSize;
}
y -= m_aLines[r].m_YOffset[OffsetType];
// cut off if msgs waste too much space
if(y < HeightLimit)
break;
float Blend = Now > m_aLines[r].m_Time+14*time_freq() && !m_Show ? 1.0f-(Now-m_aLines[r].m_Time-14*time_freq())/(2.0f*time_freq()) : 1.0f;
// reset the cursor
TextRender()->SetCursor(&Cursor, Begin, y, FontSize, TEXTFLAG_RENDER);
Cursor.m_LineWidth = LineWidth;
// render name
if(m_aLines[r].m_ClientID == -1)
TextRender()->TextColor(1.0f, 1.0f, 0.5f, Blend); // system
else if(m_aLines[r].m_Team)
TextRender()->TextColor(0.45f, 0.9f, 0.45f, Blend); // team message
else if(m_aLines[r].m_NameColor == TEAM_RED)
TextRender()->TextColor(1.0f, 0.5f, 0.5f, Blend); // red
else if(m_aLines[r].m_NameColor == TEAM_BLUE)
TextRender()->TextColor(0.7f, 0.7f, 1.0f, Blend); // blue
else if(m_aLines[r].m_NameColor == TEAM_SPECTATORS)
TextRender()->TextColor(0.75f, 0.5f, 0.75f, Blend); // spectator
else
TextRender()->TextColor(0.8f, 0.8f, 0.8f, Blend);
TextRender()->TextEx(&Cursor, m_aLines[r].m_aName, -1);
// render line
if(m_aLines[r].m_ClientID == -1)
TextRender()->TextColor(1.0f, 1.0f, 0.5f, Blend); // system
else if(m_aLines[r].m_Highlighted)
TextRender()->TextColor(1.0f, 0.5f, 0.5f, Blend); // highlighted
else if(m_aLines[r].m_Team)
TextRender()->TextColor(0.65f, 1.0f, 0.65f, Blend); // team message
else
TextRender()->TextColor(1.0f, 1.0f, 1.0f, Blend);
TextRender()->TextEx(&Cursor, m_aLines[r].m_aText, -1);
}
TextRender()->TextColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void CChat::Say(int Team, const char *pLine)
{
m_LastChatSend = time_get();
// send chat message
CNetMsg_Cl_Say Msg;
Msg.m_Team = Team;
Msg.m_pMessage = pLine;
Client()->SendPackMsg(&Msg, MSGFLAG_VITAL);
}
|