CMinimap.cpp 6.1 KB

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