CResDataBar.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/CPlayerState.h"
  24. #include "../../lib/callback/CCallback.h"
  25. #include "../../lib/texts/CGeneralTextHandler.h"
  26. #include "../../lib/ResourceSet.h"
  27. #include "../../lib/StartInfo.h"
  28. #include "../../lib/GameLibrary.h"
  29. #include "../../lib/entities/ResourceTypeHandler.h"
  30. #include "../../lib/mapObjects/IOwnableObject.h"
  31. #include "../../lib/networkPacks/Component.h"
  32. CResDataBar::CResDataBar(const ImagePath & imageName, const Point & position)
  33. {
  34. addUsedEvents(SHOW_POPUP);
  35. pos.x += position.x;
  36. pos.y += position.y;
  37. OBJECT_CONSTRUCTION;
  38. background = std::make_shared<CPicture>(imageName, 0, 0);
  39. background->setPlayerColor(GAME->interface()->playerID);
  40. pos.w = background->pos.w;
  41. pos.h = background->pos.h;
  42. }
  43. CResDataBar::CResDataBar(const ImagePath & defname, int x, int y, int offx, int offy, int resdist, int datedist):
  44. CResDataBar(defname, Point(x,y))
  45. {
  46. for (int i = 0; i < 7 ; i++)
  47. resourcePositions[GameResID(i)] = Point( offx + resdist*i, offy );
  48. datePosition = resourcePositions[EGameResID::GOLD] + Point(datedist, 0);
  49. }
  50. void CResDataBar::setDatePosition(const Point & position)
  51. {
  52. datePosition = position;
  53. }
  54. void CResDataBar::setResourcePosition(const GameResID & resource, const Point & position)
  55. {
  56. resourcePositions[resource] = position;
  57. }
  58. std::string CResDataBar::buildDateString()
  59. {
  60. std::string pattern = "%s: %d, %s: %d, %s: %d";
  61. auto formatted = boost::format(pattern)
  62. % LIBRARY->generaltexth->translate("core.genrltxt.62") % GAME->interface()->cb->getDate(Date::MONTH)
  63. % LIBRARY->generaltexth->translate("core.genrltxt.63") % GAME->interface()->cb->getDate(Date::WEEK)
  64. % LIBRARY->generaltexth->translate("core.genrltxt.64") % GAME->interface()->cb->getDate(Date::DAY_OF_WEEK);
  65. return boost::str(formatted);
  66. }
  67. void CResDataBar::showAll(Canvas & to)
  68. {
  69. CIntObject::showAll(to);
  70. //TODO: all this should be labels, but they require proper text update on change
  71. for (auto & entry : resourcePositions)
  72. {
  73. std::string text = std::to_string(GAME->interface()->cb->getResourceAmount(entry.first));
  74. to.drawText(pos.topLeft() + entry.second, FONT_SMALL, Colors::WHITE, ETextAlignment::TOPLEFT, text);
  75. }
  76. if (datePosition)
  77. to.drawText(pos.topLeft() + *datePosition, FONT_SMALL, Colors::WHITE, ETextAlignment::TOPLEFT, buildDateString());
  78. }
  79. void CResDataBar::setPlayerColor(PlayerColor player)
  80. {
  81. background->setPlayerColor(player);
  82. }
  83. void CResDataBar::showPopupWindow(const Point & cursorPosition)
  84. {
  85. if((cursorPosition.x - pos.x) > 600)
  86. return;
  87. // only daily income
  88. ResourceSet income;
  89. auto playerState = GAME->interface()->cb->getPlayerState(GAME->interface()->playerID);
  90. auto playerSettings = GAME->interface()->cb->getPlayerSettings(GAME->interface()->playerID);
  91. for(auto & k : LIBRARY->resourceTypeHandler->getAllObjects())
  92. {
  93. income += playerState->valOfBonuses(BonusType::RESOURCES_CONSTANT_BOOST, BonusSubtypeID(k));
  94. income += playerState->valOfBonuses(BonusType::RESOURCES_TOWN_MULTIPLYING_BOOST, BonusSubtypeID(k)) * playerState->getTowns().size();
  95. }
  96. TResources incomeHandicapped = income;
  97. incomeHandicapped.applyHandicap(playerSettings->handicap.percentIncome);
  98. for(auto & mapObject : playerState->getOwnedObjects())
  99. incomeHandicapped += mapObject->asOwnable()->dailyIncome();
  100. std::vector<std::shared_ptr<CComponent>> comp;
  101. for (auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  102. {
  103. std::string text = std::to_string(GAME->interface()->cb->getResourceAmount(i));
  104. if(incomeHandicapped[i])
  105. text += "\n{lightgreen|(+" + std::to_string(incomeHandicapped[i]) + ")}";
  106. comp.push_back(std::make_shared<CComponent>(ComponentType::RESOURCE, i, text));
  107. }
  108. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("core.genrltxt.270"), comp);
  109. }