QuickRecruitmentWindow.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "../widgets/Slider.h"
  17. #include "../GameEngine.h"
  18. #include "../GameInstance.h"
  19. #include "../gui/Shortcut.h"
  20. #include "../../lib/callback/CCallback.h"
  21. #include "../../lib/ResourceSet.h"
  22. #include "../../lib/CCreatureHandler.h"
  23. #include "CreaturePurchaseCard.h"
  24. void QuickRecruitmentWindow::setButtons()
  25. {
  26. setCancelButton();
  27. setBuyButton();
  28. setMaxButton();
  29. }
  30. void QuickRecruitmentWindow::setCancelButton()
  31. {
  32. cancelButton = std::make_shared<CButton>(Point((pos.w / 2) + 48, 418), AnimationPath::builtin("ICN6432.DEF"), CButton::tooltip(), [&](){ close(); }, EShortcut::GLOBAL_CANCEL);
  33. cancelButton->setImageOrder(0, 1, 2, 3);
  34. }
  35. void QuickRecruitmentWindow::setBuyButton()
  36. {
  37. buyButton = std::make_shared<CButton>(Point((pos.w / 2) - 32, 418), AnimationPath::builtin("IBY6432.DEF"), CButton::tooltip(), [&](){ purchaseUnits(); }, EShortcut::GLOBAL_ACCEPT);
  38. buyButton->setImageOrder(0, 1, 2, 3);
  39. }
  40. void QuickRecruitmentWindow::setMaxButton()
  41. {
  42. maxButton = std::make_shared<CButton>(Point((pos.w/2)-112, 418), AnimationPath::builtin("IRCBTNS.DEF"), CButton::tooltip(), [&](){ maxAllCards(cards); }, EShortcut::RECRUITMENT_MAX);
  43. maxButton->setImageOrder(0, 1, 2, 3);
  44. }
  45. void QuickRecruitmentWindow::setCreaturePurchaseCards()
  46. {
  47. int availableAmount = getAvailableCreatures();
  48. Point position = Point((pos.w - 100*availableAmount - 8*(availableAmount-1))/2,64);
  49. for (int i = 0; i < town->getTown()->creatures.size(); i++)
  50. {
  51. if(!town->getTown()->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
  52. {
  53. cards.push_back(std::make_shared<CreaturePurchaseCard>(town->creatures[i].second, position, town->creatures[i].first, this));
  54. position.x += 108;
  55. }
  56. }
  57. totalCost = std::make_shared<CreatureCostBox>(Rect((this->pos.w/2)-45, position.y+260, 97, 74), "");
  58. }
  59. void QuickRecruitmentWindow::initWindow(Rect startupPosition)
  60. {
  61. pos.x = startupPosition.x + 238;
  62. pos.y = startupPosition.y + 45;
  63. pos.w = 332;
  64. pos.h = 461;
  65. int creaturesAmount = getAvailableCreatures();
  66. if(creaturesAmount > 3)
  67. {
  68. pos.w += 108 * (creaturesAmount - 3);
  69. pos.x -= 55 * (creaturesAmount - 3);
  70. }
  71. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK.pcx"), Rect(0, 0, pos.w, pos.h));
  72. costBackground = std::make_shared<CPicture>(ImagePath::builtin("QuickRecruitmentWindow/costBackground.png"), pos.w/2-113, 335);
  73. }
  74. void QuickRecruitmentWindow::maxAllCards(std::vector<std::shared_ptr<CreaturePurchaseCard> > cards)
  75. {
  76. auto allAvailableResources = GAME->interface()->cb->getResourceAmount();
  77. for(auto i : boost::adaptors::reverse(cards))
  78. {
  79. si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
  80. vstd::amin(maxAmount, i->maxAmount);
  81. i->slider->setAmount(maxAmount);
  82. if(i->slider->getValue() != maxAmount)
  83. i->slider->scrollTo(maxAmount);
  84. else
  85. i->sliderMoved(maxAmount);
  86. i->slider->scrollToMax();
  87. allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * maxAmount);
  88. }
  89. maxButton->block(allAvailableResources == GAME->interface()->cb->getResourceAmount());
  90. }
  91. void QuickRecruitmentWindow::purchaseUnits()
  92. {
  93. int freeSlotsLeft = town->getUpperArmy()->getFreeSlots().size();
  94. for(auto selected : boost::adaptors::reverse(cards))
  95. {
  96. if(selected->slider->getValue() == 0)
  97. continue;
  98. int level = 0;
  99. int i = 0;
  100. for(auto c : town->getTown()->creatures)
  101. {
  102. for(auto c2 : c)
  103. if(c2 == selected->creatureOnTheCard->getId())
  104. level = i;
  105. i++;
  106. }
  107. CreatureID crid = selected->creatureOnTheCard->getId();
  108. SlotID dstslot = town->getUpperArmy()->getSlotFor(crid);
  109. if(town->getUpperArmy()->slotEmpty(dstslot))
  110. {
  111. if(freeSlotsLeft == 0)
  112. continue;
  113. freeSlotsLeft -= 1;
  114. }
  115. if(dstslot.validSlot())
  116. GAME->interface()->cb->recruitCreatures(town, town->getUpperArmy(), crid, selected->slider->getValue(), level);
  117. }
  118. close();
  119. }
  120. int QuickRecruitmentWindow::getAvailableCreatures()
  121. {
  122. int creaturesAmount = 0;
  123. for (int i=0; i< town->getTown()->creatures.size(); i++)
  124. if(!town->getTown()->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
  125. creaturesAmount++;
  126. return creaturesAmount;
  127. }
  128. void QuickRecruitmentWindow::updateAllSliders()
  129. {
  130. auto allAvailableResources = GAME->interface()->cb->getResourceAmount();
  131. for(auto i : boost::adaptors::reverse(cards))
  132. allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * i->slider->getValue());
  133. for(auto i : cards)
  134. {
  135. si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
  136. vstd::amin(maxAmount, i->maxAmount);
  137. if(maxAmount < 0)
  138. continue;
  139. if(i->slider->getValue() + maxAmount < i->maxAmount)
  140. i->slider->setAmount(i->slider->getValue() + maxAmount);
  141. else
  142. i->slider->setAmount(i->maxAmount);
  143. i->slider->scrollTo(i->slider->getValue());
  144. }
  145. totalCost->createItems(GAME->interface()->cb->getResourceAmount() - allAvailableResources);
  146. totalCost->set(GAME->interface()->cb->getResourceAmount() - allAvailableResources);
  147. }
  148. QuickRecruitmentWindow::QuickRecruitmentWindow(const CGTownInstance * townd, Rect startupPosition)
  149. : CWindowObject(PLAYER_COLORED | BORDERED),
  150. town(townd)
  151. {
  152. OBJECT_CONSTRUCTION;
  153. initWindow(startupPosition);
  154. setButtons();
  155. setCreaturePurchaseCards();
  156. maxAllCards(cards);
  157. center();
  158. }