CreaturePurchaseCard.cpp 3.2 KB

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