瀏覽代碼

move function; optional; json static

Laserlicht 2 年之前
父節點
當前提交
063c7f3ca0
共有 5 個文件被更改,包括 34 次插入35 次删除
  1. 27 0
      client/render/Colors.cpp
  2. 3 0
      client/render/Colors.h
  3. 3 4
      client/widgets/TextControls.cpp
  4. 1 28
      client/windows/CMessage.cpp
  5. 0 3
      client/windows/CMessage.h

+ 27 - 0
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<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;
+}

+ 3 - 0
client/render/Colors.h

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

+ 3 - 4
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);

+ 1 - 28
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<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;

+ 0 - 3
client/windows/CMessage.h

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