QuickRecruitmentWindow.cpp 4.9 KB

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