QuickRecruitmentWindow.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) + 48, 418), "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)-32, 418), "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)-112, 418), "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. costBackground = std::make_shared<CPicture>("QuickRecruitmentWindow/costBackground.png", pos.w/2-113, pos.y+290);
  70. }
  71. void QuickRecruitmentWindow::maxAllCards(std::vector<std::shared_ptr<CreaturePurchaseCard> > cards)
  72. {
  73. auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
  74. for(auto i : boost::adaptors::reverse(cards))
  75. {
  76. si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
  77. vstd::amin(maxAmount, i->maxAmount);
  78. i->slider->setAmount(maxAmount);
  79. if(i->slider->getValue() != maxAmount)
  80. i->slider->moveTo(maxAmount);
  81. else
  82. i->sliderMoved(maxAmount);
  83. i->slider->moveToMax();
  84. allAvailableResources -= (i->creatureOnTheCard->cost * maxAmount);
  85. }
  86. maxButton->block(allAvailableResources == LOCPLINT->cb->getResourceAmount());
  87. }
  88. void QuickRecruitmentWindow::purhaseUnits()
  89. {
  90. for(auto selected : cards)
  91. {
  92. if(selected->slider->getValue())
  93. {
  94. auto onRecruit = [=](CreatureID id, int count){ LOCPLINT->cb->recruitCreatures(town, town->getUpperArmy(), id, count, selected->creatureOnTheCard->level-1); };
  95. CreatureID crid = selected->creatureOnTheCard->idNumber;
  96. SlotID dstslot = town -> getSlotFor(crid);
  97. if(!dstslot.validSlot())
  98. continue;
  99. onRecruit(crid, selected->slider->getValue());
  100. }
  101. }
  102. close();
  103. }
  104. int QuickRecruitmentWindow::getAvailableCreatures()
  105. {
  106. int creaturesAmount = 0;
  107. for (int i=0; i< GameConstants::CREATURES_PER_TOWN; i++)
  108. if(!town->town->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
  109. creaturesAmount++;
  110. return creaturesAmount;
  111. }
  112. void QuickRecruitmentWindow::updateAllSliders()
  113. {
  114. auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
  115. for(auto i : boost::adaptors::reverse(cards))
  116. allAvailableResources -= (i->creatureOnTheCard->cost * i->slider->getValue());
  117. for(auto i : cards)
  118. {
  119. si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
  120. vstd::amin(maxAmount, i->maxAmount);
  121. if(maxAmount < 0)
  122. continue;
  123. if(i->slider->getValue() + maxAmount < i->maxAmount)
  124. i->slider->setAmount(i->slider->getValue() + maxAmount);
  125. else
  126. i->slider->setAmount(i->maxAmount);
  127. i->slider->moveTo(i->slider->getValue());
  128. }
  129. totalCost->createItems(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
  130. totalCost->set(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
  131. }
  132. QuickRecruitmentWindow::QuickRecruitmentWindow(const CGTownInstance * townd, Rect startupPosition)
  133. : CWindowObject(PLAYER_COLORED | BORDERED),
  134. town(townd)
  135. {
  136. OBJECT_CONSTRUCTION_CAPTURING(ACTIVATE + DEACTIVATE + UPDATE + SHOWALL);
  137. initWindow(startupPosition);
  138. setButtons();
  139. setCreaturePurhaseCards();
  140. maxAllCards(cards);
  141. }