CMinimap.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * CMinimap.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 "CMinimap.h"
  12. #include "AdventureMapInterface.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../gui/MouseButton.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../render/CAnimation.h"
  19. #include "../render/Canvas.h"
  20. #include "../render/Colors.h"
  21. #include "../render/Graphics.h"
  22. #include "../render/IRenderHandler.h"
  23. #include "../widgets/Images.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/TerrainHandler.h"
  27. #include "../../lib/callback/CCallback.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/mapping/CMapDefines.h"
  30. #include "../../lib/texts/CGeneralTextHandler.h"
  31. ColorRGBA CMinimapInstance::getTileColor(const int3 & pos) const
  32. {
  33. const TerrainTile * tile = GAME->interface()->cb->getTile(pos, false);
  34. // if tile is not visible it will be black on minimap
  35. if(!tile)
  36. return Colors::BLACK;
  37. // if object at tile is owned - it will be colored as its owner
  38. for (const ObjectInstanceID objectID : tile->blockingObjects)
  39. {
  40. const auto * obj = GAME->interface()->cb->getObj(objectID);
  41. PlayerColor player = obj->getOwner();
  42. if(player == PlayerColor::NEUTRAL)
  43. return graphics->neutralColor;
  44. if (player.isValidPlayer())
  45. return graphics->playerColors[player.getNum()];
  46. }
  47. if (tile->blocked() && !tile->visitable())
  48. return tile->getTerrain()->minimapBlocked;
  49. else
  50. return tile->getTerrain()->minimapUnblocked;
  51. }
  52. void CMinimapInstance::refreshTile(const int3 &tile)
  53. {
  54. if (level == tile.z)
  55. minimap->drawPoint(Point(tile.x, tile.y), getTileColor(tile));
  56. }
  57. void CMinimapInstance::redrawMinimap()
  58. {
  59. int3 mapSizes = GAME->interface()->cb->getMapSize();
  60. for (int y = 0; y < mapSizes.y; ++y)
  61. for (int x = 0; x < mapSizes.x; ++x)
  62. minimap->drawPoint(Point(x, y), getTileColor(int3(x, y, level)));
  63. }
  64. CMinimapInstance::CMinimapInstance(const Point & position, const Point & dimensions, int Level):
  65. minimap(new Canvas(Point(GAME->interface()->cb->getMapSize().x, GAME->interface()->cb->getMapSize().y), CanvasScalingPolicy::IGNORE)),
  66. level(Level)
  67. {
  68. pos += position;
  69. pos.w = dimensions.x;
  70. pos.h = dimensions.y;
  71. redrawMinimap();
  72. }
  73. CMinimapInstance::~CMinimapInstance() = default;
  74. void CMinimapInstance::showAll(Canvas & to)
  75. {
  76. to.drawScaled(*minimap, pos.topLeft(), pos.dimensions());
  77. }
  78. CMinimap::CMinimap(const Rect & position)
  79. : CIntObject(LCLICK | SHOW_POPUP | DRAG | MOVE | GESTURE, position.topLeft())
  80. , heroIcon(ENGINE->renderHandler().loadImage(ImagePath::builtin("minimapIcons/hero"), EImageBlitMode::WITH_SHADOW_AND_FLAG_COLOR))
  81. , level(0)
  82. {
  83. OBJECT_CONSTRUCTION;
  84. double maxSideLengthSrc = std::max(GAME->interface()->cb->getMapSize().x, GAME->interface()->cb->getMapSize().y);
  85. double maxSideLengthDst = std::max(position.w, position.h);
  86. double resize = maxSideLengthSrc / maxSideLengthDst;
  87. Point newMinimapSize(GAME->interface()->cb->getMapSize().x/ resize, GAME->interface()->cb->getMapSize().y / resize);
  88. Point offset = Point((std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.x) / 2, (std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.y) / 2);
  89. pos.x += offset.x;
  90. pos.y += offset.y;
  91. pos.w = newMinimapSize.x;
  92. pos.h = newMinimapSize.y;
  93. aiShield = std::make_shared<CPicture>(ImagePath::builtin("AIShield"), -offset);
  94. aiShield->disable();
  95. }
  96. int3 CMinimap::pixelToTile(const Point & cursorPos) const
  97. {
  98. // 0 = top-left corner, 1 = bottom-right corner
  99. double dx = static_cast<double>(cursorPos.x) / pos.w;
  100. double dy = static_cast<double>(cursorPos.y) / pos.h;
  101. int3 mapSizes = GAME->interface()->cb->getMapSize();
  102. int tileX(std::round(mapSizes.x * dx));
  103. int tileY(std::round(mapSizes.y * dy));
  104. return int3(tileX, tileY, level);
  105. }
  106. Point CMinimap::tileToPixels(const int3 &tile) const
  107. {
  108. int3 mapSizes = GAME->interface()->cb->getMapSize();
  109. double stepX = static_cast<double>(pos.w) / mapSizes.x;
  110. double stepY = static_cast<double>(pos.h) / mapSizes.y;
  111. int x = static_cast<int>(stepX * tile.x);
  112. int y = static_cast<int>(stepY * tile.y);
  113. return Point(x,y);
  114. }
  115. void CMinimap::moveAdvMapSelection(const Point & positionGlobal)
  116. {
  117. int3 newLocation = pixelToTile(positionGlobal - pos.topLeft());
  118. adventureInt->centerOnTile(newLocation);
  119. if (!(adventureInt->isActive()))
  120. ENGINE->windows().totalRedraw(); //redraw this as well as inactive adventure map
  121. else
  122. redraw();//redraw only this
  123. }
  124. void CMinimap::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  125. {
  126. if (pos.isInside(currentPosition))
  127. moveAdvMapSelection(currentPosition);
  128. }
  129. void CMinimap::clickPressed(const Point & cursorPosition)
  130. {
  131. moveAdvMapSelection(cursorPosition);
  132. }
  133. void CMinimap::showPopupWindow(const Point & cursorPosition)
  134. {
  135. CRClickPopup::createAndPush(LIBRARY->generaltexth->zelp[291].second);
  136. }
  137. void CMinimap::hover(bool on)
  138. {
  139. if(on)
  140. ENGINE->statusbar()->write(LIBRARY->generaltexth->zelp[291].first);
  141. else
  142. ENGINE->statusbar()->clear();
  143. }
  144. void CMinimap::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
  145. {
  146. moveAdvMapSelection(cursorPosition);
  147. }
  148. void CMinimap::showAll(Canvas & to)
  149. {
  150. CanvasClipRectGuard guard(to, aiShield->pos);
  151. CIntObject::showAll(to);
  152. if(minimap)
  153. {
  154. int3 mapSizes = GAME->interface()->cb->getMapSize();
  155. Canvas clippedTarget(to, pos);
  156. if (settings["adventure"]["minimapShowHeroes"].Bool())
  157. {
  158. for (const auto objectID : visibleHeroes)
  159. {
  160. const auto * object = GAME->interface()->cb->getObj(objectID);
  161. if (object->anchorPos().z != level)
  162. continue;
  163. heroIcon->setOverlayColor(graphics->playerColors[object->getOwner().getNum()]);
  164. clippedTarget.draw(heroIcon, tileToPixels(object->visitablePos()) - heroIcon->dimensions() / 2);
  165. }
  166. }
  167. //draw radar
  168. Rect radar =
  169. {
  170. screenArea.x * pos.w / mapSizes.x,
  171. screenArea.y * pos.h / mapSizes.y,
  172. screenArea.w * pos.w / mapSizes.x - 1,
  173. screenArea.h * pos.h / mapSizes.y - 1
  174. };
  175. clippedTarget.drawBorderDashed(radar, Colors::PURPLE);
  176. }
  177. }
  178. void CMinimap::update()
  179. {
  180. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  181. return;
  182. OBJECT_CONSTRUCTION;
  183. minimap = std::make_shared<CMinimapInstance>(Point(0,0), pos.dimensions(), level);
  184. updateVisibleHeroes();
  185. redraw();
  186. }
  187. void CMinimap::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  188. {
  189. if (screenArea == visibleArea && level == mapLevel)
  190. return;
  191. screenArea = visibleArea;
  192. if(level != mapLevel)
  193. {
  194. level = mapLevel;
  195. update();
  196. }
  197. else
  198. redraw();
  199. }
  200. void CMinimap::setAIRadar(bool on)
  201. {
  202. if(on)
  203. {
  204. aiShield->enable();
  205. minimap.reset();
  206. }
  207. else
  208. {
  209. aiShield->disable();
  210. update();
  211. }
  212. redraw();
  213. }
  214. void CMinimap::updateVisibleHeroes()
  215. {
  216. visibleHeroes.clear();
  217. for (const auto & player : PlayerColor::ALL_PLAYERS())
  218. {
  219. if (GAME->interface()->cb->getPlayerStatus(player, false) != EPlayerStatus::INGAME)
  220. continue;
  221. for (const auto & hero : GAME->interface()->cb->getHeroes(player))
  222. visibleHeroes.push_back(hero->id);
  223. }
  224. }
  225. void CMinimap::updateTiles(const std::unordered_set<int3> & positions)
  226. {
  227. if(minimap)
  228. {
  229. for (auto const & tile : positions)
  230. minimap->refreshTile(tile);
  231. }
  232. updateVisibleHeroes();
  233. redraw();
  234. }