CMapOverview.cpp 7.2 KB

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