소스 검색

textcolor

Laserlicht 2 년 전
부모
커밋
dd4c95bd04
2개의 변경된 파일26개의 추가작업 그리고 2개의 파일을 삭제
  1. 2 0
      Global.h
  2. 24 2
      client/widgets/TextControls.cpp

+ 2 - 0
Global.h

@@ -122,6 +122,7 @@ static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
 #include <optional>
 #include <queue>
 #include <random>
+#include <regex>
 #include <set>
 #include <sstream>
 #include <string>
@@ -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 <boost/algorithm/hex.hpp>
 #include <boost/algorithm/string.hpp>
 #include <boost/crc.hpp>
 #include <boost/current_function.hpp>

+ 24 - 2
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);