From d4098059b835adcfad018deb39521276f5531806 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 3 May 2023 19:05:07 +0300 Subject: [PATCH] Proper support for player-colored buttons in UI --- client/adventureMap/CAdventureMapWidget.cpp | 4 +++- client/battle/BattleWindow.cpp | 2 +- client/widgets/Buttons.cpp | 14 +++++++------- client/widgets/Buttons.h | 4 ++-- client/widgets/Images.cpp | 17 +++++++++-------- client/widgets/Images.h | 14 +++++++++----- config/widgets/adventureMap.json | 16 +++++++++++++++- 7 files changed, 46 insertions(+), 25 deletions(-) diff --git a/client/adventureMap/CAdventureMapWidget.cpp b/client/adventureMap/CAdventureMapWidget.cpp index 9894e1a3c..fc74c6a32 100644 --- a/client/adventureMap/CAdventureMapWidget.cpp +++ b/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 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(position.topLeft(), image, help); + auto button = std::make_shared(position.topLeft(), image, help, 0, EShortcut::NONE, playerColored); loadButtonHotkey(button, input["hotkey"]); diff --git a/client/battle/BattleWindow.cpp b/client/battle/BattleWindow.cpp index 048406205..4e5d1fa46 100644 --- a/client/battle/BattleWindow.cpp +++ b/client/battle/BattleWindow.cpp @@ -359,7 +359,7 @@ void BattleWindow::showAlternativeActionIcon(PossiblePlayerBattleAction action) } auto anim = std::make_shared(iconName); - w->setImage(anim, false); + w->setImage(anim); w->redraw(); } diff --git a/client/widgets/Buttons.cpp b/client/widgets/Buttons.cpp index 45ea19df4..395f13d71 100644 --- a/client/widgets/Buttons.cpp +++ b/client/widgets/Buttons.cpp @@ -249,32 +249,32 @@ CButton::CButton(Point position, const std::string &defName, const std::pairplayerColored(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(imageNames[index]); - setImage(anim, playerColoredButton); + setImage(anim); } -void CButton::setImage(std::shared_ptr anim, bool playerColoredButton, int animFlags) +void CButton::setImage(std::shared_ptr anim, int animFlags) { OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE); image = std::make_shared(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); } diff --git a/client/widgets/Buttons.h b/client/widgets/Buttons.h index 82fbd58a1..4a7b6d034 100644 --- a/client/widgets/Buttons.h +++ b/client/widgets/Buttons.h @@ -98,8 +98,8 @@ public: CFunctionList Callback = 0, EShortcut key = {}, bool playerColoredButton = false ); /// Appearance modifiers - void setIndex(size_t index, bool playerColoredButton=false); - void setImage(std::shared_ptr anim, bool playerColoredButton=false, int animFlags=0); + void setIndex(size_t index); + void setImage(std::shared_ptr anim, int animFlags=0); void setPlayerColor(PlayerColor player); /// CIntObject overrides diff --git a/client/widgets/Images.cpp b/client/widgets/Images.cpp index 1f64b3f71..0b67fc104 100644 --- a/client/widgets/Images.cpp +++ b/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 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 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): diff --git a/client/widgets/Images.h b/client/widgets/Images.h index 160d0f5ad..ad309ce2c 100644 --- a/client/widgets/Images.h +++ b/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 player; + bool isScaled() const; void setSizeFromImage(const IImage &img); void init(); @@ -101,15 +103,18 @@ public: CAnimImage(std::shared_ptr 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: diff --git a/config/widgets/adventureMap.json b/config/widgets/adventureMap.json index c6869ba36..a0ae3ec89 100644 --- a/config/widgets/adventureMap.json +++ b/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 } }, {