2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CGarrisonInt.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2014-07-13 16:11:25 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CGarrisonInt.h"
|
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2014-07-13 16:11:25 +03:00
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/TextControls.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../windows/CCreatureWindow.h"
|
|
|
|
#include "../windows/GUIClasses.h"
|
2014-07-13 16:11:25 +03:00
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
|
|
|
|
|
|
|
#include "../../lib/CGameState.h"
|
|
|
|
|
|
|
|
void CGarrisonSlot::setHighlight(bool on)
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
selectionImage->enable(); //show
|
|
|
|
else
|
|
|
|
selectionImage->disable(); //hide
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonSlot::hover (bool on)
|
|
|
|
{
|
|
|
|
////Hoverable::hover(on);
|
|
|
|
if(on)
|
|
|
|
{
|
|
|
|
std::string temp;
|
|
|
|
if(creature)
|
|
|
|
{
|
|
|
|
if(owner->getSelection())
|
|
|
|
{
|
|
|
|
if(owner->getSelection() == this)
|
|
|
|
{
|
|
|
|
temp = CGI->generaltexth->tcommands[4]; //View %s
|
|
|
|
boost::algorithm::replace_first(temp,"%s",creature->nameSing);
|
|
|
|
}
|
|
|
|
else if (owner->getSelection()->creature == creature)
|
|
|
|
{
|
|
|
|
temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
|
|
|
|
boost::algorithm::replace_first(temp,"%s",creature->nameSing);
|
|
|
|
}
|
|
|
|
else if (owner->getSelection()->creature)
|
|
|
|
{
|
|
|
|
temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
|
|
|
|
boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->nameSing);
|
|
|
|
boost::algorithm::replace_first(temp,"%s",creature->nameSing);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->warnStream() << "Warning - shouldn't be - highlighted void slot "<<owner->getSelection();
|
|
|
|
logGlobal->warnStream() << "Highlighted set to nullptr";
|
2014-07-13 16:11:25 +03:00
|
|
|
owner->selectSlot(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-24 23:40:12 +02:00
|
|
|
if(upg == EGarrisonType::UP)
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2016-01-21 08:57:52 +02:00
|
|
|
temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
2015-08-24 20:31:08 +02:00
|
|
|
else if(owner->armedObjs[0] && (owner->armedObjs[0]->ID == Obj::TOWN || owner->armedObjs[0]->ID == Obj::HERO))
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2016-01-21 08:57:52 +02:00
|
|
|
temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp = CGI->generaltexth->allTexts[481]; //Select %s
|
|
|
|
}
|
|
|
|
boost::algorithm::replace_first(temp,"%s",creature->nameSing);
|
2017-07-12 21:01:10 +02:00
|
|
|
}
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(owner->getSelection())
|
|
|
|
{
|
|
|
|
const CArmedInstance *highl = owner->getSelection()->getObj();
|
|
|
|
if( highl->needsLastStack() //we are moving stack from hero's
|
|
|
|
&& highl->stacksCount() == 1 //it's only stack
|
|
|
|
&& owner->getSelection()->upg != upg //we're moving it to the other garrison
|
|
|
|
)
|
|
|
|
{
|
|
|
|
temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp = CGI->generaltexth->tcommands[6]; //Move %s
|
|
|
|
boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->nameSing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp = CGI->generaltexth->tcommands[11]; //Empty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GH.statusbar->setText(temp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GH.statusbar->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CArmedInstance * CGarrisonSlot::getObj() const
|
|
|
|
{
|
2015-08-24 23:40:12 +02:00
|
|
|
return owner->armedObjs[upg];
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
|
2015-08-24 21:19:58 +02:00
|
|
|
/// @return Whether the unit in the slot belongs to the current player.
|
2014-07-13 16:11:25 +03:00
|
|
|
bool CGarrisonSlot::our() const
|
|
|
|
{
|
2015-08-24 23:40:12 +02:00
|
|
|
return owner->owned[upg];
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
|
2015-08-24 21:19:58 +02:00
|
|
|
/// @return Whether the unit in the slot belongs to an ally but not to the current player.
|
2015-03-16 01:04:18 +02:00
|
|
|
bool CGarrisonSlot::ally() const
|
|
|
|
{
|
|
|
|
if(!getObj())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return PlayerRelations::ALLIES == LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, getObj()->tempOwner);
|
|
|
|
}
|
|
|
|
|
2015-08-25 00:41:41 +02:00
|
|
|
/// The creature slot has been clicked twice, therefore the creature info should be shown
|
|
|
|
/// @return Whether the view should be refreshed
|
2015-08-24 22:39:56 +02:00
|
|
|
bool CGarrisonSlot::viewInfo()
|
|
|
|
{
|
|
|
|
UpgradeInfo pom;
|
|
|
|
LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
|
2014-07-13 16:11:25 +03:00
|
|
|
|
2015-08-24 22:39:56 +02:00
|
|
|
bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
|
|
|
|
bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
|
|
|
|
std::function<void(CreatureID)> upgr = nullptr;
|
|
|
|
std::function<void()> dism = nullptr;
|
|
|
|
if(canUpgrade) upgr = [=] (CreatureID newID) { LOCPLINT->cb->upgradeCreature(getObj(), ID, newID); };
|
2017-07-17 14:35:57 +02:00
|
|
|
if(canDismiss) dism = [=](){ LOCPLINT->cb->dismissCreature(getObj(), ID); };
|
2014-07-13 16:11:25 +03:00
|
|
|
|
2015-08-24 22:39:56 +02:00
|
|
|
owner->selectSlot(nullptr);
|
|
|
|
owner->setSplittingMode(false);
|
2014-07-13 16:11:25 +03:00
|
|
|
|
2015-08-24 22:39:56 +02:00
|
|
|
for(auto & elem : owner->splitButtons)
|
|
|
|
elem->block(true);
|
2014-07-13 16:11:25 +03:00
|
|
|
|
2015-08-24 22:39:56 +02:00
|
|
|
redraw();
|
|
|
|
GH.pushInt(new CStackWindow(myStack, dism, pom, upgr));
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-13 16:11:25 +03:00
|
|
|
|
2015-08-25 00:41:41 +02:00
|
|
|
/// The selection is empty, therefore the creature should be moved
|
|
|
|
/// @return Whether the view should be refreshed
|
2015-08-24 22:39:56 +02:00
|
|
|
bool CGarrisonSlot::highlightOrDropArtifact()
|
|
|
|
{
|
|
|
|
bool artSelected = false;
|
|
|
|
if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution
|
|
|
|
{
|
2016-08-30 06:05:31 +02:00
|
|
|
const std::shared_ptr<CArtifactsOfHero::SCommonPart> commonInfo = chw->artSets.front()->commonInfo;
|
2015-08-24 22:39:56 +02:00
|
|
|
if (const CArtifactInstance *art = commonInfo->src.art)
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2015-08-24 22:39:56 +02:00
|
|
|
const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
|
|
|
|
artSelected = true;
|
|
|
|
if (myStack) // try dropping the artifact only if the slot isn't empty
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2015-08-24 22:39:56 +02:00
|
|
|
ArtifactLocation src(srcHero, commonInfo->src.slotID);
|
|
|
|
ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
|
|
|
|
if (art->canBePutAt(dst, true))
|
|
|
|
{ //equip clicked stack
|
|
|
|
if(dst.getArt())
|
2015-01-24 01:21:18 +02:00
|
|
|
{
|
2015-08-24 22:39:56 +02:00
|
|
|
//creature can wear only one active artifact
|
|
|
|
//if we are placing a new one, the old one will be returned to the hero's backpack
|
|
|
|
LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(srcHero, dst.getArt()->firstBackpackSlot(srcHero)));
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
2015-08-24 22:39:56 +02:00
|
|
|
LOCPLINT->cb->swapArtifacts(src, dst);
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 22:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!artSelected && creature)
|
|
|
|
{
|
|
|
|
owner->selectSlot(this);
|
|
|
|
if(creature)
|
|
|
|
{
|
|
|
|
for(auto & elem : owner->splitButtons)
|
|
|
|
elem->block(!our());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-25 00:41:41 +02:00
|
|
|
/// The creature is only being partially moved
|
|
|
|
/// @return Whether the view should be refreshed
|
2015-08-24 22:39:56 +02:00
|
|
|
bool CGarrisonSlot::split()
|
|
|
|
{
|
|
|
|
const CGarrisonSlot * selection = owner->getSelection();
|
|
|
|
owner->p2 = ID; // store the second stack pos
|
|
|
|
owner->pb = upg; // store the second stack owner (up or down army)
|
|
|
|
owner->setSplittingMode(false);
|
|
|
|
|
|
|
|
int minLeft=0, minRight=0;
|
|
|
|
|
|
|
|
if(upg != selection->upg) // not splitting within same army
|
|
|
|
{
|
|
|
|
if(selection->getObj()->stacksCount() == 1 // we're splitting away the last stack
|
|
|
|
&& selection->getObj()->needsLastStack() )
|
|
|
|
{
|
|
|
|
minLeft = 1;
|
|
|
|
}
|
|
|
|
// destination army can't be emptied, unless we're rebalancing two stacks of same creature
|
|
|
|
if(getObj()->stacksCount() == 1
|
|
|
|
&& selection->creature == creature
|
|
|
|
&& getObj()->needsLastStack() )
|
|
|
|
{
|
|
|
|
minRight = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int countLeft = selection->myStack ? selection->myStack->count : 0;
|
|
|
|
int countRight = myStack ? myStack->count : 0;
|
|
|
|
|
|
|
|
GH.pushInt(new CSplitWindow(selection->creature, std::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
|
|
|
|
minLeft, minRight, countLeft, countRight));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-25 00:41:41 +02:00
|
|
|
/// If certain creates cannot be moved, the selection should change
|
|
|
|
/// Force reselection in these cases
|
2015-08-25 02:43:10 +02:00
|
|
|
/// * When attempting to take creatures from ally
|
2015-08-25 00:41:41 +02:00
|
|
|
/// * When attempting to swap creatures with an ally
|
|
|
|
/// * When attempting to take unremovable units
|
|
|
|
/// @return Whether reselection must be done
|
|
|
|
bool CGarrisonSlot::mustForceReselection() const
|
|
|
|
{
|
|
|
|
const CGarrisonSlot * selection = owner->getSelection();
|
2015-08-25 02:43:10 +02:00
|
|
|
bool withAlly = selection->our() ^ our();
|
|
|
|
if (!creature || !selection->creature)
|
|
|
|
return false;
|
|
|
|
// Attempt to take creatures from ally (select theirs first)
|
|
|
|
if (!selection->our())
|
|
|
|
return true;
|
|
|
|
// Attempt to swap creatures with ally (select ours first)
|
|
|
|
if (selection->creature != creature && withAlly)
|
2015-08-25 00:41:41 +02:00
|
|
|
return true;
|
|
|
|
if (!owner->removableUnits)
|
|
|
|
{
|
|
|
|
if (selection->upg == EGarrisonType::UP)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return creature || upg == EGarrisonType::UP;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonSlot::clickRight(tribool down, bool previousState)
|
|
|
|
{
|
2016-11-28 01:09:24 +02:00
|
|
|
if(creature && down)
|
2015-08-25 00:41:41 +02:00
|
|
|
{
|
|
|
|
GH.pushInt(new CStackWindow(myStack, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:39:56 +02:00
|
|
|
void CGarrisonSlot::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if(down)
|
|
|
|
{
|
|
|
|
bool refr = false;
|
|
|
|
const CGarrisonSlot * selection = owner->getSelection();
|
|
|
|
if(!selection)
|
|
|
|
refr = highlightOrDropArtifact();
|
|
|
|
else if(selection == this)
|
|
|
|
refr = viewInfo();
|
2015-08-25 00:41:41 +02:00
|
|
|
// Re-highlight if troops aren't removable or not ours.
|
|
|
|
else if (mustForceReselection())
|
2015-08-24 22:39:56 +02:00
|
|
|
{
|
|
|
|
if(creature)
|
2014-07-13 16:11:25 +03:00
|
|
|
owner->selectSlot(this);
|
|
|
|
redraw();
|
|
|
|
refr = true;
|
|
|
|
}
|
2015-08-24 22:39:56 +02:00
|
|
|
else
|
2015-08-25 02:43:52 +02:00
|
|
|
{
|
|
|
|
const CArmedInstance * selectedObj = owner->armedObjs[selection->upg];
|
2015-09-23 07:12:43 +02:00
|
|
|
bool lastHeroStackSelected = false;
|
|
|
|
if(selectedObj->stacksCount() == 1
|
|
|
|
&& owner->getSelection()->upg != upg
|
|
|
|
&& dynamic_cast<const CGHeroInstance*>(selectedObj))
|
|
|
|
{
|
|
|
|
lastHeroStackSelected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((owner->getSplittingMode() || LOCPLINT->shiftPressed()) // split window
|
|
|
|
&& (!creature || creature == selection->creature))
|
|
|
|
{
|
|
|
|
refr = split();
|
|
|
|
}
|
|
|
|
else if(!creature && lastHeroStackSelected) // split all except last creature
|
|
|
|
LOCPLINT->cb->splitStack(selectedObj, owner->armedObjs[upg], selection->ID, ID, selection->myStack->count - 1);
|
|
|
|
else if(creature != selection->creature) // swap
|
|
|
|
LOCPLINT->cb->swapCreatures(owner->armedObjs[upg], selectedObj, ID, selection->ID);
|
|
|
|
else if(lastHeroStackSelected) // merge last stack to other hero stack
|
|
|
|
refr = split();
|
|
|
|
else // merge
|
|
|
|
LOCPLINT->cb->mergeStacks(selectedObj, owner->armedObjs[upg], selection->ID, ID);
|
2015-08-25 02:43:52 +02:00
|
|
|
}
|
2015-08-24 23:40:12 +02:00
|
|
|
if(refr)
|
|
|
|
{
|
|
|
|
// Refresh Statusbar
|
|
|
|
hover(false);
|
|
|
|
hover(true);
|
|
|
|
}
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonSlot::update()
|
|
|
|
{
|
|
|
|
if (getObj() != nullptr)
|
|
|
|
{
|
|
|
|
addUsedEvents(LCLICK | RCLICK | HOVER);
|
|
|
|
myStack = getObj()->getStackPtr(ID);
|
|
|
|
creature = myStack ? myStack->type : nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removeUsedEvents(LCLICK | RCLICK | HOVER);
|
|
|
|
myStack = nullptr;
|
|
|
|
creature = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creature)
|
|
|
|
{
|
|
|
|
creatureImage->enable();
|
|
|
|
creatureImage->setFrame(creature->iconIndex);
|
|
|
|
|
|
|
|
stackCount->enable();
|
|
|
|
stackCount->setText(boost::lexical_cast<std::string>(myStack->count));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
creatureImage->disable();
|
|
|
|
stackCount->disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 23:40:12 +02:00
|
|
|
CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, CGarrisonSlot::EGarrisonType Upg, const CStackInstance * Creature):
|
2014-07-13 16:11:25 +03:00
|
|
|
ID(IID),
|
|
|
|
owner(Owner),
|
|
|
|
myStack(Creature),
|
|
|
|
creature(Creature ? Creature->type : nullptr),
|
|
|
|
upg(Upg)
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
|
|
|
if (getObj())
|
|
|
|
addUsedEvents(LCLICK | RCLICK | HOVER);
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
|
|
|
|
std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
|
|
|
|
|
|
|
|
creatureImage = new CAnimImage(imgName, creature ? creature->iconIndex : 0);
|
|
|
|
if (!creature)
|
|
|
|
creatureImage->disable();
|
|
|
|
|
|
|
|
selectionImage = new CAnimImage(imgName, 1);
|
|
|
|
selectionImage->disable();
|
|
|
|
|
|
|
|
if(Owner->smallIcons)
|
|
|
|
{
|
|
|
|
pos.w = 32;
|
|
|
|
pos.h = 32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos.w = 58;
|
|
|
|
pos.h = 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
stackCount = new CLabel(pos.w, pos.h, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, BOTTOMRIGHT, Colors::WHITE);
|
|
|
|
if (!creature)
|
|
|
|
stackCount->disable();
|
|
|
|
else
|
|
|
|
stackCount->setText(boost::lexical_cast<std::string>(myStack->count));
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CGarrisonInt::addSplitBtn(CButton * button)
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
|
|
|
addChild(button);
|
|
|
|
button->recActions = defActions;
|
|
|
|
splitButtons.push_back(button);
|
|
|
|
button->block(getSelection() == nullptr);
|
|
|
|
}
|
|
|
|
|
2017-03-18 15:21:41 +02:00
|
|
|
void CGarrisonInt::createSlots()
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2017-03-18 15:21:41 +02:00
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
|
|
|
int distance = interx + (smallIcons? 32 : 58);
|
|
|
|
for(int i=0; i<2; i++)
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2017-03-18 15:21:41 +02:00
|
|
|
std::vector<CGarrisonSlot*> garrisonSlots;
|
|
|
|
garrisonSlots.resize(7);
|
|
|
|
if (armedObjs[i])
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2017-03-18 15:21:41 +02:00
|
|
|
for(auto & elem : armedObjs[i]->Slots())
|
|
|
|
{
|
|
|
|
garrisonSlots[elem.first.getNum()] = new CGarrisonSlot(this, i*garOffset.x + (elem.first.getNum()*distance), i*garOffset.y, elem.first, static_cast<CGarrisonSlot::EGarrisonType>(i), elem.second);
|
|
|
|
}
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
2017-03-18 15:21:41 +02:00
|
|
|
for(int j=0; j<7; j++)
|
2014-07-13 16:11:25 +03:00
|
|
|
{
|
2017-03-18 15:21:41 +02:00
|
|
|
if(!garrisonSlots[j])
|
|
|
|
garrisonSlots[j] = new CGarrisonSlot(this, i*garOffset.x + (j*distance), i*garOffset.y, SlotID(j), static_cast<CGarrisonSlot::EGarrisonType>(i), nullptr);
|
|
|
|
if (twoRows && j>=4)
|
|
|
|
{
|
2017-05-22 22:34:05 +02:00
|
|
|
garrisonSlots[j]->moveBy(Point(-126, 37));
|
2017-03-18 15:21:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::copy(garrisonSlots.begin(), garrisonSlots.end(), std::back_inserter(availableSlots));
|
|
|
|
}
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonInt::recreateSlots()
|
|
|
|
{
|
|
|
|
selectSlot(nullptr);
|
|
|
|
setSplittingMode(false);
|
|
|
|
|
|
|
|
for(auto & elem : splitButtons)
|
|
|
|
elem->block(true);
|
|
|
|
|
|
|
|
|
2017-03-18 15:21:41 +02:00
|
|
|
for(CGarrisonSlot * slot : availableSlots)
|
2014-07-13 16:11:25 +03:00
|
|
|
slot->update();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonInt::splitClick()
|
|
|
|
{
|
|
|
|
if(!getSelection())
|
|
|
|
return;
|
|
|
|
setSplittingMode(!getSplittingMode());
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
void CGarrisonInt::splitStacks(int, int amountRight)
|
|
|
|
{
|
|
|
|
LOCPLINT->cb->splitStack(armedObjs[getSelection()->upg], armedObjs[pb], getSelection()->ID, p2, amountRight);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
|
|
|
|
SDL_Surface *pomsur, const Point& SurOffset,
|
|
|
|
const CArmedInstance *s1, const CArmedInstance *s2,
|
|
|
|
bool _removableUnits, bool smallImgs, bool _twoRows ) :
|
|
|
|
highlighted(nullptr),
|
|
|
|
inSplittingMode(false),
|
|
|
|
interx(inx),
|
|
|
|
garOffset(garsOffset),
|
2016-11-27 17:17:20 +02:00
|
|
|
pb(false),
|
2014-07-13 16:11:25 +03:00
|
|
|
smallIcons(smallImgs),
|
|
|
|
removableUnits(_removableUnits),
|
|
|
|
twoRows(_twoRows)
|
|
|
|
{
|
|
|
|
setArmy(s1, false);
|
|
|
|
setArmy(s2, true);
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
createSlots();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGarrisonSlot * CGarrisonInt::getSelection()
|
|
|
|
{
|
|
|
|
return highlighted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
|
|
|
|
{
|
|
|
|
if (slot != highlighted)
|
|
|
|
{
|
|
|
|
if (highlighted)
|
|
|
|
highlighted->setHighlight(false);
|
|
|
|
|
|
|
|
highlighted = slot;
|
|
|
|
for (auto button : splitButtons)
|
2015-08-24 21:20:24 +02:00
|
|
|
button->block(highlighted == nullptr || !slot->our());
|
2014-07-13 16:11:25 +03:00
|
|
|
|
|
|
|
if (highlighted)
|
|
|
|
highlighted->setHighlight(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonInt::setSplittingMode(bool on)
|
|
|
|
{
|
|
|
|
assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
|
|
|
|
|
|
|
|
if (inSplittingMode || on)
|
|
|
|
{
|
2017-03-18 15:21:41 +02:00
|
|
|
for(CGarrisonSlot * slot : availableSlots)
|
2017-03-18 08:58:40 +02:00
|
|
|
{
|
2017-03-17 22:24:55 +02:00
|
|
|
if(slot!=getSelection())
|
|
|
|
slot->setHighlight( ( on && (slot->our() || slot->ally()) && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
|
|
|
|
}
|
2014-07-13 16:11:25 +03:00
|
|
|
inSplittingMode = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGarrisonInt::getSplittingMode()
|
|
|
|
{
|
|
|
|
return inSplittingMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
|
|
|
|
{
|
|
|
|
owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
|
|
|
|
armedObjs[bottomGarrison] = army;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits ):
|
|
|
|
CWindowObject(PLAYER_COLORED, "GARRISON")
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
|
|
|
|
|
|
|
garr = new CGarrisonInt(92, 127, 4, Point(0,96), background->bg, Point(93,127), up, down, removableUnits);
|
|
|
|
{
|
2017-07-17 14:35:57 +02:00
|
|
|
CButton *split = new CButton(Point(88, 314), "IDV6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3], ""), [&](){ garr->splitClick(); } );
|
2014-07-13 16:11:25 +03:00
|
|
|
removeChild(split);
|
|
|
|
garr->addSplitBtn(split);
|
|
|
|
}
|
2017-07-17 14:35:57 +02:00
|
|
|
quit = new CButton(Point(399, 314), "IOK6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[8], ""), [&](){ close(); }, SDLK_RETURN);
|
2014-07-13 16:11:25 +03:00
|
|
|
|
|
|
|
std::string titleText;
|
2016-11-28 01:47:40 +02:00
|
|
|
if (down->tempOwner == up->tempOwner)
|
2014-07-13 16:11:25 +03:00
|
|
|
titleText = CGI->generaltexth->allTexts[709];
|
|
|
|
else
|
|
|
|
{
|
2016-11-28 01:47:40 +02:00
|
|
|
//assume that this is joining monsters dialog
|
|
|
|
if(up->Slots().size() > 0)
|
|
|
|
{
|
|
|
|
titleText = CGI->generaltexth->allTexts[35];
|
|
|
|
boost::algorithm::replace_first(titleText, "%s", up->Slots().begin()->second->type->namePl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logGlobal->error("Invalid armed instance for garrison window.");
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
new CLabel(275, 30, FONT_BIG, CENTER, Colors::YELLOW, titleText);
|
|
|
|
|
2016-11-28 01:47:40 +02:00
|
|
|
new CAnimImage("CREST58", up->getOwner().getNum(), 0, 28, 124);
|
|
|
|
new CAnimImage("PortraitsLarge", down->portrait, 0, 29, 222);
|
2014-07-13 16:11:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CGarrisonHolder::CGarrisonHolder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWindowWithGarrison::updateGarrisons()
|
|
|
|
{
|
|
|
|
garr->recreateSlots();
|
|
|
|
}
|