CreaturePurchaseCard.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * CreaturePurchaseCard.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 "CreaturePurchaseCard.h"
  12. #include "CHeroWindow.h"
  13. #include "QuickRecruitmentWindow.h"
  14. #include "CCreatureWindow.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/TextAlignment.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/TextControls.h"
  19. #include "../widgets/CreatureCostBox.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/CCreatureHandler.h"
  22. void CreaturePurchaseCard::initButtons()
  23. {
  24. initMaxButton();
  25. initMinButton();
  26. initCreatureSwitcherButton();
  27. }
  28. void CreaturePurchaseCard::initMaxButton()
  29. {
  30. maxButton = std::make_shared<CButton>(Point(pos.x + 52, pos.y + 180), "QuickRecruitmentWindow/QuickRecruitmentAllButton.def", CButton::tooltip(), std::bind(&CSlider::moveToMax,slider), SDLK_LSHIFT);
  31. }
  32. void CreaturePurchaseCard::initMinButton()
  33. {
  34. minButton = std::make_shared<CButton>(Point(pos.x, pos.y + 180), "QuickRecruitmentWindow/QuickRecruitmentNoneButton.def", CButton::tooltip(), std::bind(&CSlider::moveToMin,slider), SDLK_LCTRL);
  35. }
  36. void CreaturePurchaseCard::initCreatureSwitcherButton()
  37. {
  38. creatureSwitcher = std::make_shared<CButton>(Point(pos.x + 18, pos.y-37), "iDv6432.def", CButton::tooltip(), [&](){ switchCreatureLevel(); });
  39. }
  40. void CreaturePurchaseCard::switchCreatureLevel()
  41. {
  42. OBJECT_CONSTRUCTION_CAPTURING(ACTIVATE + DEACTIVATE + UPDATE + SHOWALL + SHARE_POS);
  43. auto index = vstd::find_pos(upgradesID, creatureOnTheCard->getId());
  44. auto nextCreatureId = vstd::circularAt(upgradesID, ++index);
  45. creatureOnTheCard = nextCreatureId.toCreature();
  46. picture = std::make_shared<CCreaturePic>(parent->pos.x, parent->pos.y, creatureOnTheCard);
  47. creatureClickArea = std::make_shared<CCreatureClickArea>(Point(parent->pos.x, parent->pos.y), picture, creatureOnTheCard);
  48. parent->updateAllSliders();
  49. cost->set(creatureOnTheCard->getFullRecruitCost() * slider->getValue());
  50. }
  51. void CreaturePurchaseCard::initAmountInfo()
  52. {
  53. availableAmount = std::make_shared<CLabel>(pos.x + 25, pos.y + 146, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW);
  54. purchaseAmount = std::make_shared<CLabel>(pos.x + 76, pos.y + 146, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  55. updateAmountInfo(0);
  56. }
  57. void CreaturePurchaseCard::updateAmountInfo(int value)
  58. {
  59. availableAmount->setText(std::to_string(maxAmount-value));
  60. purchaseAmount->setText(std::to_string(value));
  61. }
  62. void CreaturePurchaseCard::initSlider()
  63. {
  64. slider = std::make_shared<CSlider>(Point(pos.x, pos.y + 158), 102, std::bind(&CreaturePurchaseCard::sliderMoved, this , _1), 0, maxAmount, 0);
  65. }
  66. void CreaturePurchaseCard::initCostBox()
  67. {
  68. cost = std::make_shared<CreatureCostBox>(Rect(pos.x+2, pos.y + 194, 97, 74), "");
  69. cost->createItems(creatureOnTheCard->getFullRecruitCost());
  70. }
  71. void CreaturePurchaseCard::sliderMoved(int to)
  72. {
  73. updateAmountInfo(to);
  74. cost->set(creatureOnTheCard->getFullRecruitCost() * to);
  75. parent->updateAllSliders();
  76. }
  77. CreaturePurchaseCard::CreaturePurchaseCard(const std::vector<CreatureID> & creaturesID, Point position, int creaturesMaxAmount, QuickRecruitmentWindow * parents)
  78. : upgradesID(creaturesID),
  79. parent(parents),
  80. maxAmount(creaturesMaxAmount)
  81. {
  82. creatureOnTheCard = upgradesID.back().toCreature();
  83. moveTo(Point(position.x, position.y));
  84. initView();
  85. }
  86. void CreaturePurchaseCard::initView()
  87. {
  88. picture = std::make_shared<CCreaturePic>(pos.x, pos.y, creatureOnTheCard);
  89. background = std::make_shared<CPicture>("QuickRecruitmentWindow/CreaturePurchaseCard.png", pos.x-4, pos.y-50);
  90. creatureClickArea = std::make_shared<CCreatureClickArea>(Point(pos.x, pos.y), picture, creatureOnTheCard);
  91. initAmountInfo();
  92. initSlider();
  93. initButtons(); // order important! buttons need slider!
  94. initCostBox();
  95. }
  96. CreaturePurchaseCard::CCreatureClickArea::CCreatureClickArea(const Point & position, const std::shared_ptr<CCreaturePic> creaturePic, const CCreature * creatureOnTheCard)
  97. : CIntObject(RCLICK),
  98. creatureOnTheCard(creatureOnTheCard)
  99. {
  100. pos.x += position.x;
  101. pos.y += position.y;
  102. pos.w = CREATURE_WIDTH;
  103. pos.h = CREATURE_HEIGHT;
  104. }
  105. void CreaturePurchaseCard::CCreatureClickArea::clickRight(tribool down, bool previousState)
  106. {
  107. if (down)
  108. GH.pushIntT<CStackWindow>(creatureOnTheCard, true);
  109. }