diff --git a/src/framework/ui/uitextedit.cpp b/src/framework/ui/uitextedit.cpp index a384aa1c8..edda70737 100644 --- a/src/framework/ui/uitextedit.cpp +++ b/src/framework/ui/uitextedit.cpp @@ -76,14 +76,15 @@ void UITextEdit::drawSelf(DrawPoolType drawPane) m_placeholderFont->drawText(m_placeholder, m_drawArea, m_placeholderColor, m_placeholderAlign); } } + if (m_color != Color::alpha) { - if (glyphsMustRecache) { - m_glyphsTextRectCache.clear(); - for (int i = -1; ++i < textLength;) - m_glyphsTextRectCache.emplace_back(m_glyphsCoords[i].first, m_glyphsCoords[i].second); + if (m_drawTextColors.empty() || m_colorCoordsBuffer.empty()) { + g_drawPool.addTexturedCoordsBuffer(texture, m_coordsBuffer, m_color, m_textDrawConductor); + } else { + for (const auto& [color, coordsBuffer] : m_colorCoordsBuffer) { + g_drawPool.addTexturedCoordsBuffer(texture, coordsBuffer, color, m_textDrawConductor); + } } - for (const auto& [dest, src] : m_glyphsTextRectCache) - g_drawPool.addTexturedRect(dest, texture, src, m_color); } if (hasSelection()) { @@ -258,12 +259,40 @@ void UITextEdit::update(bool focusCursor) } else { // AlignLeft } + std::map colorCoordsMap; + uint32_t curColorRgba; + int32_t nextColorIndex = 0; + int32_t colorIndex = -1; + CoordsBufferPtr coords; + + const int textColorsSize = m_drawTextColors.size(); + m_colorCoordsBuffer.clear(); + m_coordsBuffer->clear(); + for (int i = 0; i < textLength; ++i) { + if (i >= nextColorIndex) { + colorIndex = colorIndex + 1; + if (colorIndex < textColorsSize) { + curColorRgba = m_drawTextColors[colorIndex].second.rgba(); + } + if (colorIndex + 1 < textColorsSize) { + nextColorIndex = m_drawTextColors[colorIndex + 1].first; + } else { + nextColorIndex = textLength; + } + + if (colorCoordsMap.find(curColorRgba) == colorCoordsMap.end()) { + colorCoordsMap.insert(std::make_pair(curColorRgba, std::make_shared())); + } + + coords = colorCoordsMap[curColorRgba]; + } + glyph = static_cast(text[i]); m_glyphsCoords[i].first.clear(); // skip invalid glyphs - if (glyph < 32 && glyph != static_cast('\n')) + if (glyph < 32) continue; // calculate initial glyph rect and texture coords @@ -324,6 +353,16 @@ void UITextEdit::update(bool focusCursor) // render glyph m_glyphsCoords[i].first = glyphScreenCoords; m_glyphsCoords[i].second = glyphTextureCoords; + + if (textColorsSize > 0) { + coords->addRect(glyphScreenCoords, glyphTextureCoords); + } else { + m_coordsBuffer->addRect(glyphScreenCoords, glyphTextureCoords); + } + } + + for (auto& [rgba, crds] : colorCoordsMap) { + m_colorCoordsBuffer.emplace_back(Color(rgba), crds); } if (fireAreaUpdate) @@ -575,8 +614,11 @@ void UITextEdit::updateDisplayedText() else text = m_text; - if (isTextWrap() && m_rect.isValid()) + m_drawTextColors = m_textColors; + + if (isTextWrap() && m_rect.isValid()) { text = m_font->wrapText(text, getPaddingRect().width() - m_textOffset.x); + } m_displayedText = text; } diff --git a/src/framework/ui/uitextedit.h b/src/framework/ui/uitextedit.h index fbb5555eb..76c376643 100644 --- a/src/framework/ui/uitextedit.h +++ b/src/framework/ui/uitextedit.h @@ -152,7 +152,6 @@ class UITextEdit : public UIWidget std::vector> m_glyphsCoords; - std::vector> m_glyphsTextRectCache; std::vector> m_glyphsSelectRectCache; std::string m_displayedText; diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index fab87c84d..f8316b3c2 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -238,7 +238,6 @@ class UIWidget : public LuaObject DrawConductor m_backgroundDrawConductor; DrawConductor m_imageDrawConductor; DrawConductor m_iconDrawConductor; - DrawConductor m_textDrawConductor; DrawConductor m_borderDrawConductor; // state managment @@ -383,6 +382,8 @@ class UIWidget : public LuaObject uint16_t m_autoRepeatDelay{ 500 }; Point m_lastClickPosition; + DrawConductor m_textDrawConductor; + public: void setX(int x) { move(x, getY()); } void setY(int y) { move(getX(), y); } @@ -573,8 +574,6 @@ class UIWidget : public LuaObject Rect m_textCachedScreenCoords; std::vector m_glyphsPositionsCache; Size m_textSize; - CoordsBufferPtr m_coordsBuffer; - std::vector> m_colorCoordsBuffer; protected: virtual void updateText(); @@ -592,6 +591,9 @@ class UIWidget : public LuaObject std::vector> m_textColors; std::vector> m_drawTextColors; + CoordsBufferPtr m_coordsBuffer; + std::vector> m_colorCoordsBuffer; + float m_fontScale{ 1.f }; public: