QuickRecruitmentWindow.cpp 5.4 KB

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