CResDataBar.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * CResDataBar.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 "CResDataBar.h"
  12. #include "../CPlayerInterface.h"
  13. #include "../render/Canvas.h"
  14. #include "../render/Colors.h"
  15. #include "../render/EFont.h"
  16. #include "../GameEngine.h"
  17. #include "../GameInstance.h"
  18. #include "../gui/TextAlignment.h"
  19. #include "../widgets/Images.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/CConfigHandler.h"
  22. #include "../../lib/texts/CGeneralTextHandler.h"
  23. #include "../../lib/ResourceSet.h"
  24. #include "../../lib/GameLibrary.h"
  25. CResDataBar::CResDataBar(const ImagePath & imageName, const Point & position)
  26. {
  27. pos.x += position.x;
  28. pos.y += position.y;
  29. OBJECT_CONSTRUCTION;
  30. background = std::make_shared<CPicture>(imageName, 0, 0);
  31. background->setPlayerColor(GAME->interface()->playerID);
  32. pos.w = background->pos.w;
  33. pos.h = background->pos.h;
  34. }
  35. CResDataBar::CResDataBar(const ImagePath & defname, int x, int y, int offx, int offy, int resdist, int datedist):
  36. CResDataBar(defname, Point(x,y))
  37. {
  38. for (int i = 0; i < 7 ; i++)
  39. resourcePositions[GameResID(i)] = Point( offx + resdist*i, offy );
  40. datePosition = resourcePositions[EGameResID::GOLD] + Point(datedist, 0);
  41. }
  42. void CResDataBar::setDatePosition(const Point & position)
  43. {
  44. datePosition = position;
  45. }
  46. void CResDataBar::setResourcePosition(const GameResID & resource, const Point & position)
  47. {
  48. resourcePositions[resource] = position;
  49. }
  50. std::string CResDataBar::buildDateString()
  51. {
  52. std::string pattern = "%s: %d, %s: %d, %s: %d";
  53. auto formatted = boost::format(pattern)
  54. % LIBRARY->generaltexth->translate("core.genrltxt.62") % GAME->interface()->cb->getDate(Date::MONTH)
  55. % LIBRARY->generaltexth->translate("core.genrltxt.63") % GAME->interface()->cb->getDate(Date::WEEK)
  56. % LIBRARY->generaltexth->translate("core.genrltxt.64") % GAME->interface()->cb->getDate(Date::DAY_OF_WEEK);
  57. return boost::str(formatted);
  58. }
  59. void CResDataBar::showAll(Canvas & to)
  60. {
  61. CIntObject::showAll(to);
  62. //TODO: all this should be labels, but they require proper text update on change
  63. for (auto & entry : resourcePositions)
  64. {
  65. std::string text = std::to_string(GAME->interface()->cb->getResourceAmount(entry.first));
  66. to.drawText(pos.topLeft() + entry.second, FONT_SMALL, Colors::WHITE, ETextAlignment::TOPLEFT, text);
  67. }
  68. if (datePosition)
  69. to.drawText(pos.topLeft() + *datePosition, FONT_SMALL, Colors::WHITE, ETextAlignment::TOPLEFT, buildDateString());
  70. }
  71. void CResDataBar::setPlayerColor(PlayerColor player)
  72. {
  73. background->setPlayerColor(player);
  74. }