From 5506ae036a32987d0670093f6e880b0fb78d12dd Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Sun, 16 Oct 2016 07:51:07 +0300 Subject: [PATCH] Reworked CHillFortWindow * fixed displaying free upgrades * do not try to print 3rd or more resource in slot cost (in theory possible with mods) ** we need dynamic gui for that * CDefEssential-- --- client/gui/CIntObject.h | 5 -- client/widgets/Images.cpp | 8 +- client/widgets/Images.h | 2 + client/windows/GUIClasses.cpp | 148 +++++++++++++++++++--------------- client/windows/GUIClasses.h | 34 ++++---- 5 files changed, 113 insertions(+), 84 deletions(-) diff --git a/client/gui/CIntObject.h b/client/gui/CIntObject.h index 203088f85..75d8c2507 100644 --- a/client/gui/CIntObject.h +++ b/client/gui/CIntObject.h @@ -90,11 +90,6 @@ public: */ std::vector children; - //FIXME: workaround - void deactivateKeyboard() - { - deactivate(KEYBOARD); - } /* * Public interface diff --git a/client/widgets/Images.cpp b/client/widgets/Images.cpp index 494df7360..e65d29f7a 100644 --- a/client/widgets/Images.cpp +++ b/client/widgets/Images.cpp @@ -144,12 +144,12 @@ void CPicture::convertToScreenBPP() { SDL_Surface *hlp = bg; bg = SDL_ConvertSurface(hlp,screen->format,0); - CSDL_Ext::setDefaultColorKey(bg); + CSDL_Ext::setDefaultColorKey(bg); SDL_FreeSurface(hlp); } void CPicture::setAlpha(int value) -{ +{ CSDL_Ext::setAlpha (bg, value); } @@ -241,6 +241,7 @@ size_t CAnimImage::size() void CAnimImage::init() { + visible = true; anim->load(frame, group); if (flags & CShowableAnim::BASE) anim->load(0,group); @@ -263,6 +264,9 @@ CAnimImage::~CAnimImage() void CAnimImage::showAll(SDL_Surface * to) { + if(!visible) + return; + IImage *img; if ( flags & CShowableAnim::BASE && frame != 0) diff --git a/client/widgets/Images.h b/client/widgets/Images.h index f73920bce..27b31ab78 100644 --- a/client/widgets/Images.h +++ b/client/widgets/Images.h @@ -80,6 +80,8 @@ private: void init(); public: + bool visible; + CAnimImage(std::string name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0); CAnimImage(CAnimation* anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0); ~CAnimImage();//d-tor diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index 448728f7b..f7541bc49 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -8,7 +8,6 @@ #include "CSpellWindow.h" #include "../CBitmapHandler.h" -#include "../CDefHandler.h" #include "../CGameInfo.h" #include "../CMessage.h" #include "../CMusicHandler.h" @@ -1371,26 +1370,29 @@ CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectIn { OBJ_CONSTRUCTION_CAPTURING_ALL; - slotsCount=7; - resources = CDefHandler::giveDefEss("SMALRES.DEF"); - new CLabel(325, 32, FONT_BIG, CENTER, Colors::YELLOW, fort->getObjectName());//Hill Fort heroPic = new CHeroArea(30, 60, hero); - currState.resize(slotsCount+1); - costs.resize(slotsCount); - totalSumm.resize(GameConstants::RESOURCE_QUANTITY); + for (int i=0; iaddImage(image); + + for(int j : {0,1}) + { + slotIcons[i][j] = new CAnimImage("SMALRES", 0, 0, 104 + 76 * i, 128 + 20 * j); + slotLabels[i][j] = new CLabel(168 + 76 * i, 144 + 20 * j, FONT_SMALL, BOTTOMRIGHT); + } } - currState[slotsCount] = getState(SlotID(slotsCount)); upgradeAll = new CButton(Point(30, 231), "", CButton::tooltip(CGI->generaltexth->allTexts[432]), [&]{ makeDeal(SlotID(slotsCount));}, SDLK_0); for (auto image : { "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF" }) upgradeAll->addImage(image); @@ -1404,6 +1406,11 @@ CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectIn void CHillFortWindow::updateGarrisons() { + std::array costs;// costs [slot ID] [resource ID] = resource count for upgrade + + TResources totalSumm; // totalSum[resource ID] = value + totalSumm.resize(GameConstants::RESOURCE_QUANTITY); + for (int i=0; iaddHoverText(CButton::NORMAL, getTextForSlot(SlotID(i))); } - int newState = getState(SlotID(slotsCount)); + //"Upgrade all" slot + int newState = 2; + { + TResources myRes = LOCPLINT->cb->getResourceAmount(); + + bool allUpgraded = true;//All creatures are upgraded? + for (int i=0; isetIndex(newState); - garr->recreateSlots(); + + CWindowWithGarrison::updateGarrisons(); + + for (int i = 0; i < slotsCount; i++) + { + //hide all first + for(int j : {0,1}) + { + slotIcons[i][j]->visible = false; + slotLabels[i][j]->setText(""); + } + //if can upgrade or can not afford, draw cost + if (currState[i] == 0 || currState[i] == 2) + { + if (costs[i].nonZero()) + { + //reverse iterator is used to display gold as first element + int j = 0; + for(int res = costs[i].size()-1; (res >= 0) && (j < 2); res--) + { + int val = costs[i][res]; + if(!val) + continue; + + slotIcons[i][j]->visible = true; + slotIcons[i][j]->setFrame(res); + + slotLabels[i][j]->setText(boost::lexical_cast(val)); + j++; + } + } + else//free upgrade - print gold image and "Free" text + { + slotIcons[i][0]->visible = true; + slotIcons[i][0]->setFrame(Res::GOLD); + slotLabels[i][0]->setText(CGI->generaltexth->allTexts[344]); + } + } + } + + for (int i = 0; i < resCount; i++) + { + if(totalSumm[i] == 0) + { + totalIcons[i]->visible = false; + totalLabels[i]->setText(""); + } + else + { + totalIcons[i]->visible = true; + totalLabels[i]->setText(boost::lexical_cast(totalSumm[i])); + } + } } void CHillFortWindow::makeDeal(SlotID slot) @@ -1461,44 +1535,6 @@ void CHillFortWindow::makeDeal(SlotID slot) } } -void CHillFortWindow::showAll (SDL_Surface *to) -{ - CWindowObject::showAll(to); - - for ( int i=0; i= 0; j--) - { - int val = costs[i][j]; - if(!val) continue; - - blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to); - printToLoc(boost::lexical_cast(val), 168+76*i, curY+16, FONT_SMALL, Colors::WHITE, to); - curY += 20; - } - } - else//free upgrade - print gold image and "Free" text - { - blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to); - printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::WHITE, to); - } - } - } - for (int i=0; iourImages[i].bitmap, 104+76*i, 237, to); - printToLoc(boost::lexical_cast(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::WHITE, to); - } - } -} - std::string CHillFortWindow::getTextForSlot(SlotID slot) { if ( !hero->getCreature(slot) )//we don`t have creature here @@ -1517,20 +1553,6 @@ std::string CHillFortWindow::getTextForSlot(SlotID slot) int CHillFortWindow::getState(SlotID slot) { TResources myRes = LOCPLINT->cb->getResourceAmount(); - if ( slot.getNum() == slotsCount )//"Upgrade all" slot - { - bool allUpgraded = true;//All creatures are upgraded? - for (int i=0; islotEmpty(slot))//no creature here return -1; diff --git a/client/windows/GUIClasses.h b/client/windows/GUIClasses.h index 086ec9c12..a4df00d0a 100644 --- a/client/windows/GUIClasses.h +++ b/client/windows/GUIClasses.h @@ -401,29 +401,35 @@ public: /// Hill fort is the building where you can upgrade units class CHillFortWindow : public CWindowObject, public CWindowWithGarrison { -public: - - int slotsCount;//=7; - CGStatusBar * bar; - CDefEssential *resources; - CHeroArea *heroPic;//clickable hero image - CButton *quit,//closes window - *upgradeAll,//upgrade all creatures - *upgrade[7];//upgrade single creature +private: + static const int slotsCount = 7; + //todo: mithril support + static const int resCount = 7; const CGObjectInstance * fort; const CGHeroInstance * hero; - std::vector currState;//current state of slot - to avoid calls to getState or updating buttons - std::vector costs;// costs [slot ID] [resource ID] = resource count for upgrade - TResources totalSumm; // totalSum[resource ID] = value - CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object); //c-tor + CGStatusBar * bar; + CHeroArea * heroPic;//clickable hero image + CButton * quit;//closes window + CButton * upgradeAll;//upgrade all creatures + + std::array upgrade;//upgrade single creature + std::array currState;//current state of slot - to avoid calls to getState or updating buttons + + //there is a place for only 2 resources per slot + std::array< std::array, slotsCount> slotIcons; + std::array< std::array, slotsCount> slotLabels; + + std::array totalIcons; + std::array totalLabels; - void showAll (SDL_Surface *to) override; std::string getDefForSlot(SlotID slot);//return def name for this slot std::string getTextForSlot(SlotID slot);//return hover text for this slot void makeDeal(SlotID slot);//-1 for upgrading all creatures int getState(SlotID slot); //-1 = no creature 0=can't upgrade, 1=upgraded, 2=can upgrade +public: + CHillFortWindow(const CGHeroInstance * visitor, const CGObjectInstance * object); //c-tor void updateGarrisons() override;//update buttons after garrison changes };