-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsourcechat.cpp
More file actions
1983 lines (1572 loc) · 61.1 KB
/
sourcechat.cpp
File metadata and controls
1983 lines (1572 loc) · 61.1 KB
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <SDL.h>
#include <string>
#include <dbg.h>
#include <convar.h>
#include <keydefs.h>
#include <ISvenModAPI.h>
#include <IMemoryUtils.h>
#include <hl_sdk/common/protocol.h>
#include "imm/public/IMuteManager.h"
#include "plugin.h"
#include "sourcechat.h"
#include "patterns.h"
#include "chat_scheme.h"
#include "keyboard_layout_map.h"
#if IMGUI_USE_GL3
#include <backends/imgui_impl_opengl3.h>
#else
#include <backends/imgui_impl_opengl2.h>
#endif
#if IMGUI_USE_SDL
#include <backends/imgui_impl_sdl2.h>
#else
#include <backends/imgui_impl_win32.h>
#endif
#include <misc/freetype/imgui_freetype.h>
// ImGui's WndProc / SDL events handler
#if IMGUI_USE_SDL
extern bool ImGui_ImplSDL2_ProcessEvent( const SDL_Event *event );
#else
extern LRESULT ImGui_ImplWin32_WndProcHandler( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
#endif
//-----------------------------------------------------------------------------
// Declare hooks
//-----------------------------------------------------------------------------
#if IMGUI_USE_SDL
DECLARE_HOOK( int, __cdecl, SDL_PollEvent, SDL_Event * );
DECLARE_HOOK( int, __cdecl, SDL_GL_SwapWindow, SDL_Window * );
#else
DECLARE_HOOK( BOOL, APIENTRY, wglSwapBuffers, HDC );
#endif
DECLARE_HOOK( BOOL, WINAPI, SetCursorPos, int, int );
DECLARE_HOOK( void, __cdecl, Key_Event, int, int );
DECLARE_HOOK( void, __cdecl, IN_Move, float, usercmd_t * );
DECLARE_CLASS_HOOK( int, CHudTextMessage__MsgFunc_TextMsg, void *, const char *, int, void * );
CommandCallbackFn ORIG_messagemode = NULL;
CommandCallbackFn ORIG_messagemode2 = NULL;
UserMsgHookFn ORIG_UserMsgHook_SayText = NULL;
UserMsgHookFn ORIG_UserMsgHook_TextMsg = NULL;
NetMsgHookFn ORIG_NetMsgHook_TempEntity = NULL;
//-----------------------------------------------------------------------------
// Singleton
//-----------------------------------------------------------------------------
CSourceChat g_SourceChat;
//-----------------------------------------------------------------------------
// ConVars & ConCommands
//-----------------------------------------------------------------------------
ConVar sourcechat( "sourcechat", "1", FCVAR_CLIENTDLL, "Enable Source-like chat" );
ConVar sourcechat_width_fraction( "sourcechat_width_fraction", "0.0115", FCVAR_CLIENTDLL, "Screen's fraction of width" );
ConVar sourcechat_height_fraction( "sourcechat_height_fraction", "0.5732", FCVAR_CLIENTDLL, "Screen's fraction of height" );
ConVar sourcechat_fadein_duration( "sourcechat_fadein_duration", "0.3", FCVAR_CLIENTDLL, "Fade-in duration of chatbox" );
ConVar sourcechat_fadeout_duration( "sourcechat_fadeout_duration", "0.3", FCVAR_CLIENTDLL, "Fade-out duration of chatbox" );
ConVar sourcechat_text_stay_time( "sourcechat_text_stay_time", "10.0", FCVAR_CLIENTDLL, "Stay time of recently message" );
ConVar sourcechat_text_fade_duration( "sourcechat_text_fade_duration", "2.0", FCVAR_CLIENTDLL, "Fade-out duration of recently message" );
ConVar sourcechat_monsterinfo_width_fraction( "sourcechat_monsterinfo_width_fraction", "1.0", FCVAR_CLIENTDLL, "Text message's width fraction of monster info" );
ConVar sourcechat_monsterinfo_height_fraction( "sourcechat_monsterinfo_height_fraction", "0.8", FCVAR_CLIENTDLL, "Text message's height fraction of monster info" );
CON_COMMAND( sourcechat_clear, "Clear Source-like chat" )
{
g_SourceChat.Clear();
}
//-----------------------------------------------------------------------------
// Purpose: process inputs from keyboard / mouse / joystick ...
//-----------------------------------------------------------------------------
#if IMGUI_USE_SDL
DECLARE_FUNC( int, __cdecl, HOOKED_SDL_PollEvent, SDL_Event *event )
#else
DECLARE_FUNC( LRESULT, CALLBACK, HOOKED_WndProc, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
#endif
{
if ( !sourcechat.GetBool() )
{
g_SourceChat.SetOpened_Internal( false );
#if IMGUI_USE_SDL
return ORIG_SDL_PollEvent( event );
#else
return CallWindowProc( g_SourceChat.GetGameWindowProc(), hWnd, uMsg, wParam, lParam );
#endif
}
/*
#if IMGUI_USE_SDL
if ( g_SourceChat.IsOpened_Internal() && event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_ESCAPE )
#else
if ( g_SourceChat.IsOpened_Internal() && uMsg == WM_KEYDOWN && wParam == VK_ESCAPE )
#endif
{
g_SourceChat.OnClose();
return 0;
}
*/
if ( g_SourceChat.IsOpened_Internal() )
{
#if IMGUI_USE_SDL
ImGui_ImplSDL2_ProcessEvent( event );
#else
ImGui_ImplWin32_WndProcHandler( hWnd, uMsg, wParam, lParam );
#endif
return 0;
}
#if IMGUI_USE_SDL
return ORIG_SDL_PollEvent( event );
#else
return CallWindowProc( g_SourceChat.GetGameWindowProc(), hWnd, uMsg, wParam, lParam );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: draw chat on every swap buffers
//-----------------------------------------------------------------------------
#if IMGUI_USE_SDL
DECLARE_FUNC( int, __cdecl, HOOKED_SDL_GL_SwapWindow, SDL_Window *window )
#else
DECLARE_FUNC( BOOL, APIENTRY, HOOKED_wglSwapBuffers, HDC hdc )
#endif
{
static bool s_bImGuiInitialized = false;
if ( !s_bImGuiInitialized )
{
#if IMGUI_USE_SDL
g_SourceChat.InitImGui( window );
#else
g_SourceChat.InitImGui( hdc );
#endif
s_bImGuiInitialized = true;
}
#if IMGUI_USE_GL3
ImGui_ImplOpenGL3_NewFrame();
#else
ImGui_ImplOpenGL2_NewFrame();
#endif
#if IMGUI_USE_SDL
ImGui_ImplSDL2_NewFrame();
#else
ImGui_ImplWin32_NewFrame();
#endif
ImGui::NewFrame();
g_SourceChat.Draw();
ImGui::Render();
#if IMGUI_USE_GL3
ImGui_ImplOpenGL3_RenderDrawData( ImGui::GetDrawData() );
#else
ImGui_ImplOpenGL2_RenderDrawData( ImGui::GetDrawData() );
#endif
#if IMGUI_USE_SDL
return ORIG_SDL_GL_SwapWindow( window );
#else
return ORIG_wglSwapBuffers( hdc );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: forgot why did we hook it :D
//-----------------------------------------------------------------------------
DECLARE_FUNC( BOOL, WINAPI, HOOKED_SetCursorPos, int X, int Y )
{
if ( g_SourceChat.IsOpened_Internal() )
return FALSE;
return ORIG_SetCursorPos( X, Y );
}
//-----------------------------------------------------------------------------
// Purpose: stop key inputs when chat is open
//-----------------------------------------------------------------------------
DECLARE_FUNC( void, __cdecl, HOOKED_Key_Event, int key, int down )
{
if ( g_SourceChat.IsOpened_Internal() )
{
if ( down )
{
if ( key == K_ESCAPE )
g_SourceChat.OnClose();
return;
}
}
ORIG_Key_Event( key, down );
}
//-----------------------------------------------------------------------------
// Purpose: prevent mouse movements on game's camera
//-----------------------------------------------------------------------------
DECLARE_FUNC( void, __cdecl, HOOKED_IN_Move, float frametime, usercmd_t *cmd )
{
if ( g_SourceChat.IsOpened_Internal() )
return;
ORIG_IN_Move( frametime, cmd );
}
//-----------------------------------------------------------------------------
// Purpose: intercept open of original game's chat
//-----------------------------------------------------------------------------
DECLARE_FUNC( void, __cdecl, HOOKED_messagemode )
{
if ( !sourcechat.GetBool() )
return ORIG_messagemode();
g_SourceChat.OnOpen( false );
}
//-----------------------------------------------------------------------------
// Purpose: intercept open of original game's team chat
//-----------------------------------------------------------------------------
DECLARE_FUNC( void, __cdecl, HOOKED_messagemode2 )
{
if ( !sourcechat.GetBool() )
return ORIG_messagemode2();
g_SourceChat.OnOpen( true );
}
//-----------------------------------------------------------------------------
// Purpose: intercept incoming chat messages
//-----------------------------------------------------------------------------
DECLARE_FUNC( int, __cdecl, UserMsgHook_SayText, const char *pszUserMsg, int iSize, void *pBuffer )
{
if ( !sourcechat.GetBool() )
return ORIG_UserMsgHook_SayText( pszUserMsg, iSize, pBuffer );
CMessageBuffer message( pBuffer, iSize, true );
int src;
int client = message.ReadByte();
const char *pszMessage = message.ReadString();
if ( *pszMessage > 0 && *pszMessage <= 3 )
{
src = *pszMessage;
pszMessage = pszMessage + 1;
}
else
{
src = 0;
client = 0;
}
if ( pszMessage[ 0 ] != '\0' )
g_SourceChat.PrintMessage( client, pszMessage, src );
return 0;
}
//-----------------------------------------------------------------------------
// Purpose: intercept incoming chat messages
//-----------------------------------------------------------------------------
#if 0
DECLARE_FUNC( int, __cdecl, UserMsgHook_TextMsg, const char *pszUserMsg, int iSize, void *pBuffer )
#else
DECLARE_CLASS_FUNC( int, HOOKED_CHudTextMessage__MsgFunc_TextMsg, void *thisptr, const char *pszUserMsg, int iSize, void *pBuffer )
#endif
{
if ( !sourcechat.GetBool() )
#if 0
return ORIG_UserMsgHook_TextMsg( pszUserMsg, iSize, pBuffer );
#else
return ORIG_CHudTextMessage__MsgFunc_TextMsg( thisptr, pszUserMsg, iSize, pBuffer );
#endif
CMessageBuffer message( pBuffer, iSize, true );
if ( message.ReadByte() == HUD_PRINTTALK )
{
static char buffer[ 256 ];
const char *str;
std::vector<std::string> formattingStrings;
std::string msg = message.ReadString();
size_t length = strlen( msg.c_str() ) + 1;
// #1
str = message.ReadString();
if ( *str != '\0' )
formattingStrings.push_back( str );
// #2
str = message.ReadString();
if ( *str != '\0' )
formattingStrings.push_back( str );
// #3
str = message.ReadString();
if ( *str != '\0' )
formattingStrings.push_back( str );
// #4
str = message.ReadString();
if ( *str != '\0' )
formattingStrings.push_back( str );
switch ( formattingStrings.size() )
{
case 0:
if ( length >= M_ARRAYSIZE( buffer ) )
length = M_ARRAYSIZE( buffer ) - 1;
memcpy( buffer, msg.c_str(), length);
buffer [length] = '\0';
break;
case 1:
snprintf( buffer, M_ARRAYSIZE( buffer ), msg.c_str(), formattingStrings[ 0 ].c_str() );
break;
case 2:
snprintf( buffer, M_ARRAYSIZE( buffer ), msg.c_str(), formattingStrings[ 0 ].c_str(), formattingStrings[ 1 ].c_str() );
break;
case 3:
snprintf( buffer, M_ARRAYSIZE( buffer ), msg.c_str(), formattingStrings[ 0 ].c_str(), formattingStrings[ 1 ].c_str(), formattingStrings[ 2 ].c_str() );
break;
case 4:
snprintf( buffer, M_ARRAYSIZE( buffer ), msg.c_str(), formattingStrings[ 0 ].c_str(), formattingStrings[ 1 ].c_str(), formattingStrings[ 2 ].c_str(), formattingStrings[ 3 ].c_str() );
break;
}
if ( buffer[ 0 ] != '\0' )
g_SourceChat.PrintMessage( -1, buffer, 0 );
return 0;
}
#if 0
return ORIG_UserMsgHook_TextMsg( pszUserMsg, iSize, pBuffer );
#else
return ORIG_CHudTextMessage__MsgFunc_TextMsg( thisptr, pszUserMsg, iSize, pBuffer );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: adjust text message's position of monster info
//-----------------------------------------------------------------------------
void NetMsgHook_TempEntity( void )
{
auto FixedSigned16 = []( float value, float scale ) -> short
{
int output;
output = value * scale;
if ( output > 32767 )
output = 32767;
if ( output < -32768 )
output = -32768;
return (short)output;
};
CNetMessageParams *params = Utils()->GetNetMessageParams();
CMessageBuffer message( params->buffer, params->readcount, params->badread );
int entitytype = message.ReadByte();
if ( entitytype != TE_TEXTMESSAGE )
{
ORIG_NetMsgHook_TempEntity();
return;
}
int coords_offset = message.GetReadCount();
sizebuf_t *buffer = const_cast<sizebuf_t *>( message.GetBuffer() );
int channel = message.ReadByte();
float x = message.ReadShort() * ( 1.f / ( 1 << 13 ) );
float y = message.ReadShort() * ( 1.f / ( 1 << 13 ) );
const float eps = 0.00001f;
if ( !( 0.569946f - eps <= y && y <= 0.569946f + eps ) ) // monster info
{
ORIG_NetMsgHook_TempEntity();
return;
}
x *= sourcechat_monsterinfo_width_fraction.GetFloat();
y *= sourcechat_monsterinfo_height_fraction.GetFloat();
// set x
coords_offset += 1; // skip entity type, channel
*(short *)( buffer->data + coords_offset ) = FixedSigned16( x, 1 << 13 );
// set y
coords_offset += 2; // skip x
*(short *)( buffer->data + coords_offset ) = FixedSigned16( y, 1 << 13 );
ORIG_NetMsgHook_TempEntity();
}
//-----------------------------------------------------------------------------
// CSourceChat methods
//-----------------------------------------------------------------------------
CSourceChat::CSourceChat()
{
#if !IMGUI_USE_SDL
m_hGameWnd = NULL;
m_hGameWndProc = NULL;
#endif
m_flWindowWidth = 0.f;
m_flWindowHeight = 0.f;
m_szInputBuffer[ 0 ] = '\0';
m_szHistoryBuffer[ 0 ] = '\0';
m_bOpened = false;
m_bWasOpenedRightNow = false;
m_bTeamChat = false;
m_bCalcTextHistoryHeight = false;
m_flOpenTime = -1.f;
m_flCloseTime = -1.f;
m_flCurrentTime = -1.f;
m_flTextHistoryHeight = 0.f;
m_flTextHistoryDefaultColor[ 0 ] = 1.f;
m_flTextHistoryDefaultColor[ 1 ] = 1.f;
m_flTextHistoryDefaultColor[ 2 ] = 1.f;
m_pFont = NULL;
m_pFontBitmap = NULL;
m_pFontSmall = NULL;
hud_draw = NULL;
m_pfnGetClientColor = NULL;
m_pfnCClient_SoundEngine__Play2DSound = NULL;
m_pfnGetClientVoiceMgr = NULL;
m_pfnCVoiceStatus__IsPlayerBlocked = NULL;
m_pSoundEngine = NULL;
m_dbRealtime = NULL;
// Detour members
m_pfnKey_Event = NULL;
m_pfnIN_Move = NULL;
#if IMGUI_USE_SDL
m_pfnSDL_PollEvent = NULL;
m_pfnSDL_GL_SwapWindow = NULL;
#else
m_pfnwglSwapBuffers = NULL;
#endif
m_pfnSetCursorPos = NULL;
m_hKey_Event = DETOUR_INVALID_HANDLE;
m_hIN_Move = DETOUR_INVALID_HANDLE;
m_hMessageMode = DETOUR_INVALID_HANDLE;
m_hMessageMode2 = DETOUR_INVALID_HANDLE;
m_hUserMsgHook_SayText = DETOUR_INVALID_HANDLE;
m_hUserMsgHook_TextMsg = DETOUR_INVALID_HANDLE;
m_hNetMsgHook_TempEntity = DETOUR_INVALID_HANDLE;
#if IMGUI_USE_SDL
m_hSDL_PollEvent = DETOUR_INVALID_HANDLE;
m_hSDL_GL_SwapWindow = DETOUR_INVALID_HANDLE;
#else
m_hwglSwapBuffers = DETOUR_INVALID_HANDLE;
#endif
m_hSetCursorPos = DETOUR_INVALID_HANDLE;
}
//-----------------------------------------------------------------------------
// Purpose: reset/change chat state when entering server
//-----------------------------------------------------------------------------
void CSourceChat::OnEnterToServer()
{
if ( m_bOpened )
{
//OnClose();
m_bWasOpenedRightNow = true;
}
m_TextOpacity.clear();
m_flOpenTime = -1.f;
m_flCloseTime = -1.f;
}
//-----------------------------------------------------------------------------
// Purpose: reset/change chat state when disconnecting
//-----------------------------------------------------------------------------
void CSourceChat::OnDisconnect()
{
if ( m_bOpened )
{
OnClose();
}
m_TextOpacity.clear();
m_flOpenTime = -1.f;
m_flCloseTime = -1.f;
}
//-----------------------------------------------------------------------------
// Purpose: get current time
//-----------------------------------------------------------------------------
float CSourceChat::GetTime() const
{
return (float)*m_dbRealtime;
//return g_pEngineFuncs->GetClientTime();
}
//-----------------------------------------------------------------------------
// Purpose: draw chat
//-----------------------------------------------------------------------------
void CSourceChat::Draw( void )
{
m_flCurrentTime = GetTime();
ImGui::GetIO().MouseDrawCursor = m_bOpened;
ImGui::DisableCursorBlinking = !m_bOpened;
FadeThink();
if ( sourcechat.GetBool() && // Source chat enabled
SvenModAPI()->GetClientState() == CLS_ACTIVE && // We're currently playing
!VGameUI()->GameUI()->IsGameUIActive() && // Menu is not active
*(unsigned long *)&( hud_draw->value ) != 0 ) // HUD is enabled
{
constexpr int window_flags = ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoBringToFrontOnFocus;
ImVec4 *colors = ImGui::GetStyle().Colors;
const float flChatPosX = floorf( m_flWindowWidth * sourcechat_width_fraction.GetFloat() );
const float flChatPosY = floorf( m_flWindowHeight * sourcechat_height_fraction.GetFloat() );
// Colors of main and child windows
colors[ ImGuiCol_WindowBg ] = ImColor( ColorSchemeActive.WindowBackground );
colors[ ImGuiCol_Border ] = ImColor( ColorSchemeActive.WindowBorder );
colors[ ImGuiCol_ChildBg ] = ImColor( ColorSchemeActive.ChildBackground );
// Use our font as default
ImGui::PushFont( m_pFont );
// Chatbox window
ImGui::SetNextWindowPos( ImVec2( flChatPosX, flChatPosY ) );
ImGui::SetNextWindowSize( ImVec2( ChatSchemeActive.ChatSizeX, ChatSchemeActive.ChatSizeY ) );
ImGui::Begin( "chatbox", 0, window_flags );
{
// Display key codes as strings
DrawKeyCodes();
// Enable text shadow
ImGui::TextShadow = true;
// Draw chatbox's rich text
DrawTextHistory();
// Draw name of input line
DrawInputLineName();
// Draw input line
DrawInputLine();
// Disable text shadow
ImGui::TextShadow = false;
}
ImGui::End();
ImGui::PopFont();
m_bWasOpenedRightNow = false;
}
}
//-----------------------------------------------------------------------------
// Purpose: show pressed Ctrl / Alt
//-----------------------------------------------------------------------------
void CSourceChat::DrawKeyCodes( void )
{
ImGui::SetCursorPosX( ChatSchemeActive.KeyCodesPosX );
ImGui::SetCursorPosY( ChatSchemeActive.KeyCodesPosY );
ImGui::PushFont( m_pFontSmall );
ImGui::PushStyleColor( ImGuiCol_Text, ColorSchemeActive.KeyCodesText );
bool bCtrlPressed = GetAsyncKeyState( VK_RCONTROL ) || GetAsyncKeyState( VK_LCONTROL );
bool bAltPressed = GetAsyncKeyState( VK_LMENU ) || GetAsyncKeyState( VK_RMENU );
if ( bCtrlPressed && bAltPressed )
ImGui::Text( "[CTRL+ALT]" );
else if ( bCtrlPressed )
ImGui::Text( "[CTRL]" );
else if ( bAltPressed )
ImGui::Text( "[ALT]" );
ImGui::PopStyleColor();
ImGui::PopFont();
}
//-----------------------------------------------------------------------------
// Purpose: draw chatbox and its text
//-----------------------------------------------------------------------------
void CSourceChat::DrawTextHistory( void )
{
ImGui::SetCursorPosX( ChatSchemeActive.TextHistoryPosX );
ImGui::SetCursorPosY( ChatSchemeActive.TextHistoryPosY );
ImGui::PushStyleColor( ImGuiCol_ScrollbarBg, ColorSchemeActive.ScrollbarBg );
ImGui::PushStyleColor( ImGuiCol_ScrollbarGrab, ColorSchemeActive.ScrollbarGrab );
ImGui::PushStyleColor( ImGuiCol_ScrollbarGrabHovered, ColorSchemeActive.ScrollbarGrabHovered );
ImGui::PushStyleColor( ImGuiCol_ScrollbarGrabActive, ColorSchemeActive.ScrollbarGrabActive );
ImGui::BeginChild( "text-history", ImVec2( ChatSchemeActive.TextHistorySizeX, ChatSchemeActive.TextHistorySizeY ), false );
ImGui::PopStyleColor( 4 );
// Main body
ImGui::SetCursorPosX( ImGui::GetCursorPosX() + ChatSchemeActive.RichTextPosX );
ImGui::SetCursorPosY( ImGui::GetCursorPosY() + ChatSchemeActive.RichTextPosY );
ImGui::PushStyleColor( ImGuiCol_Text, ColorSchemeActive.Text ); // Text
ImGui::PushStyleColor( ImGuiCol_TextSelectedBg, ColorSchemeActive.SelectedText ); // Selected Text Background color
ImGui::PushStyleColor( ImGuiCol_ChildBg, IM_COL32( 0, 0, 0, 0 ) ); // Background (should be invisible if we keep BeginChild)
if ( m_TextOpacity.size() > 0 )
ImGui::TextDontIgnoreColorAbsence = true;
ImGui::TextLineSpacing = ChatSchemeActive.RichTextLineSpacing;
ImGui::ColorfulTextStyle = &m_ColorfulTextStyle;
ImGui::TextOpacity = &m_TextOpacity;
// Calculate new height of text history
if ( m_bCalcTextHistoryHeight )
{
m_flTextHistoryHeight = ImGui::CalcMultilineWordWrapTextHeight( m_szHistoryBuffer,
strlen( m_szHistoryBuffer ),
ChatSchemeActive.RichTextWidth ) + ChatSchemeActive.RichTextExtraSpace; // actual height + extra space
m_flTextHistoryHeight = max( m_flTextHistoryHeight, ChatSchemeActive.TextHistorySizeY - 1.f );
m_bCalcTextHistoryHeight = false;
}
// Text history
ImGui::InputTextMultiline( "##chatbox",
m_szHistoryBuffer,
strlen( m_szHistoryBuffer ) + 1,
ImVec2( ChatSchemeActive.RichTextWidth, m_flTextHistoryHeight ),
ImGuiInputTextFlags_ReadOnly | ImGuiInputTextFlags_WordWrapping | ImGuiInputTextFlags_NoVerticalScroll, NULL, NULL, false );
ImGui::TextOpacity = NULL;
ImGui::ColorfulTextStyle = NULL;
ImGui::TextLineSpacing = 0.f;
ImGui::TextDontIgnoreColorAbsence = false;
ImGui::PopStyleColor( 3 );
// Scroll to end when we open chat or it's closed
if ( m_bWasOpenedRightNow || !m_bOpened )
ImGui::SetScrollY( ImGui::GetScrollMaxY() );
ImGui::EndChild();
}
//-----------------------------------------------------------------------------
// Purpose: draw name of input line (team chat or not)
//-----------------------------------------------------------------------------
void CSourceChat::DrawInputLineName( void )
{
ImGui::SetCursorPosX( ChatSchemeActive.InputLineNamePosX );
ImGui::SetCursorPosY( ChatSchemeActive.InputLineNamePosY );
ImGui::PushStyleVar( ImGuiStyleVar_TextUnformattedAlign, ImVec2( ChatSchemeActive.InputLineNameTextAlignX, ChatSchemeActive.InputLineNameTextAlignY ) ); // Unformatted text align
ImGui::PushStyleColor( ImGuiCol_Text, ColorSchemeActive.InputLineNameText ); // Text
if ( m_bTeamChat )
{
ImGui::BeginChild( "##say", ImVec2( ChatSchemeActive.InputLineNameTeamSizeX, ChatSchemeActive.InputLineNameTeamSizeY ) );
ImGui::SetCursorPosX( ChatSchemeActive.InputLineNameTextPosX );
ImGui::SetCursorPosY( ChatSchemeActive.InputLineNameTextPosY );
ImGui::TextUnformatted( "Say (Team):" );
ImGui::EndChild();
}
else
{
ImGui::BeginChild( "##sayteam", ImVec2( ChatSchemeActive.InputLineNameSizeX, ChatSchemeActive.InputLineNameSizeY ) );
ImGui::SetCursorPosX( ChatSchemeActive.InputLineNameTextPosX );
ImGui::SetCursorPosY( ChatSchemeActive.InputLineNameTextPosY );
ImGui::TextUnformatted( "Say:" );
ImGui::EndChild();
}
ImGui::PopStyleColor( 2 );
ImGui::PopStyleVar();
}
//-----------------------------------------------------------------------------
// Purpose: draw input line
//-----------------------------------------------------------------------------
void CSourceChat::DrawInputLine( void )
{
const char *pszLayoutName = KeyboardLayoutMap.GetCurrentLayoutName();
//if ( pszLayoutName != NULL && ( !strcmp( pszLayoutName, "US" ) || !strcmp( pszLayoutName, "UK" ) ) )
// pszLayoutName = NULL;
if ( m_bTeamChat )
{
ImGui::SetCursorPosX( pszLayoutName != NULL ? ChatSchemeActive.InputLineKeyboardLayoutTeamPosX : ChatSchemeActive.InputLineTeamPosX );
ImGui::PushItemWidth( pszLayoutName != NULL ? ChatSchemeActive.InputLineKeyboardLayoutTeamWidth : ChatSchemeActive.InputLineTeamWidth );
}
else
{
ImGui::SetCursorPosX( pszLayoutName != NULL ? ChatSchemeActive.InputLineKeyboardLayoutPosX : ChatSchemeActive.InputLinePosX );
ImGui::PushItemWidth( pszLayoutName != NULL ? ChatSchemeActive.InputLineKeyboardLayoutWidth : ChatSchemeActive.InputLineWidth );
}
ImGui::SetCursorPosY( ChatSchemeActive.InputLinePosY );
ImGui::PushStyleColor( ImGuiCol_Text, ColorSchemeActive.InputLineText ); // Text
ImGui::PushStyleColor( ImGuiCol_TextSelectedBg, ColorSchemeActive.SelectedText ); // Selected Text Background color
ImGui::PushStyleColor( ImGuiCol_FrameBg, ColorSchemeActive.ChildBackground ); // Background of input line
ImGui::PushStyleVar( ImGuiStyleVar_FramePadding, ImVec2( ChatSchemeActive.InputLineFramePaddingX, ChatSchemeActive.InputLineFramePaddingY ) );
// Focus on input line when we open chat
if ( m_bWasOpenedRightNow || !m_bOpened )
ImGui::SetKeyboardFocusHere();
ImGui::CursorColor = ColorSchemeActive.Cursor;
ImGui::FrameBackgroundOffset = ImVec2( ChatSchemeActive.InputLineFrameBgOffsetX, ChatSchemeActive.InputLineFrameBgOffsetY );
if ( ImGui::InputText( "##input-line", m_szInputBuffer, CHAT_INPUT_BUFFER_SIZE, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_EscapeClearsAll, NULL, NULL, true ) )
{
// Have some symbols to send
if ( m_szInputBuffer[ 0 ] != '\0' )
{
SendMessageFromChat();
}
// Now close the chat
OnClose();
ImGui::GetIO().ClearInputKeys(); // prevent pogo stick
}
else if ( !m_bOpened && m_szInputBuffer[ 0 ] != '\0' )
{
// Clear the buffer if chat is inactive
ImGui::ClearInputText( "##input-line" );
m_szInputBuffer[ 0 ] = '\0';
}
// Show keyboard's current layout (not US / UK)
if ( pszLayoutName != NULL )
{
// Draw dummy background
if ( m_bTeamChat )
ImGui::SetCursorPosX( ChatSchemeActive.KeyboardLayoutBackgroundTeamPosX );
else
ImGui::SetCursorPosX( ChatSchemeActive.KeyboardLayoutBackgroundPosX );
ImGui::SetCursorPosY( ChatSchemeActive.KeyboardLayoutBackgroundPosY );
ImGui::BeginChild( "##kb-layout-bg", ImVec2( ChatSchemeActive.KeyboardLayoutBackgroundSizeX, ChatSchemeActive.KeyboardLayoutBackgroundSizeY ) );
ImGui::EndChild();
// Now draw main part
DrawKeyboardLayout( pszLayoutName );
}
ImGui::FrameBackgroundOffset = ImVec2( 0.f, 0.f );
ImGui::CursorColor = IM_COL32_WHITE;
ImGui::PopStyleVar();
ImGui::PopStyleColor( 3 );
ImGui::PopItemWidth();
}
//-----------------------------------------------------------------------------
// Purpose: draw current keyboard layout
//-----------------------------------------------------------------------------
void CSourceChat::DrawKeyboardLayout( const char *pszLayoutName )
{
// Disable text shadow
ImGui::TextShadow = false;
ImGui::PushFont( m_pFontBitmap );
if ( m_bTeamChat )
ImGui::SetCursorPosX( ChatSchemeActive.KeyboardLayoutTeamPosX );
else
ImGui::SetCursorPosX( ChatSchemeActive.KeyboardLayoutPosX );
ImGui::SetCursorPosY( ChatSchemeActive.KeyboardLayoutPosY );
ImGui::PushStyleVar( ImGuiStyleVar_TextUnformattedAlign, ImVec2( ChatSchemeActive.KeyboardLayoutTextAlignX, ChatSchemeActive.KeyboardLayoutTextAlignY ) ); // Unformatted text align
ImGui::PushStyleColor( ImGuiCol_Text, ColorSchemeActive.KeyboardLayoutText ); // Text
ImGui::PushStyleColor( ImGuiCol_ChildBg, ColorSchemeActive.KeyboardLayoutBackground ); // Background color
ImVec2 text_size = ImGui::CalcTextSize( pszLayoutName );
ImGui::BeginChild( "##kb-layout", ImVec2( ChatSchemeActive.KeyboardLayoutSizeX, ChatSchemeActive.KeyboardLayoutSizeY ) );
ImGui::SetCursorPosX( (float)( ( (int)ChatSchemeActive.KeyboardLayoutSizeX / 2 ) - (int)text_size.x / 2 ) ); // center alignment
ImGui::SetCursorPosY( 0.f );
ImGui::TextUnformatted( pszLayoutName );
ImGui::EndChild();
ImGui::PopStyleColor( 2 );
ImGui::PopStyleVar();
ImGui::PopFont();
// Enable text shadow
ImGui::TextShadow = true;
}
//-----------------------------------------------------------------------------
// Purpose: print incoming message from server
//-----------------------------------------------------------------------------
void CSourceChat::PrintMessage( int client, const char *pszMessage, int src )
{
// Check for muted player
if ( client > 0 )
{
const char *pszLevelName = g_pEngineFuncs->GetLevelName();
cl_entity_t *pLocal = g_pEngineFuncs->GetLocalPlayer();
if ( pszLevelName && *pszLevelName && pLocal && pLocal->index != client )
{
IMuteManager *pMuteManager = NULL;
CreateInterfaceFn ImmFactory = Sys_GetFactory( Sys_GetModuleHandle( "improved_mute_manager.dll" ) );
if ( ImmFactory != NULL && ( pMuteManager = reinterpret_cast<IMuteManager *>( ImmFactory( MUTE_MANAGER_INTERFACE_VERSION, NULL ) ) ) != NULL )
{
bool bSkipMessage = false;
pMuteManager->SetInsideChat( true );
if ( m_pfnCVoiceStatus__IsPlayerBlocked( m_pfnGetClientVoiceMgr(), client ) )
{
bSkipMessage = true;
}
pMuteManager->SetInsideChat( false );
if ( bSkipMessage )
return;
}
else if ( m_pfnCVoiceStatus__IsPlayerBlocked( m_pfnGetClientVoiceMgr(), client ) )
{
return;
}
}
}
int shiftQuantity;
const char *pszMessagePos;
float *pflClientColor = m_pfnGetClientColor( client );
std::string sMessage = pszMessage;
// Fix string without new line symbol
if ( sMessage.back() != '\n' )
sMessage += "\n";
pszMessagePos = PushMessageToBuffer( m_szHistoryBuffer, CHAT_HISTORY_BUFFER_SIZE - 1, sMessage.c_str(), &shiftQuantity );
if ( shiftQuantity > 0 )
{
ApplyShiftQuantityToTextColor( shiftQuantity );
RemoveInvalidTextColor();
ApplyShiftQuantityToTextOpacity( shiftQuantity );
RemoveInvalidTextOpacity();
}
AddTextOpacity( pszMessagePos, GetTime() );
switch ( src )
{
case 0: // just message from the Server
{
ConColorMsg( Color( m_flTextHistoryDefaultColor[ 0 ], m_flTextHistoryDefaultColor[ 1 ], m_flTextHistoryDefaultColor[ 2 ], 1.f ), sMessage.c_str() );
AddTextColor( pszMessagePos, m_flTextHistoryDefaultColor );
break;
}
case 1: // message from the Server's Console
{
const char *pszMessageSender = "<Server Console>";
ConColorMsg( Color( pflClientColor[ 0 ], pflClientColor[ 1 ], pflClientColor[ 2 ], 1.f ), pszMessageSender );
ConColorMsg( Color( m_flTextHistoryDefaultColor[ 0 ], m_flTextHistoryDefaultColor[ 1 ], m_flTextHistoryDefaultColor[ 2 ], 1.f ), sMessage.c_str() + strlen( pszMessageSender ) );
AddTextColor( pszMessagePos, pflClientColor );
AddTextColor( pszMessagePos + strlen( pszMessageSender ) + 1, m_flTextHistoryDefaultColor );
break;
}
case 2: // message from the Player
{
player_info_t *pPlayerInfo = g_pEngineStudio->PlayerInfo( client - 1 );
if ( pPlayerInfo != NULL )
{
std::string sMessageSender = pPlayerInfo->name;
sMessageSender += ":";
// sMessage starts with sMessageSender
if ( strncmp( sMessageSender.c_str(), sMessage.c_str(), strlen( sMessageSender.c_str() ) ) == 0 )
{
sMessageSender[ sMessageSender.length() - 1 ] = '\0';
ConColorMsg( Color( pflClientColor[ 0 ], pflClientColor[ 1 ], pflClientColor[ 2 ], 1.f ), sMessageSender.c_str() );
ConColorMsg( Color( m_flTextHistoryDefaultColor[ 0 ], m_flTextHistoryDefaultColor[ 1 ], m_flTextHistoryDefaultColor[ 2 ], 1.f ), sMessage.c_str() + sMessageSender.length() - 1 );
sMessageSender[ sMessageSender.length() - 1 ] = ':';
AddTextColor( pszMessagePos, pflClientColor );
AddTextColor( pszMessagePos + sMessageSender.length(), m_flTextHistoryDefaultColor );
}
else
{
// fuck this retarded TEAM
char *msgpos = const_cast<char *>( pszMessagePos );
msgpos[ 2 ] = 'e';
msgpos[ 3 ] = 'a';
msgpos[ 4 ] = 'm';