CMapOverview.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * CMapOverview.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 "CMapOverview.h"
  12. #include "../lobby/SelectionTab.h"
  13. #include <vstd/DateUtils.h>
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../widgets/CComponent.h"
  17. #include "../widgets/MiscWidgets.h"
  18. #include "../widgets/TextControls.h"
  19. #include "../windows/GUIClasses.h"
  20. #include "../windows/InfoWindows.h"
  21. #include "../render/CAnimation.h"
  22. #include "../render/Canvas.h"
  23. #include "../render/IImage.h"
  24. #include "../render/IRenderHandler.h"
  25. #include "../render/Graphics.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/campaign/CampaignState.h"
  29. #include "../../lib/mapping/CMap.h"
  30. #include "../../lib/mapping/CMapService.h"
  31. #include "../../lib/mapping/CMapInfo.h"
  32. #include "../../lib/mapping/CMapHeader.h"
  33. #include "../../lib/mapping/MapFormat.h"
  34. #include "../../lib/TerrainHandler.h"
  35. #include "../../lib/filesystem/Filesystem.h"
  36. #include "../../lib/serializer/BinaryDeserializer.h"
  37. #include "../../lib/StartInfo.h"
  38. #include "../../lib/rmg/CMapGenOptions.h"
  39. CMapOverview::CMapOverview(std::string mapName, std::string fileName, std::string date, ResourcePath resource, ESelectionScreen tabType)
  40. : CWindowObject(BORDERED | RCLICK_POPUP), resource(resource), mapName(mapName), fileName(fileName), date(date), tabType(tabType)
  41. {
  42. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  43. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  44. pos = Rect(0, 0, config["items"][0]["rect"]["w"].Integer(), config["items"][0]["rect"]["h"].Integer());
  45. widget = std::make_shared<CMapOverviewWidget>(*this);
  46. updateShadow();
  47. center(GH.getCursorPosition()); //center on mouse
  48. #ifdef VCMI_MOBILE
  49. moveBy({0, -pos.h / 2});
  50. #endif
  51. fitToScreen(10);
  52. }
  53. Canvas CMapOverview::CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
  54. {
  55. Canvas canvas = Canvas(Point(map->width, map->height));
  56. for (int y = 0; y < map->height; ++y)
  57. for (int x = 0; x < map->width; ++x)
  58. {
  59. TerrainTile & tile = map->getTile(int3(x, y, layer));
  60. ColorRGBA color = tile.terType->minimapUnblocked;
  61. if (tile.blocked && (!tile.visitable))
  62. color = tile.terType->minimapBlocked;
  63. if(drawPlayerElements)
  64. // if object at tile is owned - it will be colored as its owner
  65. for (const CGObjectInstance *obj : tile.blockingObjects)
  66. {
  67. PlayerColor player = obj->getOwner();
  68. if(player == PlayerColor::NEUTRAL)
  69. {
  70. color = graphics->neutralColor;
  71. break;
  72. }
  73. if (player.isValidPlayer())
  74. {
  75. color = graphics->playerColors[player.getNum()];
  76. break;
  77. }
  78. }
  79. canvas.drawPoint(Point(x, y), color);
  80. }
  81. return canvas;
  82. }
  83. std::vector<Canvas> CMapOverview::CMapOverviewWidget::createMinimaps(ResourcePath resource) const
  84. {
  85. std::vector<Canvas> ret = std::vector<Canvas>();
  86. CMapService mapService;
  87. std::unique_ptr<CMap> map;
  88. try
  89. {
  90. map = mapService.loadMap(resource);
  91. }
  92. catch (...)
  93. {
  94. return ret;
  95. }
  96. return createMinimaps(map);
  97. }
  98. std::vector<Canvas> CMapOverview::CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map) const
  99. {
  100. std::vector<Canvas> ret = std::vector<Canvas>();
  101. for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
  102. ret.push_back(createMinimapForLayer(map, i));
  103. return ret;
  104. }
  105. std::shared_ptr<CPicture> CMapOverview::CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const
  106. {
  107. logGlobal->debug("Building widget drawMinimap");
  108. auto rect = readRect(config["rect"]);
  109. auto id = config["id"].Integer();
  110. if(id >= minimaps.size())
  111. return nullptr;
  112. Canvas canvasScaled = Canvas(Point(rect.w, rect.h));
  113. canvasScaled.drawScaled(minimaps[id], Point(0, 0), Point(rect.w, rect.h));
  114. std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
  115. return std::make_shared<CPicture>(img, Point(rect.x, rect.y));
  116. }
  117. CMapOverview::CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
  118. InterfaceObjectConfigurable(), p(parent)
  119. {
  120. drawPlayerElements = p.tabType == ESelectionScreen::newGame;
  121. renderImage = settings["lobby"]["mapPreview"].Bool();
  122. // create minimaps
  123. ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP);
  124. std::unique_ptr<CMap> campaignMap = nullptr;
  125. if(p.tabType != ESelectionScreen::newGame)
  126. {
  127. CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), MINIMAL_SERIALIZATION_VERSION);
  128. lf.checkMagicBytes(SAVEGAME_MAGIC);
  129. std::unique_ptr<CMapHeader> mapHeader = std::make_unique<CMapHeader>();
  130. StartInfo * startInfo;
  131. lf >> *(mapHeader) >> startInfo;
  132. if(startInfo->campState)
  133. campaignMap = startInfo->campState->getMap(*startInfo->campState->currentScenario());
  134. res = ResourcePath(startInfo->fileURI, EResType::MAP);
  135. }
  136. if(!campaignMap)
  137. minimaps = createMinimaps(res);
  138. else
  139. minimaps = createMinimaps(campaignMap);
  140. // config
  141. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  142. REGISTER_BUILDER("drawMinimap", &CMapOverview::CMapOverviewWidget::buildDrawMinimap);
  143. build(config);
  144. if(auto w = widget<CTextBox>("fileName"))
  145. {
  146. w->setText(p.fileName);
  147. }
  148. if(auto w = widget<CLabel>("mapName"))
  149. {
  150. w->setText(p.mapName);
  151. }
  152. if(auto w = widget<CLabel>("date"))
  153. {
  154. if(p.date.empty())
  155. {
  156. std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::MAP)));
  157. w->setText(vstd::getFormattedDateTime(time));
  158. }
  159. else
  160. w->setText(p.date);
  161. }
  162. }