Skip to content

Commit b2ae4fd

Browse files
author
Marek Kulik
committed
Add chat text alignment
1 parent bff2d0a commit b2ae4fd

File tree

8 files changed

+153
-78
lines changed

8 files changed

+153
-78
lines changed

Client/core/CChat.cpp

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -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.0125;
61+
m_fPositionOffsetY = 0.0150;
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 ();
@@ -125,46 +130,11 @@ void CChat::LoadCVars ( void )
125130
CVARS_GET ( "chat_line_fade_out", (unsigned int &)m_ulChatLineFadeOut );
126131
CVARS_GET ( "chat_font", (unsigned int &)Font ); SetChatFont ( (eChatFont)Font );
127132
CVARS_GET ( "chat_nickcompletion", m_bNickCompletion );
128-
129-
CVector2D vecResolution = m_pManager->GetResolution();
130-
float fLineDifference = CChat::GetFontHeight ( m_vecScale.fY );
131-
float fRelativeWidth = (m_vecBackgroundSize.fX / vecResolution.fX),
132-
fRelativeHeight = ((m_vecBackgroundSize.fY + fLineDifference * 1.25f) / vecResolution.fY);
133-
134-
int iHorizontal, iVertical;
135-
float fOffsetX, fOffsetY, fPosX, fPosY;
136-
CVARS_GET ( "chat_position_offset_x", fOffsetX );
137-
CVARS_GET ( "chat_position_offset_y", fOffsetY );
138-
CVARS_GET ( "chat_position_horizontal", iHorizontal );
139-
CVARS_GET ( "chat_position_vertical", iVertical );
140-
switch ( iHorizontal )
141-
{
142-
case Chat::Position::Horizontal::RIGHT:
143-
fPosX = 1.0 - fRelativeWidth + fOffsetX;
144-
break;
145-
case Chat::Position::Horizontal::CENTER:
146-
fPosX = (1.0 - fRelativeWidth) / 2 + fOffsetX;
147-
break;
148-
case Chat::Position::Horizontal::LEFT:
149-
default:
150-
fPosX = fOffsetX;
151-
break;
152-
}
153-
switch ( iVertical )
154-
{
155-
case Chat::Position::Vertical::BOTTOM:
156-
fPosY = 1.0 - fRelativeHeight + fOffsetY;
157-
break;
158-
case Chat::Position::Vertical::CENTER:
159-
fPosY = (1.0 - fRelativeHeight) / 2 + fOffsetY;
160-
break;
161-
case Chat::Position::Vertical::TOP:
162-
default:
163-
fPosY = fOffsetY;
164-
break;
165-
}
166-
m_vecBackgroundPosition = CVector2D( fPosX, fPosY ) * vecResolution;
167-
m_pBackground->SetPosition( m_vecBackgroundPosition );
133+
CVARS_GET ( "chat_position_offset_x", m_fPositionOffsetX );
134+
CVARS_GET ( "chat_position_offset_y", m_fPositionOffsetY );
135+
CVARS_GET ( "chat_position_horizontal", (unsigned int &)m_ePositionHorizontal );
136+
CVARS_GET ( "chat_position_vertical", (unsigned int &)m_ePositionVertical );
137+
CVARS_GET ( "chat_text_alignment", (unsigned int &)m_eTextAlign );
168138

169139
// Modify default chat box to be like 'Transparent' preset
170140
SString strFlags;
@@ -317,8 +287,6 @@ void CChat::Draw ( bool bUseCacheTexture )
317287
//
318288
void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOffset )
319289
{
320-
CGraphics::GetSingleton ().BeginDrawBatch ();
321-
322290
CVector2D chatTopLeft ( drawList.renderBounds.fX1, drawList.renderBounds.fY1 );
323291
CVector2D chatBotRight ( drawList.renderBounds.fX2, drawList.renderBounds.fY2 );
324292
CVector2D chatSize = chatBotRight - chatTopLeft;
@@ -329,9 +297,10 @@ void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOf
329297
chatBounds.fX2 += topLeftOffset.fX;
330298
chatBounds.fY2 += topLeftOffset.fY;
331299

332-
for ( uint i = 0 ; i < drawList.lineItemList.size () ; i++ )
300+
CGraphics::GetSingleton ().BeginDrawBatch ();
301+
302+
for ( const auto & item : drawList.lineItemList )
333303
{
334-
const SDrawListLineItem& item = drawList.lineItemList[i];
335304
m_Lines [ item.uiLine ].Draw ( item.vecPosition - chatTopLeft + topLeftOffset, item.ucAlpha, drawList.bShadow, chatBounds );
336305
}
337306

@@ -347,6 +316,7 @@ void CChat::GetDrawList ( SDrawList& outDrawList )
347316
{
348317
float fLineDifference = CChat::GetFontHeight ( m_vecScale.fY );
349318
CVector2D vecPosition ( m_vecBackgroundPosition.fX + ( 5.0f * m_vecScale.fX ), m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY - ( fLineDifference * 1.25f ) );
319+
float fMaxLineWidth = m_vecBackgroundSize.fX - ( 10.0f * m_vecScale.fX );
350320
unsigned long ulTime = GetTickCount32 ();
351321
float fRcpChatLineFadeOut = 1.0f / m_ulChatLineFadeOut;
352322
bool bShadow = ( m_Color.A * m_fBackgroundAlpha == 0.f );
@@ -400,9 +370,15 @@ void CChat::GetDrawList ( SDrawList& outDrawList )
400370

401371
if ( fLineAlpha > 0.f )
402372
{
373+
CVector2D vecOffset;
374+
375+
if ( m_eTextAlign == Chat::Text::Align::RIGHT ) {
376+
vecOffset.fX = fMaxLineWidth - m_Lines[ uiLine ].GetWidth();
377+
}
378+
403379
SDrawListLineItem item;
404380
item.uiLine = uiLine;
405-
item.vecPosition = vecPosition;
381+
item.vecPosition = vecPosition + vecOffset;
406382
item.ucAlpha = static_cast < unsigned char >( fLineAlpha * 255.0f );
407383
outDrawList.lineItemList.push_back( item );
408384
}
@@ -570,8 +546,10 @@ void CChat::ClearInput ( void )
570546
m_strInputText.clear ();
571547
m_InputLine.Clear ();
572548
m_vecInputSize = CalcInputSize ();
573-
if ( m_pInput )
549+
if ( m_pInput ) {
574550
m_pInput->SetSize ( m_vecInputSize );
551+
UpdatePosition();
552+
}
575553
}
576554

577555
void CChat::ScrollUp ()
@@ -774,7 +752,7 @@ void CChat::SetInputVisible ( bool bVisible )
774752
if ( !IsVisible () )
775753
bVisible = false;
776754

777-
if ( bVisible )
755+
if ( !bVisible )
778756
{
779757
ClearInput ();
780758
}
@@ -854,11 +832,6 @@ void CChat::UpdateGUI ( void )
854832
m_vecBackgroundPosition.fY = Round ( m_vecBackgroundPosition.fY );
855833
m_pBackground->SetSize ( m_vecBackgroundSize );
856834

857-
m_vecInputPosition = CVector2D (
858-
m_vecBackgroundPosition.fX,
859-
m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY
860-
);
861-
862835
// Make sure there is enough room for all the lines
863836
uint uiMaxNumLines = g_pCore->GetGraphics ()->GetViewportHeight () / std::max ( 1.f, CChat::GetFontHeight ( m_vecScale.fY ) ) - 3;
864837
if ( m_uiNumLines > uiMaxNumLines )
@@ -867,9 +840,60 @@ void CChat::UpdateGUI ( void )
867840
m_vecInputSize = CalcInputSize ();
868841
if ( m_pInput )
869842
{
870-
m_pInput->SetPosition ( m_vecInputPosition );
871843
m_pInput->SetSize ( m_vecInputSize );
872844
}
845+
846+
UpdatePosition();
847+
}
848+
849+
850+
void CChat::UpdatePosition ( void )
851+
{
852+
CVector2D vecResolution = m_pManager->GetResolution();
853+
854+
float fRelativeWidth = ( m_vecBackgroundSize.fX / vecResolution.fX ),
855+
fRelativeHeight = ( ( m_vecBackgroundSize.fY + m_vecInputSize.fY ) / vecResolution.fY ),
856+
fPosX, fPosY;
857+
858+
switch ( m_ePositionHorizontal )
859+
{
860+
case Chat::Position::Horizontal::RIGHT:
861+
fPosX = 1.0 - fRelativeWidth + m_fPositionOffsetX;
862+
break;
863+
case Chat::Position::Horizontal::CENTER:
864+
fPosX = (1.0 - fRelativeWidth) / 2 + m_fPositionOffsetX;
865+
break;
866+
case Chat::Position::Horizontal::LEFT:
867+
default:
868+
fPosX = m_fPositionOffsetX;
869+
break;
870+
}
871+
872+
switch ( m_ePositionVertical )
873+
{
874+
case Chat::Position::Vertical::BOTTOM:
875+
fPosY = 1.0 - fRelativeHeight + m_fPositionOffsetY;
876+
break;
877+
case Chat::Position::Vertical::CENTER:
878+
fPosY = (1.0 - fRelativeHeight) / 2 + m_fPositionOffsetY;
879+
break;
880+
case Chat::Position::Vertical::TOP:
881+
default:
882+
fPosY = m_fPositionOffsetY;
883+
break;
884+
}
885+
886+
m_vecBackgroundPosition = CVector2D ( fPosX, fPosY ) * vecResolution;
887+
m_vecInputPosition = CVector2D (
888+
m_vecBackgroundPosition.fX,
889+
m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY
890+
);
891+
892+
m_pBackground->SetPosition ( m_vecBackgroundPosition );
893+
894+
if ( m_pInput ) {
895+
m_pInput->SetPosition ( m_vecInputPosition );
896+
}
873897
}
874898

875899

@@ -936,8 +960,10 @@ void CChat::SetInputText ( const char* szText )
936960
m_strInputText.resize ( szRemainingText - szText );
937961

938962
m_vecInputSize = CalcInputSize ();
939-
if ( m_pInput )
963+
if ( m_pInput ) {
940964
m_pInput->SetSize ( m_vecInputSize );
965+
UpdatePosition();
966+
}
941967
}
942968

943969

@@ -1080,7 +1106,7 @@ const char* CChatLine::Format ( const char* szStringAnsi, float fWidth, CColor&
10801106
{
10811107
float fSectionWidth = CChat::GetTextExtent ( UTF16ToMbUTF8 ( strSectionStart.substr ( 0 , uiSeekPos ) ).c_str (), g_pChat->m_vecScale.fX );
10821108

1083-
if ( *szSectionEnd == '\0' || *szSectionEnd == '\n' || fPrevSectionsWidth + fSectionWidth > fWidth )
1109+
if ( *szSectionEnd == '\0' || *szSectionEnd == '\n' || std::ceil(fPrevSectionsWidth + fSectionWidth) > fWidth )
10841110
{
10851111
bLastSection = true;
10861112
break;
@@ -1237,7 +1263,7 @@ float CChatLineSection::GetWidth ()
12371263
{
12381264
if ( m_fCachedWidth < 0.0f || m_strText.size () != m_uiCachedLength )
12391265
{
1240-
m_fCachedWidth = CChat::GetTextExtent ( m_strText.c_str (), g_pChat->m_vecScale.fX ) / std::max ( 0.01f, g_pChat->m_vecScale.fX );
1266+
m_fCachedWidth = std::ceil ( CChat::GetTextExtent ( m_strText.c_str (), g_pChat->m_vecScale.fX ) / std::max ( 0.01f, g_pChat->m_vecScale.fX ) );
12411267
m_uiCachedLength = m_strText.size ();
12421268
}
12431269
return m_fCachedWidth * g_pChat->m_vecScale.fX;

Client/core/CChat.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class CChat
231231

232232
protected:
233233
void UpdateGUI ( void );
234+
void UpdatePosition ( void );
234235
void UpdateSmoothScroll ( float* pfPixelScroll, int *piLineScroll );
235236
void DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOffset = CVector2D ( 0, 0 ) );
236237
void GetDrawList ( SDrawList& outDrawList );
@@ -248,6 +249,12 @@ class CChat
248249
SString m_strLastPlayerNamePart;
249250
SString m_strLastPlayerName;
250251

252+
float m_fPositionOffsetX;
253+
float m_fPositionOffsetY;
254+
eChatPositionHorizontal m_ePositionHorizontal;
255+
eChatPositionVertical m_ePositionVertical;
256+
eChatTextAlign m_eTextAlign;
257+
251258
CGUI* m_pManager;
252259
CGUIFont* m_pFont;
253260
LPD3DXFONT m_pDXFont;

Client/core/CClientVariables.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ void CClientVariables::ValidateValues ( void )
241241
ClampValue ( "chat_width", 0.5f, 4.f );
242242
ClampValue ( "chat_line_life", 1000, 120000000 );
243243
ClampValue ( "chat_line_fade_out", 1000, 30000000 );
244-
ClampValue ( "chat_position_offset_x", 0.0f, 1.0f );
245-
ClampValue ( "chat_position_offset_y", 0.0f, 1.0f );
244+
ClampValue ( "chat_position_offset_x", -1.0f, 1.0f );
245+
ClampValue ( "chat_position_offset_y", -1.0f, 1.0f );
246246
ClampValue ( "chat_position_horizontal",Chat::Position::Horizontal::LEFT, Chat::Position::Horizontal::RIGHT );
247247
ClampValue ( "chat_position_vertical", Chat::Position::Vertical::TOP, Chat::Position::Vertical::BOTTOM );
248+
ClampValue ( "chat_text_alignment", Chat::Text::Align::LEFT, Chat::Text::Align::RIGHT );
248249
ClampValue ( "text_scale", 0.8f, 3.0f );
249250
ClampValue ( "mtavolume", 0.0f, 1.0f );
250251
ClampValue ( "voicevolume", 0.0f, 1.0f );
@@ -292,8 +293,9 @@ void CClientVariables::LoadDefaults ( void )
292293
DEFAULT ( "chat_nickcompletion", true ); // chatbox nick completion
293294
DEFAULT ( "chat_position_offset_x", 0.0125f ); // chatbox relative x position offset
294295
DEFAULT ( "chat_position_offset_y", 0.015f ); // chatbox relative y position offset
295-
DEFAULT ( "chat_position_horizontal", Chat::Position::Horizontal::LEFT); // chatbox horizontal position
296-
DEFAULT ( "chat_position_vertical", Chat::Position::Vertical::TOP); // chatbox vertical position
296+
DEFAULT ( "chat_position_horizontal", Chat::Position::Horizontal::LEFT ); // chatbox horizontal position
297+
DEFAULT ( "chat_position_vertical", Chat::Position::Vertical::TOP ); // chatbox vertical position
298+
DEFAULT ( "chat_text_alignment", Chat::Text::Align::LEFT ); // chatbox horizontal text alignment
297299
DEFAULT ( "server_can_flash_window", true ); // allow server to flash the window
298300
DEFAULT ( "allow_tray_notifications", true ); // allow scripts to create tray balloon notifications
299301
DEFAULT ( "text_scale", 1.0f ); // text scale

Client/core/CDebugView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class CDebugView : public CChat
2828

2929
void Draw ( bool bUseCacheTexture );
3030
void Output ( const char* szText, bool bColorCoded );
31+
32+
protected:
33+
// Debug view doesn't support position changes unlike chat box
34+
void UpdatePosition ( void ) { };
3135
};
3236

3337
#endif

Client/core/CSettings.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,10 +2089,10 @@ void CSettings::CreateInterfaceTabGUI( void )
20892089
{
20902090
SString strHorizontal = _("Horizontal:"),
20912091
strVertical = _("Vertical:"),
2092-
strInputAlign = _("Input:"),
2092+
strTextAlign = _("Text-Align:"),
20932093
strXOffset = _("X-Offset:"),
20942094
strYOffset = _("Y-Offset:");
2095-
fIndentX = pManager->CGUI_GetMaxTextExtent ( "default-normal", strHorizontal, strVertical, strXOffset, strYOffset, strInputAlign ) + 10.0f;
2095+
fIndentX = pManager->CGUI_GetMaxTextExtent ( "default-normal", strHorizontal, strVertical, strXOffset, strYOffset, strTextAlign ) + 10.0f;
20962096

20972097
pTabLayout->GetSize ( vecSize );
20982098
fComboWidth = std::min ( 150.0f, vecSize.fX - fMarginX - fIndentX - 10.0f );
@@ -2131,18 +2131,18 @@ void CSettings::CreateInterfaceTabGUI( void )
21312131
m_pChatVerticalCombo->SetReadOnly ( true );
21322132
m_pChatVerticalCombo->SetSelectedItemByIndex( 0 );
21332133

2134-
/*pLabel = reinterpret_cast < CGUILabel* > ( pManager->CreateLabel ( pTabLayout, strInputAlign ) );
2134+
pLabel = reinterpret_cast < CGUILabel* > ( pManager->CreateLabel ( pTabLayout, strTextAlign ) );
21352135
pLabel->SetPosition ( CVector2D ( vecTemp.fX, vecTemp.fY + fLineSizeY + fLineGapY ) );
21362136
pLabel->GetPosition ( vecTemp );
21372137
pLabel->AutoSize ( );
21382138

2139-
auto pComboBox = reinterpret_cast < CGUIComboBox* > ( pManager->CreateComboBox ( pTabLayout, "" ) );
2140-
pComboBox->SetPosition ( CVector2D ( vecTemp.fX + fIndentX, vecTemp.fY - 1.0f ) );
2141-
pComboBox->SetSize ( CVector2D ( fComboWidth, 85.0f ) );
2142-
pComboBox->AddItem ( _("Left") )->SetData( (void *) Chat::Position::Horizontal::LEFT );
2143-
pComboBox->AddItem ( _("Right") )->SetData( (void *) Chat::Position::Horizontal::RIGHT );
2144-
pComboBox->SetReadOnly ( true );
2145-
pComboBox->SetSelectedItemByIndex( 0 );*/
2139+
m_pChatTextAlignCombo = reinterpret_cast < CGUIComboBox* > ( pManager->CreateComboBox ( pTabLayout, "" ) );
2140+
m_pChatTextAlignCombo->SetPosition ( CVector2D ( vecTemp.fX + fIndentX, vecTemp.fY - 1.0f ) );
2141+
m_pChatTextAlignCombo->SetSize ( CVector2D ( fComboWidth, 85.0f ) );
2142+
m_pChatTextAlignCombo->AddItem ( _("Left") )->SetData( (void *) Chat::Text::Align::LEFT );
2143+
m_pChatTextAlignCombo->AddItem ( _("Right") )->SetData( (void *) Chat::Text::Align::RIGHT );
2144+
m_pChatTextAlignCombo->SetReadOnly ( true );
2145+
m_pChatTextAlignCombo->SetSelectedItemByIndex( 0 );
21462146

21472147
pLabel = reinterpret_cast < CGUILabel* > ( pManager->CreateLabel ( pTabLayout, strXOffset ) );
21482148
pLabel->SetPosition ( CVector2D ( vecTemp.fX, vecTemp.fY + fLineSizeY + fLineGapY ) );
@@ -3023,12 +3023,17 @@ void CSettings::LoadData ( void )
30233023
CVARS_GET ( "chat_position_horizontal", iVar );
30243024
if ( iVar > Chat::Position::Horizontal::RIGHT )
30253025
iVar = Chat::Position::Horizontal::LEFT;
3026-
m_pChatHorizontalCombo->SetSelectedItemByIndex( iVar );
3026+
m_pChatHorizontalCombo->SetSelectedItemByIndex ( iVar );
30273027

30283028
CVARS_GET ( "chat_position_vertical", iVar );
30293029
if ( iVar > Chat::Position::Vertical::BOTTOM )
30303030
iVar = Chat::Position::Vertical::TOP;
3031-
m_pChatVerticalCombo->SetSelectedItemByIndex( iVar );
3031+
m_pChatVerticalCombo->SetSelectedItemByIndex ( iVar );
3032+
3033+
CVARS_GET ( "chat_text_alignment", iVar );
3034+
if ( iVar > Chat::Text::Align::RIGHT )
3035+
iVar = Chat::Text::Align::LEFT;
3036+
m_pChatTextAlignCombo->SetSelectedItemByIndex ( iVar );
30323037

30333038
CVARS_GET ( "chat_position_offset_x", strVar ); m_pChatOffsetX->SetText( strVar.c_str() );
30343039
CVARS_GET ( "chat_position_offset_y", strVar ); m_pChatOffsetY->SetText( strVar.c_str() );
@@ -3351,6 +3356,11 @@ void CSettings::SaveData ( void )
33513356
int iSelected = ( int ) pSelected->GetData();
33523357
CVARS_SET ( "chat_position_vertical", iSelected );
33533358
}
3359+
if ( CGUIListItem* pSelected = m_pChatTextAlignCombo->GetSelectedItem() )
3360+
{
3361+
int iSelected = ( int ) pSelected->GetData();
3362+
CVARS_SET ( "chat_text_alignment", iSelected );
3363+
}
33543364

33553365
// Interface
33563366
CVARS_SET ( "server_can_flash_window", m_pFlashWindow->GetSelected ( ) );
@@ -3768,7 +3778,7 @@ bool CSettings::OnChatLoadPresetClick( CGUIElement* pElement )
37683778
pSubNode->GetTagContent ( iValue );
37693779

37703780
if ( iValue >= Chat::Position::Horizontal::LEFT && iValue <= Chat::Position::Horizontal::RIGHT ) {
3771-
m_pChatHorizontalCombo->SetSelectedItemByIndex( iValue );
3781+
m_pChatHorizontalCombo->SetSelectedItemByIndex ( iValue );
37723782
}
37733783
}
37743784
else if ( strTag == "position_vertical" )
@@ -3777,7 +3787,16 @@ bool CSettings::OnChatLoadPresetClick( CGUIElement* pElement )
37773787
pSubNode->GetTagContent ( iValue );
37783788

37793789
if ( iValue >= Chat::Position::Vertical::TOP && iValue <= Chat::Position::Vertical::BOTTOM ) {
3780-
m_pChatVerticalCombo->SetSelectedItemByIndex( iValue );
3790+
m_pChatVerticalCombo->SetSelectedItemByIndex ( iValue );
3791+
}
3792+
}
3793+
else if ( strTag == "text_alignment" )
3794+
{
3795+
int iValue;
3796+
pSubNode->GetTagContent ( iValue );
3797+
3798+
if ( iValue >= Chat::Text::Align::LEFT && iValue <= Chat::Text::Align::RIGHT ) {
3799+
m_pChatTextAlignCombo->SetSelectedItemByIndex ( iValue );
37813800
}
37823801
}
37833802
else if ( strTag == "offset_x" )

0 commit comments

Comments
 (0)