1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00

move function; optional; json static

This commit is contained in:
Laserlicht 2023-09-13 16:02:54 +02:00
parent 922a775108
commit 063c7f3ca0
5 changed files with 34 additions and 35 deletions

View File

@ -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<ColorRGBA> 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;
}

View File

@ -49,4 +49,7 @@ public:
static const ColorRGBA BLACK;
static const ColorRGBA TRANSPARENCY;
/// parse color
static std::optional<ColorRGBA> parseColor(std::string text);
};

View File

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

View File

@ -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<std::string> 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<std::string> 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;

View File

@ -36,9 +36,6 @@ public:
/// split text in lines
static std::vector<std::string> 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);