CMinimap.cpp 5.2 KB

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