Skip to content

Commit c12aea3

Browse files
author
Marek Kulik
authored
Merge pull request #130 from Necktrox/feature/chatbox-position
Add chatbox position settings
2 parents fa50dcf + 0e7cd50 commit c12aea3

File tree

19 files changed

+1042
-542
lines changed

19 files changed

+1042
-542
lines changed

Client/core/CChat.cpp

Lines changed: 111 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ extern CCore* g_pCore;
2121
CChat * g_pChat = NULL;
2222

2323
CChat::CChat ( CGUI* pManager, const CVector2D & vecPosition )
24-
{
24+
{
2525
g_pChat = this;
26-
m_pManager = pManager;
26+
m_pManager = pManager;
2727

2828
// Calculate relative position (assuming a 800x600 native resolution for our defined CCHAT_* values)
2929
CVector2D vecResolution = m_pManager->GetResolution ();
@@ -57,6 +57,11 @@ CChat::CChat ( CGUI* pManager, const CVector2D & vecPosition )
5757
m_pCacheTexture = NULL;
5858
m_iCacheTextureRevision = 0;
5959
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;
6065

6166
// Background area
6267
m_pBackground = m_pManager->CreateStaticImage ();
@@ -126,6 +131,11 @@ void CChat::LoadCVars ( void )
126131
CVARS_GET ( "chat_line_fade_out", (unsigned int &)m_ulChatLineFadeOut );
127132
CVARS_GET ( "chat_font", (unsigned int &)Font ); SetChatFont ( (eChatFont)Font );
128133
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 );
129139
}
130140

131141

@@ -139,7 +149,8 @@ void CChat::Draw ( bool bUseCacheTexture )
139149
return;
140150

141151
// 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+
{
143154
m_iCVarsRevision = CClientVariables::GetSingleton ().GetRevision ();
144155
LoadCVars ();
145156
UpdateGUI ();
@@ -169,6 +180,7 @@ void CChat::Draw ( bool bUseCacheTexture )
169180
{
170181
SAFE_RELEASE( m_pCacheTexture );
171182
}
183+
172184
// Create rendertarget if required
173185
if ( !m_pCacheTexture && ( CTickCount::Now () - m_lastRenderTargetCreationFail ).ToLongLong () > 60000 )
174186
{
@@ -234,8 +246,6 @@ void CChat::Draw ( bool bUseCacheTexture )
234246
//
235247
void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOffset )
236248
{
237-
CGraphics::GetSingleton ().BeginDrawBatch ();
238-
239249
CVector2D chatTopLeft ( drawList.renderBounds.fX1, drawList.renderBounds.fY1 );
240250
CVector2D chatBotRight ( drawList.renderBounds.fX2, drawList.renderBounds.fY2 );
241251
CVector2D chatSize = chatBotRight - chatTopLeft;
@@ -246,9 +256,10 @@ void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOf
246256
chatBounds.fX2 += topLeftOffset.fX;
247257
chatBounds.fY2 += topLeftOffset.fY;
248258

249-
for ( uint i = 0 ; i < drawList.lineItemList.size () ; i++ )
259+
CGraphics::GetSingleton ().BeginDrawBatch ();
260+
261+
for ( const auto & item : drawList.lineItemList )
250262
{
251-
const SDrawListLineItem& item = drawList.lineItemList[i];
252263
m_Lines [ item.uiLine ].Draw ( item.vecPosition - chatTopLeft + topLeftOffset, item.ucAlpha, drawList.bShadow, drawList.bOutline, chatBounds );
253264
}
254265

@@ -264,6 +275,7 @@ void CChat::GetDrawList ( SDrawList& outDrawList )
264275
{
265276
float fLineDifference = CChat::GetFontHeight ( m_vecScale.fY );
266277
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 );
267279
unsigned long ulTime = GetTickCount32 ();
268280
float fRcpChatLineFadeOut = 1.0f / m_ulChatLineFadeOut;
269281
bool bShadow = ( m_Color.A * m_fBackgroundAlpha == 0.f ) && !m_bTextBlackOutline;
@@ -318,9 +330,16 @@ void CChat::GetDrawList ( SDrawList& outDrawList )
318330

319331
if ( fLineAlpha > 0.f )
320332
{
333+
CVector2D vecOffset;
334+
335+
if ( m_eTextAlign == Chat::Text::Align::RIGHT )
336+
{
337+
vecOffset.fX = fMaxLineWidth - m_Lines[ uiLine ].GetWidth();
338+
}
339+
321340
SDrawListLineItem item;
322341
item.uiLine = uiLine;
323-
item.vecPosition = vecPosition;
342+
item.vecPosition = vecPosition + vecOffset;
324343
item.ucAlpha = static_cast < unsigned char >( fLineAlpha * 255.0f );
325344
outDrawList.lineItemList.push_back( item );
326345
}
@@ -488,8 +507,12 @@ void CChat::ClearInput ( void )
488507
m_strInputText.clear ();
489508
m_InputLine.Clear ();
490509
m_vecInputSize = CalcInputSize ();
510+
491511
if ( m_pInput )
512+
{
492513
m_pInput->SetSize ( m_vecInputSize );
514+
UpdatePosition();
515+
}
493516
}
494517

495518
void CChat::ScrollUp ()
@@ -549,7 +572,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
549572
// If theres a command to call, call it
550573
if ( !m_strCommand.empty () && !m_strInputText.empty () )
551574
CCommands::GetSingleton().Execute ( m_strCommand.c_str (), m_strInputText.c_str () );
552-
575+
553576
SetInputVisible ( false );
554577

555578
m_fSmoothScrollResetTime = GetSecondCount ();
@@ -569,7 +592,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
569592
iFound = 0;
570593
else
571594
++iFound;
572-
595+
573596
SString strPlayerNamePart = strCurrentInput.substr ( iFound );
574597

575598
CModManager* pModManager = CModManager::GetSingletonPtr ();
@@ -588,7 +611,8 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
588611
// Check if there is another player after our last result
589612
if ( m_strLastPlayerName.size () != 0 )
590613
{
591-
if ( strPlayerName.CompareI ( m_strLastPlayerName ) ) {
614+
if ( strPlayerName.CompareI ( m_strLastPlayerName ) )
615+
{
592616
m_strLastPlayerName.clear ();
593617
if ( *iter == vPlayerNames.back () )
594618
{
@@ -632,7 +656,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
632656
}
633657
break;
634658
}
635-
659+
636660
default:
637661
{
638662
// Clear last namepart when pressing letter
@@ -645,11 +669,11 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
645669

646670
// If we haven't exceeded the maximum number of characters per chat message, append the char to the message and update the input control
647671
if ( MbUTF8ToUTF16(m_strInputText).size () < CHAT_MAX_CHAT_LENGTH )
648-
{
672+
{
649673
if ( KeyboardArgs.codepoint >= 32 )
650674
{
651675
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
653677
{
654678
// injecting as is
655679
m_strInputText += static_cast < char > ( KeyboardArgs.codepoint );
@@ -688,11 +712,11 @@ void CChat::SetVisible ( bool bVisible )
688712

689713

690714
void CChat::SetInputVisible ( bool bVisible )
691-
{
715+
{
692716
if ( !IsVisible () )
693717
bVisible = false;
694718

695-
if ( bVisible )
719+
if ( !bVisible )
696720
{
697721
ClearInput ();
698722
}
@@ -719,21 +743,21 @@ void CChat::SetChatFont ( eChatFont Font )
719743
float fReqestedDxFontScale = std::max( m_vecScale.fX, m_vecScale.fY );
720744
switch ( Font )
721745
{
722-
case ChatFonts::CHAT_FONT_DEFAULT:
746+
case Chat::Font::DEFAULT:
723747
pFont = g_pCore->GetGUI ()->GetDefaultFont ();
724748
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_DEFAULT, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
725749
break;
726-
case ChatFonts::CHAT_FONT_CLEAR:
750+
case Chat::Font::CLEAR:
727751
pFont = g_pCore->GetGUI ()->GetClearFont ();
728752
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_CLEAR, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
729753
break;
730-
case ChatFonts::CHAT_FONT_BOLD:
754+
case Chat::Font::BOLD:
731755
pFont = g_pCore->GetGUI ()->GetBoldFont ();
732756
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_DEFAULT_BOLD, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
733757
break;
734-
case ChatFonts::CHAT_FONT_ARIAL:
758+
case Chat::Font::ARIAL:
735759
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_ARIAL, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
736-
break;
760+
break;
737761
}
738762

739763
m_fRcpUsingDxFontScale = 1 / fUsingDxFontScale;
@@ -772,11 +796,6 @@ void CChat::UpdateGUI ( void )
772796
m_vecBackgroundPosition.fY = Round ( m_vecBackgroundPosition.fY );
773797
m_pBackground->SetSize ( m_vecBackgroundSize );
774798

775-
m_vecInputPosition = CVector2D (
776-
m_vecBackgroundPosition.fX,
777-
m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY
778-
);
779-
780799
// Make sure there is enough room for all the lines
781800
uint uiMaxNumLines = g_pCore->GetGraphics ()->GetViewportHeight () / std::max ( 1.f, CChat::GetFontHeight ( m_vecScale.fY ) ) - 3;
782801
if ( m_uiNumLines > uiMaxNumLines )
@@ -785,9 +804,61 @@ void CChat::UpdateGUI ( void )
785804
m_vecInputSize = CalcInputSize ();
786805
if ( m_pInput )
787806
{
788-
m_pInput->SetPosition ( m_vecInputPosition );
789807
m_pInput->SetSize ( m_vecInputSize );
790808
}
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+
}
791862
}
792863

793864

@@ -831,14 +902,14 @@ CVector2D CChat::CalcInputSize ( void )
831902
void CChat::SetInputText ( const char* szText )
832903
{
833904
m_InputLine.Clear ();
834-
905+
835906
CColor color = m_InputTextColor;
836907
const char* szRemainingText = m_InputLine.Format ( szText,
837908
( m_vecInputSize.fX - ( 10.0f * m_vecScale.fX ) - m_InputLine.m_Prefix.GetWidth () ),
838909
color, false );
839910

840911
CChatLine* pLine = NULL;
841-
912+
842913
while ( szRemainingText && m_InputLine.m_ExtraLines.size () < 3 )
843914
{
844915
m_InputLine.m_ExtraLines.resize ( m_InputLine.m_ExtraLines.size () + 1 );
@@ -854,8 +925,12 @@ void CChat::SetInputText ( const char* szText )
854925
m_strInputText.resize ( szRemainingText - szText );
855926

856927
m_vecInputSize = CalcInputSize ();
928+
857929
if ( m_pInput )
930+
{
858931
m_pInput->SetSize ( m_vecInputSize );
932+
UpdatePosition();
933+
}
859934
}
860935

861936

@@ -888,7 +963,9 @@ float CChat::GetFontHeight ( float fScale )
888963
{
889964
return g_pChat->m_pFont->GetFontHeight ( fScale );
890965
}
966+
891967
fScale *= g_pChat->m_fRcpUsingDxFontScale;
968+
892969
return g_pCore->GetGraphics ()->GetDXFontHeight ( fScale, g_pChat->m_pDXFont );
893970
}
894971

@@ -902,7 +979,9 @@ float CChat::GetTextExtent ( const char * szText, float fScale )
902979
{
903980
return g_pChat->m_pFont->GetTextExtent ( szText, fScale );
904981
}
982+
905983
fScale *= g_pChat->m_fRcpUsingDxFontScale;
984+
906985
return g_pCore->GetGraphics ()->GetDXTextExtent ( szText, fScale, g_pChat->m_pDXFont );
907986
}
908987

@@ -928,8 +1007,7 @@ void CChat::DrawTextString ( const char * szText, CRect2D DrawArea, float fZ, CR
9281007
if ( DrawArea.fY1 + fLineHeight - RenderBounds.fY1 > 1 )
9291008
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 );
9301009
}
931-
else
932-
if ( DrawArea.fY1 + fLineHeight > RenderBounds.fY2 )
1010+
else if ( DrawArea.fY1 + fLineHeight > RenderBounds.fY2 )
9331011
{
9341012
// Clip text at the bottom
9351013
if ( RenderBounds.fY2 - DrawArea.fY1 > 1 )
@@ -998,7 +1076,7 @@ const char* CChatLine::Format ( const char* szStringAnsi, float fWidth, CColor&
9981076
{
9991077
float fSectionWidth = CChat::GetTextExtent ( UTF16ToMbUTF8 ( strSectionStart.substr ( 0 , uiSeekPos ) ).c_str (), g_pChat->m_vecScale.fX );
10001078

1001-
if ( *szSectionEnd == '\0' || *szSectionEnd == '\n' || fPrevSectionsWidth + fSectionWidth > fWidth )
1079+
if ( *szSectionEnd == '\0' || *szSectionEnd == '\n' || std::ceil(fPrevSectionsWidth + fSectionWidth) > fWidth )
10021080
{
10031081
bLastSection = true;
10041082
break;
@@ -1155,7 +1233,7 @@ float CChatLineSection::GetWidth ()
11551233
{
11561234
if ( m_fCachedWidth < 0.0f || m_strText.size () != m_uiCachedLength )
11571235
{
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 ) );
11591237
m_uiCachedLength = m_strText.size ();
11601238
}
11611239
return m_fCachedWidth * g_pChat->m_vecScale.fX;

Client/core/CChat.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
*
1111
*****************************************************************************/
1212

13-
#ifndef __CCHAT_H
14-
#define __CCHAT_H
13+
#pragma once
1514

1615
#include "CGUI.h"
1716
#include <core/CCoreInterface.h>
@@ -234,6 +233,7 @@ class CChat
234233

235234
protected:
236235
void UpdateGUI ( void );
236+
void UpdatePosition ( void );
237237
void UpdateSmoothScroll ( float* pfPixelScroll, int *piLineScroll );
238238
void DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOffset = CVector2D ( 0, 0 ) );
239239
void GetDrawList ( SDrawList& outDrawList );
@@ -251,6 +251,12 @@ class CChat
251251
SString m_strLastPlayerNamePart;
252252
SString m_strLastPlayerName;
253253

254+
float m_fPositionOffsetX;
255+
float m_fPositionOffsetY;
256+
eChatPositionHorizontal m_ePositionHorizontal;
257+
eChatPositionVertical m_ePositionVertical;
258+
eChatTextAlign m_eTextAlign;
259+
254260
CGUI* m_pManager;
255261
CGUIFont* m_pFont;
256262
LPD3DXFONT m_pDXFont;
@@ -303,5 +309,3 @@ class CChat
303309

304310
bool m_bNickCompletion;
305311
};
306-
307-
#endif

0 commit comments

Comments
 (0)