/
CChat.cpp
1160 lines (976 loc) · 40.1 KB
/
CChat.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
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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: core/CChat.cpp
* PURPOSE: In-game chat box user interface implementation
* DEVELOPERS: Jax <>
* arc_
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
#include <game/CGame.h>
using std::vector;
extern CCore* g_pCore;
CChat * g_pChat = NULL;
CChat::CChat ( CGUI* pManager, const CVector2D & vecPosition )
{
g_pChat = this;
m_pManager = pManager;
// Calculate relative position (assuming a 800x600 native resolution for our defined CCHAT_* values)
CVector2D vecResolution = m_pManager->GetResolution ();
m_vecScale = CVector2D ( vecResolution.fX / 800.0f, vecResolution.fY / 600.0f );
m_vecBackgroundPosition = vecPosition * vecResolution;
// Initialize variables
m_iScrollState = 0;
m_uiMostRecentLine = 0;
m_uiScrollOffset = 0;
m_fSmoothScroll = 0;
m_fSmoothLastTimeSeconds = 0;
m_fSmoothAllowAfter = 0;
m_fSmoothScrollResetTime = 0;
m_fSmoothRepeatTimer = 0;
m_TextColor = CHAT_TEXT_COLOR;
m_bUseCEGUI = false;
m_iCVarsRevision = -1;
m_bVisible = false;
m_bInputVisible = false;
m_pFont = m_pManager->GetClearFont ();
m_pDXFont = NULL;
SetDxFont( g_pCore->GetGraphics ()->GetFont () );
m_fNativeWidth = CHAT_WIDTH;
m_fRcpUsingDxFontScale = 1;
m_bCanChangeWidth = true;
m_iScrollingBack = 0;
m_fCssStyleOverrideAlpha = 0.0f;
m_fBackgroundAlpha = 0.0f;
m_fInputBackgroundAlpha = 0.f;
m_pCacheTexture = NULL;
m_iCacheTextureRevision = 0;
m_iReportCount = 0;
// Background area
m_pBackground = m_pManager->CreateStaticImage ();
m_pBackgroundTexture = m_pManager->CreateTexture ();
m_pBackground->LoadFromTexture ( m_pBackgroundTexture );
m_pBackground->MoveToBack ();
m_pBackground->SetPosition ( m_vecBackgroundPosition );
m_pBackground->SetSize ( m_vecBackgroundSize );
m_pBackground->SetEnabled ( false );
m_pBackground->SetVisible ( false );
// Input area
m_pInput = m_pManager->CreateStaticImage ();
m_pInputTexture = m_pManager->CreateTexture ();
m_pInput->LoadFromTexture ( m_pInputTexture );
m_pInput->MoveToBack ();
m_pInput->SetPosition ( m_vecInputPosition );
m_pInput->SetSize ( m_vecInputSize );
m_pInput->SetEnabled ( false );
m_pInput->SetVisible ( false );
SetInputPrefix ( "Say: " );
// Load cvars and position the GUI
LoadCVars ();
UpdateGUI ();
}
CChat::~CChat ( void )
{
Clear ();
ClearInput ();
SetDxFont( NULL );
SAFE_DELETE ( m_pBackground );
SAFE_DELETE ( m_pBackgroundTexture );
SAFE_DELETE ( m_pInput );
SAFE_DELETE ( m_pInputTexture );
if ( g_pChat == this )
g_pChat = NULL;
}
void CChat::OnModLoad ( void )
{
// Set handlers
m_pManager->SetCharacterKeyHandler ( INPUT_MOD, GUI_CALLBACK_KEY ( &CChat::CharacterKeyHandler, this ) );
}
void CChat::LoadCVars ( void )
{
unsigned int Font;
float fWidth = 1;
CVARS_GET ( "chat_color", m_Color ); if( m_bCanChangeWidth ) SetColor ( m_Color );
CVARS_GET ( "chat_input_color", m_InputColor ); SetInputColor ( m_InputColor );
CVARS_GET ( "chat_input_text_color", m_InputTextColor );
CVARS_GET ( "chat_use_cegui", m_bUseCEGUI );
CVARS_GET ( "chat_lines", m_uiNumLines ); SetNumLines ( m_uiNumLines);
CVARS_GET ( "chat_text_color", m_TextColor );
CVARS_GET ( "chat_scale", m_vecScale );
CVARS_GET ( "chat_width", fWidth ); if( m_bCanChangeWidth ) m_fNativeWidth = fWidth * CHAT_WIDTH;
CVARS_GET ( "chat_css_style_text", m_bCssStyleText );
CVARS_GET ( "chat_css_style_background", m_bCssStyleBackground );
CVARS_GET ( "chat_line_life", (unsigned int &)m_ulChatLineLife );
CVARS_GET ( "chat_line_fade_out", (unsigned int &)m_ulChatLineFadeOut );
CVARS_GET ( "chat_font", (unsigned int &)Font ); SetChatFont ( (eChatFont)Font );
CVARS_GET ( "chat_nickcompletion", m_bNickCompletion );
}
//
// Draw
//
void CChat::Draw ( bool bUseCacheTexture )
{
// Are we visible?
if ( !m_bVisible )
return;
// Is it time to update all the chat related cvars?
if( m_iCVarsRevision != CClientVariables::GetSingleton ().GetRevision () ) {
m_iCVarsRevision = CClientVariables::GetSingleton ().GetRevision ();
LoadCVars ();
UpdateGUI ();
}
// Get drawList for the chat box text
SDrawList drawList;
GetDrawList ( drawList );
// Calc some size info
CVector2D chatTopLeft ( drawList.renderBounds.fX1, drawList.renderBounds.fY1 );
CVector2D chatBotRight ( drawList.renderBounds.fX2, drawList.renderBounds.fY2 );
CVector2D chatSize = chatBotRight - chatTopLeft;
CRect2D chatBounds ( 0, 0, chatSize.fX, chatSize.fY );
// If we are not using a cache texture, just render the text directly to the screen
if ( !bUseCacheTexture )
{
DrawDrawList ( drawList, chatTopLeft );
return;
}
CGraphics* pGraphics = CGraphics::GetSingletonPtr ();
// Validate rendertarget
if ( m_pCacheTexture && chatSize != m_RenderTargetChatSize )
{
SAFE_RELEASE( m_pCacheTexture );
}
// Create rendertarget if required
if ( !m_pCacheTexture && ( CTickCount::Now () - m_lastRenderTargetCreationFail ).ToLongLong () > 60000 )
{
// Make sure render target size is reasonable
int iRenderTargetSizeX = Clamp < int > ( 8, chatSize.fX, pGraphics->GetViewportWidth () );
int iRenderTargetSizeY = Clamp < int > ( 8, chatSize.fY, pGraphics->GetViewportHeight () );
m_pCacheTexture = pGraphics->GetRenderItemManager ()->CreateRenderTarget ( iRenderTargetSizeX, iRenderTargetSizeY, true, true );
if ( m_pCacheTexture )
m_RenderTargetChatSize = chatSize;
else
{
m_lastRenderTargetCreationFail = CTickCount::Now ();
// Maybe log render target creation fail
if ( m_iReportCount < 5 )
{
m_iReportCount++;
SString strAdapterName = g_pDeviceState->AdapterState.Name;
AddReportLog ( 6532, SString ( "Chat rt chatSize:%2.0f %2.0f rtsize:%d %d card:%s"
,chatSize.fX, chatSize.fY
,iRenderTargetSizeX, iRenderTargetSizeY
,*strAdapterName
) );
}
}
m_iCacheTextureRevision = -1; // Make sure the graphics will be updated
}
// If we can't get a rendertarget for some reason, just render the text directly to the screen
if ( !m_pCacheTexture )
{
DrawDrawList ( drawList, chatTopLeft );
return;
}
// Update the cached graphics if the draw list has changed, or the render target has been recreated
if ( m_PrevDrawList != drawList || m_pCacheTexture->GetRevision () != m_iCacheTextureRevision )
{
m_PrevDrawList = drawList;
m_iCacheTextureRevision = m_pCacheTexture->GetRevision ();
pGraphics->GetRenderItemManager ()->SetRenderTarget ( m_pCacheTexture, true );
// Draw new stuff
pGraphics->SetBlendMode ( EBlendMode::MODULATE_ADD );
DrawDrawList ( drawList );
pGraphics->SetBlendMode ( EBlendMode::BLEND );
pGraphics->GetRenderItemManager ()->RestoreDefaultRenderTarget ();
}
// Draw the cache texture
pGraphics->SetBlendMode ( EBlendMode::ADD );
pGraphics->DrawTexture ( m_pCacheTexture, chatTopLeft.fX, chatTopLeft.fY );
pGraphics->SetBlendMode ( EBlendMode::BLEND );
}
//
// DrawDrawList
//
// Draw the contents of the drawList
//
void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOffset )
{
CGraphics::GetSingleton ().BeginDrawBatch ();
CVector2D chatTopLeft ( drawList.renderBounds.fX1, drawList.renderBounds.fY1 );
CVector2D chatBotRight ( drawList.renderBounds.fX2, drawList.renderBounds.fY2 );
CVector2D chatSize = chatBotRight - chatTopLeft;
CRect2D chatBounds ( 0, 0, chatSize.fX, chatSize.fY );
chatBounds.fX1 += topLeftOffset.fX;
chatBounds.fY1 += topLeftOffset.fY;
chatBounds.fX2 += topLeftOffset.fX;
chatBounds.fY2 += topLeftOffset.fY;
for ( uint i = 0 ; i < drawList.lineItemList.size () ; i++ )
{
const SDrawListLineItem& item = drawList.lineItemList[i];
m_Lines [ item.uiLine ].Draw ( item.vecPosition - chatTopLeft + topLeftOffset, item.ucAlpha, drawList.bShadow, chatBounds );
}
CGraphics::GetSingleton ().EndDrawBatch ();
}
//
// GetDrawList
//
// Get list of text lines to draw
//
void CChat::GetDrawList ( SDrawList& outDrawList )
{
float fLineDifference = CChat::GetFontHeight ( m_vecScale.fY );
CVector2D vecPosition ( m_vecBackgroundPosition.fX + ( 5.0f * m_vecScale.fX ), m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY - ( fLineDifference * 1.25f ) );
unsigned long ulTime = GetTickCount32 ();
float fRcpChatLineFadeOut = 1.0f / m_ulChatLineFadeOut;
bool bShadow = ( m_Color.A * m_fBackgroundAlpha == 0.f );
bool bInputShadow = ( m_InputColor.A * m_fInputBackgroundAlpha == 0.f );
if ( m_Color.A * m_fBackgroundAlpha > 0.f )
{
// Hack to draw the background behind the text.
m_pBackground->SetAlpha ( m_fBackgroundAlpha );
m_pBackground->SetVisible ( true );
m_pBackground->Render ();
m_pBackground->SetVisible ( false );
}
if ( m_bInputVisible )
{
// ChrML: Hack so chatbox input always works. It might get unfocused..
if ( !m_pBackground->IsActive () )
{
m_pBackground->Activate ();
}
}
// Used for render clipping in CChat::DrawTextString
CRect2D RenderBounds ( m_vecBackgroundPosition.fX, m_vecBackgroundPosition.fY + 1, m_vecBackgroundPosition.fX + m_vecBackgroundSize.fX, m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY );
outDrawList.renderBounds = RenderBounds;
outDrawList.bShadow = bShadow;
// Smooth scroll
int iLineScroll;
float fPixelScroll;
UpdateSmoothScroll ( &fPixelScroll, &iLineScroll );
// Apply pixel smooth scroll
vecPosition.fY -= fPixelScroll * fLineDifference;
// Apply line smooth scroll
unsigned int uiLine = (m_uiMostRecentLine + m_uiScrollOffset - iLineScroll) % CHAT_MAX_LINES;
unsigned int uiLinesDrawn = 0;
// Loop over the circular buffer
while ( m_Lines [ uiLine ].IsActive () && uiLinesDrawn < m_uiNumLines + (fabsf( m_fSmoothScroll ) > 0.1f ? 1 : 0) )
{
// Calculate line alpha
long ulLineAge = ulTime - m_Lines [ uiLine ].GetCreationTime ();
float fLineAlpha = (long)( m_ulChatLineLife + m_ulChatLineFadeOut - ulLineAge ) * fRcpChatLineFadeOut;
// Clamp line alpha range and combine with Css override
fLineAlpha = Clamp ( 0.0f, fLineAlpha, 1.0f );
fLineAlpha += ( 1.0f - fLineAlpha ) * m_fCssStyleOverrideAlpha;
if ( fLineAlpha > 0.f )
{
SDrawListLineItem item;
item.uiLine = uiLine;
item.vecPosition = vecPosition;
item.ucAlpha = static_cast < unsigned char >( fLineAlpha * 255.0f );
outDrawList.lineItemList.push_back( item );
}
vecPosition.fY -= fLineDifference;
uiLine = (uiLine + 1) % CHAT_MAX_LINES;
uiLinesDrawn++;
if ( uiLine == m_uiMostRecentLine ) // Went through all lines?
break;
}
if ( m_InputColor.A * m_fInputBackgroundAlpha > 0.f )
{
if ( m_pInput )
{
// Hack to draw the input background behind the text.
m_pInput->SetAlpha ( m_fInputBackgroundAlpha );
m_pInput->SetVisible ( true );
m_pInput->Render ();
m_pInput->SetVisible ( false );
}
}
if ( m_bInputVisible )
{
CVector2D vecPosition ( m_vecInputPosition.fX + ( 5.0f * m_vecScale.fX ), m_vecInputPosition.fY + ( fLineDifference * 0.125f ) );
m_InputLine.Draw ( vecPosition, 255, bInputShadow );
}
}
//
// CChat::UpdateSmoothScroll
//
void CChat::UpdateSmoothScroll ( float* pfPixelScroll, int *piLineScroll )
{
// Calc frame time
float fTimeSeconds = GetSecondCount ();
float fDeltaSeconds = fTimeSeconds - m_fSmoothLastTimeSeconds;
m_fSmoothLastTimeSeconds = fTimeSeconds;
// Handle auto repeat
if ( m_iScrollState == 0 )
{
m_fSmoothRepeatTimer = -100;
}
else
{
if ( m_fSmoothRepeatTimer < 0.0f )
{
if ( m_fSmoothRepeatTimer < -50 )
m_fSmoothRepeatTimer = 0.250f;
else
m_fSmoothRepeatTimer += 1/22.f;
if ( m_iScrollState == 1 )
ScrollUp ();
else if ( m_iScrollState == -1 )
ScrollDown ();
}
m_fSmoothRepeatTimer -= fDeltaSeconds;
}
// Time to reset?
if ( m_fSmoothScrollResetTime && m_fSmoothScrollResetTime < GetSecondCount () )
{
m_fSmoothScrollResetTime = 0;
m_fSmoothScroll -= m_uiScrollOffset;
m_uiScrollOffset -= m_uiScrollOffset;
}
// Calc smooth scroll deceleration
float fFixedChange = ( m_fSmoothScroll < 0 ? -1 : 1 ) * -std::min ( fDeltaSeconds / 0.2f, 1.0f );
float fLerpChange = m_fSmoothScroll * ( 1 - std::min ( fDeltaSeconds / 0.3f, 1.0f ) ) - m_fSmoothScroll;
float fChange = fLerpChange * 0.7f + fFixedChange * 0.4f;
fChange = Clamp ( -fabsf ( m_fSmoothScroll ), fChange, fabsf ( m_fSmoothScroll ) );
// Apply smooth scroll deceleration
m_fSmoothScroll += fChange;
// Break into line and pixels
int iLineScroll = (int)( m_fSmoothScroll );
if ( m_fSmoothScroll > 0.14 )
iLineScroll += 1;
float fPixelScroll = m_fSmoothScroll - iLineScroll;
// Inhibit pixel scroll if frame rate falls to low
if ( fDeltaSeconds > 0.1f )
m_fSmoothAllowAfter = fTimeSeconds + 1.0f;
if ( m_fSmoothAllowAfter > fTimeSeconds )
fPixelScroll = 0;
// Set return values
*pfPixelScroll = fPixelScroll;
*piLineScroll = iLineScroll;
// Update 'scrolling back' flag
if ( m_uiScrollOffset != 0 )
m_iScrollingBack = 10;
else
m_iScrollingBack = std::max ( 0, m_iScrollingBack - 1 );
//
// Also update CssStyle override alpha
//
float fTarget = ( !m_bCssStyleText || m_bInputVisible || m_iScrollingBack ) ? 1.0f : 0.0f;
float fMaxAmount = fDeltaSeconds * 2.0f; // 0.5 seconds fade time
m_fCssStyleOverrideAlpha += Clamp ( -fMaxAmount, fTarget - m_fCssStyleOverrideAlpha, fMaxAmount );
//
// Also update background alpha
//
fTarget = ( !m_bCssStyleBackground || m_bInputVisible || m_iScrollingBack ) ? 1.0f : 0.0f;
fMaxAmount = fDeltaSeconds * 5.0f; // 0.2 seconds fade time
m_fBackgroundAlpha += Clamp ( -fMaxAmount, fTarget - m_fBackgroundAlpha, fMaxAmount );
//
// Also update input background alpha
//
fTarget = ( m_bInputVisible ) ? 1.0f : 0.0f;
fMaxAmount = fDeltaSeconds * 5.0f; // 0.2 seconds fade time
m_fInputBackgroundAlpha += Clamp ( -fMaxAmount, fTarget - m_fInputBackgroundAlpha, fMaxAmount );
}
void CChat::Output ( const char* szText, bool bColorCoded )
{
CChatLine* pLine = NULL;
const char* szRemainingText = szText;
CColor color = m_TextColor;
// Allow smooth scroll when text is added if game FX Quality is not low
CGameSettings * gameSettings = CCore::GetSingleton ( ).GetGame ( )->GetSettings();
if ( gameSettings->GetFXQuality() > 0 )
m_fSmoothScroll -= 1.0f;
do
{
m_uiMostRecentLine = (m_uiMostRecentLine == 0 ? CHAT_MAX_LINES - 1 : m_uiMostRecentLine - 1);
pLine = &m_Lines [ m_uiMostRecentLine ];
szRemainingText = pLine->Format ( szRemainingText,
( m_vecBackgroundSize.fX - ( 10.0f * m_vecScale.fX ) ), color, bColorCoded );
pLine->SetActive ( true );
pLine->UpdateCreationTime ();
}
while ( szRemainingText );
}
void CChat::Clear ( void )
{
for ( int i = 0; i < CHAT_MAX_LINES; i++ )
{
m_Lines [ i ].SetActive ( false );
}
m_uiMostRecentLine = 0;
m_fSmoothScroll = 0;
}
void CChat::ClearInput ( void )
{
m_strInputText.clear ();
m_InputLine.Clear ();
m_vecInputSize = CalcInputSize ();
if ( m_pInput )
m_pInput->SetSize ( m_vecInputSize );
}
void CChat::ScrollUp ()
{
if ( m_Lines [ (m_uiMostRecentLine + m_uiScrollOffset + m_uiNumLines) % CHAT_MAX_LINES ].IsActive ()
&& !(
((m_uiMostRecentLine + m_uiScrollOffset) % CHAT_MAX_LINES < m_uiMostRecentLine) &&
((m_uiMostRecentLine + m_uiScrollOffset + m_uiNumLines) % CHAT_MAX_LINES >= m_uiMostRecentLine)
)
)
{
m_uiScrollOffset += 1;
m_fSmoothScroll += 1;
m_fSmoothScrollResetTime = GetSecondCount () + 5;
}
}
void CChat::ScrollDown ()
{
if ( m_uiScrollOffset <= 0 )
{
m_fSmoothScroll -= m_uiScrollOffset;
m_uiScrollOffset -= m_uiScrollOffset;
}
else
{
m_uiScrollOffset -= 1;
m_fSmoothScroll -= 1;
m_fSmoothScrollResetTime = GetSecondCount () + 5;
}
}
bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
{
// If we can take input
if ( !CLocalGUI::GetSingleton ().GetConsole ()->IsVisible () &&
IsInputVisible() )
{
// Check if it's a special key like enter and backspace, if not, add it as a character to the message
switch ( KeyboardArgs.codepoint )
{
case VK_BACK:
{
if ( m_strInputText.size () > 0 )
{
// Convert our string to UTF8 before resizing, then back to ANSI.
std::wstring strText = MbUTF8ToUTF16(m_strInputText);
strText.resize ( strText.size () - 1 );
SetInputText ( UTF16ToMbUTF8(strText).c_str() );
}
break;
}
case VK_RETURN:
{
// Empty the chat and hide the input stuff
// If theres a command to call, call it
if ( !m_strCommand.empty () && !m_strInputText.empty () )
CCommands::GetSingleton().Execute ( m_strCommand.c_str (), m_strInputText.c_str () );
SetInputVisible ( false );
m_fSmoothScrollResetTime = GetSecondCount ();
break;
}
case VK_TAB:
{
if ( m_bNickCompletion && m_strInputText.size () > 0 )
{
bool bSuccess = false;
SString strCurrentInput = GetInputText ();
size_t iFound;
iFound = strCurrentInput.find_last_of ( " " );
if ( iFound == std::string::npos )
iFound = 0;
else
++iFound;
SString strPlayerNamePart = strCurrentInput.substr ( iFound );
CModManager* pModManager = CModManager::GetSingletonPtr ();
if ( pModManager && pModManager->GetCurrentMod () )
{
//Create vector and get playernames from deathmatch module
std::vector<SString> vPlayerNames;
pModManager->GetCurrentMod ()->GetPlayerNames ( vPlayerNames );
for ( std::vector<SString>::iterator iter = vPlayerNames.begin ();
iter != vPlayerNames.end ();
++iter )
{
SString strPlayerName = *iter;
// Check if there is another player after our last result
if ( m_strLastPlayerName.size () != 0 )
{
if ( strPlayerName.CompareI ( m_strLastPlayerName ) ) {
m_strLastPlayerName.clear ();
if ( *iter == vPlayerNames.back () )
{
CharacterKeyHandler ( KeyboardArgs );
return true;
}
}
continue;
}
// Already a part?
if ( m_strLastPlayerNamePart.size () != 0 )
{
strPlayerNamePart = m_strLastPlayerNamePart;
}
// Check namepart
if ( !strPlayerName.BeginsWith ( strPlayerNamePart ) )
continue;
else
{
//Check size if it's ok, then output
SString strOutput = strCurrentInput.replace ( iFound, std::string::npos, strPlayerName );
if ( MbUTF8ToUTF16 ( strOutput ).size () < CHAT_MAX_CHAT_LENGTH )
{
bSuccess = true;
m_strLastPlayerNamePart = strPlayerNamePart;
m_strLastPlayerName = strPlayerName;
SetInputText ( strOutput );
}
break;
}
}
// No success? Try again!
if ( !bSuccess )
m_strLastPlayerName.clear ();
}
}
break;
}
default:
{
// Clear last namepart when pressing letter
if (m_strLastPlayerNamePart.size() != 0)
m_strLastPlayerNamePart.clear();
// Clear last name when pressing letter
if (m_strLastPlayerName.size() != 0)
m_strLastPlayerName.clear();
// If we haven't exceeded the maximum number of characters per chat message, append the char to the message and update the input control
if ( MbUTF8ToUTF16(m_strInputText).size () < CHAT_MAX_CHAT_LENGTH )
{
if ( KeyboardArgs.codepoint >= 32 )
{
unsigned int uiCharacter = KeyboardArgs.codepoint;
if ( uiCharacter < 127 ) // we have any char from ASCII
{
// injecting as is
m_strInputText += static_cast < char > ( KeyboardArgs.codepoint );
SetInputText ( m_strInputText.c_str () );
}
else // we have any char from Extended ASCII, any ANSI code page or UNICODE range
{
// Generate a null-terminating string for our character
wchar_t wUNICODE[2] = { uiCharacter, '\0' };
// Convert our UTF character into an ANSI string
std::string strANSI = UTF16ToMbUTF8(wUNICODE);
// Append the ANSI string, and update
m_strInputText.append(strANSI);
SetInputText ( m_strInputText.c_str () );
}
}
}
break;
}
}
}
return true;
}
void CChat::SetVisible ( bool bVisible )
{
m_bVisible = bVisible;
// If hiding chat, also reset chat input line
if ( !m_bVisible )
SetInputVisible( false );
}
void CChat::SetInputVisible ( bool bVisible )
{
if ( !IsVisible () )
bVisible = false;
if ( bVisible )
{
ClearInput ();
}
m_bInputVisible = bVisible;
}
void CChat::SetNumLines ( unsigned int uiNumLines )
{
if ( uiNumLines <= CHAT_MAX_LINES )
{
m_uiNumLines = uiNumLines;
UpdateGUI ();
}
}
void CChat::SetChatFont ( eChatFont Font )
{
CGUIFont* pFont = g_pCore->GetGUI ()->GetDefaultFont ();
ID3DXFont* pDXFont = g_pCore->GetGraphics ()->GetFont ();
float fUsingDxFontScale = 1;
float fReqestedDxFontScale = std::max( m_vecScale.fX, m_vecScale.fY );
switch ( Font )
{
case ChatFonts::CHAT_FONT_DEFAULT:
pFont = g_pCore->GetGUI ()->GetDefaultFont ();
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_DEFAULT, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
break;
case ChatFonts::CHAT_FONT_CLEAR:
pFont = g_pCore->GetGUI ()->GetClearFont ();
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_CLEAR, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
break;
case ChatFonts::CHAT_FONT_BOLD:
pFont = g_pCore->GetGUI ()->GetBoldFont ();
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_DEFAULT_BOLD, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
break;
case ChatFonts::CHAT_FONT_ARIAL:
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_ARIAL, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
break;
}
m_fRcpUsingDxFontScale = 1 / fUsingDxFontScale;
SAFE_RELEASE( m_pCacheTexture );
// Set fonts
m_pFont = pFont;
SetDxFont( pDXFont );
}
void CChat::SetDxFont ( LPD3DXFONT pDXFont )
{
if ( m_pDXFont != pDXFont )
{
if ( m_pDXFont )
m_pDXFont->Release();
m_pDXFont = pDXFont;
if ( m_pDXFont )
m_pDXFont->AddRef();
}
}
void CChat::UpdateGUI ( void )
{
m_vecBackgroundSize = CVector2D (
m_fNativeWidth * m_vecScale.fX,
CChat::GetFontHeight ( m_vecScale.fY ) * (float(m_uiNumLines) + 0.5f)
);
m_vecBackgroundSize.fX = Round ( m_vecBackgroundSize.fX );
m_vecBackgroundSize.fY = Round ( m_vecBackgroundSize.fY );
m_vecBackgroundPosition.fX = Round ( m_vecBackgroundPosition.fX );
m_vecBackgroundPosition.fY = Round ( m_vecBackgroundPosition.fY );
m_pBackground->SetSize ( m_vecBackgroundSize );
m_vecInputPosition = CVector2D (
m_vecBackgroundPosition.fX,
m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY
);
// Make sure there is enough room for all the lines
uint uiMaxNumLines = g_pCore->GetGraphics ()->GetViewportHeight () / std::max ( 1.f, CChat::GetFontHeight ( m_vecScale.fY ) ) - 3;
if ( m_uiNumLines > uiMaxNumLines )
SetNumLines ( uiMaxNumLines );
m_vecInputSize = CalcInputSize ();
if ( m_pInput )
{
m_pInput->SetPosition ( m_vecInputPosition );
m_pInput->SetSize ( m_vecInputSize );
}
}
void CChat::SetColor ( const CColor& Color )
{
unsigned long ulBackgroundColor = COLOR_ARGB ( Color.A, Color.R, Color.G, Color.B );
m_pBackgroundTexture->LoadFromMemory ( &ulBackgroundColor, 1, 1 );
m_pBackground->LoadFromTexture ( m_pBackgroundTexture );
}
void CChat::SetInputColor ( const CColor& Color )
{
unsigned long ulInputColor = COLOR_ARGB ( Color.A, Color.R, Color.G, Color.B );
if ( m_pInputTexture )
m_pInputTexture->LoadFromMemory ( &ulInputColor, 1, 1 );
if ( m_pInput )
m_pInput->LoadFromTexture ( m_pInputTexture );
}
const char* CChat::GetInputPrefix ( void )
{
return m_InputLine.m_Prefix.GetText ();
}
void CChat::SetInputPrefix ( const char* szPrefix )
{
m_InputLine.m_Prefix.SetText ( szPrefix );
}
CVector2D CChat::CalcInputSize ( void )
{
return CVector2D ( m_vecBackgroundSize.fX, ( GetFontHeight ( m_vecScale.fY ) * ( ( float ) m_InputLine.m_ExtraLines.size () + 1.25f ) ) );
}
void CChat::SetInputText ( const char* szText )
{
m_InputLine.Clear ();
CColor color = m_InputTextColor;
const char* szRemainingText = m_InputLine.Format ( szText,
( m_vecInputSize.fX - ( 10.0f * m_vecScale.fX ) - m_InputLine.m_Prefix.GetWidth () ),
color, false );
CChatLine* pLine = NULL;
while ( szRemainingText && m_InputLine.m_ExtraLines.size () < 3 )
{
m_InputLine.m_ExtraLines.resize ( m_InputLine.m_ExtraLines.size () + 1 );
CChatLine& line = *(m_InputLine.m_ExtraLines.end () - 1);
szRemainingText = line.Format ( szRemainingText,
( m_vecInputSize.fX - ( 10.0f * m_vecScale.fX ) ), color, false );
}
if ( szText != m_strInputText.c_str () )
m_strInputText = szText;
if ( szRemainingText )
m_strInputText.resize ( szRemainingText - szText );
m_vecInputSize = CalcInputSize ();
if ( m_pInput )
m_pInput->SetSize ( m_vecInputSize );
}
void CChat::SetCommand ( const char* szCommand )
{
if ( !szCommand )
return;
m_strCommand = szCommand;
if ( strcmp ( szCommand, "chatboxsay" ) == 0 )
{
SetInputPrefix ( "Say: " );
}
else
{
std::string strPrefix = m_strCommand + ": ";
strPrefix[0] = toupper ( strPrefix[0] );
SetInputPrefix ( strPrefix.c_str () );
}
}
float CChat::GetFontHeight ( float fScale )
{
if ( !g_pChat )
return 0.0f;
if ( g_pChat->m_bUseCEGUI )
{
return g_pChat->m_pFont->GetFontHeight ( fScale );
}
fScale *= g_pChat->m_fRcpUsingDxFontScale;
return g_pCore->GetGraphics ()->GetDXFontHeight ( fScale, g_pChat->m_pDXFont );
}
float CChat::GetTextExtent ( const char * szText, float fScale )
{
if ( !g_pChat )
return 0.0f;
if ( g_pChat->m_bUseCEGUI )
{
return g_pChat->m_pFont->GetTextExtent ( szText, fScale );
}
fScale *= g_pChat->m_fRcpUsingDxFontScale;
return g_pCore->GetGraphics ()->GetDXTextExtent ( szText, fScale, g_pChat->m_pDXFont );
}
void CChat::DrawTextString ( const char * szText, CRect2D DrawArea, float fZ, CRect2D ClipRect, unsigned long ulFormat, unsigned long ulColor, float fScaleX, float fScaleY, const CRect2D& RenderBounds )
{
if ( !g_pChat )
return;
if ( g_pChat->m_bUseCEGUI )
{
g_pChat->m_pFont->DrawTextString ( szText, DrawArea, fZ, ClipRect, ulFormat, ulColor, fScaleX, fScaleY );
}
else
{
float fLineHeight = CChat::GetFontHeight ( g_pChat->m_vecScale.fY );
fScaleX *= g_pChat->m_fRcpUsingDxFontScale;
fScaleY *= g_pChat->m_fRcpUsingDxFontScale;
if ( DrawArea.fY1 < RenderBounds.fY1 )
{
// Clip text at the top
if ( DrawArea.fY1 + fLineHeight - RenderBounds.fY1 > 1 )
g_pCore->GetGraphics ()->DrawString ( ( int ) DrawArea.fX1, ( int ) RenderBounds.fY1, ( int ) DrawArea.fX2, ( int ) DrawArea.fY1 + fLineHeight, ulColor, szText, fScaleX, fScaleY, DT_LEFT | DT_BOTTOM | DT_SINGLELINE , g_pChat->m_pDXFont );
}
else
if ( DrawArea.fY1 + fLineHeight > RenderBounds.fY2 )
{
// Clip text at the bottom
if ( RenderBounds.fY2 - DrawArea.fY1 > 1 )
g_pCore->GetGraphics ()->DrawString ( ( int ) DrawArea.fX1, ( int ) DrawArea.fY1, ( int ) DrawArea.fX2, ( int ) RenderBounds.fY2, ulColor, szText, fScaleX, fScaleY, DT_LEFT | DT_TOP | DT_SINGLELINE , g_pChat->m_pDXFont );
}
else
{
// Text not clipped
g_pCore->GetGraphics ()->DrawString ( ( int ) DrawArea.fX1, ( int ) DrawArea.fY1, ( int ) DrawArea.fX1, ( int ) DrawArea.fY1, ulColor, szText, fScaleX, fScaleY, DT_LEFT | DT_TOP | DT_NOCLIP, g_pChat->m_pDXFont );
}
}
}
CChatLine::CChatLine ( void )
{
m_bActive = false;
UpdateCreationTime ();
}
void CChatLine::UpdateCreationTime ()
{
m_ulCreationTime = GetTickCount32 ();
}
//
// Calculate the equivalent ansi string pointer of szPosition.
//
const char* CalcAnsiPtr ( const char* szStringAnsi, const wchar_t* szPosition )
{
int iOrigSize = strlen ( szStringAnsi );
int iEndSize = UTF16ToMbUTF8 ( szPosition ).length();
int iOffset = iOrigSize - iEndSize;
return szStringAnsi + iOffset;
}
const char* CChatLine::Format ( const char* szStringAnsi, float fWidth, CColor& color, bool bColorCoded )
{
std::wstring wString = MbUTF8ToUTF16 ( szStringAnsi );
const wchar_t* szString = wString.c_str ();
float fPrevSectionsWidth = 0.0f;
m_Sections.clear ();
const wchar_t* szSectionStart = szString;
const wchar_t* szSectionEnd = szString;
const wchar_t* szLastWrapPoint = szString;
bool bLastSection = false;
while ( !bLastSection ) // iterate over sections
{
m_Sections.resize ( m_Sections.size () + 1 );
CChatLineSection& section = *(m_Sections.end () - 1);
section.SetColor ( color );
if ( m_Sections.size () > 1 && bColorCoded) // If we've processed sections before
szSectionEnd += 7; // skip the color code
szSectionStart = szSectionEnd;
szLastWrapPoint = szSectionStart;
unsigned int uiSeekPos = 0;
std::wstring strSectionStart = szSectionStart;
while ( true ) // find end of this section
{
float fSectionWidth = CChat::GetTextExtent ( UTF16ToMbUTF8 ( strSectionStart.substr ( 0 , uiSeekPos ) ).c_str (), g_pChat->m_vecScale.fX );
if ( *szSectionEnd == '\0' || *szSectionEnd == '\n' || fPrevSectionsWidth + fSectionWidth > fWidth )
{