-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathchatwindow.cpp
462 lines (391 loc) · 11.5 KB
/
chatwindow.cpp
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
#include "main.h"
#include "gui/gui.h"
#include "chatwindow.h"
#include "keyboard.h"
#include "settings.h"
#include "game/game.h"
#include "net/netgame.h"
#include "dialog.h"
uint32_t MAX_CHAT_MESSAGES = 40; // 8
extern CGUI *pGUI;
extern CKeyBoard *pKeyBoard;
extern CSettings *pSettings;
extern CNetGame *pNetGame;
extern CAMERA_AIM * pcaInternalAim;
extern CGame * pGame;
extern CDialogWindow *pDialogWindow;
std::unordered_map<std::string, CMDPROC> m_mapCmds;
void ChatWindowInputHandler(const char* str)
{
if(!str || *str == '\0') return;
if(!pNetGame) return;
if(*str == '/')
{
char *szCmdEndPos = (char*)str + 1;
while(*szCmdEndPos && *szCmdEndPos != ' ') szCmdEndPos++;
if(*szCmdEndPos == '\0') {
std::unordered_map<std::string, CMDPROC>::iterator cmd = m_mapCmds.find(str + 1);
if(cmd != m_mapCmds.end())
{
cmd->second("");
}
else
{
if(pNetGame)
{
pNetGame->SendChatCommand(str);
}
}
} else {
char szCopiedBuffer[256];
strcpy(szCopiedBuffer, str);
*szCmdEndPos = '\0';
szCmdEndPos++;
std::unordered_map<std::string, CMDPROC>::iterator cmd = m_mapCmds.find(str + 1);
if(cmd != m_mapCmds.end())
{
cmd->second(szCmdEndPos);
}
else
{
if(pNetGame)
{
pNetGame->SendChatCommand(szCopiedBuffer);
}
}
}
}
else
pNetGame->SendChatMessage(str);
return;
}
CChatWindow::CChatWindow()
{
Log("Initializng Chat Window..");
m_fChatPosX = pGUI->ScaleX( pSettings->Get().fChatPosX );
m_fChatPosY = pGUI->ScaleY( pSettings->Get().fChatPosY );
m_fChatSizeX = pGUI->ScaleX( pSettings->Get().fChatSizeX );
m_fChatSizeY = pGUI->ScaleY( pSettings->Get().fChatSizeY );
m_iMaxMessages = pSettings->Get().iChatMaxMessages;
Log("Chat pos: %f, %f, size: %f, %f", m_fChatPosX, m_fChatPosY, m_fChatSizeX, m_fChatSizeY);
m_dwTextColor = 0xFFFFFFFF;
m_dwInfoColor = 0x00C8C8FF;
m_dwDebugColor = 0xBEBEBEFF;
MAX_CHAT_MESSAGES = m_iMaxMessages*5;
m_bIsOpened = false;
m_iOffsetY = 0;
}
CChatWindow::~CChatWindow()
{
m_mapCmds.clear();
}
bool CChatWindow::CheckScrollBar(int x, int y)
{
float size = pGUI->GetFontSize()*m_iMaxMessages;
float scrollBarSize = m_ChatWindowEntries.size() * (MAX_CHAT_MESSAGES/m_iMaxMessages);
if ( m_bIsOpened && x >= m_fChatPosX-pGUI->ScaleX(40.0f) && x <= m_fChatPosX-pGUI->ScaleX(3.0f) &&
y >= m_fChatPosY + pGUI->ScaleY(scrollBarSize) + m_iOffsetY && y <= m_fChatPosY + size + m_iOffsetY )
{
bSwipeScroll = true;
//m_bIsOpened = true;
return true;
}
return false;
}
void CChatWindow::OnExitFromInput()
{
m_bIsOpened = false;
bSwipeScroll = false;
}
bool CChatWindow::OnTouchEvent(int type, bool multi, int x, int y)
{
static bool bWannaOpenChat = false;
float size = pGUI->GetFontSize()*m_iMaxMessages;
float scrollBarSize = m_ChatWindowEntries.size() * (MAX_CHAT_MESSAGES/m_iMaxMessages);
switch(type)
{
case TOUCH_PUSH:
if (m_bIsOpened && x >= m_fChatPosX-pGUI->ScaleX(40.0f) && x <= m_fChatPosX-pGUI->ScaleX(3.0f) &&
y >= m_fChatPosY + pGUI->ScaleY(scrollBarSize) + m_iOffsetY &&
y <= m_fChatPosY + size + m_iOffsetY )
{
bSwipeScroll = true;
return true;
}
if(x >= m_fChatPosX && x <= m_fChatPosX + m_fChatSizeX &&
y >= m_fChatPosY && y <= m_fChatPosY + m_fChatSizeY)
bWannaOpenChat = true;
break;
case TOUCH_POP:
if (m_bIsOpened && bSwipeScroll)
{
bSwipeScroll = false;
return true;
}
if(bWannaOpenChat &&
x >= m_fChatPosX && x <= m_fChatPosX + m_fChatSizeX &&
y >= m_fChatPosY && y <= m_fChatPosY + m_fChatSizeY)
{
if(!(pDialogWindow->m_bIsActive == true && pDialogWindow->m_byteDialogStyle == DIALOG_STYLE_LIST))
{
m_bIsOpened = true;
pKeyBoard->Open(&ChatWindowInputHandler);
}
}
bWannaOpenChat = false;
break;
case TOUCH_MOVE:
if (m_bIsOpened && bSwipeScroll)
{
if(m_iLastPosY > y)
{
if( m_fChatPosY + pGUI->ScaleY(scrollBarSize) + m_iOffsetY > m_fChatPosY )
m_iOffsetY -= 3;
}
if(m_iLastPosY < y)
{
if( m_fChatPosY + size + m_iOffsetY <= m_fChatPosY + size )
m_iOffsetY += 3;
}
m_iLastPosY = y;
return false;
}
break;
}
return true;
}
void CChatWindow::Render()
{
if(pSettings->Get().bDebug)
{
ImGui::GetBackgroundDrawList()->AddRect(
ImVec2(m_fChatPosX, m_fChatPosY),
ImVec2( m_fChatPosX + m_fChatSizeX, m_fChatPosY + m_fChatSizeY),
IM_COL32_BLACK);
}
float size = pGUI->GetFontSize()*m_iMaxMessages;
ImVec2 pos = ImVec2(m_fChatPosX, m_fChatPosY+size-pGUI->GetFontSize());
float scrollBarSize = m_ChatWindowEntries.size() * (MAX_CHAT_MESSAGES/m_iMaxMessages);
if(m_bIsOpened)
{
if(m_ChatWindowEntries.size() > m_iMaxMessages)
{
ImGui::GetOverlayDrawList()->AddRectFilled(
ImVec2(m_fChatPosX-pGUI->ScaleX(40.0f + pSettings->Get().iFontOutline), m_fChatPosY + size + m_iOffsetY + pSettings->Get().iFontOutline), //
ImVec2(m_fChatPosX-pGUI->ScaleX(3.0f - pSettings->Get().iFontOutline),
m_fChatPosY + pGUI->ScaleY(scrollBarSize) + m_iOffsetY - pSettings->Get().iFontOutline), // m_iOffsetY
0xB0000000
);
ImGui::GetOverlayDrawList()->AddRectFilled(
ImVec2(m_fChatPosX-pGUI->ScaleX(40.0f), m_fChatPosY + size + m_iOffsetY), //
ImVec2(m_fChatPosX-pGUI->ScaleX(3.0f),
m_fChatPosY + pGUI->ScaleY(scrollBarSize) + m_iOffsetY), // m_iOffsetY
0xFF3291FF
);
}
}
float scrollbarSize = (m_fChatPosY + size + m_iOffsetY) - (m_fChatPosY + pGUI->ScaleY(scrollBarSize) + m_iOffsetY) ;
float scroll = size/scrollbarSize;
int counter = -1;
int counterEx = 0;
for (std::list<CHAT_WINDOW_ENTRY>::iterator entry = m_ChatWindowEntries.end(); entry != m_ChatWindowEntries.begin(); entry--)
{
//
counter++;
if( -(m_iOffsetY/scroll) > counter && m_iOffsetY/scroll < 0 )
{
continue;
}
counterEx++;
if(counterEx > m_iMaxMessages)
{
continue;
}
if(counter == 0 && m_iOffsetY <= 3 )
continue;
switch(entry->eType)
{
case CHAT_TYPE_CHAT:
if(entry->szNick[0] != 0)
{
RenderText(entry->szNick, pos.x, pos.y, entry->dwNickColor);
pos.x += ImGui::CalcTextSize(entry->szNick).x + ImGui::CalcTextSize(" ").x; //+ pGUI->GetFontSize() * 0.4;
}
RenderText(entry->utf8Message, pos.x, pos.y, entry->dwTextColor);
break;
case CHAT_TYPE_INFO:
case CHAT_TYPE_DEBUG:
RenderText(entry->utf8Message, pos.x, pos.y, entry->dwTextColor);
break;
}
pos.x = m_fChatPosX;
pos.y -= pGUI->GetFontSize();
}
}
bool ProcessInlineHexColor(const char* start, const char* end, ImVec4& color)
{
const int hexCount = (int)(end-start);
if(hexCount == 6 || hexCount == 8)
{
char hex[9];
strncpy(hex, start, hexCount);
hex[hexCount] = 0;
unsigned int hexColor = 0;
if(sscanf(hex, "%x", &hexColor) > 0)
{
color.x = static_cast< float >((hexColor & 0x00FF0000) >> 16) / 255.0f;
color.y = static_cast< float >((hexColor & 0x0000FF00) >> 8) / 255.0f;
color.z = static_cast< float >((hexColor & 0x000000FF)) / 255.0f;
color.w = 1.0f;
if(hexCount == 8)
color.w = static_cast< float >((hexColor & 0xFF000000) >> 24) / 255.0f;
return true;
}
}
return false;
}
void CChatWindow::RenderText(const char* u8Str, float posX, float posY, uint32_t dwColor)
{
const char* textStart = u8Str;
const char* textCur = u8Str;
const char* textEnd = u8Str + strlen(u8Str);
ImVec2 posCur = ImVec2(posX, posY);
ImColor colorCur = ImColor(dwColor);
ImVec4 col;
while(*textCur)
{
// {BBCCDD}
// '{' � '}' ������������� ASCII ���������
if(textCur[0] == '{' && ((&textCur[7] < textEnd) && textCur[7] == '}'))
{
// ������� ����� �� �������� ������
if(textCur != textStart)
{
// ������� �� �������� �������
pGUI->RenderText(posCur, colorCur, true, textStart, textCur);
// ����������� ����� ��������
posCur.x += ImGui::CalcTextSize(textStart, textCur).x;
}
// �������� ����
if(ProcessInlineHexColor(textCur+1, textCur+7, col))
colorCur = col;
// ������� ��������
textCur += 7;
textStart = textCur + 1;
}
textCur++;
}
if(textCur != textStart)
pGUI->RenderText(posCur, colorCur, true, textStart, textCur);
return;
}
void CChatWindow::AddChatMessage(char* szNick, uint32_t dwNickColor, char* szMessage)
{
FilterInvalidChars(szMessage);
AddToChatWindowBuffer(CHAT_TYPE_CHAT, szMessage, szNick, m_dwTextColor, dwNickColor);
}
void CChatWindow::AddInfoMessage(const char* szFormat, ...)
{
char tmp_buf[512];
memset(tmp_buf, 0, sizeof(tmp_buf));
va_list args;
va_start(args, szFormat);
vsprintf(tmp_buf, szFormat, args);
va_end(args);
FilterInvalidChars(tmp_buf);
AddToChatWindowBuffer(CHAT_TYPE_INFO, tmp_buf, nullptr, m_dwInfoColor, 0);
}
void CChatWindow::AddDebugMessage(char *szFormat, ...)
{
char tmp_buf[512];
memset(tmp_buf, 0, sizeof(tmp_buf));
va_list args;
va_start(args, szFormat);
vsprintf(tmp_buf, szFormat, args);
va_end(args);
FilterInvalidChars(tmp_buf);
AddToChatWindowBuffer(CHAT_TYPE_DEBUG, tmp_buf, nullptr, m_dwDebugColor, 0);
}
void CChatWindow::AddClientMessage(uint32_t dwColor, char* szStr)
{
FilterInvalidChars(szStr);
AddToChatWindowBuffer(CHAT_TYPE_INFO, szStr, nullptr, dwColor, 0);
}
void CChatWindow::PushBack(CHAT_WINDOW_ENTRY &entry)
{
if(m_ChatWindowEntries.size() >= MAX_CHAT_MESSAGES)
{
m_ChatWindowEntries.pop_front();
}
m_ChatWindowEntries.push_back(entry);
return;
}
void CChatWindow::AddToChatWindowBuffer(eChatMessageType type, char* szString, char* szNick,
uint32_t dwTextColor, uint32_t dwNickColor)
{
int iBestLineLength = 0;
CHAT_WINDOW_ENTRY entry;
entry.eType = type;
entry.dwNickColor = __builtin_bswap32(dwNickColor | 0x000000FF);
entry.dwTextColor = __builtin_bswap32(dwTextColor | 0x000000FF);
if(szNick)
{
strcpy(entry.szNick, szNick);
strcat(entry.szNick, ":");
}
else
entry.szNick[0] = '\0';
if(type == CHAT_TYPE_CHAT && strlen(szString) > MAX_LINE_LENGTH)
{
iBestLineLength = MAX_LINE_LENGTH;
// ������� ������ ������ � �����
while(szString[iBestLineLength] != ' ' && iBestLineLength)
iBestLineLength--;
// ���� ��������� ����� ������ 12 ��������
if((MAX_LINE_LENGTH - iBestLineLength) > 12)
{
// ������� �� MAX_MESSAGE_LENGTH/2
cp1251_to_utf8(entry.utf8Message, szString, MAX_LINE_LENGTH);
PushBack(entry);
// ������� ����� MAX_MESSAGE_LENGTH/2
entry.szNick[0] = '\0';
cp1251_to_utf8(entry.utf8Message, szString+MAX_LINE_LENGTH);
PushBack(entry);
}
else
{
// ������� �� �������
cp1251_to_utf8(entry.utf8Message, szString, iBestLineLength);
PushBack(entry);
// ������� ����� �������
entry.szNick[0] = '\0';
cp1251_to_utf8(entry.utf8Message, szString+(iBestLineLength+1));
PushBack(entry);
}
}
else
{
cp1251_to_utf8(entry.utf8Message, szString, MAX_MESSAGE_LENGTH);
PushBack(entry);
}
return;
}
void CChatWindow::FilterInvalidChars(char *szString)
{
while(*szString)
{
if(*szString > 0 && *szString < ' ')
*szString = ' ';
szString++;
}
}
void CChatWindow::AddCmdProc(const char *cmdname, CMDPROC cmdproc)
{
m_mapCmds.insert(std::make_pair(cmdname, cmdproc));
}
void CChatWindow::DeleteCmdProc(const char *cmdname)
{
std::unordered_map<std::string, CMDPROC>::iterator cmd = m_mapCmds.find(cmdname);
if(cmd != m_mapCmds.end())
m_mapCmds.erase(cmd);
}