CreatureCostBox.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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;
  18. setRedrawParent(true);
  19. pos = position + pos.topLeft();
  20. title = std::make_shared<CLabel>(pos.w/2, 10, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, titleText);
  21. }
  22. void CreatureCostBox::set(TResources res)
  23. {
  24. for(auto & item : resources)
  25. item.second.first->setText(std::to_string(res[item.first]));
  26. }
  27. void CreatureCostBox::createItems(TResources res)
  28. {
  29. resources.clear();
  30. OBJECT_CONSTRUCTION;
  31. TResources::nziterator iter(res);
  32. while(iter.valid())
  33. {
  34. auto image = std::make_shared<CAnimImage>(AnimationPath::builtin("RESOURCE"), iter->resType);
  35. auto text = std::make_shared<CLabel>(15, 43, FONT_SMALL, ETextAlignment::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;
  42. int spacing = 48;
  43. int resourcesCount = static_cast<int>(resources.size());
  44. if (resources.size() > 2)
  45. {
  46. spacing = 32;
  47. curx -= (15 + 16 * (resourcesCount - 1));
  48. }
  49. else
  50. {
  51. curx -= ((16 * resourcesCount) + (8 * (resourcesCount - 1)));
  52. }
  53. //reverse to display gold as first resource
  54. for(auto & currentRes : boost::adaptors::reverse(resources))
  55. {
  56. currentRes.second.first->moveBy(Point(curx + 2, 22));
  57. currentRes.second.second->moveBy(Point(curx, 22));
  58. curx += spacing;
  59. }
  60. }
  61. redraw();
  62. }