CMapOverview.cpp 6.2 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/Canvas.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. Canvas CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
  51. {
  52. Canvas canvas = Canvas(Point(map->width, map->height), CanvasScalingPolicy::IGNORE);
  53. for (int y = 0; y < map->height; ++y)
  54. for (int x = 0; x < map->width; ++x)
  55. {
  56. TerrainTile & tile = map->getTile(int3(x, y, layer));
  57. ColorRGBA color = tile.getTerrain()->minimapUnblocked;
  58. if (tile.blocked() && !tile.visitable())
  59. color = tile.getTerrain()->minimapBlocked;
  60. if(drawPlayerElements)
  61. // if object at tile is owned - it will be colored as its owner
  62. for (const CGObjectInstance *obj : tile.blockingObjects)
  63. {
  64. PlayerColor player = obj->getOwner();
  65. if(player == PlayerColor::NEUTRAL)
  66. {
  67. color = graphics->neutralColor;
  68. break;
  69. }
  70. if (player.isValidPlayer())
  71. {
  72. color = graphics->playerColors[player.getNum()];
  73. break;
  74. }
  75. }
  76. canvas.drawPoint(Point(x, y), color);
  77. }
  78. return canvas;
  79. }
  80. std::vector<Canvas> CMapOverviewWidget::createMinimaps(ResourcePath resource) const
  81. {
  82. auto ret = std::vector<Canvas>();
  83. CMapService mapService;
  84. std::unique_ptr<CMap> map;
  85. try
  86. {
  87. map = mapService.loadMap(resource, nullptr);
  88. }
  89. catch (const std::exception & e)
  90. {
  91. logGlobal->warn("Failed to generate map preview! %s", e.what());
  92. return ret;
  93. }
  94. return createMinimaps(map);
  95. }
  96. std::vector<Canvas> CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map) const
  97. {
  98. auto ret = std::vector<Canvas>();
  99. for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
  100. ret.push_back(createMinimapForLayer(map, i));
  101. return ret;
  102. }
  103. std::shared_ptr<CPicture> CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const
  104. {
  105. logGlobal->debug("Building widget drawMinimap");
  106. auto rect = readRect(config["rect"]);
  107. auto id = config["id"].Integer();
  108. if(id >= minimaps.size())
  109. return nullptr;
  110. Rect minimapRect = minimaps[id].getRenderArea();
  111. double maxSideLengthSrc = std::max(minimapRect.w, minimapRect.h);
  112. double maxSideLengthDst = std::max(rect.w, rect.h);
  113. double resize = maxSideLengthSrc / maxSideLengthDst;
  114. Point newMinimapSize = Point(minimapRect.w / resize, minimapRect.h / resize);
  115. std::shared_ptr<IImage> img = GH.renderHandler().createImage(minimaps[id].getInternalSurface());
  116. img->scaleTo(newMinimapSize);
  117. return std::make_shared<CPicture>(img, 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. }