CMapOverview.cpp 6.4 KB

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