CMapOverview.cpp 6.3 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 "../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(std::string mapName, std::string fileName, std::string date, std::string author, std::string version, 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));
  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.terType->minimapUnblocked;
  58. if (tile.blocked && (!tile.visitable))
  59. color = tile.terType->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. Canvas canvasScaled = Canvas(Point(rect.w, rect.h));
  116. canvasScaled.drawScaled(minimaps[id], Point((rect.w - newMinimapSize.x) / 2, (rect.h - newMinimapSize.y) / 2), newMinimapSize);
  117. std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
  118. return std::make_shared<CPicture>(img, Point(rect.x, rect.y));
  119. }
  120. CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
  121. InterfaceObjectConfigurable(), p(parent)
  122. {
  123. drawPlayerElements = p.tabType == ESelectionScreen::newGame;
  124. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  125. if(settings["lobby"]["mapPreview"].Bool() && p.tabType != ESelectionScreen::campaignList)
  126. {
  127. ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP);
  128. std::unique_ptr<CMap> campaignMap = nullptr;
  129. if(p.tabType != ESelectionScreen::newGame && config["variables"]["mapPreviewForSaves"].Bool())
  130. {
  131. CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), ESerializationVersion::MINIMAL);
  132. lf.checkMagicBytes(SAVEGAME_MAGIC);
  133. auto mapHeader = std::make_unique<CMapHeader>();
  134. StartInfo * startInfo;
  135. lf >> *(mapHeader) >> startInfo;
  136. if(startInfo->campState)
  137. campaignMap = startInfo->campState->getMap(*startInfo->campState->currentScenario(), nullptr);
  138. res = ResourcePath(startInfo->fileURI, EResType::MAP);
  139. }
  140. if(!campaignMap)
  141. minimaps = createMinimaps(res);
  142. else
  143. minimaps = createMinimaps(campaignMap);
  144. }
  145. REGISTER_BUILDER("drawMinimap", &CMapOverviewWidget::buildDrawMinimap);
  146. build(config);
  147. if(auto w = widget<CFilledTexture>("background"))
  148. {
  149. p.pos = w->pos;
  150. }
  151. if(auto w = widget<CTextBox>("fileName"))
  152. {
  153. w->setText(p.fileName);
  154. }
  155. if(auto w = widget<CLabel>("mapName"))
  156. {
  157. w->setText(p.mapName);
  158. }
  159. if(auto w = widget<CLabel>("date"))
  160. {
  161. if(p.date.empty())
  162. {
  163. std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), p.tabType == ESelectionScreen::campaignList ? EResType::CAMPAIGN : EResType::MAP)));
  164. w->setText(TextOperations::getFormattedDateTimeLocal(time));
  165. }
  166. else
  167. w->setText(p.date);
  168. }
  169. if(auto w = widget<CLabel>("author"))
  170. {
  171. w->setText(p.author.empty() ? "-" : p.author);
  172. }
  173. if(auto w = widget<CLabel>("version"))
  174. {
  175. w->setText(p.version);
  176. }
  177. if(auto w = widget<CLabel>("noUnderground"))
  178. {
  179. if(minimaps.size() == 0)
  180. w->setText("");
  181. }
  182. }