Procházet zdrojové kódy

Configurable turn timer

nordsoft před 2 roky
rodič
revize
f01973a4f0

+ 30 - 15
client/adventureMap/TurnTimerWidget.cpp

@@ -11,8 +11,7 @@
 #include "TurnTimerWidget.h"
 
 #include "../CPlayerInterface.h"
-#include "../render/Canvas.h"
-#include "../render/Colors.h"
+
 #include "../render/EFont.h"
 #include "../gui/CGuiHandler.h"
 #include "../gui/TextAlignment.h"
@@ -20,26 +19,39 @@
 #include "../widgets/TextControls.h"
 #include "../../CCallback.h"
 #include "../../lib/CPlayerState.h"
+#include "../../lib/filesystem/ResourceID.h"
+
+TurnTimerWidget::DrawRect::DrawRect(const Rect & r, const ColorRGBA & c):
+	CIntObject(), rect(r), color(c)
+{
+}
 
-#include <SDL_render.h>
+void TurnTimerWidget::DrawRect::showAll(Canvas & to)
+{
+	to.drawColor(rect, color);
+	
+	CIntObject::showAll(to);
+}
 
 TurnTimerWidget::TurnTimerWidget():
-	CIntObject(TIME),
+	InterfaceObjectConfigurable(TIME),
 	turnTime(0), lastTurnTime(0)
 {
-	OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
-	
-	watches = std::make_shared<CAnimImage>("VCMI/BATTLEQUEUE/STATESSMALL", 1, 0, 4, 6);
-	label = std::make_shared<CLabel>(24, 2, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, std::to_string(turnTime));
+	REGISTER_BUILDER("drawRect", &TurnTimerWidget::buildDrawRect);
 	
 	recActions &= ~DEACTIVATE;
+	
+	const JsonNode config(ResourceID("config/widgets/turnTimer.json"));
+	
+	build(config);
 }
 
-void TurnTimerWidget::showAll(Canvas & to)
+std::shared_ptr<TurnTimerWidget::DrawRect> TurnTimerWidget::buildDrawRect(const JsonNode & config) const
 {
-	to.drawColor(Rect(4, 4, 68, 24), ColorRGBA(10, 10, 10, 255));
-	
-	CIntObject::showAll(to);
+	logGlobal->debug("Building widget TurnTimerWidget::DrawRect");
+	auto rect = readRect(config["rect"]);
+	auto color = readColor(config["color"]);
+	return std::make_shared<TurnTimerWidget::DrawRect>(rect, color);
 }
 
 void TurnTimerWidget::show(Canvas & to)
@@ -50,9 +62,12 @@ void TurnTimerWidget::show(Canvas & to)
 void TurnTimerWidget::setTime(int time)
 {
 	turnTime = time / 1000;
-	std::ostringstream oss;
-	oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
-	label->setText(oss.str());
+	if(auto w = widget<CLabel>("timer"))
+	{
+		std::ostringstream oss;
+		oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
+		w->setText(oss.str());
+	}
 }
 
 void TurnTimerWidget::tick(uint32_t msPassed)

+ 19 - 5
client/adventureMap/TurnTimerWidget.h

@@ -11,26 +11,40 @@
 #pragma once
 
 #include "../gui/CIntObject.h"
+#include "../gui/InterfaceObjectConfigurable.h"
+#include "../render/Canvas.h"
+#include "../render/Colors.h"
 
 class CAnimImage;
 class CLabel;
 
-class TurnTimerWidget : public CIntObject
+class TurnTimerWidget : public InterfaceObjectConfigurable
 {
 private:
+	
+	class DrawRect : public CIntObject
+	{
+		const Rect rect;
+		const ColorRGBA color;
+		
+	public:
+		DrawRect(const Rect &, const ColorRGBA &);
+		void showAll(Canvas & to) override;
+	};
 
 	int turnTime;
 	int lastTurnTime;
 	int cachedTurnTime;
 	
-	std::shared_ptr<CAnimImage> watches;
-	std::shared_ptr<CLabel> label;
-
+	//std::shared_ptr<CAnimImage> watches;
+	//std::shared_ptr<CLabel> label;
+	
+	std::shared_ptr<DrawRect> buildDrawRect(const JsonNode & config) const;
+	
 public:
 
 	//void tick(uint32_t msPassed) override;
 	void show(Canvas & to) override;
-	void showAll(Canvas & to) override;
 	void tick(uint32_t msPassed) override;
 	
 	void setTime(int time);

+ 23 - 12
client/gui/InterfaceObjectConfigurable.cpp

@@ -190,18 +190,29 @@ ColorRGBA InterfaceObjectConfigurable::readColor(const JsonNode & config) const
 	logGlobal->debug("Reading color");
 	if(!config.isNull())
 	{
-		if(config.String() == "yellow")
-			return Colors::YELLOW;
-		if(config.String() == "white")
-			return Colors::WHITE;
-		if(config.String() == "gold")
-			return Colors::METALLIC_GOLD;
-		if(config.String() == "green")
-			return Colors::GREEN;
-		if(config.String() == "orange")
-			return Colors::ORANGE;
-		if(config.String() == "bright-yellow")
-			return Colors::BRIGHT_YELLOW;
+		if(config.isString())
+		{
+			if(config.String() == "yellow")
+				return Colors::YELLOW;
+			if(config.String() == "white")
+				return Colors::WHITE;
+			if(config.String() == "gold")
+				return Colors::METALLIC_GOLD;
+			if(config.String() == "green")
+				return Colors::GREEN;
+			if(config.String() == "orange")
+				return Colors::ORANGE;
+			if(config.String() == "bright-yellow")
+				return Colors::BRIGHT_YELLOW;
+		}
+		if(config.isVector())
+		{
+			const auto & asVector = config.Vector();
+			if(asVector.size() == 4)
+				return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer(), asVector[3].Integer());
+			if(asVector.size() == 3)
+				return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer());
+		}
 	}
 	logGlobal->debug("Uknown color attribute");
 	return Colors::DEFAULT_KEY_COLOR;

+ 27 - 0
config/widgets/turnTimer.json

@@ -0,0 +1,27 @@
+{
+	"items":
+	[
+		{ //backgound color
+			"type": "drawRect",
+			"rect": {"x": 4, "y": 4, "w": 68, "h": 24},
+			"color": [10, 10, 10, 255]
+		},
+
+		{ //clocks icon
+			"type": "image",
+			"image": "VCMI/BATTLEQUEUE/STATESSMALL",
+			"frame": 1,
+			"position": {"x": 4, "y": 6}
+		},
+
+		{ //timer field label
+			"name": "timer",
+			"type": "label",
+			"font": "big",
+			"alignment": "left",
+			"color": "yellow",
+			"text": "",
+			"position": {"x": 24, "y": 2}
+		},
+	]
+}