CMinimap.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "CAdvMapInt.h"
  13. #include "CTerrainRect.h"
  14. #include "../widgets/Images.h"
  15. #include "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../render/Colors.h"
  19. #include "../renderSDL/SDL_PixelAccess.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. ColorRGBA CMinimapInstance::getTileColor(const int3 & pos) const
  27. {
  28. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  29. // if tile is not visible it will be black on minimap
  30. if(!tile)
  31. return CSDL_Ext::fromSDL(Colors::BLACK);
  32. // if object at tile is owned - it will be colored as its owner
  33. for (const CGObjectInstance *obj : tile->blockingObjects)
  34. {
  35. PlayerColor player = obj->getOwner();
  36. if(player == PlayerColor::NEUTRAL)
  37. return CSDL_Ext::fromSDL(*graphics->neutralColor);
  38. if (player < PlayerColor::PLAYER_LIMIT)
  39. return CSDL_Ext::fromSDL(graphics->playerColors[player.getNum()]);
  40. }
  41. if (tile->blocked && (!tile->visitable))
  42. return tile->terType->minimapBlocked;
  43. else
  44. return tile->terType->minimapUnblocked;
  45. }
  46. void CMinimapInstance::refreshTile(const int3 &tile)
  47. {
  48. if (level == tile.z)
  49. minimap->drawPoint(Point(tile.x, tile.y), getTileColor(tile));
  50. }
  51. void CMinimapInstance::redrawMinimap()
  52. {
  53. int3 mapSizes = LOCPLINT->cb->getMapSize();
  54. for (int y = 0; y < mapSizes.y; ++y)
  55. for (int x = 0; x < mapSizes.x; ++x)
  56. minimap->drawPoint(Point(x, y), getTileColor(int3(x, y, level)));
  57. }
  58. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  59. parent(Parent),
  60. minimap(new Canvas(Point(LOCPLINT->cb->getMapSize().x, LOCPLINT->cb->getMapSize().y))),
  61. level(Level)
  62. {
  63. pos.w = parent->pos.w;
  64. pos.h = parent->pos.h;
  65. redrawMinimap();
  66. }
  67. void CMinimapInstance::showAll(SDL_Surface * to)
  68. {
  69. Canvas target(to);
  70. target.draw(*minimap, pos.topLeft(), pos.dimensions());
  71. }
  72. CMinimap::CMinimap(const Rect & position)
  73. : CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  74. level(0)
  75. {
  76. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  77. pos.w = position.w;
  78. pos.h = position.h;
  79. aiShield = std::make_shared<CPicture>("AIShield");
  80. aiShield->disable();
  81. }
  82. int3 CMinimap::pixelToTile(const Point & cursorPos) const
  83. {
  84. // 0 = top-left corner, 1 = bottom-right corner
  85. double dx = static_cast<double>(cursorPos.x) / pos.w;
  86. double dy = static_cast<double>(cursorPos.y) / pos.h;
  87. int3 mapSizes = LOCPLINT->cb->getMapSize();
  88. int tileX(std::round(mapSizes.x * dx));
  89. int tileY(std::round(mapSizes.y * dy));
  90. return int3(tileX, tileY, level);
  91. }
  92. Point CMinimap::tileToPixels(const int3 &tile) const
  93. {
  94. int3 mapSizes = LOCPLINT->cb->getMapSize();
  95. double stepX = static_cast<double>(pos.w) / mapSizes.x;
  96. double stepY = static_cast<double>(pos.h) / mapSizes.y;
  97. int x = static_cast<int>(stepX * tile.x);
  98. int y = static_cast<int>(stepY * tile.y);
  99. return Point(x,y);
  100. }
  101. void CMinimap::moveAdvMapSelection()
  102. {
  103. int3 newLocation = pixelToTile(GH.getCursorPosition() - pos.topLeft());
  104. adventureInt->centerOn(newLocation);
  105. if (!(adventureInt->active & GENERAL))
  106. GH.totalRedraw(); //redraw this as well as inactive adventure map
  107. else
  108. redraw();//redraw only this
  109. }
  110. void CMinimap::clickLeft(tribool down, bool previousState)
  111. {
  112. if(down)
  113. moveAdvMapSelection();
  114. }
  115. void CMinimap::clickRight(tribool down, bool previousState)
  116. {
  117. if (down)
  118. CRClickPopup::createAndPush(CGI->generaltexth->zelp[291].second);
  119. }
  120. void CMinimap::hover(bool on)
  121. {
  122. if(on)
  123. GH.statusbar->write(CGI->generaltexth->zelp[291].first);
  124. else
  125. GH.statusbar->clear();
  126. }
  127. void CMinimap::mouseMoved(const Point & cursorPosition)
  128. {
  129. if(mouseState(MouseButton::LEFT))
  130. moveAdvMapSelection();
  131. }
  132. void CMinimap::showAll(SDL_Surface * to)
  133. {
  134. CIntObject::showAll(to);
  135. if(minimap)
  136. {
  137. Canvas target(to);
  138. int3 mapSizes = LOCPLINT->cb->getMapSize();
  139. Rect screenArea = adventureInt->terrainAreaTiles();
  140. //draw radar
  141. Rect radar =
  142. {
  143. screenArea.x * pos.w / mapSizes.x,
  144. screenArea.y * pos.h / mapSizes.y,
  145. screenArea.w * pos.w / mapSizes.x - 1,
  146. screenArea.h * pos.h / mapSizes.y - 1
  147. };
  148. Canvas clippedTarget(target, pos);
  149. CSDL_Ext::CClipRectGuard guard(to, 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::setLevel(int newLevel)
  162. {
  163. if (level == newLevel)
  164. return;
  165. level = newLevel;
  166. update();
  167. }
  168. void CMinimap::setAIRadar(bool on)
  169. {
  170. if(on)
  171. {
  172. aiShield->enable();
  173. minimap.reset();
  174. }
  175. else
  176. {
  177. aiShield->disable();
  178. update();
  179. }
  180. // this may happen during AI turn when this interface is inactive
  181. // force redraw in order to properly update interface
  182. GH.totalRedraw();
  183. }
  184. void CMinimap::updateTile(const int3 &pos)
  185. {
  186. if(minimap)
  187. minimap->refreshTile(pos);
  188. }