CMinimap.cpp 6.3 KB

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