CMinimap.cpp 6.2 KB

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