QuickRecruitmentWindow.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * QuickRecruitmentWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "QuickRecruitmentWindow.h"
  12. #include "../../lib/mapObjects/CGTownInstance.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../widgets/Buttons.h"
  15. #include "../widgets/CreatureCostBox.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../../CCallback.h"
  18. #include "../../lib/ResourceSet.h"
  19. #include "../../lib/CCreatureHandler.h"
  20. #include "CreaturePurchaseCard.h"
  21. void QuickRecruitmentWindow::setButtons()
  22. {
  23. setCancelButton();
  24. setBuyButton();
  25. setMaxButton();
  26. }
  27. void QuickRecruitmentWindow::setCancelButton()
  28. {
  29. cancelButton = std::make_shared<CButton>(Point((pos.w / 2) + 48, 418), "ICN6432.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_ESCAPE);
  30. cancelButton->setImageOrder(0, 1, 2, 3);
  31. }
  32. void QuickRecruitmentWindow::setBuyButton()
  33. {
  34. buyButton = std::make_shared<CButton>(Point((pos.w / 2) - 32, 418), "IBY6432.DEF", CButton::tooltip(), [&](){ purchaseUnits(); }, SDLK_RETURN);
  35. cancelButton->assignedKeys.insert(SDLK_ESCAPE);
  36. buyButton->setImageOrder(0, 1, 2, 3);
  37. }
  38. void QuickRecruitmentWindow::setMaxButton()
  39. {
  40. maxButton = std::make_shared<CButton>(Point((pos.w/2)-112, 418), "IRCBTNS.DEF", CButton::tooltip(), [&](){ maxAllCards(cards); }, SDLK_m);
  41. maxButton->setImageOrder(0, 1, 2, 3);
  42. }
  43. void QuickRecruitmentWindow::setCreaturePurchaseCards()
  44. {
  45. int availableAmount = getAvailableCreatures();
  46. Point position = Point((pos.w - 100*availableAmount - 8*(availableAmount-1))/2,64);
  47. for (int i = 0; i < GameConstants::CREATURES_PER_TOWN; i++)
  48. {
  49. if(!town->town->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
  50. {
  51. cards.push_back(std::make_shared<CreaturePurchaseCard>(town->creatures[i].second, position, town->creatures[i].first, this));
  52. position.x += 108;
  53. }
  54. }
  55. totalCost = std::make_shared<CreatureCostBox>(Rect((this->pos.w/2)-45, position.y+260, 97, 74), "");
  56. }
  57. void QuickRecruitmentWindow::initWindow(Rect startupPosition)
  58. {
  59. pos.x = startupPosition.x + 238;
  60. pos.y = startupPosition.y + 45;
  61. pos.w = 332;
  62. pos.h = 461;
  63. int creaturesAmount = getAvailableCreatures();
  64. if(creaturesAmount > 3)
  65. {
  66. pos.w += 108 * (creaturesAmount - 3);
  67. pos.x -= 55 * (creaturesAmount - 3);
  68. }
  69. backgroundTexture = std::make_shared<CFilledTexture>("DIBOXBCK.pcx", Rect(0, 0, pos.w, pos.h));
  70. costBackground = std::make_shared<CPicture>("QuickRecruitmentWindow/costBackground.png", pos.w/2-113, 335);
  71. }
  72. void QuickRecruitmentWindow::maxAllCards(std::vector<std::shared_ptr<CreaturePurchaseCard> > cards)
  73. {
  74. auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
  75. for(auto i : boost::adaptors::reverse(cards))
  76. {
  77. si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
  78. vstd::amin(maxAmount, i->maxAmount);
  79. i->slider->setAmount(maxAmount);
  80. if(i->slider->getValue() != maxAmount)
  81. i->slider->moveTo(maxAmount);
  82. else
  83. i->sliderMoved(maxAmount);
  84. i->slider->moveToMax();
  85. allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * maxAmount);
  86. }
  87. maxButton->block(allAvailableResources == LOCPLINT->cb->getResourceAmount());
  88. }
  89. void QuickRecruitmentWindow::purchaseUnits()
  90. {
  91. for(auto selected : boost::adaptors::reverse(cards))
  92. {
  93. if(selected->slider->getValue())
  94. {
  95. auto onRecruit = [=](CreatureID id, int count){ LOCPLINT->cb->recruitCreatures(town, town->getUpperArmy(), id, count, selected->creatureOnTheCard->getLevel()-1); };
  96. CreatureID crid = selected->creatureOnTheCard->getId();
  97. SlotID dstslot = town -> getSlotFor(crid);
  98. if(!dstslot.validSlot())
  99. continue;
  100. onRecruit(crid, selected->slider->getValue());
  101. }
  102. }
  103. close();
  104. }
  105. int QuickRecruitmentWindow::getAvailableCreatures()
  106. {
  107. int creaturesAmount = 0;
  108. for (int i=0; i< GameConstants::CREATURES_PER_TOWN; i++)
  109. if(!town->town->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
  110. creaturesAmount++;
  111. return creaturesAmount;
  112. }
  113. void QuickRecruitmentWindow::updateAllSliders()
  114. {
  115. auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
  116. for(auto i : boost::adaptors::reverse(cards))
  117. allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * i->slider->getValue());
  118. for(auto i : cards)
  119. {
  120. si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
  121. vstd::amin(maxAmount, i->maxAmount);
  122. if(maxAmount < 0)
  123. continue;
  124. if(i->slider->getValue() + maxAmount < i->maxAmount)
  125. i->slider->setAmount(i->slider->getValue() + maxAmount);
  126. else
  127. i->slider->setAmount(i->maxAmount);
  128. i->slider->moveTo(i->slider->getValue());
  129. }
  130. totalCost->createItems(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
  131. totalCost->set(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
  132. }
  133. QuickRecruitmentWindow::QuickRecruitmentWindow(const CGTownInstance * townd, Rect startupPosition)
  134. : CWindowObject(PLAYER_COLORED | BORDERED),
  135. town(townd)
  136. {
  137. OBJECT_CONSTRUCTION_CAPTURING(ACTIVATE + DEACTIVATE + UPDATE + SHOWALL);
  138. initWindow(startupPosition);
  139. setButtons();
  140. setCreaturePurchaseCards();
  141. maxAllCards(cards);
  142. }