2
0

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