@@ -21,9 +21,9 @@ extern CCore* g_pCore;
21
21
CChat * g_pChat = NULL ;
22
22
23
23
CChat::CChat ( CGUI* pManager, const CVector2D & vecPosition )
24
- {
24
+ {
25
25
g_pChat = this ;
26
- m_pManager = pManager;
26
+ m_pManager = pManager;
27
27
28
28
// Calculate relative position (assuming a 800x600 native resolution for our defined CCHAT_* values)
29
29
CVector2D vecResolution = m_pManager->GetResolution ();
@@ -57,6 +57,11 @@ CChat::CChat ( CGUI* pManager, const CVector2D & vecPosition )
57
57
m_pCacheTexture = NULL ;
58
58
m_iCacheTextureRevision = 0 ;
59
59
m_iReportCount = 0 ;
60
+ m_fPositionOffsetX = 0 .0125f ;
61
+ m_fPositionOffsetY = 0 .0150f ;
62
+ m_ePositionHorizontal = Chat::Position::Horizontal::LEFT;
63
+ m_ePositionVertical = Chat::Position::Vertical::TOP;
64
+ m_eTextAlign = Chat::Text::Align::LEFT;
60
65
61
66
// Background area
62
67
m_pBackground = m_pManager->CreateStaticImage ();
@@ -126,6 +131,11 @@ void CChat::LoadCVars ( void )
126
131
CVARS_GET ( " chat_line_fade_out" , (unsigned int &)m_ulChatLineFadeOut );
127
132
CVARS_GET ( " chat_font" , (unsigned int &)Font ); SetChatFont ( (eChatFont)Font );
128
133
CVARS_GET ( " chat_nickcompletion" , m_bNickCompletion );
134
+ CVARS_GET ( " chat_position_offset_x" , m_fPositionOffsetX );
135
+ CVARS_GET ( " chat_position_offset_y" , m_fPositionOffsetY );
136
+ CVARS_GET ( " chat_position_horizontal" , (unsigned int &)m_ePositionHorizontal );
137
+ CVARS_GET ( " chat_position_vertical" , (unsigned int &)m_ePositionVertical );
138
+ CVARS_GET ( " chat_text_alignment" , (unsigned int &)m_eTextAlign );
129
139
}
130
140
131
141
@@ -139,7 +149,8 @@ void CChat::Draw ( bool bUseCacheTexture )
139
149
return ;
140
150
141
151
// Is it time to update all the chat related cvars?
142
- if ( m_iCVarsRevision != CClientVariables::GetSingleton ().GetRevision () ) {
152
+ if ( m_iCVarsRevision != CClientVariables::GetSingleton ().GetRevision () )
153
+ {
143
154
m_iCVarsRevision = CClientVariables::GetSingleton ().GetRevision ();
144
155
LoadCVars ();
145
156
UpdateGUI ();
@@ -169,6 +180,7 @@ void CChat::Draw ( bool bUseCacheTexture )
169
180
{
170
181
SAFE_RELEASE ( m_pCacheTexture );
171
182
}
183
+
172
184
// Create rendertarget if required
173
185
if ( !m_pCacheTexture && ( CTickCount::Now () - m_lastRenderTargetCreationFail ).ToLongLong () > 60000 )
174
186
{
@@ -234,8 +246,6 @@ void CChat::Draw ( bool bUseCacheTexture )
234
246
//
235
247
void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOffset )
236
248
{
237
- CGraphics::GetSingleton ().BeginDrawBatch ();
238
-
239
249
CVector2D chatTopLeft ( drawList.renderBounds .fX1 , drawList.renderBounds .fY1 );
240
250
CVector2D chatBotRight ( drawList.renderBounds .fX2 , drawList.renderBounds .fY2 );
241
251
CVector2D chatSize = chatBotRight - chatTopLeft;
@@ -246,9 +256,10 @@ void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOf
246
256
chatBounds.fX2 += topLeftOffset.fX ;
247
257
chatBounds.fY2 += topLeftOffset.fY ;
248
258
249
- for ( uint i = 0 ; i < drawList.lineItemList .size () ; i++ )
259
+ CGraphics::GetSingleton ().BeginDrawBatch ();
260
+
261
+ for ( const auto & item : drawList.lineItemList )
250
262
{
251
- const SDrawListLineItem& item = drawList.lineItemList [i];
252
263
m_Lines [ item.uiLine ].Draw ( item.vecPosition - chatTopLeft + topLeftOffset, item.ucAlpha , drawList.bShadow , drawList.bOutline , chatBounds );
253
264
}
254
265
@@ -264,6 +275,7 @@ void CChat::GetDrawList ( SDrawList& outDrawList )
264
275
{
265
276
float fLineDifference = CChat::GetFontHeight ( m_vecScale.fY );
266
277
CVector2D vecPosition ( m_vecBackgroundPosition.fX + ( 5 .0f * m_vecScale.fX ), m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY - ( fLineDifference * 1 .25f ) );
278
+ float fMaxLineWidth = m_vecBackgroundSize.fX - ( 10 .0f * m_vecScale.fX );
267
279
unsigned long ulTime = GetTickCount32 ();
268
280
float fRcpChatLineFadeOut = 1 .0f / m_ulChatLineFadeOut;
269
281
bool bShadow = ( m_Color.A * m_fBackgroundAlpha == 0 .f ) && !m_bTextBlackOutline;
@@ -318,9 +330,16 @@ void CChat::GetDrawList ( SDrawList& outDrawList )
318
330
319
331
if ( fLineAlpha > 0 .f )
320
332
{
333
+ CVector2D vecOffset;
334
+
335
+ if ( m_eTextAlign == Chat::Text::Align::RIGHT )
336
+ {
337
+ vecOffset.fX = fMaxLineWidth - m_Lines[ uiLine ].GetWidth ();
338
+ }
339
+
321
340
SDrawListLineItem item;
322
341
item.uiLine = uiLine;
323
- item.vecPosition = vecPosition;
342
+ item.vecPosition = vecPosition + vecOffset ;
324
343
item.ucAlpha = static_cast < unsigned char >( fLineAlpha * 255 .0f );
325
344
outDrawList.lineItemList .push_back ( item );
326
345
}
@@ -488,8 +507,12 @@ void CChat::ClearInput ( void )
488
507
m_strInputText.clear ();
489
508
m_InputLine.Clear ();
490
509
m_vecInputSize = CalcInputSize ();
510
+
491
511
if ( m_pInput )
512
+ {
492
513
m_pInput->SetSize ( m_vecInputSize );
514
+ UpdatePosition ();
515
+ }
493
516
}
494
517
495
518
void CChat::ScrollUp ()
@@ -549,7 +572,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
549
572
// If theres a command to call, call it
550
573
if ( !m_strCommand.empty () && !m_strInputText.empty () )
551
574
CCommands::GetSingleton ().Execute ( m_strCommand.c_str (), m_strInputText.c_str () );
552
-
575
+
553
576
SetInputVisible ( false );
554
577
555
578
m_fSmoothScrollResetTime = GetSecondCount ();
@@ -569,7 +592,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
569
592
iFound = 0 ;
570
593
else
571
594
++iFound;
572
-
595
+
573
596
SString strPlayerNamePart = strCurrentInput.substr ( iFound );
574
597
575
598
CModManager* pModManager = CModManager::GetSingletonPtr ();
@@ -588,7 +611,8 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
588
611
// Check if there is another player after our last result
589
612
if ( m_strLastPlayerName.size () != 0 )
590
613
{
591
- if ( strPlayerName.CompareI ( m_strLastPlayerName ) ) {
614
+ if ( strPlayerName.CompareI ( m_strLastPlayerName ) )
615
+ {
592
616
m_strLastPlayerName.clear ();
593
617
if ( *iter == vPlayerNames.back () )
594
618
{
@@ -632,7 +656,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
632
656
}
633
657
break ;
634
658
}
635
-
659
+
636
660
default :
637
661
{
638
662
// Clear last namepart when pressing letter
@@ -645,11 +669,11 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
645
669
646
670
// If we haven't exceeded the maximum number of characters per chat message, append the char to the message and update the input control
647
671
if ( MbUTF8ToUTF16 (m_strInputText).size () < CHAT_MAX_CHAT_LENGTH )
648
- {
672
+ {
649
673
if ( KeyboardArgs.codepoint >= 32 )
650
674
{
651
675
unsigned int uiCharacter = KeyboardArgs.codepoint ;
652
- if ( uiCharacter < 127 ) // we have any char from ASCII
676
+ if ( uiCharacter < 127 ) // we have any char from ASCII
653
677
{
654
678
// injecting as is
655
679
m_strInputText += static_cast < char > ( KeyboardArgs.codepoint );
@@ -688,11 +712,11 @@ void CChat::SetVisible ( bool bVisible )
688
712
689
713
690
714
void CChat::SetInputVisible ( bool bVisible )
691
- {
715
+ {
692
716
if ( !IsVisible () )
693
717
bVisible = false ;
694
718
695
- if ( bVisible )
719
+ if ( ! bVisible )
696
720
{
697
721
ClearInput ();
698
722
}
@@ -719,21 +743,21 @@ void CChat::SetChatFont ( eChatFont Font )
719
743
float fReqestedDxFontScale = std::max ( m_vecScale.fX , m_vecScale.fY );
720
744
switch ( Font )
721
745
{
722
- case ChatFonts::CHAT_FONT_DEFAULT :
746
+ case Chat::Font::DEFAULT :
723
747
pFont = g_pCore->GetGUI ()->GetDefaultFont ();
724
748
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_DEFAULT, &fUsingDxFontScale , fReqestedDxFontScale , " chat" );
725
749
break ;
726
- case ChatFonts::CHAT_FONT_CLEAR :
750
+ case Chat::Font::CLEAR :
727
751
pFont = g_pCore->GetGUI ()->GetClearFont ();
728
752
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_CLEAR, &fUsingDxFontScale , fReqestedDxFontScale , " chat" );
729
753
break ;
730
- case ChatFonts::CHAT_FONT_BOLD :
754
+ case Chat::Font::BOLD :
731
755
pFont = g_pCore->GetGUI ()->GetBoldFont ();
732
756
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_DEFAULT_BOLD, &fUsingDxFontScale , fReqestedDxFontScale , " chat" );
733
757
break ;
734
- case ChatFonts::CHAT_FONT_ARIAL :
758
+ case Chat::Font::ARIAL :
735
759
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_ARIAL, &fUsingDxFontScale , fReqestedDxFontScale , " chat" );
736
- break ;
760
+ break ;
737
761
}
738
762
739
763
m_fRcpUsingDxFontScale = 1 / fUsingDxFontScale ;
@@ -772,11 +796,6 @@ void CChat::UpdateGUI ( void )
772
796
m_vecBackgroundPosition.fY = Round ( m_vecBackgroundPosition.fY );
773
797
m_pBackground->SetSize ( m_vecBackgroundSize );
774
798
775
- m_vecInputPosition = CVector2D (
776
- m_vecBackgroundPosition.fX ,
777
- m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY
778
- );
779
-
780
799
// Make sure there is enough room for all the lines
781
800
uint uiMaxNumLines = g_pCore->GetGraphics ()->GetViewportHeight () / std::max ( 1 .f , CChat::GetFontHeight ( m_vecScale.fY ) ) - 3 ;
782
801
if ( m_uiNumLines > uiMaxNumLines )
@@ -785,9 +804,61 @@ void CChat::UpdateGUI ( void )
785
804
m_vecInputSize = CalcInputSize ();
786
805
if ( m_pInput )
787
806
{
788
- m_pInput->SetPosition ( m_vecInputPosition );
789
807
m_pInput->SetSize ( m_vecInputSize );
790
808
}
809
+
810
+ UpdatePosition ();
811
+ }
812
+
813
+
814
+ void CChat::UpdatePosition ( void )
815
+ {
816
+ CVector2D vecResolution = m_pManager->GetResolution ();
817
+
818
+ float fRelativeWidth = ( m_vecBackgroundSize.fX / vecResolution.fX ),
819
+ fRelativeHeight = ( ( m_vecBackgroundSize.fY + m_vecInputSize.fY ) / vecResolution.fY ),
820
+ fPosX , fPosY ;
821
+
822
+ switch ( m_ePositionHorizontal )
823
+ {
824
+ case Chat::Position::Horizontal::RIGHT:
825
+ fPosX = 1.0 - fRelativeWidth + m_fPositionOffsetX;
826
+ break ;
827
+ case Chat::Position::Horizontal::CENTER:
828
+ fPosX = (1.0 - fRelativeWidth ) / 2 + m_fPositionOffsetX;
829
+ break ;
830
+ case Chat::Position::Horizontal::LEFT:
831
+ default :
832
+ fPosX = m_fPositionOffsetX;
833
+ break ;
834
+ }
835
+
836
+ switch ( m_ePositionVertical )
837
+ {
838
+ case Chat::Position::Vertical::BOTTOM:
839
+ fPosY = 1.0 - fRelativeHeight + m_fPositionOffsetY;
840
+ break ;
841
+ case Chat::Position::Vertical::CENTER:
842
+ fPosY = (1.0 - fRelativeHeight ) / 2 + m_fPositionOffsetY;
843
+ break ;
844
+ case Chat::Position::Vertical::TOP:
845
+ default :
846
+ fPosY = m_fPositionOffsetY;
847
+ break ;
848
+ }
849
+
850
+ m_vecBackgroundPosition = CVector2D ( fPosX , fPosY ) * vecResolution;
851
+ m_vecInputPosition = CVector2D (
852
+ m_vecBackgroundPosition.fX ,
853
+ m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY
854
+ );
855
+
856
+ m_pBackground->SetPosition ( m_vecBackgroundPosition );
857
+
858
+ if ( m_pInput )
859
+ {
860
+ m_pInput->SetPosition ( m_vecInputPosition );
861
+ }
791
862
}
792
863
793
864
@@ -831,14 +902,14 @@ CVector2D CChat::CalcInputSize ( void )
831
902
void CChat::SetInputText ( const char * szText )
832
903
{
833
904
m_InputLine.Clear ();
834
-
905
+
835
906
CColor color = m_InputTextColor;
836
907
const char * szRemainingText = m_InputLine.Format ( szText,
837
908
( m_vecInputSize.fX - ( 10 .0f * m_vecScale.fX ) - m_InputLine.m_Prefix .GetWidth () ),
838
909
color, false );
839
910
840
911
CChatLine* pLine = NULL ;
841
-
912
+
842
913
while ( szRemainingText && m_InputLine.m_ExtraLines .size () < 3 )
843
914
{
844
915
m_InputLine.m_ExtraLines .resize ( m_InputLine.m_ExtraLines .size () + 1 );
@@ -854,8 +925,12 @@ void CChat::SetInputText ( const char* szText )
854
925
m_strInputText.resize ( szRemainingText - szText );
855
926
856
927
m_vecInputSize = CalcInputSize ();
928
+
857
929
if ( m_pInput )
930
+ {
858
931
m_pInput->SetSize ( m_vecInputSize );
932
+ UpdatePosition ();
933
+ }
859
934
}
860
935
861
936
@@ -888,7 +963,9 @@ float CChat::GetFontHeight ( float fScale )
888
963
{
889
964
return g_pChat->m_pFont ->GetFontHeight ( fScale );
890
965
}
966
+
891
967
fScale *= g_pChat->m_fRcpUsingDxFontScale ;
968
+
892
969
return g_pCore->GetGraphics ()->GetDXFontHeight ( fScale , g_pChat->m_pDXFont );
893
970
}
894
971
@@ -902,7 +979,9 @@ float CChat::GetTextExtent ( const char * szText, float fScale )
902
979
{
903
980
return g_pChat->m_pFont ->GetTextExtent ( szText, fScale );
904
981
}
982
+
905
983
fScale *= g_pChat->m_fRcpUsingDxFontScale ;
984
+
906
985
return g_pCore->GetGraphics ()->GetDXTextExtent ( szText, fScale , g_pChat->m_pDXFont );
907
986
}
908
987
@@ -928,8 +1007,7 @@ void CChat::DrawTextString ( const char * szText, CRect2D DrawArea, float fZ, CR
928
1007
if ( DrawArea.fY1 + fLineHeight - RenderBounds.fY1 > 1 )
929
1008
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 , bOutline );
930
1009
}
931
- else
932
- if ( DrawArea.fY1 + fLineHeight > RenderBounds.fY2 )
1010
+ else if ( DrawArea.fY1 + fLineHeight > RenderBounds.fY2 )
933
1011
{
934
1012
// Clip text at the bottom
935
1013
if ( RenderBounds.fY2 - DrawArea.fY1 > 1 )
@@ -998,7 +1076,7 @@ const char* CChatLine::Format ( const char* szStringAnsi, float fWidth, CColor&
998
1076
{
999
1077
float fSectionWidth = CChat::GetTextExtent ( UTF16ToMbUTF8 ( strSectionStart.substr ( 0 , uiSeekPos ) ).c_str (), g_pChat->m_vecScale .fX );
1000
1078
1001
- if ( *szSectionEnd == ' \0 ' || *szSectionEnd == ' \n ' || fPrevSectionsWidth + fSectionWidth > fWidth )
1079
+ if ( *szSectionEnd == ' \0 ' || *szSectionEnd == ' \n ' || std::ceil ( fPrevSectionsWidth + fSectionWidth ) > fWidth )
1002
1080
{
1003
1081
bLastSection = true ;
1004
1082
break ;
@@ -1155,7 +1233,7 @@ float CChatLineSection::GetWidth ()
1155
1233
{
1156
1234
if ( m_fCachedWidth < 0 .0f || m_strText.size () != m_uiCachedLength )
1157
1235
{
1158
- m_fCachedWidth = CChat::GetTextExtent ( m_strText.c_str (), g_pChat->m_vecScale .fX ) / std::max ( 0 .01f , g_pChat->m_vecScale .fX );
1236
+ m_fCachedWidth = std::ceil ( CChat::GetTextExtent ( m_strText.c_str (), g_pChat->m_vecScale .fX ) / std::max ( 0 .01f , g_pChat->m_vecScale .fX ) );
1159
1237
m_uiCachedLength = m_strText.size ();
1160
1238
}
1161
1239
return m_fCachedWidth * g_pChat->m_vecScale .fX ;
0 commit comments