QuickRecruitmentWindow.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "../../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. for(auto selected : boost::adaptors::reverse(cards))
  94. {
  95. if(selected->slider->getValue())
  96. {
  97. int level = 0;
  98. int i = 0;
  99. for(auto c : town->getTown()->creatures)
  100. {
  101. for(auto c2 : c)
  102. if(c2 == selected->creatureOnTheCard->getId())
  103. level = i;
  104. i++;
  105. }
  106. auto onRecruit = [this, level](CreatureID id, int count){ GAME->interface()->cb->recruitCreatures(town, town->getUpperArmy(), id, count, level); };
  107. CreatureID crid = selected->creatureOnTheCard->getId();
  108. SlotID dstslot = town -> getSlotFor(crid);
  109. if(!dstslot.validSlot())
  110. continue;
  111. onRecruit(crid, selected->slider->getValue());
  112. }
  113. }
  114. close();
  115. }
  116. int QuickRecruitmentWindow::getAvailableCreatures()
  117. {
  118. int creaturesAmount = 0;
  119. for (int i=0; i< town->getTown()->creatures.size(); i++)
  120. if(!town->getTown()->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
  121. creaturesAmount++;
  122. return creaturesAmount;
  123. }
  124. void QuickRecruitmentWindow::updateAllSliders()
  125. {
  126. auto allAvailableResources = GAME->interface()->cb->getResourceAmount();
  127. for(auto i : boost::adaptors::reverse(cards))
  128. allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * i->slider->getValue());
  129. for(auto i : cards)
  130. {
  131. si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
  132. vstd::amin(maxAmount, i->maxAmount);
  133. if(maxAmount < 0)
  134. continue;
  135. if(i->slider->getValue() + maxAmount < i->maxAmount)
  136. i->slider->setAmount(i->slider->getValue() + maxAmount);
  137. else
  138. i->slider->setAmount(i->maxAmount);
  139. i->slider->scrollTo(i->slider->getValue());
  140. }
  141. totalCost->createItems(GAME->interface()->cb->getResourceAmount() - allAvailableResources);
  142. totalCost->set(GAME->interface()->cb->getResourceAmount() - allAvailableResources);
  143. }
  144. QuickRecruitmentWindow::QuickRecruitmentWindow(const CGTownInstance * townd, Rect startupPosition)
  145. : CWindowObject(PLAYER_COLORED | BORDERED),
  146. town(townd)
  147. {
  148. OBJECT_CONSTRUCTION;
  149. initWindow(startupPosition);
  150. setButtons();
  151. setCreaturePurchaseCards();
  152. maxAllCards(cards);
  153. center();
  154. }