MapView.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. }
  88. void MapView::onMapLevelSwitched()
  89. {
  90. if(LOCPLINT->cb->getMapSize().z > 1)
  91. {
  92. if(model->getLevel() == 0)
  93. controller->setViewCenter(model->getMapViewCenter(), 1);
  94. else
  95. controller->setViewCenter(model->getMapViewCenter(), 0);
  96. }
  97. }
  98. void MapView::onMapScrolled(const Point & distance)
  99. {
  100. if(!isGesturing())
  101. controller->setViewCenter(model->getMapViewCenter() + distance, model->getLevel());
  102. }
  103. void MapView::onMapSwiped(const Point & viewPosition)
  104. {
  105. if(settings["adventure"]["smoothDragging"].Bool())
  106. swipeHistory.push_back(std::pair<uint32_t, Point>(GH.input().getTicks(), viewPosition));
  107. controller->setViewCenter(model->getMapViewCenter() + viewPosition, model->getLevel());
  108. }
  109. void MapView::postSwipe(uint32_t msPassed) {
  110. if(!actions->dragActive)
  111. {
  112. if(swipeHistory.size() > 1)
  113. {
  114. Point diff = Point(0, 0);
  115. std::pair<uint32_t, Point> firstAccepted;
  116. uint32_t now = GH.input().getTicks();
  117. for (auto & x : swipeHistory) {
  118. if(now - x.first < 150) { // only the last 150 ms are catched
  119. if(firstAccepted.first == 0)
  120. firstAccepted = x;
  121. diff += x.second;
  122. }
  123. }
  124. uint32_t timediff = swipeHistory.rbegin()->first - firstAccepted.first;
  125. postSwipeAngle = diff.angle();
  126. postSwipeSpeed = static_cast<double>(diff.length()) / static_cast<double>(timediff); // unit: pixel/millisecond
  127. }
  128. swipeHistory.clear();
  129. } else
  130. postSwipeSpeed = 0.0;
  131. if(postSwipeSpeed > 0.1) {
  132. double len = postSwipeSpeed * static_cast<double>(msPassed);
  133. Point delta = Point(len * cos(postSwipeAngle), len * sin(postSwipeAngle));
  134. controller->setViewCenter(model->getMapViewCenter() + delta, model->getLevel());
  135. postSwipeSpeed /= 1.1;
  136. }
  137. }
  138. void MapView::onCenteredTile(const int3 & tile)
  139. {
  140. controller->setViewCenter(tile);
  141. }
  142. void MapView::onCenteredObject(const CGObjectInstance * target)
  143. {
  144. controller->setViewCenter(target->getSightCenter());
  145. }
  146. void MapView::onViewSpellActivated(uint32_t tileSize, const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain)
  147. {
  148. controller->activateSpellViewContext();
  149. controller->setTileSize(Point(tileSize, tileSize));
  150. controller->setOverlayVisibility(objectPositions);
  151. controller->setTerrainVisibility(showTerrain);
  152. }
  153. void MapView::onViewWorldActivated(uint32_t tileSize)
  154. {
  155. controller->activateWorldViewContext();
  156. controller->setTileSize(Point(tileSize, tileSize));
  157. }
  158. void MapView::onMapZoomLevelChanged(int stepsChange)
  159. {
  160. controller->modifyTileSize(stepsChange);
  161. }
  162. void MapView::onViewMapActivated()
  163. {
  164. controller->activateAdventureContext();
  165. controller->setTileSize(Point(32, 32));
  166. }
  167. PuzzleMapView::PuzzleMapView(const Point & offset, const Point & dimensions, const int3 & tileToCenter)
  168. : BasicMapView(offset, dimensions)
  169. {
  170. controller->activatePuzzleMapContext(tileToCenter);
  171. controller->setViewCenter(tileToCenter);
  172. }