CResDataBar.cpp 2.6 KB

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