CMinimap.cpp 5.3 KB

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