From dd4c95bd0460ccea03290c88064b66cd086459af Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:28:09 +0200 Subject: [PATCH] textcolor --- Global.h | 2 ++ client/widgets/TextControls.cpp | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Global.h b/Global.h index 675a3eff4..2eb54db45 100644 --- a/Global.h +++ b/Global.h @@ -122,6 +122,7 @@ static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size."); #include #include #include +#include #include #include #include @@ -147,6 +148,7 @@ static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size."); #define BOOST_ASIO_ENABLE_OLD_SERVICES #endif +#include #include #include #include diff --git a/client/widgets/TextControls.cpp b/client/widgets/TextControls.cpp index e1d1e5a84..24339b1b9 100644 --- a/client/widgets/TextControls.cpp +++ b/client/widgets/TextControls.cpp @@ -153,6 +153,15 @@ void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what) //We should count delimiters length from string to correct centering later. delimitersCount *= f->getStringWidth(delimeters)/2; + std::smatch match; + std::regex expr("\\{(#[0-9a-fA-F]{6})"); + std::string::const_iterator searchStart( what.cbegin() ); + while(std::regex_search(searchStart, what.cend(), match, expr)) + { + delimitersCount += f->getStringWidth(match[1].str()); + searchStart = match.suffix().first; + } + // input is rect in which given text should be placed // calculate proper position for top-left corner of the text if(alignment == ETextAlignment::TOPLEFT) @@ -189,8 +198,21 @@ void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what) { std::string toPrint = what.substr(begin, end - begin); - if(currDelimeter % 2) // Enclosed in {} text - set to yellow - to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint); + if(currDelimeter % 2) // Enclosed in {} text - set to yellow or defined color + { + std::smatch match; + std::regex expr("^#([0-9a-fA-F]{6})"); + if(std::regex_search(toPrint, match, expr)) + { + ui8 rgb[3] = {0, 0, 0}; + std::string tmp = boost::algorithm::unhex(match[1].str()); + std::copy(tmp.begin(), tmp.end(), rgb); + + to.drawText(where, font, ColorRGBA(rgb[0], rgb[1], rgb[2]), ETextAlignment::TOPLEFT, toPrint.substr(7, toPrint.length() - 7)); + } + else + to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint); + } else // Non-enclosed text, use default color to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);