CreaturePurchaseCard.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. void CreaturePurchaseCard::initButtons()
  21. {
  22. initMaxButton();
  23. initMinButton();
  24. initCreatureSwitcherButton();
  25. }
  26. void CreaturePurchaseCard::initMaxButton()
  27. {
  28. maxButton = std::make_shared<CButton>(Point(pos.x + 52, pos.y + 180), "QuickRecruitmentWindow/QuickRecruitmentAllButton.def", CButton::tooltip(), std::bind(&CSlider::moveToMax,slider), SDLK_LSHIFT);
  29. }
  30. void CreaturePurchaseCard::initMinButton()
  31. {
  32. minButton = std::make_shared<CButton>(Point(pos.x, pos.y + 180), "QuickRecruitmentWindow/QuickRecruitmentNoneButton.def", CButton::tooltip(), std::bind(&CSlider::moveToMin,slider), SDLK_LCTRL);
  33. }
  34. void CreaturePurchaseCard::initCreatureSwitcherButton()
  35. {
  36. creatureSwitcher = std::make_shared<CButton>(Point(pos.x + 18, pos.y-37), "iDv6432.def", CButton::tooltip(), [&](){ switchCreatureLevel(); });
  37. }
  38. void CreaturePurchaseCard::switchCreatureLevel()
  39. {
  40. OBJECT_CONSTRUCTION_CAPTURING(ACTIVATE + DEACTIVATE + UPDATE + SHOWALL + SHARE_POS);
  41. auto index = vstd::find_pos(upgradesID, creatureOnTheCard->idNumber);
  42. auto nextCreatureId = vstd::circularAt(upgradesID, ++index);
  43. creatureOnTheCard = nextCreatureId.toCreature();
  44. picture = std::make_shared<CCreaturePic>(parent->pos.x, parent->pos.y, creatureOnTheCard);
  45. parent->updateAllSliders();
  46. cost->set(creatureOnTheCard->cost * slider->getValue());
  47. }
  48. void CreaturePurchaseCard::initAmountInfo()
  49. {
  50. availableAmount = std::make_shared<CLabel>(pos.x + 25, pos.y + 146, FONT_SMALL, CENTER, Colors::YELLOW);
  51. purchaseAmount = std::make_shared<CLabel>(pos.x + 76, pos.y + 146, FONT_SMALL, CENTER, Colors::WHITE);
  52. updateAmountInfo(0);
  53. }
  54. void CreaturePurchaseCard::updateAmountInfo(int value)
  55. {
  56. availableAmount->setText(boost::lexical_cast<std::string>(maxAmount-value));
  57. purchaseAmount->setText(boost::lexical_cast<std::string>(value));
  58. }
  59. void CreaturePurchaseCard::initSlider()
  60. {
  61. slider = std::make_shared<CSlider>(Point(pos.x, pos.y + 158), 102, std::bind(&CreaturePurchaseCard::sliderMoved, this , _1), 0, maxAmount, 0);
  62. }
  63. void CreaturePurchaseCard::initCostBox()
  64. {
  65. cost = std::make_shared<CreatureCostBox>(Rect(pos.x+2, pos.y + 194, 97, 74), "");
  66. cost->createItems(creatureOnTheCard->cost);
  67. }
  68. void CreaturePurchaseCard::sliderMoved(int to)
  69. {
  70. updateAmountInfo(to);
  71. cost->set(creatureOnTheCard->cost * to);
  72. parent->updateAllSliders();
  73. }
  74. CreaturePurchaseCard::CreaturePurchaseCard(const std::vector<CreatureID> & creaturesID, Point position, int creaturesMaxAmount, QuickRecruitmentWindow * parents)
  75. : upgradesID(creaturesID),
  76. parent(parents),
  77. maxAmount(creaturesMaxAmount)
  78. {
  79. creatureOnTheCard = upgradesID.back().toCreature();
  80. moveTo(Point(position.x, position.y));
  81. initView();
  82. }
  83. void CreaturePurchaseCard::initView()
  84. {
  85. picture = std::make_shared<CCreaturePic>(pos.x, pos.y, creatureOnTheCard);
  86. background = std::make_shared<CPicture>("QuickRecruitmentWindow/CreaturePurchaseCard.png", pos.x-4, pos.y-50);
  87. initAmountInfo();
  88. initSlider();
  89. initButtons();
  90. initCostBox();
  91. }