CreatureCostBox.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * CreatureCostBox.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 "CreatureCostBox.h"
  12. #include "widgets/Images.h"
  13. #include "widgets/TextControls.h"
  14. #include "gui/CGuiHandler.h"
  15. CreatureCostBox::CreatureCostBox(Rect position, std::string titleText)
  16. {
  17. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  18. type |= REDRAW_PARENT;
  19. pos = position + pos;
  20. title = std::make_shared<CLabel>(pos.w/2, 10, FONT_SMALL, CENTER, Colors::WHITE, titleText);
  21. }
  22. void CreatureCostBox::set(TResources res)
  23. {
  24. for(auto & item : resources)
  25. item.second.first->setText(boost::lexical_cast<std::string>(res[item.first]));
  26. }
  27. void CreatureCostBox::createItems(TResources res)
  28. {
  29. resources.clear();
  30. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  31. TResources::nziterator iter(res);
  32. while(iter.valid())
  33. {
  34. ImagePtr image = std::make_shared<CAnimImage>("RESOURCE", iter->resType);
  35. LabelPtr text = std::make_shared<CLabel>(15, 43, FONT_SMALL, CENTER, Colors::WHITE, "0");
  36. resources.insert(std::make_pair(iter->resType, std::make_pair(text, image)));
  37. iter++;
  38. }
  39. if(!resources.empty())
  40. {
  41. int curx = pos.w / 2 - (16 * resources.size()) - (8 * (resources.size() - 1));
  42. //reverse to display gold as first resource
  43. for(auto & res : boost::adaptors::reverse(resources))
  44. {
  45. res.second.first->moveBy(Point(curx, 22));
  46. res.second.second->moveBy(Point(curx, 22));
  47. curx += 48;
  48. }
  49. }
  50. redraw();
  51. }