CResDataBar.cpp 2.6 KB

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