QuickRecruitmentWindow.cpp 5.3 KB

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