CResDataBar.cpp 2.5 KB

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