Browse Source

Proper support for player-colored buttons in UI

Ivan Savenko 2 years ago
parent
commit
d4098059b8

+ 3 - 1
client/adventureMap/CAdventureMapWidget.cpp

@@ -17,6 +17,7 @@
 #include "CResDataBar.h"
 
 #include "../gui/CGuiHandler.h"
+#include "../gui/Shortcut.h"
 #include "../mapView/MapView.h"
 #include "../render/CAnimation.h"
 #include "../render/IImage.h"
@@ -158,8 +159,9 @@ std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapButton(const JsonNode &
 	auto position = readTargetArea(input["area"]);
 	auto image = input["image"].String();
 	auto help = readHintText(input["help"]);
+	bool playerColored = input["playerColored"].Bool();
 
-	auto button = std::make_shared<CButton>(position.topLeft(), image, help);
+	auto button = std::make_shared<CButton>(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored);
 
 	loadButtonHotkey(button, input["hotkey"]);
 

+ 1 - 1
client/battle/BattleWindow.cpp

@@ -359,7 +359,7 @@ void BattleWindow::showAlternativeActionIcon(PossiblePlayerBattleAction action)
 	}
 		
 	auto anim = std::make_shared<CAnimation>(iconName);
-	w->setImage(anim, false);
+	w->setImage(anim);
 	w->redraw();
 }
 

+ 7 - 7
client/widgets/Buttons.cpp

@@ -249,32 +249,32 @@ CButton::CButton(Point position, const std::string &defName, const std::pair<std
 	if (!defName.empty())
 	{
 		imageNames.push_back(defName);
-		setIndex(0, playerColoredButton);
+		setIndex(0);
+		if (playerColoredButton)
+			image->playerColored(LOCPLINT->playerID);
 	}
 }
 
-void CButton::setIndex(size_t index, bool playerColoredButton)
+void CButton::setIndex(size_t index)
 {
 	if (index == currentImage || index>=imageNames.size())
 		return;
 	currentImage = index;
 	auto anim = std::make_shared<CAnimation>(imageNames[index]);
-	setImage(anim, playerColoredButton);
+	setImage(anim);
 }
 
-void CButton::setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton, int animFlags)
+void CButton::setImage(std::shared_ptr<CAnimation> anim, int animFlags)
 {
 	OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
 
 	image = std::make_shared<CAnimImage>(anim, getState(), 0, 0, 0, animFlags);
-	if (playerColoredButton)
-		image->playerColored(LOCPLINT->playerID);
 	pos = image->pos;
 }
 
 void CButton::setPlayerColor(PlayerColor player)
 {
-	if (image)
+	if (image && image->isPlayerColored())
 		image->playerColored(player);
 }
 

+ 2 - 2
client/widgets/Buttons.h

@@ -98,8 +98,8 @@ public:
 			CFunctionList<void()> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
 
 	/// Appearance modifiers
-	void setIndex(size_t index, bool playerColoredButton=false);
-	void setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton=false, int animFlags=0);
+	void setIndex(size_t index);
+	void setImage(std::shared_ptr<CAnimation> anim, int animFlags=0);
 	void setPlayerColor(PlayerColor player);
 
 	/// CIntObject overrides

+ 9 - 8
client/widgets/Images.cpp

@@ -138,7 +138,6 @@ void CFilledTexture::showAll(SDL_Surface *to)
 CAnimImage::CAnimImage(const std::string & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
 	frame(Frame),
 	group(Group),
-	player(-1),
 	flags(Flags)
 {
 	pos.x += x;
@@ -151,7 +150,6 @@ CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Gr
 	anim(Anim),
 	frame(Frame),
 	group(Group),
-	player(-1),
 	flags(Flags)
 {
 	pos.x += x;
@@ -163,7 +161,6 @@ CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targ
 	anim(Anim),
 	frame(Frame),
 	group(Group),
-	player(-1),
 	flags(Flags),
 	scaledSize(targetPos.w, targetPos.h)
 {
@@ -252,8 +249,8 @@ void CAnimImage::setFrame(size_t Frame, size_t Group)
 		group = Group;
 		if(auto img = anim->getImage(frame, group))
 		{
-			if (flags & CShowableAnim::PLAYER_COLORED)
-				img->playerColored(player);
+			if (player.has_value())
+				img->playerColored(*player);
 			setSizeFromImage(*img);
 		}
 	}
@@ -264,10 +261,14 @@ void CAnimImage::setFrame(size_t Frame, size_t Group)
 void CAnimImage::playerColored(PlayerColor currPlayer)
 {
 	player = currPlayer;
-	flags |= CShowableAnim::PLAYER_COLORED;
-	anim->getImage(frame, group)->playerColored(player);
+	anim->getImage(frame, group)->playerColored(*player);
 	if (flags & CShowableAnim::BASE)
-			anim->getImage(0, group)->playerColored(player);
+			anim->getImage(0, group)->playerColored(*player);
+}
+
+bool CAnimImage::isPlayerColored() const
+{
+	return player.has_value();
 }
 
 CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):

+ 9 - 5
client/widgets/Images.h

@@ -86,10 +86,12 @@ private:
 	//displayed frame/group
 	size_t frame;
 	size_t group;
-	PlayerColor player;
 	ui8 flags;
 	const Point scaledSize;
 
+	/// If set, then image is colored using player-specific palette
+	std::optional<PlayerColor> player;
+
 	bool isScaled() const;
 	void setSizeFromImage(const IImage &img);
 	void init();
@@ -101,15 +103,18 @@ public:
 	CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
 	~CAnimImage();
 
-	//size of animation
+	/// size of animation
 	size_t size();
 
-	//change displayed frame on this one
+	/// change displayed frame on this one
 	void setFrame(size_t Frame, size_t Group=0);
 
-	//makes image player-colored
+	/// makes image player-colored to specific player
 	void playerColored(PlayerColor player);
 
+	/// returns true if image has player-colored effect applied
+	bool isPlayerColored() const;
+
 	void showAll(SDL_Surface * to) override;
 };
 
@@ -122,7 +127,6 @@ public:
 		BASE=1,            //base frame will be blitted before current one
 		HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
 		VERTICAL_FLIP=4,   //TODO: will be displayed rotated
-		PLAYER_COLORED=16, //TODO: all loaded images will be player-colored
 		PLAY_ONCE=32       //play animation only once and stop at last frame
 	};
 protected:

+ 15 - 1
config/widgets/adventureMap.json

@@ -121,6 +121,7 @@
 					"image" : "IAM002.DEF",
 					"help" : "core.help.293",
 					"hotkey": "adventureKingdomOverview",
+					"playerColored" : true,
 					"area": { "top" : 0, "left": 0, "width" : 32, "height" : 32 }
 				},
 				{
@@ -129,6 +130,7 @@
 					"image" : "IAM010.DEF",
 					"help" : "core.help.294",
 					"hotkey": "adventureToggleMapLevel",
+					"playerColored" : true,
 					"area": { "top" : 0, "left": 32, "width" : 32, "height" : 32 }
 				},
 				{
@@ -137,6 +139,7 @@
 					"image" : "IAM003.DEF",
 					"help" : "core.help.294",
 					"hotkey": "adventureToggleMapLevel",
+					"playerColored" : true,
 					"area": { "top" : 0, "left": 32, "width" : 32, "height" : 32 }
 				},
 				{
@@ -145,6 +148,7 @@
 					"image" : "IAM004.DEF",
 					"help" : "core.help.295",
 					"hotkey": "adventureQuestLog",
+					"playerColored" : true,
 					"area": { "top" : 32, "left": 0, "width" : 32, "height" : 32 }
 				},
 				{
@@ -153,6 +157,7 @@
 					"image" : "IAM005.DEF",
 					"help" : "core.help.296",
 					"hotkey": "adventureSetHeroAsleep",
+					"playerColored" : true,
 					"area": { "top" : 32, "left": 32, "width" : 32, "height" : 32 }
 				},
 				{
@@ -161,6 +166,7 @@
 					"image" : "IAM011.DEF",
 					"help" : "core.help.296",
 					"hotkey": "adventureSetHeroAwake",
+					"playerColored" : true,
 					"area": { "top" : 32, "left": 32, "width" : 32, "height" : 32 }
 				},
 				{
@@ -169,6 +175,7 @@
 					"image" : "IAM006.DEF",
 					"help" : "core.help.297",
 					"hotkey": "adventureMoveHero",
+					"playerColored" : true,
 					"area": { "top" : 64, "left": 0, "width" : 32, "height" : 32 }
 				},
 				{
@@ -177,6 +184,7 @@
 					"image" : "IAM007.DEF",
 					"help" : "core.help.298",
 					"hotkey": "adventureCastSpell",
+					"playerColored" : true,
 					"area": { "top" : 64, "left": 32, "width" : 32, "height" : 32 }
 				},
 				{
@@ -185,6 +193,7 @@
 					"image" : "IAM008.DEF",
 					"help" : "core.help.299",
 					"hotkey": "adventureGameOptions",
+					"playerColored" : true,
 					"area": { "top" : 96, "left": 0, "width" : 32, "height" : 32 }
 				},
 				{
@@ -193,6 +202,7 @@
 					"image" : "IAM009.DEF",
 					"help" : "core.help.300",
 					"hotkey": "globalOptions",
+					"playerColored" : true,
 					"area": { "top" : 96, "left": 32, "width" : 32, "height" : 32 }
 				},
 				{
@@ -201,14 +211,16 @@
 					"image" : "IAM000.DEF",
 					"help" : "core.help.301",
 					"hotkey": "adventureNextHero",
+					"playerColored" : true,
 					"area": { "top" : 128, "left": 0, "width" : 64, "height" : 32 }
 				},
 				{
 					"type": "adventureMapButton",
 					"name": "buttonEndTurn",
 					"image" : "IAM001.DEF",
-					"hotkey": "adventureEndTurn",
+					"hotkey": "gameEndTurn",
 					"help" : "core.help.302",
+					"playerColored" : true,
 					"area": { "top" : 160, "left": 0, "width" : 64, "height" : 32 }
 				}
 			]
@@ -453,6 +465,7 @@
 					"type": "adventureMapButton",
 					"name": "worldViewSurface",
 					"image" : "IAM003.DEF",
+					"playerColored" : true,
 					"area": { "top" : 79, "left": 343, "width" : 32, "height" : 32 }
 				},
 				{
@@ -465,6 +478,7 @@
 					"type": "adventureMapButton",
 					"name": "worldViewUnderground",
 					"image" : "IAM010.DEF",
+					"playerColored" : true,
 					"area": { "top" : 343, "left": 79, "width" : 32, "height" : 32 }
 				},
 				{