CResDataBar.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "../widgets/CComponent.h"
  21. #include "../windows/InfoWindows.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/callback/CCallback.h"
  24. #include "../../lib/texts/CGeneralTextHandler.h"
  25. #include "../../lib/ResourceSet.h"
  26. #include "../../lib/GameLibrary.h"
  27. #include "../../lib/entities/ResourceTypeHandler.h"
  28. #include "../../lib/networkPacks/Component.h"
  29. CResDataBar::CResDataBar(const ImagePath & imageName, const Point & position)
  30. {
  31. addUsedEvents(SHOW_POPUP);
  32. pos.x += position.x;
  33. pos.y += position.y;
  34. OBJECT_CONSTRUCTION;
  35. background = std::make_shared<CPicture>(imageName, 0, 0);
  36. background->setPlayerColor(GAME->interface()->playerID);
  37. pos.w = background->pos.w;
  38. pos.h = background->pos.h;
  39. }
  40. CResDataBar::CResDataBar(const ImagePath & defname, int x, int y, int offx, int offy, int resdist, int datedist):
  41. CResDataBar(defname, Point(x,y))
  42. {
  43. for (int i = 0; i < 7 ; i++)
  44. resourcePositions[GameResID(i)] = Point( offx + resdist*i, offy );
  45. datePosition = resourcePositions[EGameResID::GOLD] + Point(datedist, 0);
  46. }
  47. void CResDataBar::setDatePosition(const Point & position)
  48. {
  49. datePosition = position;
  50. }
  51. void CResDataBar::setResourcePosition(const GameResID & resource, const Point & position)
  52. {
  53. resourcePositions[resource] = position;
  54. }
  55. std::string CResDataBar::buildDateString()
  56. {
  57. std::string pattern = "%s: %d, %s: %d, %s: %d";
  58. auto formatted = boost::format(pattern)
  59. % LIBRARY->generaltexth->translate("core.genrltxt.62") % GAME->interface()->cb->getDate(Date::MONTH)
  60. % LIBRARY->generaltexth->translate("core.genrltxt.63") % GAME->interface()->cb->getDate(Date::WEEK)
  61. % LIBRARY->generaltexth->translate("core.genrltxt.64") % GAME->interface()->cb->getDate(Date::DAY_OF_WEEK);
  62. return boost::str(formatted);
  63. }
  64. void CResDataBar::showAll(Canvas & to)
  65. {
  66. CIntObject::showAll(to);
  67. //TODO: all this should be labels, but they require proper text update on change
  68. for (auto & entry : resourcePositions)
  69. {
  70. std::string text = std::to_string(GAME->interface()->cb->getResourceAmount(entry.first));
  71. to.drawText(pos.topLeft() + entry.second, FONT_SMALL, Colors::WHITE, ETextAlignment::TOPLEFT, text);
  72. }
  73. if (datePosition)
  74. to.drawText(pos.topLeft() + *datePosition, FONT_SMALL, Colors::WHITE, ETextAlignment::TOPLEFT, buildDateString());
  75. }
  76. void CResDataBar::setPlayerColor(PlayerColor player)
  77. {
  78. background->setPlayerColor(player);
  79. }
  80. void CResDataBar::showPopupWindow(const Point & cursorPosition)
  81. {
  82. if((cursorPosition.x - pos.x) > 600)
  83. return;
  84. std::vector<std::shared_ptr<CComponent>> comp;
  85. for (auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  86. comp.push_back(std::make_shared<CComponent>(ComponentType::RESOURCE, GameResID(i), GAME->interface()->cb->getResourceAmount(i)));
  87. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("core.genrltxt.270"), comp);
  88. }