Skip to content

Commit

Permalink
Fix chat background rendering
Browse files Browse the repository at this point in the history
CEGUI seems broken at the moment (you can check with chat_use_cegui=1 in F8 console). We should probably remove the CEGUI mode for chat.
  • Loading branch information
botder committed Aug 9, 2023
1 parent e1717d0 commit 73379e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 132 deletions.
131 changes: 25 additions & 106 deletions Client/core/CChat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ CChat::CChat(CGUI* pManager, const CVector2D& vecPosition)
m_fSmoothRepeatTimer = 0;
m_TextColor = CHAT_TEXT_COLOR;
m_bUseCEGUI = false;
m_iCVarsRevision = -1;
m_bVisible = false;
m_bInputBlocked = false;
m_bInputVisible = false;
Expand All @@ -49,7 +48,6 @@ CChat::CChat(CGUI* pManager, const CVector2D& vecPosition)
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;
Expand All @@ -65,25 +63,6 @@ CChat::CChat(CGUI* pManager, const CVector2D& vecPosition)
m_iSelectedInputHistoryEntry = -1;
m_iCharacterLimit = m_iDefaultCharacterLimit;

// 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
Expand All @@ -97,10 +76,6 @@ CChat::~CChat()
ClearInput();

SetDxFont(NULL);
SAFE_DELETE(m_pBackground);
SAFE_DELETE(m_pBackgroundTexture);
SAFE_DELETE(m_pInput);
SAFE_DELETE(m_pInputTexture);
SAFE_DELETE(m_pInputHistory);

if (g_pChat == this)
Expand All @@ -119,10 +94,7 @@ void CChat::LoadCVars()
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);
Expand Down Expand Up @@ -167,15 +139,6 @@ void CChat::Draw(bool bUseCacheTexture, bool bAllowOutline)
bool bUsingOutline = m_bTextBlackOutline && bAllowOutline && bUseCacheTexture;
DrawInputLine(bUsingOutline);

if (m_bInputVisible)
{
// ChrML: Hack so chatbox input always works. It might get unfocused..
if (!m_pBackground->IsActive())
{
m_pBackground->Activate();
}
}

// Are we visible?
if (!m_bVisible)
return;
Expand Down Expand Up @@ -275,6 +238,15 @@ void CChat::DrawDrawList(const SDrawList& drawList, const CVector2D& topLeftOffs

CGraphics::GetSingleton().BeginDrawBatch();

// Draw chat background.
CColor background(m_Color);
background.A *= m_fBackgroundAlpha;

if (background.A)
{
g_pCore->GetGraphics()->DrawRectangle(0, 0, chatSize.fX, chatSize.fY, background.ToLong());
}

for (const auto& item : drawList.lineItemList)
{
CVector2D vecPosition = item.vecPosition - chatTopLeft + topLeftOffset;
Expand All @@ -299,15 +271,6 @@ void CChat::GetDrawList(SDrawList& outDrawList, bool bUsingOutline)
float fRcpChatLineFadeOut = 1.0f / m_ulChatLineFadeOut;
bool bShadow = (m_Color.A * m_fBackgroundAlpha == 0.f) && !bUsingOutline;

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);
}

// 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);
Expand Down Expand Up @@ -368,25 +331,23 @@ void CChat::GetDrawList(SDrawList& outDrawList, bool bUsingOutline)
//
void CChat::DrawInputLine(bool bUsingOutline)
{
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)
return;

if (m_bInputVisible)
// Draw chat input background.
CColor background(m_InputColor);
background.A *= m_fInputBackgroundAlpha;

if (background.A)
{
float fLineDifference = CChat::GetFontHeight(m_vecScale.fY);
bool bInputShadow = (m_InputColor.A * m_fInputBackgroundAlpha == 0.f) && !bUsingOutline;
CVector2D vecPosition(m_vecInputPosition.fX + (5.0f * m_vecScale.fX), m_vecInputPosition.fY + (fLineDifference * 0.125f));
m_InputLine.Draw(vecPosition, 255, bInputShadow, bUsingOutline);
g_pCore->GetGraphics()->DrawRectangle(m_vecInputPosition.fX, m_vecInputPosition.fY, m_vecInputSize.fX, m_vecInputSize.fY, background.ToLong());
}

// Draw chat input text.
float fLineDifference = CChat::GetFontHeight(m_vecScale.fY);
bool bInputShadow = (background.A == 0) && !bUsingOutline;
CVector2D vecPosition(m_vecInputPosition.fX + (5.0f * m_vecScale.fX), m_vecInputPosition.fY + (fLineDifference * 0.125f));
m_InputLine.Draw(vecPosition, 255, bInputShadow, bUsingOutline);
}

//
Expand Down Expand Up @@ -520,12 +481,7 @@ void CChat::ClearInput()
m_strInputText.clear();
m_InputLine.Clear();
m_vecInputSize = CalcInputSize();

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

void CChat::ScrollUp()
Expand Down Expand Up @@ -897,19 +853,13 @@ void CChat::UpdateGUI()
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);

// 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)) - m_iMaxInputLines;
if (m_uiNumLines > uiMaxNumLines)
SetNumLines(uiMaxNumLines);

m_vecInputSize = CalcInputSize();
if (m_pInput)
{
m_pInput->SetSize(m_vecInputSize);
}

UpdatePosition();
}

Expand Down Expand Up @@ -950,32 +900,6 @@ void CChat::UpdatePosition()

m_vecBackgroundPosition = CVector2D(std::floor(fPosX * vecResolution.fX), std::floor(fPosY * vecResolution.fY));
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);
}
}

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()
Expand Down Expand Up @@ -1016,12 +940,7 @@ void CChat::SetInputText(const char* szText)
m_strInputText.resize(szRemainingText - szText);

m_vecInputSize = CalcInputSize();

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

void CChat::SetCommand(const char* szCommand)
Expand Down
13 changes: 4 additions & 9 deletions Client/core/CChat.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class CColor
}
bool operator==(const CColor& other) const { return R == other.R && G == other.G && B == other.B && A == other.A; }

[[nodiscard]] unsigned long ToLong() { return (R | (G << 8) | (B << 16) | (A << 24)); }

unsigned char R, G, B, A;
};

Expand Down Expand Up @@ -190,8 +192,6 @@ class CChat
static void DrawTextString(const char* szText, CRect2D DrawArea, float fZ, CRect2D ClipRect, unsigned long ulFormat, unsigned long ulColor, float fScaleX,
float fScaleY, bool bOutline, const CRect2D& RenderBounds);

void SetColor(const CColor& Color);
void SetInputColor(const CColor& Color);
void SetTextColor(const CColor& Color) { m_TextColor = Color; };
void SetNumLines(unsigned int uiNumLines);

Expand Down Expand Up @@ -247,11 +247,6 @@ class CChat
CVector2D m_vecInputPosition;
CVector2D m_vecInputSize;

CGUITexture* m_pBackgroundTexture;
CGUITexture* m_pInputTexture;
CGUIStaticImage* m_pBackground;
CGUIStaticImage* m_pInput;

std::string m_strInputText;
std::string m_strCommand;

Expand Down Expand Up @@ -284,8 +279,8 @@ class CChat
float m_fNativeWidth;
float m_fRcpUsingDxFontScale;

bool m_bCanChangeWidth;
int m_iCVarsRevision;
bool m_bCanChangeWidth{true};
int m_iCVarsRevision{-1};

SDrawList m_PrevDrawList;
CRenderTargetItem* m_pCacheTexture;
Expand Down
18 changes: 1 addition & 17 deletions Client/core/CDebugView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,7 @@ CDebugView::CDebugView(CGUI* pManager, const CVector2D& vecPosition) : CChat()
m_iReportCount = 0;
m_Color = CColor(0, 0, 0, 100);
m_TextColor = DEBUGVIEW_TEXT_COLOR;
unsigned long ulBackgroundColor = COLOR_ARGB(m_Color.A, m_Color.R, m_Color.G, m_Color.B);

m_pBackground = m_pManager->CreateStaticImage();
m_pBackgroundTexture = m_pManager->CreateTexture();

m_pBackgroundTexture->LoadFromMemory(&ulBackgroundColor, 1, 1);
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);

m_pInput = NULL;
m_pInputTexture = NULL;


g_pChat = pChat;

UpdateGUI();
Expand All @@ -93,7 +78,6 @@ void CDebugView::Draw(bool bUseCacheTexture, bool bAllowOutline)
m_vecBackgroundPosition = vecPosition * vecResolution - CVector2D(0, height);
m_vecBackgroundPosition.fX = Round(m_vecBackgroundPosition.fX);
m_vecBackgroundPosition.fY = Round(m_vecBackgroundPosition.fY);
m_pBackground->SetPosition(m_vecBackgroundPosition);

CChat::Draw(bUseCacheTexture, bAllowOutline);
g_pChat = pChat;
Expand Down

0 comments on commit 73379e4

Please sign in to comment.