CMapOverview.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/CConfigHandler.h"
  27. #include "../../lib/campaign/CampaignState.h"
  28. #include "../../lib/mapping/CMap.h"
  29. #include "../../lib/mapping/CMapService.h"
  30. #include "../../lib/mapping/CMapInfo.h"
  31. #include "../../lib/mapping/CMapHeader.h"
  32. #include "../../lib/mapping/MapFormat.h"
  33. #include "../../lib/TerrainHandler.h"
  34. CMapOverview::CMapOverview(std::string mapName, std::string fileName, std::string date, ResourcePath resource, ESelectionScreen tabType)
  35. : CWindowObject(BORDERED | RCLICK_POPUP), resource(resource), mapName(mapName), fileName(fileName), date(date), tabType(tabType)
  36. {
  37. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  38. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  39. pos = Rect(0, 0, config["items"][0]["rect"]["w"].Integer(), config["items"][0]["rect"]["h"].Integer());
  40. widget = std::make_shared<CMapOverviewWidget>(*this);
  41. updateShadow();
  42. center(GH.getCursorPosition()); //center on mouse
  43. #ifdef VCMI_MOBILE
  44. moveBy({0, -pos.h / 2});
  45. #endif
  46. fitToScreen(10);
  47. }
  48. Canvas CMapOverview::CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
  49. {
  50. Canvas canvas = Canvas(Point(map->width, map->height));
  51. for (int y = 0; y < map->height; ++y)
  52. for (int x = 0; x < map->width; ++x)
  53. {
  54. TerrainTile & tile = map->getTile(int3(x, y, layer));
  55. ColorRGBA color = tile.terType->minimapUnblocked;
  56. if (tile.blocked && (!tile.visitable))
  57. color = tile.terType->minimapBlocked;
  58. if(drawPlayerElements)
  59. // if object at tile is owned - it will be colored as its owner
  60. for (const CGObjectInstance *obj : tile.blockingObjects)
  61. {
  62. PlayerColor player = obj->getOwner();
  63. if(player == PlayerColor::NEUTRAL)
  64. {
  65. color = graphics->neutralColor;
  66. break;
  67. }
  68. if (player.isValidPlayer())
  69. {
  70. color = graphics->playerColors[player.getNum()];
  71. break;
  72. }
  73. }
  74. canvas.drawPoint(Point(x, y), color);
  75. }
  76. return canvas;
  77. }
  78. std::vector<std::shared_ptr<IImage>> CMapOverview::CMapOverviewWidget::createMinimaps(ResourcePath resource, Point size) const
  79. {
  80. std::vector<std::shared_ptr<IImage>> ret = std::vector<std::shared_ptr<IImage>>();
  81. CMapService mapService;
  82. std::unique_ptr<CMap> map;
  83. try
  84. {
  85. map = mapService.loadMap(resource);
  86. }
  87. catch (...)
  88. {
  89. return ret;
  90. }
  91. for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
  92. {
  93. Canvas canvas = createMinimapForLayer(map, i);
  94. Canvas canvasScaled = Canvas(size);
  95. canvasScaled.drawScaled(canvas, Point(0, 0), size);
  96. std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
  97. ret.push_back(img);
  98. }
  99. return ret;
  100. }
  101. std::shared_ptr<TransparentFilledRectangle> CMapOverview::CMapOverviewWidget::CMapOverviewWidget::buildDrawTransparentRect(const JsonNode & config) const
  102. {
  103. logGlobal->debug("Building widget drawTransparentRect");
  104. auto rect = readRect(config["rect"]);
  105. auto color = readColor(config["color"]);
  106. if(!config["colorLine"].isNull())
  107. {
  108. auto colorLine = readColor(config["colorLine"]);
  109. return std::make_shared<TransparentFilledRectangle>(rect, color, colorLine);
  110. }
  111. return std::make_shared<TransparentFilledRectangle>(rect, color);
  112. }
  113. std::shared_ptr<CPicture> CMapOverview::CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const
  114. {
  115. logGlobal->debug("Building widget drawMinimap");
  116. auto rect = readRect(config["rect"]);
  117. auto id = config["id"].Integer();
  118. if(!renderImage)
  119. return nullptr;
  120. const std::vector<std::shared_ptr<IImage>> images = createMinimaps(ResourcePath(parent.resource.getName(), EResType::MAP), Point(rect.w, rect.h));
  121. if(id >= images.size())
  122. return nullptr;
  123. return std::make_shared<CPicture>(images[id], Point(rect.x, rect.y));
  124. }
  125. std::shared_ptr<CTextBox> CMapOverview::CMapOverviewWidget::buildDrawPath(const JsonNode & config) const
  126. {
  127. logGlobal->debug("Building widget drawPath");
  128. auto rect = readRect(config["rect"]);
  129. auto font = readFont(config["font"]);
  130. auto alignment = readTextAlignment(config["alignment"]);
  131. auto color = readColor(config["color"]);
  132. return std::make_shared<CTextBox>(parent.fileName, rect, 0, font, alignment, color);
  133. }
  134. std::shared_ptr<CLabel> CMapOverview::CMapOverviewWidget::buildDrawString(const JsonNode & config) const
  135. {
  136. logGlobal->debug("Building widget drawString");
  137. auto font = readFont(config["font"]);
  138. auto alignment = readTextAlignment(config["alignment"]);
  139. auto color = readColor(config["color"]);
  140. std::string text = "";
  141. if("mapname" == config["text"].String())
  142. text = parent.mapName;
  143. if("date" == config["text"].String())
  144. if(parent.date.empty())
  145. {
  146. //std::time_t time =
  147. }
  148. else
  149. text = parent.date;
  150. auto position = readPosition(config["position"]);
  151. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  152. }
  153. CMapOverview::CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
  154. InterfaceObjectConfigurable(), parent(parent)
  155. {
  156. drawPlayerElements = parent.tabType == ESelectionScreen::newGame;
  157. renderImage = parent.tabType == ESelectionScreen::newGame && settings["lobby"]["mapPreview"].Bool();
  158. const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
  159. REGISTER_BUILDER("drawTransparentRect", &CMapOverview::CMapOverviewWidget::buildDrawTransparentRect);
  160. REGISTER_BUILDER("drawMinimap", &CMapOverview::CMapOverviewWidget::buildDrawMinimap);
  161. REGISTER_BUILDER("drawPath", &CMapOverview::CMapOverviewWidget::buildDrawPath);
  162. REGISTER_BUILDER("drawString", &CMapOverview::CMapOverviewWidget::buildDrawString);
  163. drawPlayerElements = parent.tabType == ESelectionScreen::newGame;
  164. renderImage = parent.tabType == ESelectionScreen::newGame && settings["lobby"]["mapPreview"].Bool();
  165. build(config);
  166. }