CreaturePurchaseCard.cpp 4.5 KB

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