MapView.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "../eventsSDL/InputHandler.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.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. }
  49. void BasicMapView::render(Canvas & target, bool fullUpdate)
  50. {
  51. Canvas targetClipped(target, pos);
  52. tilesCache->update(controller->getContext());
  53. tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
  54. }
  55. void BasicMapView::tick(uint32_t msPassed)
  56. {
  57. controller->tick(msPassed);
  58. }
  59. void BasicMapView::show(Canvas & to)
  60. {
  61. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), pos);
  62. render(to, false);
  63. controller->afterRender();
  64. }
  65. void BasicMapView::showAll(Canvas & to)
  66. {
  67. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), pos);
  68. render(to, true);
  69. }
  70. void MapView::tick(uint32_t msPassed)
  71. {
  72. if(settings["adventure"]["smoothDragging"].Bool())
  73. postSwipe(msPassed);
  74. BasicMapView::tick(msPassed);
  75. }
  76. void MapView::show(Canvas & to)
  77. {
  78. actions->setContext(controller->getContext());
  79. BasicMapView::show(to);
  80. }
  81. MapView::MapView(const Point & offset, const Point & dimensions)
  82. : BasicMapView(offset, dimensions)
  83. {
  84. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  85. actions = std::make_shared<MapViewActions>(*this, model);
  86. actions->setContext(controller->getContext());
  87. // catch min 10 frames
  88. postSwipeCatchIntervalMs = static_cast<int>(10.0 * 1000.0 * (1.0 / settings["video"]["targetfps"].Float()));
  89. }
  90. void MapView::onMapLevelSwitched()
  91. {
  92. if(LOCPLINT->cb->getMapSize().z > 1)
  93. {
  94. if(model->getLevel() == 0)
  95. controller->setViewCenter(model->getMapViewCenter(), 1);
  96. else
  97. controller->setViewCenter(model->getMapViewCenter(), 0);
  98. }
  99. }
  100. void MapView::onMapScrolled(const Point & distance)
  101. {
  102. if(!isGesturing())
  103. controller->setViewCenter(model->getMapViewCenter() + distance, model->getLevel());
  104. }
  105. void MapView::onMapSwiped(const Point & viewPosition)
  106. {
  107. if(settings["adventure"]["smoothDragging"].Bool())
  108. swipeHistory.push_back(std::pair<uint32_t, Point>(GH.input().getTicks(), viewPosition));
  109. controller->setViewCenter(model->getMapViewCenter() + viewPosition, model->getLevel());
  110. }
  111. void MapView::postSwipe(uint32_t msPassed)
  112. {
  113. if(!actions->dragActive)
  114. {
  115. if(swipeHistory.size() > 1)
  116. {
  117. Point diff = Point(0, 0);
  118. std::pair<uint32_t, Point> firstAccepted;
  119. uint32_t now = GH.input().getTicks();
  120. for (auto & x : swipeHistory) {
  121. if(now - x.first < postSwipeCatchIntervalMs) { // only the last x ms are catched
  122. if(firstAccepted.first == 0)
  123. firstAccepted = x;
  124. diff += x.second;
  125. }
  126. }
  127. uint32_t timediff = swipeHistory.back().first - firstAccepted.first;
  128. if(diff.length() > 0 && timediff > 0)
  129. {
  130. postSwipeAngle = diff.angle();
  131. postSwipeSpeed = static_cast<double>(diff.length()) / static_cast<double>(timediff); // unit: pixel/millisecond
  132. }
  133. }
  134. swipeHistory.clear();
  135. } else
  136. postSwipeSpeed = 0.0;
  137. if(postSwipeSpeed > postSwipeMinimalSpeed) {
  138. double len = postSwipeSpeed * static_cast<double>(msPassed);
  139. Point delta = Point(len * cos(postSwipeAngle), len * sin(postSwipeAngle));
  140. controller->setViewCenter(model->getMapViewCenter() + delta, model->getLevel());
  141. postSwipeSpeed /= 1 + msPassed * postSwipeSlowdownSpeed;
  142. }
  143. }
  144. void MapView::onCenteredTile(const int3 & tile)
  145. {
  146. controller->setViewCenter(tile);
  147. }
  148. void MapView::onCenteredObject(const CGObjectInstance * target)
  149. {
  150. controller->setViewCenter(target->getSightCenter());
  151. }
  152. void MapView::onViewSpellActivated(uint32_t tileSize, const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain)
  153. {
  154. controller->activateSpellViewContext();
  155. controller->setTileSize(Point(tileSize, tileSize));
  156. controller->setOverlayVisibility(objectPositions);
  157. controller->setTerrainVisibility(showTerrain);
  158. }
  159. void MapView::onViewWorldActivated(uint32_t tileSize)
  160. {
  161. controller->activateWorldViewContext();
  162. controller->setTileSize(Point(tileSize, tileSize));
  163. }
  164. void MapView::onMapZoomLevelChanged(int stepsChange)
  165. {
  166. controller->modifyTileSize(stepsChange);
  167. }
  168. void MapView::onViewMapActivated()
  169. {
  170. controller->activateAdventureContext();
  171. controller->setTileSize(Point(32, 32));
  172. }
  173. PuzzleMapView::PuzzleMapView(const Point & offset, const Point & dimensions, const int3 & tileToCenter)
  174. : BasicMapView(offset, dimensions)
  175. {
  176. controller->activatePuzzleMapContext(tileToCenter);
  177. controller->setViewCenter(tileToCenter);
  178. }