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 <vstd/DateUtils.h>
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../widgets/CComponent.h"
  17. #include "../widgets/MiscWidgets.h"
  18. #include "../widgets/TextControls.h"
  19. #include "../windows/GUIClasses.h"
  20. #include "../windows/InfoWindows.h"
  21. #include "../render/CAnimation.h"
  22. #include "../render/Canvas.h"
  23. #include "../render/IImage.h"
  24. #include "../render/IRenderHandler.h"
  25. #include "../render/Graphics.h"
  26. #include "../../lib/CGeneralTextHandler.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. CMapOverview::CMapOverview(std::string mapName, std::string fileName, std::string date, ResourcePath resource, ESelectionScreen tabType)
  40. : CWindowObject(BORDERED | RCLICK_POPUP), resource(resource), mapName(mapName), fileName(fileName), date(date), tabType(tabType)
  41. {
  42. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  43. widget = std::make_shared<CMapOverviewWidget>(*this);
  44. updateShadow();
  45. center(GH.getCursorPosition()); //center on mouse
  46. #ifdef VCMI_MOBILE
  47. moveBy({0, -pos.h / 2});
  48. #endif
  49. fitToScreen(10);
  50. }
  51. Canvas CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
  52. {
  53. Canvas canvas = Canvas(Point(map->width, map->height));
  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.terType->minimapUnblocked;
  59. if (tile.blocked && (!tile.visitable))
  60. color = tile.terType->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 canvas;
  80. }
  81. std::vector<Canvas> CMapOverviewWidget::createMinimaps(ResourcePath resource) const
  82. {
  83. std::vector<Canvas> ret = std::vector<Canvas>();
  84. CMapService mapService;
  85. std::unique_ptr<CMap> map;
  86. try
  87. {
  88. map = mapService.loadMap(resource);
  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<Canvas> CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map) const
  98. {
  99. std::vector<Canvas> ret = std::vector<Canvas>();
  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. Rect minimapRect = minimaps[id].getRenderArea();
  112. double maxSideLenghtSrc = std::max(minimapRect.w, minimapRect.h);
  113. double maxSideLenghtDst = std::max(rect.w, rect.h);
  114. double resize = maxSideLenghtSrc / maxSideLenghtDst;
  115. Point newMinimapSize = Point(minimapRect.w / resize, minimapRect.h / resize);
  116. Canvas canvasScaled = Canvas(Point(rect.w, rect.h));
  117. canvasScaled.drawScaled(minimaps[id], Point((rect.w - newMinimapSize.x) / 2, (rect.h - newMinimapSize.y) / 2), newMinimapSize);
  118. std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
  119. return std::make_shared<CPicture>(img, Point(rect.x, rect.y));
  120. }
  121. CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
  122. InterfaceObjectConfigurable(), p(parent)
  123. {
  124. drawPlayerElements = p.tabType == ESelectionScreen::newGame;
  125. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  126. if(settings["lobby"]["mapPreview"].Bool() && p.tabType != ESelectionScreen::campaignList)
  127. {
  128. ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP);
  129. std::unique_ptr<CMap> campaignMap = nullptr;
  130. if(p.tabType != ESelectionScreen::newGame && config["variables"]["mapPreviewForSaves"].Bool())
  131. {
  132. CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), MINIMAL_SERIALIZATION_VERSION);
  133. lf.checkMagicBytes(SAVEGAME_MAGIC);
  134. std::unique_ptr<CMapHeader> mapHeader = std::make_unique<CMapHeader>();
  135. StartInfo * startInfo;
  136. lf >> *(mapHeader) >> startInfo;
  137. if(startInfo->campState)
  138. campaignMap = startInfo->campState->getMap(*startInfo->campState->currentScenario());
  139. res = ResourcePath(startInfo->fileURI, EResType::MAP);
  140. }
  141. if(!campaignMap)
  142. minimaps = createMinimaps(res);
  143. else
  144. minimaps = createMinimaps(campaignMap);
  145. }
  146. REGISTER_BUILDER("drawMinimap", &CMapOverviewWidget::buildDrawMinimap);
  147. build(config);
  148. if(auto w = widget<CFilledTexture>("background"))
  149. {
  150. p.pos = w->pos;
  151. }
  152. if(auto w = widget<CTextBox>("fileName"))
  153. {
  154. w->setText(p.fileName);
  155. }
  156. if(auto w = widget<CLabel>("mapName"))
  157. {
  158. w->setText(p.mapName);
  159. }
  160. if(auto w = widget<CLabel>("date"))
  161. {
  162. if(p.date.empty())
  163. {
  164. std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), p.tabType == ESelectionScreen::campaignList ? EResType::CAMPAIGN : EResType::MAP)));
  165. w->setText(vstd::getFormattedDateTime(time));
  166. }
  167. else
  168. w->setText(p.date);
  169. }
  170. if(auto w = widget<CLabel>("noUnderground"))
  171. {
  172. if(minimaps.size() == 0)
  173. w->setText("");
  174. }
  175. }