CMapOverview.cpp 6.5 KB

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