MapView.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. BasicMapView::~BasicMapView() = default;
  29. std::shared_ptr<MapViewModel> BasicMapView::createModel(const Point & dimensions) const
  30. {
  31. auto result = std::make_shared<MapViewModel>();
  32. result->setLevel(0);
  33. result->setTileSize(Point(32, 32));
  34. result->setViewCenter(Point(0, 0));
  35. result->setViewDimensions(dimensions);
  36. return result;
  37. }
  38. BasicMapView::BasicMapView(const Point & offset, const Point & dimensions)
  39. : model(createModel(dimensions))
  40. , tilesCache(new MapViewCache(model))
  41. , controller(new MapViewController(model, tilesCache))
  42. {
  43. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  44. pos += offset;
  45. pos.w = dimensions.x;
  46. pos.h = dimensions.y;
  47. }
  48. void BasicMapView::render(Canvas & target, bool fullUpdate)
  49. {
  50. Canvas targetClipped(target, pos);
  51. tilesCache->update(controller->getContext());
  52. tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
  53. }
  54. void BasicMapView::tick(uint32_t msPassed)
  55. {
  56. controller->tick(msPassed);
  57. }
  58. void BasicMapView::show(Canvas & to)
  59. {
  60. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), pos);
  61. render(to, false);
  62. controller->afterRender();
  63. }
  64. void BasicMapView::showAll(Canvas & to)
  65. {
  66. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), pos);
  67. render(to, true);
  68. }
  69. void MapView::show(Canvas & to)
  70. {
  71. actions->setContext(controller->getContext());
  72. BasicMapView::show(to);
  73. }
  74. MapView::MapView(const Point & offset, const Point & dimensions)
  75. : BasicMapView(offset, dimensions)
  76. {
  77. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  78. actions = std::make_shared<MapViewActions>(*this, model);
  79. actions->setContext(controller->getContext());
  80. }
  81. void MapView::onMapLevelSwitched()
  82. {
  83. if(LOCPLINT->cb->getMapSize().z > 1)
  84. {
  85. if(model->getLevel() == 0)
  86. controller->setViewCenter(model->getMapViewCenter(), 1);
  87. else
  88. controller->setViewCenter(model->getMapViewCenter(), 0);
  89. }
  90. }
  91. void MapView::onMapScrolled(const Point & distance)
  92. {
  93. if(!isGesturing())
  94. controller->setViewCenter(model->getMapViewCenter() + distance, model->getLevel());
  95. }
  96. void MapView::onMapSwiped(const Point & viewPosition)
  97. {
  98. controller->setViewCenter(model->getMapViewCenter() + viewPosition, model->getLevel());
  99. }
  100. void MapView::onCenteredTile(const int3 & tile)
  101. {
  102. controller->setViewCenter(tile);
  103. }
  104. void MapView::onCenteredObject(const CGObjectInstance * target)
  105. {
  106. controller->setViewCenter(target->getSightCenter());
  107. }
  108. void MapView::onViewSpellActivated(uint32_t tileSize, const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain)
  109. {
  110. controller->activateSpellViewContext();
  111. controller->setTileSize(Point(tileSize, tileSize));
  112. controller->setOverlayVisibility(objectPositions);
  113. controller->setTerrainVisibility(showTerrain);
  114. }
  115. void MapView::onViewWorldActivated(uint32_t tileSize)
  116. {
  117. controller->activateWorldViewContext();
  118. controller->setTileSize(Point(tileSize, tileSize));
  119. }
  120. void MapView::onMapZoomLevelChanged(int stepsChange)
  121. {
  122. controller->modifyTileSize(stepsChange);
  123. }
  124. void MapView::onViewMapActivated()
  125. {
  126. controller->activateAdventureContext();
  127. controller->setTileSize(Point(32, 32));
  128. }
  129. PuzzleMapView::PuzzleMapView(const Point & offset, const Point & dimensions, const int3 & tileToCenter)
  130. : BasicMapView(offset, dimensions)
  131. {
  132. controller->activatePuzzleMapContext(tileToCenter);
  133. controller->setViewCenter(tileToCenter);
  134. }