CMapOverview.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "../GameEngine.h"
  14. #include "../gui/WindowHandler.h"
  15. #include "../widgets/CComponent.h"
  16. #include "../widgets/MiscWidgets.h"
  17. #include "../widgets/TextControls.h"
  18. #include "../windows/GUIClasses.h"
  19. #include "../windows/InfoWindows.h"
  20. #include "../render/CanvasImage.h"
  21. #include "../render/IImage.h"
  22. #include "../render/IRenderHandler.h"
  23. #include "../render/Graphics.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/campaign/CampaignState.h"
  26. #include "../../lib/mapping/CMap.h"
  27. #include "../../lib/mapping/CMapService.h"
  28. #include "../../lib/mapping/CMapInfo.h"
  29. #include "../../lib/mapping/CMapHeader.h"
  30. #include "../../lib/mapping/MapFormat.h"
  31. #include "../../lib/TerrainHandler.h"
  32. #include "../../lib/filesystem/Filesystem.h"
  33. #include "../../lib/StartInfo.h"
  34. #include "../../lib/mapObjects/CGHeroInstance.h"
  35. #include "../../lib/rmg/CMapGenOptions.h"
  36. #include "../../lib/serializer/CLoadFile.h"
  37. #include "../../lib/texts/CGeneralTextHandler.h"
  38. #include "../../lib/texts/TextOperations.h"
  39. CMapOverview::CMapOverview(const std::string & mapName, const std::string & fileName, const std::string & date, const std::string & author, const std::string & version, const ResourcePath & resource, ESelectionScreen tabType)
  40. : CWindowObject(BORDERED | RCLICK_POPUP), resource(resource), mapName(mapName), fileName(fileName), date(date), author(author), version(version), tabType(tabType)
  41. {
  42. OBJECT_CONSTRUCTION;
  43. widget = std::make_shared<CMapOverviewWidget>(*this);
  44. updateShadow();
  45. center(ENGINE->getCursorPosition()); //center on mouse
  46. #ifdef VCMI_MOBILE
  47. moveBy({0, -pos.h / 2});
  48. #endif
  49. fitToScreen(10);
  50. }
  51. std::shared_ptr<CanvasImage> CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
  52. {
  53. auto canvasImage = ENGINE->renderHandler().createImage(Point(map->width, map->height), CanvasScalingPolicy::IGNORE);
  54. auto canvas = canvasImage->getCanvas();
  55. for (int y = 0; y < map->height; ++y)
  56. for (int x = 0; x < map->width; ++x)
  57. {
  58. TerrainTile & tile = map->getTile(int3(x, y, layer));
  59. ColorRGBA color = tile.getTerrain()->minimapUnblocked;
  60. if (tile.blocked() && !tile.visitable())
  61. color = tile.getTerrain()->minimapBlocked;
  62. if(drawPlayerElements)
  63. // if object at tile is owned - it will be colored as its owner
  64. for (ObjectInstanceID objectID : tile.blockingObjects)
  65. {
  66. const auto * object = map->getObject(objectID);
  67. PlayerColor player = object->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 canvasImage;
  82. }
  83. std::vector<std::shared_ptr<CanvasImage>> CMapOverviewWidget::createMinimaps(const ResourcePath & resource) const
  84. {
  85. std::vector<std::shared_ptr<CanvasImage>> ret;
  86. CMapService mapService;
  87. std::unique_ptr<CMap> map;
  88. try
  89. {
  90. map = mapService.loadMap(resource, nullptr);
  91. }
  92. catch (const std::exception & e)
  93. {
  94. logGlobal->warn("Failed to generate map preview! %s", e.what());
  95. return ret;
  96. }
  97. return createMinimaps(map);
  98. }
  99. std::vector<std::shared_ptr<CanvasImage>> CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map) const
  100. {
  101. std::vector<std::shared_ptr<CanvasImage>> ret;
  102. for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
  103. ret.push_back(createMinimapForLayer(map, i));
  104. return ret;
  105. }
  106. std::shared_ptr<CPicture> CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const
  107. {
  108. logGlobal->debug("Building widget drawMinimap");
  109. auto rect = readRect(config["rect"]);
  110. auto id = config["id"].Integer();
  111. if(id >= minimaps.size())
  112. return nullptr;
  113. Point minimapRect = minimaps[id]->dimensions();
  114. double maxSideLengthSrc = std::max(minimapRect.x, minimapRect.y);
  115. double maxSideLengthDst = std::max(rect.w, rect.h);
  116. double resize = maxSideLengthSrc / maxSideLengthDst;
  117. Point newMinimapSize(minimapRect.x / resize, minimapRect.y / resize);
  118. minimaps[id]->scaleTo(newMinimapSize, EScalingAlgorithm::NEAREST); // for sharp-looking minimap
  119. return std::make_shared<CPicture>(minimaps[id], Point(rect.x, rect.y));
  120. }
  121. CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
  122. InterfaceObjectConfigurable(), p(parent)
  123. {
  124. drawPlayerElements = p.tabType == ESelectionScreen::newGame;
  125. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  126. if(settings["lobby"]["mapPreview"].Bool() && p.tabType != ESelectionScreen::campaignList)
  127. {
  128. ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP);
  129. std::unique_ptr<CMap> campaignMap = nullptr;
  130. if(p.tabType != ESelectionScreen::newGame && config["variables"]["mapPreviewForSaves"].Bool())
  131. {
  132. CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), nullptr);
  133. CMapHeader mapHeader;
  134. StartInfo startInfo;
  135. lf.load(mapHeader);
  136. lf.load(startInfo);
  137. if(startInfo.campState)
  138. campaignMap = startInfo.campState->getMap(*startInfo.campState->currentScenario(), nullptr);
  139. res = ResourcePath(startInfo.fileURI, EResType::MAP);
  140. }
  141. if(!campaignMap)
  142. minimaps = createMinimaps(res);
  143. else
  144. minimaps = createMinimaps(campaignMap);
  145. }
  146. REGISTER_BUILDER("drawMinimap", &CMapOverviewWidget::buildDrawMinimap);
  147. build(config);
  148. if(auto w = widget<CFilledTexture>("background"))
  149. {
  150. p.pos = w->pos;
  151. }
  152. if(auto w = widget<CTextBox>("fileName"))
  153. {
  154. w->setText(p.fileName);
  155. }
  156. if(auto w = widget<CLabel>("mapName"))
  157. {
  158. w->setText(p.mapName);
  159. }
  160. if(auto w = widget<CLabel>("date"))
  161. {
  162. if(p.date.empty())
  163. {
  164. std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), p.tabType == ESelectionScreen::campaignList ? EResType::CAMPAIGN : EResType::MAP)));
  165. w->setText(TextOperations::getFormattedDateTimeLocal(time));
  166. }
  167. else
  168. w->setText(p.date);
  169. }
  170. if(auto w = widget<CLabel>("author"))
  171. {
  172. w->setText(p.author.empty() ? "-" : p.author);
  173. }
  174. if(auto w = widget<CLabel>("version"))
  175. {
  176. w->setText(p.version);
  177. }
  178. if(auto w = widget<CLabel>("noUnderground"))
  179. {
  180. if(minimaps.size() == 0)
  181. w->setText("");
  182. }
  183. }