CMinimap.cpp 5.4 KB

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