CreaturePurchaseCard.cpp 4.3 KB

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