CTerrainRect.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 "CAdvMapInt.h"
  14. #include "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../gui/CursorHandler.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../render/CAnimation.h"
  19. #include "../render/CFadeAnimation.h"
  20. #include "../render/IImage.h"
  21. #include "../renderSDL/SDL_Extensions.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/mapping/CMap.h"
  26. #include "../../lib/CPathfinder.h"
  27. #include <SDL_events.h>
  28. #define ADVOPT (conf.go()->ac)
  29. CTerrainRect::CTerrainRect()
  30. : fadeSurface(nullptr),
  31. lastRedrawStatus(EMapAnimRedrawStatus::OK),
  32. fadeAnim(std::make_shared<CFadeAnimation>()),
  33. curHoveredTile(-1,-1,-1),
  34. currentPath(nullptr)
  35. {
  36. tilesw=(ADVOPT.advmapW+31)/32;
  37. tilesh=(ADVOPT.advmapH+31)/32;
  38. pos.x=ADVOPT.advmapX;
  39. pos.y=ADVOPT.advmapY;
  40. pos.w=ADVOPT.advmapW;
  41. pos.h=ADVOPT.advmapH;
  42. moveX = moveY = 0;
  43. addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
  44. }
  45. CTerrainRect::~CTerrainRect()
  46. {
  47. if(fadeSurface)
  48. SDL_FreeSurface(fadeSurface);
  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_ANDROID) || defined(VCMI_IOS)
  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. {
  71. #endif
  72. if(down == false)
  73. return;
  74. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  75. }
  76. #endif
  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_ANDROID) || defined(VCMI_IOS)
  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->map->isInTheMap(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_ANDROID) || defined(VCMI_IOS)
  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(abs(cursorPosition.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
  118. abs(cursorPosition.y - swipeInitialRealPos.y) > SwipeTouchSlop)
  119. {
  120. isSwiping = true;
  121. }
  122. }
  123. if(isSwiping)
  124. {
  125. adventureInt->swipeTargetPosition.x =
  126. swipeInitialMapPos.x + static_cast<si32>(swipeInitialRealPos.x - cursorPosition.x) / 32;
  127. adventureInt->swipeTargetPosition.y =
  128. swipeInitialMapPos.y + static_cast<si32>(swipeInitialRealPos.y - cursorPosition.y) / 32;
  129. adventureInt->swipeMovementRequested = true;
  130. }
  131. }
  132. bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
  133. {
  134. if(btnPressed)
  135. {
  136. swipeInitialRealPos = Point(GH.getCursorPosition().x, GH.getCursorPosition().y);
  137. swipeInitialMapPos = int3(adventureInt->position);
  138. return true;
  139. }
  140. else if(isSwiping) // only accept this touch if it wasn't a swipe
  141. {
  142. isSwiping = false;
  143. return true;
  144. }
  145. return false;
  146. }
  147. void CTerrainRect::handleHover(const Point & cursorPosition)
  148. {
  149. int3 tHovered = whichTileIsIt(cursorPosition.x, cursorPosition.y);
  150. int3 pom = adventureInt->verifyPos(tHovered);
  151. if(tHovered != pom) //tile outside the map
  152. {
  153. CCS->curh->set(Cursor::Map::POINTER);
  154. return;
  155. }
  156. if (pom != curHoveredTile)
  157. {
  158. curHoveredTile = pom;
  159. adventureInt->tileHovered(pom);
  160. }
  161. }
  162. void CTerrainRect::hover(bool on)
  163. {
  164. if (!on)
  165. {
  166. adventureInt->statusbar->clear();
  167. CCS->curh->set(Cursor::Map::POINTER);
  168. }
  169. //Hoverable::hover(on);
  170. }
  171. void CTerrainRect::showPath(const Rect & extRect, SDL_Surface * to)
  172. {
  173. const static int pns[9][9] = {
  174. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  175. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  176. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  177. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  178. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  179. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  180. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  181. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  182. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  183. }; //table of magic values TODO meaning, change variable name
  184. for (int i = 0; i < -1 + (int)currentPath->nodes.size(); ++i)
  185. {
  186. const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
  187. if(curPos.z != adventureInt->position.z)
  188. continue;
  189. int pn=-1;//number of picture
  190. if (i==0) //last tile
  191. {
  192. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  193. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  194. if (x<0 || y<0 || x>pos.w || y>pos.h)
  195. continue;
  196. pn=0;
  197. }
  198. else
  199. {
  200. const int3 &prevPos = currentPath->nodes[i-1].coord;
  201. std::vector<CGPathNode> & cv = currentPath->nodes;
  202. /* Vector directions
  203. * 0 1 2
  204. * \ | /
  205. * 3 - 4 - 5
  206. * / | \
  207. * 6 7 8
  208. *For example:
  209. * |
  210. * |__\
  211. * /
  212. * is id1=7, id2=5 (pns[7][5])
  213. */
  214. bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
  215. if(pathContinuous && cv[i].action != CGPathNode::EMBARK && cv[i].action != CGPathNode::DISEMBARK)
  216. {
  217. int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
  218. int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
  219. pn=pns[id1][id2];
  220. }
  221. else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
  222. {
  223. pn = 0;
  224. }
  225. }
  226. if (currentPath->nodes[i].turns)
  227. pn+=25;
  228. if (pn>=0)
  229. {
  230. const auto arrow = graphics->heroMoveArrows->getImage(pn);
  231. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  232. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  233. if (x< -32 || y< -32 || x>pos.w || y>pos.h)
  234. continue;
  235. int hvx = (x + arrow->width()) - (pos.x + pos.w),
  236. hvy = (y + arrow->height()) - (pos.y + pos.h);
  237. Rect prevClip;
  238. CSDL_Ext::getClipRect(to, prevClip);
  239. CSDL_Ext::setClipRect(to, extRect); //preventing blitting outside of that rect
  240. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  241. {
  242. if (hvx<0 && hvy<0)
  243. {
  244. arrow->draw(to, x + moveX, y + moveY);
  245. }
  246. else if(hvx<0)
  247. {
  248. Rect srcRect = CSDL_Ext::genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  249. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  250. }
  251. else if (hvy<0)
  252. {
  253. Rect srcRect = CSDL_Ext::genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  254. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  255. }
  256. else
  257. {
  258. Rect srcRect = CSDL_Ext::genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  259. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  260. }
  261. }
  262. else //standard version
  263. {
  264. if (hvx<0 && hvy<0)
  265. {
  266. arrow->draw(to, x, y);
  267. }
  268. else if(hvx<0)
  269. {
  270. Rect srcRect = CSDL_Ext::genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  271. arrow->draw(to, x, y, &srcRect);
  272. }
  273. else if (hvy<0)
  274. {
  275. Rect srcRect = CSDL_Ext::genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  276. arrow->draw(to, x, y, &srcRect);
  277. }
  278. else
  279. {
  280. Rect srcRect = CSDL_Ext::genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  281. arrow->draw(to, x, y, &srcRect);
  282. }
  283. }
  284. CSDL_Ext::setClipRect(to, prevClip);
  285. }
  286. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  287. }
  288. void CTerrainRect::show(SDL_Surface * to)
  289. {
  290. if (adventureInt->mode == EAdvMapMode::NORMAL)
  291. {
  292. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), pos);
  293. info.otherheroAnim = true;
  294. info.anim = adventureInt->anim;
  295. info.heroAnim = adventureInt->heroAnim;
  296. if (ADVOPT.smoothMove)
  297. info.movement = int3(moveX, moveY, 0);
  298. lastRedrawStatus = CGI->mh->drawTerrainRectNew(to, &info);
  299. if (fadeAnim->isFading())
  300. {
  301. Rect r(pos);
  302. fadeAnim->update();
  303. fadeAnim->draw(to, r.topLeft());
  304. }
  305. if (currentPath/* && adventureInt->position.z==currentPath->startPos().z*/) //drawing path
  306. {
  307. showPath(pos, to);
  308. }
  309. }
  310. }
  311. void CTerrainRect::showAll(SDL_Surface * to)
  312. {
  313. // world view map is static and doesn't need redraw every frame
  314. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  315. {
  316. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), pos, adventureInt->worldViewIcons);
  317. info.scaled = true;
  318. info.scale = adventureInt->worldViewScale;
  319. adventureInt->worldViewOptions.adjustDrawingInfo(info);
  320. CGI->mh->drawTerrainRectNew(to, &info);
  321. }
  322. }
  323. void CTerrainRect::showAnim(SDL_Surface * to)
  324. {
  325. if (fadeAnim->isFading())
  326. show(to);
  327. else if (lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED)
  328. show(to); // currently the same; maybe we should pass some flag to map handler so it redraws ONLY tiles that need redraw instead of full
  329. }
  330. int3 CTerrainRect::whichTileIsIt(const int x, const int y)
  331. {
  332. int3 ret;
  333. ret.x = adventureInt->position.x + ((x-CGI->mh->offsetX-pos.x)/32);
  334. ret.y = adventureInt->position.y + ((y-CGI->mh->offsetY-pos.y)/32);
  335. ret.z = adventureInt->position.z;
  336. return ret;
  337. }
  338. int3 CTerrainRect::whichTileIsIt()
  339. {
  340. return whichTileIsIt(GH.getCursorPosition().x, GH.getCursorPosition().y);
  341. }
  342. int3 CTerrainRect::tileCountOnScreen()
  343. {
  344. switch (adventureInt->mode)
  345. {
  346. default:
  347. logGlobal->error("Unknown map mode %d", (int)adventureInt->mode);
  348. return int3();
  349. case EAdvMapMode::NORMAL:
  350. return int3(tilesw, tilesh, 1);
  351. case EAdvMapMode::WORLD_VIEW:
  352. return int3((si32)(tilesw / adventureInt->worldViewScale), (si32)(tilesh / adventureInt->worldViewScale), 1);
  353. }
  354. }
  355. void CTerrainRect::fadeFromCurrentView()
  356. {
  357. if (!ADVOPT.screenFading)
  358. return;
  359. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  360. return;
  361. if (!fadeSurface)
  362. fadeSurface = CSDL_Ext::newSurface(pos.w, pos.h);
  363. CSDL_Ext::blitSurface(screen, fadeSurface, Point(0,0));
  364. fadeAnim->init(CFadeAnimation::EMode::OUT, fadeSurface);
  365. }
  366. bool CTerrainRect::needsAnimUpdate()
  367. {
  368. return fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED;
  369. }