CTerrainRect.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * CTerrainRect.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 "CTerrainRect.h"
  12. #include "CAdvMapInt.h"
  13. #include "../CGameInfo.h"
  14. #include "../CMT.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/CursorHandler.h"
  18. #include "../mapRenderer/mapHandler.h"
  19. #include "../render/CAnimation.h"
  20. #include "../render/IImage.h"
  21. #include "../renderSDL/SDL_Extensions.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../mapRenderer/MapView.h"
  24. #include "../mapRenderer/MapViewController.h"
  25. #include "../mapRenderer/MapViewModel.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/mapping/CMap.h"
  29. #include "../../lib/CPathfinder.h"
  30. #include <SDL_surface.h>
  31. #define ADVOPT (conf.go()->ac)
  32. CTerrainRect::CTerrainRect()
  33. : curHoveredTile(-1, -1, -1)
  34. , isSwiping(false)
  35. {
  36. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  37. pos.x=ADVOPT.advmapX;
  38. pos.y=ADVOPT.advmapY;
  39. pos.w=ADVOPT.advmapW;
  40. pos.h=ADVOPT.advmapH;
  41. addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
  42. renderer = std::make_shared<MapView>( Point(0,0), pos.dimensions() );
  43. }
  44. void CTerrainRect::setViewCenter(const int3 &coordinates)
  45. {
  46. renderer->getController()->setViewCenter(coordinates);
  47. }
  48. void CTerrainRect::setViewCenter(const Point & position, int level)
  49. {
  50. renderer->getController()->setViewCenter(position, level);
  51. }
  52. void CTerrainRect::deactivate()
  53. {
  54. CIntObject::deactivate();
  55. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  56. }
  57. void CTerrainRect::clickLeft(tribool down, bool previousState)
  58. {
  59. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  60. return;
  61. if(indeterminate(down))
  62. return;
  63. #if defined(VCMI_MOBILE)
  64. if(adventureInt->swipeEnabled)
  65. {
  66. if(handleSwipeStateChange((bool)down == true))
  67. {
  68. return; // if swipe is enabled, we don't process "down" events and wait for "up" (to make sure this wasn't a swiping gesture)
  69. }
  70. }
  71. else
  72. #endif
  73. {
  74. if(down == false)
  75. return;
  76. }
  77. int3 mp = whichTileIsIt();
  78. if(mp.x < 0 || mp.y < 0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  79. return;
  80. adventureInt->tileLClicked(mp);
  81. }
  82. void CTerrainRect::clickRight(tribool down, bool previousState)
  83. {
  84. #if defined(VCMI_MOBILE)
  85. if(adventureInt->swipeEnabled && isSwiping)
  86. return;
  87. #endif
  88. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  89. return;
  90. int3 mp = whichTileIsIt();
  91. if(CGI->mh->isInMap(mp) && down)
  92. adventureInt->tileRClicked(mp);
  93. }
  94. void CTerrainRect::clickMiddle(tribool down, bool previousState)
  95. {
  96. handleSwipeStateChange((bool)down == true);
  97. }
  98. void CTerrainRect::mouseMoved(const Point & cursorPosition)
  99. {
  100. handleHover(cursorPosition);
  101. if(!adventureInt->swipeEnabled)
  102. return;
  103. handleSwipeMove(cursorPosition);
  104. }
  105. void CTerrainRect::handleSwipeMove(const Point & cursorPosition)
  106. {
  107. #if defined(VCMI_MOBILE)
  108. if(!GH.isMouseButtonPressed() || GH.multifinger) // any "button" is enough on mobile
  109. return;
  110. #else
  111. if(!GH.isMouseButtonPressed(MouseButton::MIDDLE)) // swipe only works with middle mouse on other platforms
  112. return;
  113. #endif
  114. if(!isSwiping)
  115. {
  116. // try to distinguish if this touch was meant to be a swipe or just fat-fingering press
  117. if(std::abs(cursorPosition.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
  118. std::abs(cursorPosition.y - swipeInitialRealPos.y) > SwipeTouchSlop)
  119. {
  120. isSwiping = true;
  121. }
  122. }
  123. if(isSwiping)
  124. {
  125. adventureInt->swipeTargetPosition.x = swipeInitialViewPos.x + swipeInitialRealPos.x - cursorPosition.x;
  126. adventureInt->swipeTargetPosition.y = swipeInitialViewPos.y + swipeInitialRealPos.y - cursorPosition.y;
  127. adventureInt->swipeMovementRequested = true;
  128. }
  129. }
  130. bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
  131. {
  132. if(btnPressed)
  133. {
  134. swipeInitialRealPos = Point(GH.getCursorPosition().x, GH.getCursorPosition().y);
  135. swipeInitialViewPos = getViewCenter();
  136. return true;
  137. }
  138. if(isSwiping) // only accept this touch if it wasn't a swipe
  139. {
  140. isSwiping = false;
  141. return true;
  142. }
  143. return false;
  144. }
  145. void CTerrainRect::handleHover(const Point & cursorPosition)
  146. {
  147. int3 tHovered = whichTileIsIt(cursorPosition);
  148. if(!CGI->mh->isInMap(tHovered))
  149. {
  150. CCS->curh->set(Cursor::Map::POINTER);
  151. return;
  152. }
  153. if (tHovered != curHoveredTile)
  154. {
  155. curHoveredTile = tHovered;
  156. adventureInt->tileHovered(tHovered);
  157. }
  158. }
  159. void CTerrainRect::hover(bool on)
  160. {
  161. if (!on)
  162. {
  163. GH.statusbar->clear();
  164. CCS->curh->set(Cursor::Map::POINTER);
  165. }
  166. //Hoverable::hover(on);
  167. }
  168. int3 CTerrainRect::whichTileIsIt(const Point &position)
  169. {
  170. return renderer->getModel()->getTileAtPoint(position - pos);
  171. }
  172. int3 CTerrainRect::whichTileIsIt()
  173. {
  174. return whichTileIsIt(GH.getCursorPosition());
  175. }
  176. Rect CTerrainRect::visibleTilesArea()
  177. {
  178. return renderer->getModel()->getTilesTotalRect();
  179. }
  180. void CTerrainRect::fadeFromCurrentView()
  181. {
  182. assert(0);//TODO
  183. }
  184. void CTerrainRect::setLevel(int level)
  185. {
  186. renderer->getController()->setViewCenter(renderer->getModel()->getMapViewCenter(), level);
  187. }
  188. void CTerrainRect::moveViewBy(const Point & delta)
  189. {
  190. renderer->getController()->setViewCenter(renderer->getModel()->getMapViewCenter() + delta, getLevel());
  191. }
  192. Point CTerrainRect::getViewCenter()
  193. {
  194. return renderer->getModel()->getMapViewCenter();
  195. }
  196. int CTerrainRect::getLevel()
  197. {
  198. return renderer->getModel()->getLevel();
  199. }
  200. void CTerrainRect::setTileSize(int sizePixels)
  201. {
  202. renderer->getController()->setTileSize(Point(sizePixels, sizePixels));
  203. }