MapView.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * MapView.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 "MapView.h"
  12. #include "MapViewActions.h"
  13. #include "MapViewCache.h"
  14. #include "MapViewController.h"
  15. #include "MapViewModel.h"
  16. #include "mapHandler.h"
  17. #include "../CGameInfo.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../adventureMap/AdventureMapInterface.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../render/CAnimation.h"
  22. #include "../render/Canvas.h"
  23. #include "../render/IImage.h"
  24. #include "../renderSDL/SDL_Extensions.h"
  25. #include "../../CCallback.h"
  26. #include "../../lib/CConfigHandler.h"
  27. #include "../../lib/mapObjects/CGHeroInstance.h"
  28. #include "../../lib/mapping/CMap.h"
  29. BasicMapView::~BasicMapView() = default;
  30. std::shared_ptr<MapViewModel> BasicMapView::createModel(const Point & dimensions) const
  31. {
  32. auto result = std::make_shared<MapViewModel>();
  33. result->setLevel(0);
  34. result->setTileSize(Point(32, 32));
  35. result->setViewCenter(Point(0, 0));
  36. result->setViewDimensions(dimensions);
  37. return result;
  38. }
  39. BasicMapView::BasicMapView(const Point & offset, const Point & dimensions)
  40. : model(createModel(dimensions))
  41. , tilesCache(new MapViewCache(model))
  42. , controller(new MapViewController(model, tilesCache))
  43. {
  44. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  45. pos += offset;
  46. pos.w = dimensions.x;
  47. pos.h = dimensions.y;
  48. addUsedEvents(TIME);
  49. }
  50. void BasicMapView::render(Canvas & target, bool fullUpdate)
  51. {
  52. Canvas targetClipped(target, pos);
  53. tilesCache->update(controller->getContext());
  54. tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
  55. }
  56. void BasicMapView::tick(uint32_t msPassed)
  57. {
  58. controller->tick(msPassed);
  59. }
  60. void BasicMapView::show(SDL_Surface * to)
  61. {
  62. Canvas target(to);
  63. CSDL_Ext::CClipRectGuard guard(to, pos);
  64. render(target, false);
  65. controller->afterRender();
  66. }
  67. void BasicMapView::showAll(SDL_Surface * to)
  68. {
  69. Canvas target(to);
  70. CSDL_Ext::CClipRectGuard guard(to, pos);
  71. render(target, true);
  72. }
  73. void MapView::show(SDL_Surface * to)
  74. {
  75. actions->setContext(controller->getContext());
  76. BasicMapView::show(to);
  77. }
  78. MapView::MapView(const Point & offset, const Point & dimensions)
  79. : BasicMapView(offset, dimensions)
  80. , isSwiping(false)
  81. {
  82. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  83. actions = std::make_shared<MapViewActions>(*this, model);
  84. actions->setContext(controller->getContext());
  85. }
  86. void MapView::onMapLevelSwitched()
  87. {
  88. if(LOCPLINT->cb->getMapSize().z > 1)
  89. {
  90. if(model->getLevel() == 0)
  91. controller->setViewCenter(model->getMapViewCenter(), 1);
  92. else
  93. controller->setViewCenter(model->getMapViewCenter(), 0);
  94. }
  95. }
  96. void MapView::onMapScrolled(const Point & distance)
  97. {
  98. if(!isSwiping)
  99. controller->setViewCenter(model->getMapViewCenter() + distance, model->getLevel());
  100. }
  101. void MapView::onMapSwiped(const Point & viewPosition)
  102. {
  103. isSwiping = true;
  104. controller->setViewCenter(viewPosition, model->getLevel());
  105. }
  106. void MapView::onMapSwipeEnded()
  107. {
  108. isSwiping = false;
  109. }
  110. void MapView::onCenteredTile(const int3 & tile)
  111. {
  112. controller->setViewCenter(tile);
  113. }
  114. void MapView::onCenteredObject(const CGObjectInstance * target)
  115. {
  116. controller->setViewCenter(target->getSightCenter());
  117. }
  118. void MapView::onViewSpellActivated(uint32_t tileSize, const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain)
  119. {
  120. controller->activateSpellViewContext();
  121. controller->setTileSize(Point(tileSize, tileSize));
  122. controller->setOverlayVisibility(objectPositions);
  123. controller->setTerrainVisibility(showTerrain);
  124. }
  125. void MapView::onViewWorldActivated(uint32_t tileSize)
  126. {
  127. controller->activateWorldViewContext();
  128. controller->setTileSize(Point(tileSize, tileSize));
  129. }
  130. void MapView::onViewMapActivated()
  131. {
  132. controller->activateAdventureContext();
  133. controller->setTileSize(Point(32, 32));
  134. }
  135. PuzzleMapView::PuzzleMapView(const Point & offset, const Point & dimensions, const int3 & tileToCenter)
  136. : BasicMapView(offset, dimensions)
  137. {
  138. controller->activatePuzzleMapContext(tileToCenter);
  139. controller->setViewCenter(tileToCenter);
  140. }