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"
|
2023-05-30 16:31:45 +02:00
|
|
|
#include "../widgets/Slider.h"
|
2017-11-01 15:58:49 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-04-27 19:21:06 +02:00
|
|
|
#include "../gui/Shortcut.h"
|
2017-11-01 15:58:49 +02:00
|
|
|
#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()
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
cancelButton = std::make_shared<CButton>(Point((pos.w / 2) + 48, 418), AnimationPath::builtin("ICN6432.DEF"), CButton::tooltip(), [&](){ close(); }, EShortcut::GLOBAL_CANCEL);
|
2017-11-01 15:58:49 +02:00
|
|
|
cancelButton->setImageOrder(0, 1, 2, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::setBuyButton()
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
buyButton = std::make_shared<CButton>(Point((pos.w / 2) - 32, 418), AnimationPath::builtin("IBY6432.DEF"), CButton::tooltip(), [&](){ purchaseUnits(); }, EShortcut::GLOBAL_ACCEPT);
|
2017-11-01 15:58:49 +02:00
|
|
|
buyButton->setImageOrder(0, 1, 2, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::setMaxButton()
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
maxButton = std::make_shared<CButton>(Point((pos.w/2)-112, 418), AnimationPath::builtin("IRCBTNS.DEF"), CButton::tooltip(), [&](){ maxAllCards(cards); }, EShortcut::RECRUITMENT_MAX);
|
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);
|
2024-08-05 23:03:19 +02:00
|
|
|
for (int i = 0; i < town->town->creatures.size(); 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);
|
|
|
|
}
|
2023-08-23 14:07:50 +02:00
|
|
|
backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK.pcx"), Rect(0, 0, pos.w, pos.h));
|
|
|
|
costBackground = std::make_shared<CPicture>(ImagePath::builtin("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)
|
2023-05-30 17:10:22 +02:00
|
|
|
i->slider->scrollTo(maxAmount);
|
2017-11-01 15:58:49 +02:00
|
|
|
else
|
|
|
|
i->sliderMoved(maxAmount);
|
|
|
|
|
2023-05-30 17:10:22 +02:00
|
|
|
i->slider->scrollToMax();
|
2023-04-05 02:26:29 +02:00
|
|
|
allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * 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())
|
|
|
|
{
|
2024-08-06 01:02:38 +02:00
|
|
|
int level = 0;
|
|
|
|
int i = 0;
|
|
|
|
for(auto c : town->town->creatures)
|
|
|
|
{
|
|
|
|
for(auto c2 : c)
|
|
|
|
if(c2 == selected->creatureOnTheCard->getId())
|
|
|
|
level = i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
auto onRecruit = [=](CreatureID id, int count){ LOCPLINT->cb->recruitCreatures(town, town->getUpperArmy(), id, count, level); };
|
2023-04-05 02:26:29 +02:00
|
|
|
CreatureID crid = selected->creatureOnTheCard->getId();
|
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;
|
2024-08-05 23:03:19 +02:00
|
|
|
for (int i=0; i< town->town->creatures.size(); 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)
|
|
|
|
creaturesAmount++;
|
|
|
|
return creaturesAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickRecruitmentWindow::updateAllSliders()
|
|
|
|
{
|
|
|
|
auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
|
|
|
|
for(auto i : boost::adaptors::reverse(cards))
|
2023-04-05 02:26:29 +02:00
|
|
|
allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * 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);
|
2023-05-30 17:10:22 +02:00
|
|
|
i->slider->scrollTo(i->slider->getValue());
|
2017-11-01 15:58:49 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2017-11-14 09:50:04 +02:00
|
|
|
|
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);
|
2024-10-07 18:01:36 +02:00
|
|
|
|
|
|
|
center();
|
2017-11-01 15:58:49 +02:00
|
|
|
}
|