CTerrainRect.cpp 5.6 KB

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