Skip to content

Commit

Permalink
Merge pull request #130 from Necktrox/feature/chatbox-position
Browse files Browse the repository at this point in the history
Add chatbox position settings
  • Loading branch information
Necktrox committed Jul 3, 2017
2 parents fa50dcf + 0e7cd50 commit c12aea3
Show file tree
Hide file tree
Showing 19 changed files with 1,042 additions and 542 deletions.
144 changes: 111 additions & 33 deletions Client/core/CChat.cpp
Expand Up @@ -21,9 +21,9 @@ extern CCore* g_pCore;
CChat * g_pChat = NULL;

CChat::CChat ( CGUI* pManager, const CVector2D & vecPosition )
{
{
g_pChat = this;
m_pManager = pManager;
m_pManager = pManager;

// Calculate relative position (assuming a 800x600 native resolution for our defined CCHAT_* values)
CVector2D vecResolution = m_pManager->GetResolution ();
Expand Down Expand Up @@ -57,6 +57,11 @@ CChat::CChat ( CGUI* pManager, const CVector2D & vecPosition )
m_pCacheTexture = NULL;
m_iCacheTextureRevision = 0;
m_iReportCount = 0;
m_fPositionOffsetX = 0.0125f;
m_fPositionOffsetY = 0.0150f;
m_ePositionHorizontal = Chat::Position::Horizontal::LEFT;
m_ePositionVertical = Chat::Position::Vertical::TOP;
m_eTextAlign = Chat::Text::Align::LEFT;

// Background area
m_pBackground = m_pManager->CreateStaticImage ();
Expand Down Expand Up @@ -126,6 +131,11 @@ void CChat::LoadCVars ( void )
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 );
CVARS_GET ( "chat_position_offset_x", m_fPositionOffsetX );
CVARS_GET ( "chat_position_offset_y", m_fPositionOffsetY );
CVARS_GET ( "chat_position_horizontal", (unsigned int &)m_ePositionHorizontal );
CVARS_GET ( "chat_position_vertical", (unsigned int &)m_ePositionVertical );
CVARS_GET ( "chat_text_alignment", (unsigned int &)m_eTextAlign );
}


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

// Is it time to update all the chat related cvars?
if( m_iCVarsRevision != CClientVariables::GetSingleton ().GetRevision () ) {
if( m_iCVarsRevision != CClientVariables::GetSingleton ().GetRevision () )
{
m_iCVarsRevision = CClientVariables::GetSingleton ().GetRevision ();
LoadCVars ();
UpdateGUI ();
Expand Down Expand Up @@ -169,6 +180,7 @@ void CChat::Draw ( bool bUseCacheTexture )
{
SAFE_RELEASE( m_pCacheTexture );
}

// Create rendertarget if required
if ( !m_pCacheTexture && ( CTickCount::Now () - m_lastRenderTargetCreationFail ).ToLongLong () > 60000 )
{
Expand Down Expand Up @@ -234,8 +246,6 @@ void CChat::Draw ( bool bUseCacheTexture )
//
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;
Expand All @@ -246,9 +256,10 @@ void CChat::DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOf
chatBounds.fX2 += topLeftOffset.fX;
chatBounds.fY2 += topLeftOffset.fY;

for ( uint i = 0 ; i < drawList.lineItemList.size () ; i++ )
CGraphics::GetSingleton ().BeginDrawBatch ();

for ( const auto & item : drawList.lineItemList )
{
const SDrawListLineItem& item = drawList.lineItemList[i];
m_Lines [ item.uiLine ].Draw ( item.vecPosition - chatTopLeft + topLeftOffset, item.ucAlpha, drawList.bShadow, drawList.bOutline, chatBounds );
}

Expand All @@ -264,6 +275,7 @@ 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 ) );
float fMaxLineWidth = m_vecBackgroundSize.fX - ( 10.0f * m_vecScale.fX );
unsigned long ulTime = GetTickCount32 ();
float fRcpChatLineFadeOut = 1.0f / m_ulChatLineFadeOut;
bool bShadow = ( m_Color.A * m_fBackgroundAlpha == 0.f ) && !m_bTextBlackOutline;
Expand Down Expand Up @@ -318,9 +330,16 @@ void CChat::GetDrawList ( SDrawList& outDrawList )

if ( fLineAlpha > 0.f )
{
CVector2D vecOffset;

if ( m_eTextAlign == Chat::Text::Align::RIGHT )
{
vecOffset.fX = fMaxLineWidth - m_Lines[ uiLine ].GetWidth();
}

SDrawListLineItem item;
item.uiLine = uiLine;
item.vecPosition = vecPosition;
item.vecPosition = vecPosition + vecOffset;
item.ucAlpha = static_cast < unsigned char >( fLineAlpha * 255.0f );
outDrawList.lineItemList.push_back( item );
}
Expand Down Expand Up @@ -488,8 +507,12 @@ void CChat::ClearInput ( void )
m_strInputText.clear ();
m_InputLine.Clear ();
m_vecInputSize = CalcInputSize ();

if ( m_pInput )
{
m_pInput->SetSize ( m_vecInputSize );
UpdatePosition();
}
}

void CChat::ScrollUp ()
Expand Down Expand Up @@ -549,7 +572,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
// 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 ();
Expand All @@ -569,7 +592,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
iFound = 0;
else
++iFound;

SString strPlayerNamePart = strCurrentInput.substr ( iFound );

CModManager* pModManager = CModManager::GetSingletonPtr ();
Expand All @@ -588,7 +611,8 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
// Check if there is another player after our last result
if ( m_strLastPlayerName.size () != 0 )
{
if ( strPlayerName.CompareI ( m_strLastPlayerName ) ) {
if ( strPlayerName.CompareI ( m_strLastPlayerName ) )
{
m_strLastPlayerName.clear ();
if ( *iter == vPlayerNames.back () )
{
Expand Down Expand Up @@ -632,7 +656,7 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
}
break;
}

default:
{
// Clear last namepart when pressing letter
Expand All @@ -645,11 +669,11 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )

// 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
if ( uiCharacter < 127 ) // we have any char from ASCII
{
// injecting as is
m_strInputText += static_cast < char > ( KeyboardArgs.codepoint );
Expand Down Expand Up @@ -688,11 +712,11 @@ void CChat::SetVisible ( bool bVisible )


void CChat::SetInputVisible ( bool bVisible )
{
{
if ( !IsVisible () )
bVisible = false;

if ( bVisible )
if ( !bVisible )
{
ClearInput ();
}
Expand All @@ -719,21 +743,21 @@ void CChat::SetChatFont ( eChatFont Font )
float fReqestedDxFontScale = std::max( m_vecScale.fX, m_vecScale.fY );
switch ( Font )
{
case ChatFonts::CHAT_FONT_DEFAULT:
case Chat::Font::DEFAULT:
pFont = g_pCore->GetGUI ()->GetDefaultFont ();
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_DEFAULT, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
break;
case ChatFonts::CHAT_FONT_CLEAR:
case Chat::Font::CLEAR:
pFont = g_pCore->GetGUI ()->GetClearFont ();
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_CLEAR, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
break;
case ChatFonts::CHAT_FONT_BOLD:
case 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:
case Chat::Font::ARIAL:
pDXFont = g_pCore->GetGraphics ()->GetFont ( FONT_ARIAL, &fUsingDxFontScale, fReqestedDxFontScale, "chat" );
break;
break;
}

m_fRcpUsingDxFontScale = 1 / fUsingDxFontScale;
Expand Down Expand Up @@ -772,11 +796,6 @@ void CChat::UpdateGUI ( void )
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 )
Expand All @@ -785,9 +804,61 @@ void CChat::UpdateGUI ( void )
m_vecInputSize = CalcInputSize ();
if ( m_pInput )
{
m_pInput->SetPosition ( m_vecInputPosition );
m_pInput->SetSize ( m_vecInputSize );
}

UpdatePosition();
}


void CChat::UpdatePosition ( void )
{
CVector2D vecResolution = m_pManager->GetResolution();

float fRelativeWidth = ( m_vecBackgroundSize.fX / vecResolution.fX ),
fRelativeHeight = ( ( m_vecBackgroundSize.fY + m_vecInputSize.fY ) / vecResolution.fY ),
fPosX, fPosY;

switch ( m_ePositionHorizontal )
{
case Chat::Position::Horizontal::RIGHT:
fPosX = 1.0 - fRelativeWidth + m_fPositionOffsetX;
break;
case Chat::Position::Horizontal::CENTER:
fPosX = (1.0 - fRelativeWidth) / 2 + m_fPositionOffsetX;
break;
case Chat::Position::Horizontal::LEFT:
default:
fPosX = m_fPositionOffsetX;
break;
}

switch ( m_ePositionVertical )
{
case Chat::Position::Vertical::BOTTOM:
fPosY = 1.0 - fRelativeHeight + m_fPositionOffsetY;
break;
case Chat::Position::Vertical::CENTER:
fPosY = (1.0 - fRelativeHeight) / 2 + m_fPositionOffsetY;
break;
case Chat::Position::Vertical::TOP:
default:
fPosY = m_fPositionOffsetY;
break;
}

m_vecBackgroundPosition = CVector2D ( fPosX, fPosY ) * vecResolution;
m_vecInputPosition = CVector2D (
m_vecBackgroundPosition.fX,
m_vecBackgroundPosition.fY + m_vecBackgroundSize.fY
);

m_pBackground->SetPosition ( m_vecBackgroundPosition );

if ( m_pInput )
{
m_pInput->SetPosition ( m_vecInputPosition );
}
}


Expand Down Expand Up @@ -831,14 +902,14 @@ CVector2D CChat::CalcInputSize ( void )
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 );
Expand All @@ -854,8 +925,12 @@ void CChat::SetInputText ( const char* szText )
m_strInputText.resize ( szRemainingText - szText );

m_vecInputSize = CalcInputSize ();

if ( m_pInput )
{
m_pInput->SetSize ( m_vecInputSize );
UpdatePosition();
}
}


Expand Down Expand Up @@ -888,7 +963,9 @@ float CChat::GetFontHeight ( float fScale )
{
return g_pChat->m_pFont->GetFontHeight ( fScale );
}

fScale *= g_pChat->m_fRcpUsingDxFontScale;

return g_pCore->GetGraphics ()->GetDXFontHeight ( fScale, g_pChat->m_pDXFont );
}

Expand All @@ -902,7 +979,9 @@ float CChat::GetTextExtent ( const char * szText, float fScale )
{
return g_pChat->m_pFont->GetTextExtent ( szText, fScale );
}

fScale *= g_pChat->m_fRcpUsingDxFontScale;

return g_pCore->GetGraphics ()->GetDXTextExtent ( szText, fScale, g_pChat->m_pDXFont );
}

Expand All @@ -928,8 +1007,7 @@ void CChat::DrawTextString ( const char * szText, CRect2D DrawArea, float fZ, CR
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, bOutline );
}
else
if ( DrawArea.fY1 + fLineHeight > RenderBounds.fY2 )
else if ( DrawArea.fY1 + fLineHeight > RenderBounds.fY2 )
{
// Clip text at the bottom
if ( RenderBounds.fY2 - DrawArea.fY1 > 1 )
Expand Down Expand Up @@ -998,7 +1076,7 @@ const char* CChatLine::Format ( const char* szStringAnsi, float fWidth, CColor&
{
float fSectionWidth = CChat::GetTextExtent ( UTF16ToMbUTF8 ( strSectionStart.substr ( 0 , uiSeekPos ) ).c_str (), g_pChat->m_vecScale.fX );

if ( *szSectionEnd == '\0' || *szSectionEnd == '\n' || fPrevSectionsWidth + fSectionWidth > fWidth )
if ( *szSectionEnd == '\0' || *szSectionEnd == '\n' || std::ceil(fPrevSectionsWidth + fSectionWidth) > fWidth )
{
bLastSection = true;
break;
Expand Down Expand Up @@ -1155,7 +1233,7 @@ float CChatLineSection::GetWidth ()
{
if ( m_fCachedWidth < 0.0f || m_strText.size () != m_uiCachedLength )
{
m_fCachedWidth = CChat::GetTextExtent ( m_strText.c_str (), g_pChat->m_vecScale.fX ) / std::max ( 0.01f, g_pChat->m_vecScale.fX );
m_fCachedWidth = std::ceil ( CChat::GetTextExtent ( m_strText.c_str (), g_pChat->m_vecScale.fX ) / std::max ( 0.01f, g_pChat->m_vecScale.fX ) );
m_uiCachedLength = m_strText.size ();
}
return m_fCachedWidth * g_pChat->m_vecScale.fX;
Expand Down
12 changes: 8 additions & 4 deletions Client/core/CChat.h
Expand Up @@ -10,8 +10,7 @@
*
*****************************************************************************/

#ifndef __CCHAT_H
#define __CCHAT_H
#pragma once

#include "CGUI.h"
#include <core/CCoreInterface.h>
Expand Down Expand Up @@ -234,6 +233,7 @@ class CChat

protected:
void UpdateGUI ( void );
void UpdatePosition ( void );
void UpdateSmoothScroll ( float* pfPixelScroll, int *piLineScroll );
void DrawDrawList ( const SDrawList& drawList, const CVector2D& topLeftOffset = CVector2D ( 0, 0 ) );
void GetDrawList ( SDrawList& outDrawList );
Expand All @@ -251,6 +251,12 @@ class CChat
SString m_strLastPlayerNamePart;
SString m_strLastPlayerName;

float m_fPositionOffsetX;
float m_fPositionOffsetY;
eChatPositionHorizontal m_ePositionHorizontal;
eChatPositionVertical m_ePositionVertical;
eChatTextAlign m_eTextAlign;

CGUI* m_pManager;
CGUIFont* m_pFont;
LPD3DXFONT m_pDXFont;
Expand Down Expand Up @@ -303,5 +309,3 @@ class CChat

bool m_bNickCompletion;
};

#endif

0 comments on commit c12aea3

Please sign in to comment.