CMapOverview.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/CAnimation.h"
  21. #include "../render/Canvas.h"
  22. #include "../render/IImage.h"
  23. #include "../render/IRenderHandler.h"
  24. #include "../render/Graphics.h"
  25. #include "../../lib/CGeneralTextHandler.h"
  26. #include "../../lib/TextOperations.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/campaign/CampaignState.h"
  29. #include "../../lib/mapping/CMap.h"
  30. #include "../../lib/mapping/CMapService.h"
  31. #include "../../lib/mapping/CMapInfo.h"
  32. #include "../../lib/mapping/CMapHeader.h"
  33. #include "../../lib/mapping/MapFormat.h"
  34. #include "../../lib/TerrainHandler.h"
  35. #include "../../lib/filesystem/Filesystem.h"
  36. #include "../../lib/serializer/CLoadFile.h"
  37. #include "../../lib/StartInfo.h"
  38. #include "../../lib/rmg/CMapGenOptions.h"
  39. #include "../../lib/Languages.h"
  40. CMapOverview::CMapOverview(std::string mapName, std::string fileName, std::string date, ResourcePath resource, ESelectionScreen tabType)
  41. : CWindowObject(BORDERED | RCLICK_POPUP), resource(resource), mapName(mapName), fileName(fileName), date(date), tabType(tabType)
  42. {
  43. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  44. widget = std::make_shared<CMapOverviewWidget>(*this);
  45. updateShadow();
  46. center(GH.getCursorPosition()); //center on mouse
  47. #ifdef VCMI_MOBILE
  48. moveBy({0, -pos.h / 2});
  49. #endif
  50. fitToScreen(10);
  51. }
  52. Canvas CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
  53. {
  54. Canvas canvas = Canvas(Point(map->width, map->height));
  55. for (int y = 0; y < map->height; ++y)
  56. for (int x = 0; x < map->width; ++x)
  57. {
  58. TerrainTile & tile = map->getTile(int3(x, y, layer));
  59. ColorRGBA color = tile.terType->minimapUnblocked;
  60. if (tile.blocked && (!tile.visitable))
  61. color = tile.terType->minimapBlocked;
  62. if(drawPlayerElements)
  63. // if object at tile is owned - it will be colored as its owner
  64. for (const CGObjectInstance *obj : tile.blockingObjects)
  65. {
  66. PlayerColor player = obj->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 canvas;
  81. }
  82. std::vector<Canvas> CMapOverviewWidget::createMinimaps(ResourcePath resource) const
  83. {
  84. auto ret = std::vector<Canvas>();
  85. CMapService mapService;
  86. std::unique_ptr<CMap> map;
  87. try
  88. {
  89. map = mapService.loadMap(resource);
  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<Canvas> CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map) const
  99. {
  100. auto ret = std::vector<Canvas>();
  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. Rect minimapRect = minimaps[id].getRenderArea();
  113. double maxSideLenghtSrc = std::max(minimapRect.w, minimapRect.h);
  114. double maxSideLenghtDst = std::max(rect.w, rect.h);
  115. double resize = maxSideLenghtSrc / maxSideLenghtDst;
  116. Point newMinimapSize = Point(minimapRect.w / resize, minimapRect.h / resize);
  117. Canvas canvasScaled = Canvas(Point(rect.w, rect.h));
  118. canvasScaled.drawScaled(minimaps[id], Point((rect.w - newMinimapSize.x) / 2, (rect.h - newMinimapSize.y) / 2), newMinimapSize);
  119. std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
  120. return std::make_shared<CPicture>(img, Point(rect.x, rect.y));
  121. }
  122. CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
  123. InterfaceObjectConfigurable(), p(parent)
  124. {
  125. drawPlayerElements = p.tabType == ESelectionScreen::newGame;
  126. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  127. if(settings["lobby"]["mapPreview"].Bool() && p.tabType != ESelectionScreen::campaignList)
  128. {
  129. ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP);
  130. std::unique_ptr<CMap> campaignMap = nullptr;
  131. if(p.tabType != ESelectionScreen::newGame && config["variables"]["mapPreviewForSaves"].Bool())
  132. {
  133. CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), MINIMAL_SERIALIZATION_VERSION);
  134. lf.checkMagicBytes(SAVEGAME_MAGIC);
  135. auto mapHeader = std::make_unique<CMapHeader>();
  136. StartInfo * startInfo;
  137. lf >> *(mapHeader) >> startInfo;
  138. if(startInfo->campState)
  139. campaignMap = startInfo->campState->getMap(*startInfo->campState->currentScenario());
  140. res = ResourcePath(startInfo->fileURI, EResType::MAP);
  141. }
  142. if(!campaignMap)
  143. minimaps = createMinimaps(res);
  144. else
  145. minimaps = createMinimaps(campaignMap);
  146. }
  147. REGISTER_BUILDER("drawMinimap", &CMapOverviewWidget::buildDrawMinimap);
  148. build(config);
  149. if(auto w = widget<CFilledTexture>("background"))
  150. {
  151. p.pos = w->pos;
  152. }
  153. if(auto w = widget<CTextBox>("fileName"))
  154. {
  155. w->setText(p.fileName);
  156. }
  157. if(auto w = widget<CLabel>("mapName"))
  158. {
  159. w->setText(p.mapName);
  160. }
  161. if(auto w = widget<CLabel>("date"))
  162. {
  163. if(p.date.empty())
  164. {
  165. std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), p.tabType == ESelectionScreen::campaignList ? EResType::CAMPAIGN : EResType::MAP)));
  166. w->setText(TextOperations::getFormattedDateTimeLocal(time));
  167. }
  168. else
  169. w->setText(p.date);
  170. }
  171. if(auto w = widget<CLabel>("noUnderground"))
  172. {
  173. if(minimaps.size() == 0)
  174. w->setText("");
  175. }
  176. }