CMapOverview.cpp 6.4 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 "../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/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(ENGINE->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 = ENGINE->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 (ObjectInstanceID objectID : tile.blockingObjects)
  64. {
  65. const auto * object = map->getObject(objectID);
  66. PlayerColor player = object->getOwner();
  67. if(player == PlayerColor::NEUTRAL)
  68. {
  69. color = graphics->neutralColor;
  70. break;
  71. }
  72. if (player.isValidPlayer())
  73. {
  74. color = graphics->playerColors[player.getNum()];
  75. break;
  76. }
  77. }
  78. canvas.drawPoint(Point(x, y), color);
  79. }
  80. return canvasImage;
  81. }
  82. std::vector<std::shared_ptr<CanvasImage>> CMapOverviewWidget::createMinimaps(const ResourcePath & resource) const
  83. {
  84. std::vector<std::shared_ptr<CanvasImage>> ret;
  85. CMapService mapService;
  86. std::unique_ptr<CMap> map;
  87. try
  88. {
  89. map = mapService.loadMap(resource, nullptr);
  90. }
  91. catch (const std::exception & e)
  92. {
  93. logGlobal->warn("Failed to generate map preview! %s", e.what());
  94. return ret;
  95. }
  96. return createMinimaps(map);
  97. }
  98. std::vector<std::shared_ptr<CanvasImage>> CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map) const
  99. {
  100. std::vector<std::shared_ptr<CanvasImage>> ret;
  101. for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
  102. ret.push_back(createMinimapForLayer(map, i));
  103. return ret;
  104. }
  105. std::shared_ptr<CPicture> CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const
  106. {
  107. logGlobal->debug("Building widget drawMinimap");
  108. auto rect = readRect(config["rect"]);
  109. auto id = config["id"].Integer();
  110. if(id >= minimaps.size())
  111. return nullptr;
  112. Point minimapRect = minimaps[id]->dimensions();
  113. double maxSideLengthSrc = std::max(minimapRect.x, minimapRect.y);
  114. double maxSideLengthDst = std::max(rect.w, rect.h);
  115. double resize = maxSideLengthSrc / maxSideLengthDst;
  116. Point newMinimapSize(minimapRect.x / resize, minimapRect.y / resize);
  117. minimaps[id]->scaleTo(newMinimapSize, EScalingAlgorithm::NEAREST); // for sharp-looking minimap
  118. return std::make_shared<CPicture>(minimaps[id], 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. }