2017-11-01 15:58:49 +02:00
|
|
|
/*
|
|
|
|
* QuickRecruitmentWindow.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "QuickRecruitmentWindow.h"
|
|
|
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../widgets/CreatureCostBox.h"
|
2017-11-01 15:58:49 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../../CCallback.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../../lib/ResourceSet.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
2017-12-20 21:08:21 +02:00
|
|
|
#include "CreaturePurchaseCard.h"
|
2017-11-01 15:58:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::setButtons()
|
|
|
|
{
|
|
|
|
setCancelButton();
|
|
|
|
setBuyButton();
|
|
|
|
setMaxButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::setCancelButton()
|
|
|
|
{
|
2022-09-10 17:09:19 +02:00
|
|
|
cancelButton = std::make_shared<CButton>(Point((pos.w / 2) + 48, 418), "ICN6432.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_ESCAPE);
|
2017-11-01 15:58:49 +02:00
|
|
|
cancelButton->setImageOrder(0, 1, 2, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::setBuyButton()
|
|
|
|
{
|
2022-09-17 21:22:09 +02:00
|
|
|
buyButton = std::make_shared<CButton>(Point((pos.w / 2) - 32, 418), "IBY6432.DEF", CButton::tooltip(), [&](){ purchaseUnits(); }, SDLK_RETURN);
|
2017-11-01 15:58:49 +02:00
|
|
|
cancelButton->assignedKeys.insert(SDLK_ESCAPE);
|
|
|
|
buyButton->setImageOrder(0, 1, 2, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::setMaxButton()
|
|
|
|
{
|
2022-09-10 17:09:19 +02:00
|
|
|
maxButton = std::make_shared<CButton>(Point((pos.w/2)-112, 418), "IRCBTNS.DEF", CButton::tooltip(), [&](){ maxAllCards(cards); }, SDLK_m);
|
2017-11-01 15:58:49 +02:00
|
|
|
maxButton->setImageOrder(0, 1, 2, 3);
|
|
|
|
}
|
|
|
|
|
2022-09-17 14:44:13 +02:00
|
|
|
void QuickRecruitmentWindow::setCreaturePurchaseCards()
|
2017-11-01 15:58:49 +02:00
|
|
|
{
|
2017-11-15 20:44:54 +02:00
|
|
|
int availableAmount = getAvailableCreatures();
|
|
|
|
Point position = Point((pos.w - 100*availableAmount - 8*(availableAmount-1))/2,64);
|
|
|
|
for (int i = 0; i < GameConstants::CREATURES_PER_TOWN; i++)
|
2017-11-01 15:58:49 +02:00
|
|
|
{
|
|
|
|
if(!town->town->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
|
|
|
|
{
|
2017-12-20 21:08:21 +02:00
|
|
|
cards.push_back(std::make_shared<CreaturePurchaseCard>(town->creatures[i].second, position, town->creatures[i].first, this));
|
2017-11-15 20:44:54 +02:00
|
|
|
position.x += 108;
|
2017-11-01 15:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 20:44:54 +02:00
|
|
|
totalCost = std::make_shared<CreatureCostBox>(Rect((this->pos.w/2)-45, position.y+260, 97, 74), "");
|
2017-11-01 15:58:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 20:44:54 +02:00
|
|
|
void QuickRecruitmentWindow::initWindow(Rect startupPosition)
|
2017-11-01 15:58:49 +02:00
|
|
|
{
|
2017-11-15 20:44:54 +02:00
|
|
|
pos.x = startupPosition.x + 238;
|
|
|
|
pos.y = startupPosition.y + 45;
|
2017-11-01 15:58:49 +02:00
|
|
|
pos.w = 332;
|
|
|
|
pos.h = 461;
|
|
|
|
int creaturesAmount = getAvailableCreatures();
|
|
|
|
if(creaturesAmount > 3)
|
|
|
|
{
|
|
|
|
pos.w += 108 * (creaturesAmount - 3);
|
|
|
|
pos.x -= 55 * (creaturesAmount - 3);
|
|
|
|
}
|
|
|
|
backgroundTexture = std::make_shared<CFilledTexture>("DIBOXBCK.pcx", Rect(0, 0, pos.w, pos.h));
|
2018-01-15 19:00:41 +02:00
|
|
|
costBackground = std::make_shared<CPicture>("QuickRecruitmentWindow/costBackground.png", pos.w/2-113, 335);
|
2017-11-01 15:58:49 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 21:08:21 +02:00
|
|
|
void QuickRecruitmentWindow::maxAllCards(std::vector<std::shared_ptr<CreaturePurchaseCard> > cards)
|
2017-11-01 15:58:49 +02:00
|
|
|
{
|
|
|
|
auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
|
|
|
|
for(auto i : boost::adaptors::reverse(cards))
|
|
|
|
{
|
2017-11-15 20:44:54 +02:00
|
|
|
si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
|
|
|
|
vstd::amin(maxAmount, i->maxAmount);
|
2017-11-01 15:58:49 +02:00
|
|
|
|
|
|
|
i->slider->setAmount(maxAmount);
|
|
|
|
|
|
|
|
if(i->slider->getValue() != maxAmount)
|
|
|
|
i->slider->moveTo(maxAmount);
|
|
|
|
else
|
|
|
|
i->sliderMoved(maxAmount);
|
|
|
|
|
|
|
|
i->slider->moveToMax();
|
2017-11-15 20:44:54 +02:00
|
|
|
allAvailableResources -= (i->creatureOnTheCard->cost * maxAmount);
|
2017-11-01 15:58:49 +02:00
|
|
|
}
|
|
|
|
maxButton->block(allAvailableResources == LOCPLINT->cb->getResourceAmount());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-17 14:44:13 +02:00
|
|
|
void QuickRecruitmentWindow::purchaseUnits()
|
2017-11-01 15:58:49 +02:00
|
|
|
{
|
2023-01-19 19:35:32 +02:00
|
|
|
for(auto selected : boost::adaptors::reverse(cards))
|
2017-11-01 15:58:49 +02:00
|
|
|
{
|
|
|
|
if(selected->slider->getValue())
|
|
|
|
{
|
2017-11-15 20:44:54 +02:00
|
|
|
auto onRecruit = [=](CreatureID id, int count){ LOCPLINT->cb->recruitCreatures(town, town->getUpperArmy(), id, count, selected->creatureOnTheCard->level-1); };
|
|
|
|
CreatureID crid = selected->creatureOnTheCard->idNumber;
|
2017-11-01 15:58:49 +02:00
|
|
|
SlotID dstslot = town -> getSlotFor(crid);
|
|
|
|
if(!dstslot.validSlot())
|
|
|
|
continue;
|
|
|
|
onRecruit(crid, selected->slider->getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
int QuickRecruitmentWindow::getAvailableCreatures()
|
|
|
|
{
|
|
|
|
int creaturesAmount = 0;
|
|
|
|
for (int i=0; i< GameConstants::CREATURES_PER_TOWN; i++)
|
|
|
|
if(!town->town->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
|
|
|
|
creaturesAmount++;
|
|
|
|
return creaturesAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::updateAllSliders()
|
|
|
|
{
|
|
|
|
auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
|
|
|
|
for(auto i : boost::adaptors::reverse(cards))
|
2017-11-15 20:44:54 +02:00
|
|
|
allAvailableResources -= (i->creatureOnTheCard->cost * i->slider->getValue());
|
2017-11-01 15:58:49 +02:00
|
|
|
for(auto i : cards)
|
|
|
|
{
|
2017-11-15 20:44:54 +02:00
|
|
|
si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
|
|
|
|
vstd::amin(maxAmount, i->maxAmount);
|
2017-11-01 15:58:49 +02:00
|
|
|
if(maxAmount < 0)
|
|
|
|
continue;
|
2017-11-15 20:44:54 +02:00
|
|
|
if(i->slider->getValue() + maxAmount < i->maxAmount)
|
2017-11-01 15:58:49 +02:00
|
|
|
i->slider->setAmount(i->slider->getValue() + maxAmount);
|
|
|
|
else
|
2017-11-15 20:44:54 +02:00
|
|
|
i->slider->setAmount(i->maxAmount);
|
2017-11-01 15:58:49 +02:00
|
|
|
i->slider->moveTo(i->slider->getValue());
|
|
|
|
}
|
|
|
|
totalCost->createItems(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
|
|
|
|
totalCost->set(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
|
|
|
|
}
|
|
|
|
|
2017-11-15 20:44:54 +02:00
|
|
|
QuickRecruitmentWindow::QuickRecruitmentWindow(const CGTownInstance * townd, Rect startupPosition)
|
2017-12-21 20:27:39 +02:00
|
|
|
: CWindowObject(PLAYER_COLORED | BORDERED),
|
|
|
|
town(townd)
|
2017-11-01 15:58:49 +02:00
|
|
|
{
|
2017-11-14 09:50:04 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(ACTIVATE + DEACTIVATE + UPDATE + SHOWALL);
|
|
|
|
|
2017-11-15 20:44:54 +02:00
|
|
|
initWindow(startupPosition);
|
2017-11-01 15:58:49 +02:00
|
|
|
setButtons();
|
2022-09-17 14:44:13 +02:00
|
|
|
setCreaturePurchaseCards();
|
2017-11-01 15:58:49 +02:00
|
|
|
maxAllCards(cards);
|
|
|
|
}
|