CreaturePurchaseCard.cpp 4.2 KB

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