From 063c7f3ca0848c9dca9f8e2fef54892f8453124f Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Wed, 13 Sep 2023 16:02:54 +0200 Subject: [PATCH] move function; optional; json static --- client/render/Colors.cpp | 27 +++++++++++++++++++++++++++ client/render/Colors.h | 3 +++ client/widgets/TextControls.cpp | 7 +++---- client/windows/CMessage.cpp | 29 +---------------------------- client/windows/CMessage.h | 3 --- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/client/render/Colors.cpp b/client/render/Colors.cpp index c7f5c1f4b..0c239e879 100644 --- a/client/render/Colors.cpp +++ b/client/render/Colors.cpp @@ -10,6 +10,7 @@ #include "StdInc.h" #include "Colors.h" +#include "../../lib/JsonNode.h" const ColorRGBA Colors::YELLOW = { 229, 215, 123, ColorRGBA::ALPHA_OPAQUE }; const ColorRGBA Colors::WHITE = { 255, 243, 222, ColorRGBA::ALPHA_OPAQUE }; @@ -23,3 +24,29 @@ const ColorRGBA Colors::RED = {255, 0, 0, ColorRGBA::ALPHA_OPAQUE}; const ColorRGBA Colors::PURPLE = {255, 75, 125, ColorRGBA::ALPHA_OPAQUE}; const ColorRGBA Colors::BLACK = {0, 0, 0, ColorRGBA::ALPHA_OPAQUE}; const ColorRGBA Colors::TRANSPARENCY = {0, 0, 0, ColorRGBA::ALPHA_TRANSPARENT}; + +std::optional Colors::parseColor(std::string text) +{ + std::smatch match; + std::regex expr("^#([0-9a-fA-F]{6})$"); + ui8 rgb[3] = {0, 0, 0}; + if(std::regex_search(text, match, expr)) + { + std::string tmp = boost::algorithm::unhex(match[1].str()); + std::copy(tmp.begin(), tmp.end(), rgb); + return ColorRGBA(rgb[0], rgb[1], rgb[2]); + } + + static const JsonNode config(JsonPath::builtin("CONFIG/textColors")); + auto colors = config["colors"].Struct(); + for(auto & color : colors) { + if(boost::algorithm::to_lower_copy(color.first) == boost::algorithm::to_lower_copy(text)) + { + std::string tmp = boost::algorithm::unhex(color.second.String()); + std::copy(tmp.begin(), tmp.end(), rgb); + return ColorRGBA(rgb[0], rgb[1], rgb[2]); + } + } + + return std::nullopt; +} \ No newline at end of file diff --git a/client/render/Colors.h b/client/render/Colors.h index 5db94e4ed..a73b5a856 100644 --- a/client/render/Colors.h +++ b/client/render/Colors.h @@ -49,4 +49,7 @@ public: static const ColorRGBA BLACK; static const ColorRGBA TRANSPARENCY; + + /// parse color + static std::optional parseColor(std::string text); }; diff --git a/client/widgets/TextControls.cpp b/client/widgets/TextControls.cpp index 88b9f38b9..e6cce7c1a 100644 --- a/client/widgets/TextControls.cpp +++ b/client/widgets/TextControls.cpp @@ -159,7 +159,7 @@ void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what) while(std::regex_search(searchStart, what.cend(), match, expr)) { std::string colorText = match[1].str(); - if(CMessage::parseColor(colorText).a != Colors::TRANSPARENCY.ALPHA_TRANSPARENT) + if(auto c = Colors::parseColor(colorText)) delimitersCount += f->getStringWidth(colorText + "|"); searchStart = match.suffix().first; } @@ -207,12 +207,11 @@ void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what) if(std::regex_search(toPrint, match, expr)) { std::string colorText = match[1].str(); - ColorRGBA color = CMessage::parseColor(colorText); - if(color.a != Colors::TRANSPARENCY.ALPHA_TRANSPARENT) + if(auto color = Colors::parseColor(colorText)) { toPrint = toPrint.substr(colorText.length() + 1, toPrint.length() - colorText.length()); - to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint); + to.drawText(where, font, *color, ETextAlignment::TOPLEFT, toPrint); } else to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint); diff --git a/client/windows/CMessage.cpp b/client/windows/CMessage.cpp index dc8cba620..ce508fa66 100644 --- a/client/windows/CMessage.cpp +++ b/client/windows/CMessage.cpp @@ -14,7 +14,6 @@ #include "../CGameInfo.h" #include "../../lib/CGeneralTextHandler.h" #include "../../lib/TextOperations.h" -#include "../../lib/JsonNode.h" #include "../windows/InfoWindows.h" #include "../widgets/Buttons.h" @@ -158,7 +157,7 @@ std::vector CMessage::breakText( std::string text, size_t maxLineWi if(std::regex_search(tmp, match, expr)) { std::string colorText = match[1].str(); - if(CMessage::parseColor(colorText).a != Colors::TRANSPARENCY.ALPHA_TRANSPARENT) + if(auto c = Colors::parseColor(colorText)) { color = colorText + "|"; currPos += colorText.length() + 1; @@ -227,32 +226,6 @@ std::vector CMessage::breakText( std::string text, size_t maxLineWi return ret; } -ColorRGBA CMessage::parseColor(std::string text) -{ - std::smatch match; - std::regex expr("^#([0-9a-fA-F]{6})$"); - ui8 rgb[3] = {0, 0, 0}; - if(std::regex_search(text, match, expr)) - { - std::string tmp = boost::algorithm::unhex(match[1].str()); - std::copy(tmp.begin(), tmp.end(), rgb); - return ColorRGBA(rgb[0], rgb[1], rgb[2]); - } - - const JsonNode config(JsonPath::builtin("CONFIG/textColors")); - auto colors = config["colors"].Struct(); - for(auto & color : colors) { - if(boost::algorithm::to_lower_copy(color.first) == boost::algorithm::to_lower_copy(text)) - { - std::string tmp = boost::algorithm::unhex(color.second.String()); - std::copy(tmp.begin(), tmp.end(), rgb); - return ColorRGBA(rgb[0], rgb[1], rgb[2]); - } - } - - return Colors::TRANSPARENCY; -} - std::string CMessage::guessHeader(const std::string & msg) { size_t begin = 0; diff --git a/client/windows/CMessage.h b/client/windows/CMessage.h index 12c5d0077..430c87a5b 100644 --- a/client/windows/CMessage.h +++ b/client/windows/CMessage.h @@ -36,9 +36,6 @@ public: /// split text in lines static std::vector breakText(std::string text, size_t maxLineWidth, EFonts font); - /// parse color - static ColorRGBA parseColor(std::string text); - /// Try to guess a header of a message static std::string guessHeader(const std::string & msg);