CTerrainRect.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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/CFadeAnimation.h"
  21. #include "../render/IImage.h"
  22. #include "../renderSDL/SDL_Extensions.h"
  23. #include "../widgets/TextControls.h"
  24. #include "../CMT.h"
  25. #include "../../CCallback.h"
  26. #include "../../lib/CConfigHandler.h"
  27. #include "../../lib/mapping/CMap.h"
  28. #include "../../lib/CPathfinder.h"
  29. #include <SDL_surface.h>
  30. #define ADVOPT (conf.go()->ac)
  31. CTerrainRect::CTerrainRect()
  32. : fadeSurface(nullptr),
  33. lastRedrawStatus(EMapAnimRedrawStatus::OK),
  34. fadeAnim(std::make_shared<CFadeAnimation>()),
  35. curHoveredTile(-1,-1,-1),
  36. currentPath(nullptr)
  37. {
  38. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  39. pos.x=ADVOPT.advmapX;
  40. pos.y=ADVOPT.advmapY;
  41. pos.w=ADVOPT.advmapW;
  42. pos.h=ADVOPT.advmapH;
  43. addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
  44. renderer = std::make_shared<MapView>( Point(0,0), pos.dimensions() );
  45. }
  46. CTerrainRect::~CTerrainRect()
  47. {
  48. if(fadeSurface)
  49. SDL_FreeSurface(fadeSurface);
  50. }
  51. void CTerrainRect::setViewCenter(const int3 &coordinates)
  52. {
  53. renderer->setViewCenter(coordinates);
  54. }
  55. void CTerrainRect::setViewCenter(const Point & position, int level)
  56. {
  57. renderer->setViewCenter(position, level);
  58. }
  59. void CTerrainRect::deactivate()
  60. {
  61. CIntObject::deactivate();
  62. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  63. }
  64. void CTerrainRect::clickLeft(tribool down, bool previousState)
  65. {
  66. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  67. return;
  68. if(indeterminate(down))
  69. return;
  70. #if defined(VCMI_MOBILE)
  71. if(adventureInt->swipeEnabled)
  72. {
  73. if(handleSwipeStateChange((bool)down == true))
  74. {
  75. return; // if swipe is enabled, we don't process "down" events and wait for "up" (to make sure this wasn't a swiping gesture)
  76. }
  77. }
  78. else
  79. {
  80. #endif
  81. if(down == false)
  82. return;
  83. #if defined(VCMI_MOBILE)
  84. }
  85. #endif
  86. int3 mp = whichTileIsIt();
  87. if(mp.x < 0 || mp.y < 0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  88. return;
  89. adventureInt->tileLClicked(mp);
  90. }
  91. void CTerrainRect::clickRight(tribool down, bool previousState)
  92. {
  93. #if defined(VCMI_MOBILE)
  94. if(adventureInt->swipeEnabled && isSwiping)
  95. return;
  96. #endif
  97. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  98. return;
  99. int3 mp = whichTileIsIt();
  100. if(CGI->mh->map->isInTheMap(mp) && down)
  101. adventureInt->tileRClicked(mp);
  102. }
  103. void CTerrainRect::clickMiddle(tribool down, bool previousState)
  104. {
  105. handleSwipeStateChange((bool)down == true);
  106. }
  107. void CTerrainRect::mouseMoved(const Point & cursorPosition)
  108. {
  109. handleHover(cursorPosition);
  110. if(!adventureInt->swipeEnabled)
  111. return;
  112. handleSwipeMove(cursorPosition);
  113. }
  114. void CTerrainRect::handleSwipeMove(const Point & cursorPosition)
  115. {
  116. #if defined(VCMI_MOBILE)
  117. if(!GH.isMouseButtonPressed() || GH.multifinger) // any "button" is enough on mobile
  118. return;
  119. #else
  120. if(!GH.isMouseButtonPressed(MouseButton::MIDDLE)) // swipe only works with middle mouse on other platforms
  121. return;
  122. #endif
  123. if(!isSwiping)
  124. {
  125. // try to distinguish if this touch was meant to be a swipe or just fat-fingering press
  126. if(abs(cursorPosition.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
  127. abs(cursorPosition.y - swipeInitialRealPos.y) > SwipeTouchSlop)
  128. {
  129. isSwiping = true;
  130. }
  131. }
  132. if(isSwiping)
  133. {
  134. adventureInt->swipeTargetPosition.x = swipeInitialViewPos.x + swipeInitialRealPos.x - cursorPosition.x;
  135. adventureInt->swipeTargetPosition.y = swipeInitialViewPos.y + swipeInitialRealPos.y - cursorPosition.y;
  136. adventureInt->swipeMovementRequested = true;
  137. }
  138. }
  139. bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
  140. {
  141. if(btnPressed)
  142. {
  143. swipeInitialRealPos = Point(GH.getCursorPosition().x, GH.getCursorPosition().y);
  144. swipeInitialViewPos = getViewCenter();
  145. return true;
  146. }
  147. else if(isSwiping) // only accept this touch if it wasn't a swipe
  148. {
  149. isSwiping = false;
  150. return true;
  151. }
  152. return false;
  153. }
  154. void CTerrainRect::handleHover(const Point & cursorPosition)
  155. {
  156. int3 tHovered = whichTileIsIt(cursorPosition);
  157. int3 pom = adventureInt->verifyPos(tHovered);
  158. if(tHovered != pom) //tile outside the map
  159. {
  160. CCS->curh->set(Cursor::Map::POINTER);
  161. return;
  162. }
  163. if (pom != curHoveredTile)
  164. {
  165. curHoveredTile = pom;
  166. adventureInt->tileHovered(pom);
  167. }
  168. }
  169. void CTerrainRect::hover(bool on)
  170. {
  171. if (!on)
  172. {
  173. GH.statusbar->clear();
  174. CCS->curh->set(Cursor::Map::POINTER);
  175. }
  176. //Hoverable::hover(on);
  177. }
  178. void CTerrainRect::showPath(const Rect & extRect, SDL_Surface * to)
  179. {
  180. /*
  181. const static int pns[9][9] = {
  182. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  183. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  184. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  185. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  186. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  187. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  188. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  189. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  190. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  191. }; //table of magic values TODO meaning, change variable name
  192. for (int i = 0; i < -1 + (int)currentPath->nodes.size(); ++i)
  193. {
  194. const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
  195. if(curPos.z != adventureInt->position.z)
  196. continue;
  197. int pn=-1;//number of picture
  198. if (i==0) //last tile
  199. {
  200. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  201. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  202. if (x<0 || y<0 || x>pos.w || y>pos.h)
  203. continue;
  204. pn=0;
  205. }
  206. else
  207. {
  208. const int3 &prevPos = currentPath->nodes[i-1].coord;
  209. std::vector<CGPathNode> & cv = currentPath->nodes;
  210. // Vector directions
  211. // 0 1 2
  212. // \ | /
  213. // 3 - 4 - 5
  214. // / | \
  215. // 6 7 8
  216. //For example:
  217. // |
  218. // |__\
  219. // /
  220. // is id1=7, id2=5 (pns[7][5])
  221. //
  222. bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
  223. if(pathContinuous && cv[i].action != CGPathNode::EMBARK && cv[i].action != CGPathNode::DISEMBARK)
  224. {
  225. int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
  226. int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
  227. pn=pns[id1][id2];
  228. }
  229. else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
  230. {
  231. pn = 0;
  232. }
  233. }
  234. if (currentPath->nodes[i].turns)
  235. pn+=25;
  236. if (pn>=0)
  237. {
  238. const auto arrow = graphics->heroMoveArrows->getImage(pn);
  239. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  240. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  241. if (x< -32 || y< -32 || x>pos.w || y>pos.h)
  242. continue;
  243. int hvx = (x + arrow->width()) - (pos.x + pos.w),
  244. hvy = (y + arrow->height()) - (pos.y + pos.h);
  245. Rect prevClip;
  246. CSDL_Ext::getClipRect(to, prevClip);
  247. CSDL_Ext::setClipRect(to, extRect); //preventing blitting outside of that rect
  248. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  249. {
  250. if (hvx<0 && hvy<0)
  251. {
  252. arrow->draw(to, x + moveX, y + moveY);
  253. }
  254. else if(hvx<0)
  255. {
  256. Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width()));
  257. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  258. }
  259. else if (hvy<0)
  260. {
  261. Rect srcRect (Point(0, 0), Point(arrow->height(), arrow->width() - hvx));
  262. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  263. }
  264. else
  265. {
  266. Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width() - hvx));
  267. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  268. }
  269. }
  270. else //standard version
  271. {
  272. if (hvx<0 && hvy<0)
  273. {
  274. arrow->draw(to, x, y);
  275. }
  276. else if(hvx<0)
  277. {
  278. Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width()));
  279. arrow->draw(to, x, y, &srcRect);
  280. }
  281. else if (hvy<0)
  282. {
  283. Rect srcRect (Point(0, 0), Point(arrow->height(), arrow->width() - hvx));
  284. arrow->draw(to, x, y, &srcRect);
  285. }
  286. else
  287. {
  288. Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width() - hvx));
  289. arrow->draw(to, x, y, &srcRect);
  290. }
  291. }
  292. CSDL_Ext::setClipRect(to, prevClip);
  293. }
  294. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  295. */}
  296. /*
  297. void CTerrainRect::show(SDL_Surface * to)
  298. {
  299. if (adventureInt->mode == EAdvMapMode::NORMAL)
  300. {
  301. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), pos);
  302. info.otherheroAnim = true;
  303. info.anim = adventureInt->anim;
  304. info.heroAnim = adventureInt->heroAnim;
  305. if (ADVOPT.smoothMove)
  306. info.movement = int3(moveX, moveY, 0);
  307. lastRedrawStatus = CGI->mh->drawTerrainRectNew(to, &info);
  308. if (fadeAnim->isFading())
  309. {
  310. Rect r(pos);
  311. fadeAnim->update();
  312. fadeAnim->draw(to, r.topLeft());
  313. }
  314. if (currentPath) //drawing path
  315. {
  316. showPath(pos, to);
  317. }
  318. }
  319. }
  320. void CTerrainRect::showAll(SDL_Surface * to)
  321. {
  322. // world view map is static and doesn't need redraw every frame
  323. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  324. {
  325. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), pos, adventureInt->worldViewIcons);
  326. info.scaled = true;
  327. info.scale = adventureInt->worldViewScale;
  328. adventureInt->worldViewOptions.adjustDrawingInfo(info);
  329. CGI->mh->drawTerrainRectNew(to, &info);
  330. }
  331. }
  332. void CTerrainRect::showAnim(SDL_Surface * to)
  333. {
  334. if (!needsAnimUpdate())
  335. return;
  336. if (fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED)
  337. show(to);
  338. }
  339. */
  340. int3 CTerrainRect::whichTileIsIt(const Point &position)
  341. {
  342. return renderer->getTileAtPoint(position - pos);
  343. }
  344. int3 CTerrainRect::whichTileIsIt()
  345. {
  346. return whichTileIsIt(GH.getCursorPosition());
  347. }
  348. Rect CTerrainRect::visibleTilesArea()
  349. {
  350. return renderer->getVisibleAreaTiles();
  351. }
  352. void CTerrainRect::fadeFromCurrentView()
  353. {
  354. if (!ADVOPT.screenFading)
  355. return;
  356. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  357. return;
  358. if (!fadeSurface)
  359. fadeSurface = CSDL_Ext::newSurface(pos.w, pos.h);
  360. CSDL_Ext::blitSurface(screen, fadeSurface, Point(0,0));
  361. fadeAnim->init(CFadeAnimation::EMode::OUT, fadeSurface);
  362. }
  363. bool CTerrainRect::needsAnimUpdate()
  364. {
  365. return fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED;
  366. }
  367. void CTerrainRect::setLevel(int level)
  368. {
  369. renderer->setViewCenter(renderer->getViewCenter(), level);
  370. }
  371. void CTerrainRect::moveViewBy(const Point & delta)
  372. {
  373. renderer->setViewCenter(renderer->getViewCenter() + delta, getLevel());
  374. }
  375. int3 CTerrainRect::getTileCenter()
  376. {
  377. return renderer->getTileCenter();
  378. }
  379. Point CTerrainRect::getViewCenter()
  380. {
  381. return renderer->getViewCenter();
  382. }
  383. int CTerrainRect::getLevel()
  384. {
  385. return renderer->getTileCenter().z;
  386. }