2
0

CMinimap.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/MouseButton.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../render/Colors.h"
  20. #include "../renderSDL/SDL_Extensions.h"
  21. #include "../render/Canvas.h"
  22. #include "../windows/InfoWindows.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/TerrainHandler.h"
  26. #include "../../lib/mapObjects/CGHeroInstance.h"
  27. #include "../../lib/mapping/CMapDefines.h"
  28. #include <SDL_pixels.h>
  29. ColorRGBA CMinimapInstance::getTileColor(const int3 & pos) const
  30. {
  31. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  32. // if tile is not visible it will be black on minimap
  33. if(!tile)
  34. return CSDL_Ext::fromSDL(Colors::BLACK);
  35. // if object at tile is owned - it will be colored as its owner
  36. for (const CGObjectInstance *obj : tile->blockingObjects)
  37. {
  38. PlayerColor player = obj->getOwner();
  39. if(player == PlayerColor::NEUTRAL)
  40. return CSDL_Ext::fromSDL(*graphics->neutralColor);
  41. if (player < PlayerColor::PLAYER_LIMIT)
  42. return CSDL_Ext::fromSDL(graphics->playerColors[player.getNum()]);
  43. }
  44. if (tile->blocked && (!tile->visitable))
  45. return tile->terType->minimapBlocked;
  46. else
  47. return tile->terType->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 = LOCPLINT->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(CMinimap *Parent, int Level):
  62. parent(Parent),
  63. minimap(new Canvas(Point(LOCPLINT->cb->getMapSize().x, LOCPLINT->cb->getMapSize().y))),
  64. level(Level)
  65. {
  66. pos.w = parent->pos.w;
  67. pos.h = parent->pos.h;
  68. redrawMinimap();
  69. }
  70. void CMinimapInstance::showAll(SDL_Surface * to)
  71. {
  72. Canvas target(to);
  73. target.drawScaled(*minimap, pos.topLeft(), pos.dimensions());
  74. }
  75. CMinimap::CMinimap(const Rect & position)
  76. : CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  77. level(0)
  78. {
  79. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  80. pos.w = position.w;
  81. pos.h = position.h;
  82. aiShield = std::make_shared<CPicture>("AIShield");
  83. aiShield->disable();
  84. }
  85. int3 CMinimap::pixelToTile(const Point & cursorPos) const
  86. {
  87. // 0 = top-left corner, 1 = bottom-right corner
  88. double dx = static_cast<double>(cursorPos.x) / pos.w;
  89. double dy = static_cast<double>(cursorPos.y) / pos.h;
  90. int3 mapSizes = LOCPLINT->cb->getMapSize();
  91. int tileX(std::round(mapSizes.x * dx));
  92. int tileY(std::round(mapSizes.y * dy));
  93. return int3(tileX, tileY, level);
  94. }
  95. Point CMinimap::tileToPixels(const int3 &tile) const
  96. {
  97. int3 mapSizes = LOCPLINT->cb->getMapSize();
  98. double stepX = static_cast<double>(pos.w) / mapSizes.x;
  99. double stepY = static_cast<double>(pos.h) / mapSizes.y;
  100. int x = static_cast<int>(stepX * tile.x);
  101. int y = static_cast<int>(stepY * tile.y);
  102. return Point(x,y);
  103. }
  104. void CMinimap::moveAdvMapSelection()
  105. {
  106. int3 newLocation = pixelToTile(GH.getCursorPosition() - pos.topLeft());
  107. adventureInt->centerOnTile(newLocation);
  108. if (!(adventureInt->isActive()))
  109. GH.windows().totalRedraw(); //redraw this as well as inactive adventure map
  110. else
  111. redraw();//redraw only this
  112. }
  113. void CMinimap::clickLeft(tribool down, bool previousState)
  114. {
  115. if(down)
  116. moveAdvMapSelection();
  117. }
  118. void CMinimap::clickRight(tribool down, bool previousState)
  119. {
  120. if (down)
  121. CRClickPopup::createAndPush(CGI->generaltexth->zelp[291].second);
  122. }
  123. void CMinimap::hover(bool on)
  124. {
  125. if(on)
  126. GH.statusbar()->write(CGI->generaltexth->zelp[291].first);
  127. else
  128. GH.statusbar()->clear();
  129. }
  130. void CMinimap::mouseMoved(const Point & cursorPosition)
  131. {
  132. if(isMouseButtonPressed(MouseButton::LEFT))
  133. moveAdvMapSelection();
  134. }
  135. void CMinimap::showAll(SDL_Surface * to)
  136. {
  137. CSDL_Ext::CClipRectGuard guard(to, pos);
  138. CIntObject::showAll(to);
  139. if(minimap)
  140. {
  141. Canvas target(to);
  142. int3 mapSizes = LOCPLINT->cb->getMapSize();
  143. //draw radar
  144. Rect radar =
  145. {
  146. screenArea.x * pos.w / mapSizes.x,
  147. screenArea.y * pos.h / mapSizes.y,
  148. screenArea.w * pos.w / mapSizes.x - 1,
  149. screenArea.h * pos.h / mapSizes.y - 1
  150. };
  151. Canvas clippedTarget(target, pos);
  152. clippedTarget.drawBorderDashed(radar, CSDL_Ext::fromSDL(Colors::PURPLE));
  153. }
  154. }
  155. void CMinimap::update()
  156. {
  157. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  158. return;
  159. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  160. minimap = std::make_shared<CMinimapInstance>(this, level);
  161. redraw();
  162. }
  163. void CMinimap::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  164. {
  165. if (screenArea == visibleArea && level == mapLevel)
  166. return;
  167. screenArea = visibleArea;
  168. if(level != mapLevel)
  169. {
  170. level = mapLevel;
  171. update();
  172. }
  173. else
  174. redraw();
  175. }
  176. void CMinimap::setAIRadar(bool on)
  177. {
  178. if(on)
  179. {
  180. aiShield->enable();
  181. minimap.reset();
  182. }
  183. else
  184. {
  185. aiShield->disable();
  186. update();
  187. }
  188. redraw();
  189. }
  190. void CMinimap::updateTiles(std::unordered_set<int3> positions)
  191. {
  192. if(minimap)
  193. {
  194. for (auto const & tile : positions)
  195. minimap->refreshTile(tile);
  196. }
  197. redraw();
  198. }