CreatureCostBox.cpp 1.5 KB

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