From 58b0ae5fc7396c4335c14edf89ffb5a51b2fb386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 24 May 2017 19:37:51 +0200 Subject: [PATCH] Possible emoji rendering fix --- src/engine/client/text.cpp | 4 ++ src/engine/textrender.h | 1 + src/game/client/components/chat.cpp | 66 ++++++++++++++++++++--------- src/game/client/components/chat.h | 1 + 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 9eb1908e0..4aa95cf56 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -640,6 +640,8 @@ public: Graphics()->SetColor(m_TextR, m_TextG, m_TextB, m_TextA); } + pCursor->m_EmojiX = DrawX; + while(pCurrent < pEnd && (pCursor->m_MaxLines < 1 || LineCount <= pCursor->m_MaxLines)) { int NewLine = 0; @@ -718,6 +720,8 @@ public: Graphics()->QuadsDrawTL(&QuadItem, 1); } + pCursor->m_EmojiX = DrawX + (pChr->m_OffsetX + pChr->m_Width) * Size; + DrawX += Advance*Size; pCursor->m_CharCount++; } diff --git a/src/engine/textrender.h b/src/engine/textrender.h index dacf0d128..3680cd90f 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -25,6 +25,7 @@ public: float m_StartY; float m_LineWidth; float m_X, m_Y; + float m_EmojiX; CFont *m_pFont; float m_FontSize; diff --git a/src/game/client/components/chat.cpp b/src/game/client/components/chat.cpp index 95aa4984f..b0f6863dd 100644 --- a/src/game/client/components/chat.cpp +++ b/src/game/client/components/chat.cpp @@ -603,6 +603,29 @@ void CChat::AddLine(int ClientID, int Team, const char *pLine) } m_aLines[m_CurrentLine].m_Emojis.sort_range(); + int Offset = 0; + + for(int i = 0; i < Length; ) + { + CEmojis::CEmojiInfo info; + bool Found = false; + for(int j = 0; j < m_aLines[m_CurrentLine].m_Emojis.size(); j++) + { + if(m_aLines[m_CurrentLine].m_Emojis[j].index >= i) { + Found = true; + info = m_aLines[m_CurrentLine].m_Emojis[j]; + break; + } + } + int End = Found ? info.index : str_length(m_aLines[m_CurrentLine].m_aText); + + str_copy(m_aLines[m_CurrentLine].m_aTextNoEmojis + Offset, m_aLines[m_CurrentLine].m_aText + i, End - i + 1); + + Offset += End - i; + i = End; + if (Found) i += info.length; + } + char aBuf[1024]; str_format(aBuf, sizeof(aBuf), "%s%s", m_aLines[m_CurrentLine].m_aName, m_aLines[m_CurrentLine].m_aText); Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, Team >= 2?"whisper":(m_aLines[m_CurrentLine].m_Team?"teamchat":"chat"), aBuf, Highlighted); @@ -785,10 +808,18 @@ void CChat::OnRender() { TextRender()->SetCursor(&Cursor, Begin, 0.0f, FontSize, 0); Cursor.m_LineWidth = LineWidth; + Cursor.m_X += Cursor.m_FontSize * m_aLines[r].m_Emojis.size(); + Cursor.m_Y += ((int)Cursor.m_X/(int)LineWidth) * Cursor.m_FontSize; + Cursor.m_X = (int)Cursor.m_X % (int)LineWidth; TextRender()->TextEx(&Cursor, "♥ ", -1); TextRender()->TextEx(&Cursor, aName, -1); - TextRender()->TextEx(&Cursor, m_aLines[r].m_aText, -1); - m_aLines[r].m_YOffset[OffsetType] = Cursor.m_Y + Cursor.m_FontSize; + TextRender()->TextEx(&Cursor, m_aLines[r].m_aTextNoEmojis, -1); + m_aLines[r].m_YOffset[OffsetType] = Cursor.m_Y + Cursor.m_FontSize; // 0 + 6 + //this stores float ↓ += (( 198.333 + 6 * 0 )/ 200 )* 6; + //m_aLines[r].m_YOffset[OffsetType] += (int)((Cursor.m_X + Cursor.m_FontSize * m_aLines[r].m_Emojis.size())/LineWidth) * Cursor.m_FontSize; + printf("offset:\n%f %f %d %f\n", Cursor.m_X, Cursor.m_Y, m_aLines[r].m_Emojis.size(), LineWidth); + printf("%f\n", m_aLines[r].m_YOffset[OffsetType]);/*+ + ((int)(Cursor.m_X + Cursor.m_FontSize * m_aLines[r].m_Emojis.size())/(int)LineWidth)*Cursor.m_FontSize*/; } y -= m_aLines[r].m_YOffset[OffsetType]; @@ -869,6 +900,7 @@ void CChat::OnRender() } int Length = str_length(m_aLines[r].m_aText); + char aBuf[1024]; for (int i = 0; i < Length; ) { @@ -882,32 +914,28 @@ void CChat::OnRender() break; } } + + char *pStart = &m_aLines[r].m_aText[i]; + if(!Found) { - char *pStart = &m_aLines[r].m_aText[i]; - char *pEnd = &m_aLines[r].m_aText[str_length(m_aLines[r].m_aText)]; - char *pSubstr = (char *)mem_alloc(pEnd - pStart + 1, 1); - mem_zero(pSubstr, pEnd - pStart + 1); + str_copy(aBuf, pStart, Length - i + 1); - mem_copy(pSubstr, pStart, pEnd - pStart); - TextRender()->TextEx(&Cursor, pSubstr, -1); - mem_free(pSubstr); + TextRender()->TextEx(&Cursor, aBuf, -1); - i = str_length(m_aLines[r].m_aText); + //i = str_length(m_aLines[r].m_aText); + break; } else { - char *pStart = &m_aLines[r].m_aText[i]; - char *pEnd = &m_aLines[r].m_aText[info.index]; - char *pSubstr = (char *)mem_alloc(pEnd - pStart + 1, 1); - mem_zero(pSubstr, pEnd - pStart + 1); + str_copy(aBuf, pStart, info.index - i + 1); - mem_copy(pSubstr, pStart, pEnd - pStart); - TextRender()->TextEx(&Cursor, pSubstr, -1); - mem_free(pSubstr); + TextRender()->TextEx(&Cursor, aBuf, -1); + + m_pClient->m_pEmojis->Render(info.m_ID, (Cursor.m_EmojiX) + 3, Cursor.m_Y + Cursor.m_FontSize-1, Cursor.m_FontSize, Cursor.m_FontSize); + + Cursor.m_X += Cursor.m_FontSize + (Cursor.m_EmojiX - Cursor.m_X); - m_pClient->m_pEmojis->Render(info.m_ID, Cursor.m_X + Cursor.m_FontSize * 2 / 3, Cursor.m_Y + Cursor.m_FontSize * 2 / 3, Cursor.m_FontSize, Cursor.m_FontSize); - Cursor.m_X += Cursor.m_FontSize; if(Cursor.m_LineWidth < Cursor.m_X + Cursor.m_FontSize) { Cursor.m_X = 0; diff --git a/src/game/client/components/chat.h b/src/game/client/components/chat.h index 96c6944c6..be478d733 100644 --- a/src/game/client/components/chat.h +++ b/src/game/client/components/chat.h @@ -26,6 +26,7 @@ class CChat : public CComponent int m_NameColor; char m_aName[64]; char m_aText[512]; + char m_aTextNoEmojis[512]; bool m_Friend; bool m_Highlighted; sorted_array m_Emojis;