CursorHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * CCursorHandler.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 "CursorHandler.h"
  12. #include <SDL.h>
  13. #include "SDL_Extensions.h"
  14. #include "CGuiHandler.h"
  15. #include "CAnimation.h"
  16. #include "../../lib/CConfigHandler.h"
  17. std::unique_ptr<ICursor> CursorHandler::createCursor()
  18. {
  19. if (settings["video"]["cursor"].String() == "auto")
  20. {
  21. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  22. return std::make_unique<CursorSoftware>();
  23. #else
  24. return std::make_unique<CursorHardware>();
  25. #endif
  26. }
  27. if (settings["video"]["cursor"].String() == "hardware")
  28. return std::make_unique<CursorHardware>();
  29. assert(settings["video"]["cursor"].String() == "software");
  30. return std::make_unique<CursorSoftware>();
  31. }
  32. CursorHandler::CursorHandler()
  33. : cursor(createCursor())
  34. , frameTime(0.f)
  35. , showing(false)
  36. , pos(0,0)
  37. {
  38. type = Cursor::Type::DEFAULT;
  39. dndObject = nullptr;
  40. cursors =
  41. {
  42. std::make_unique<CAnimation>("CRADVNTR"),
  43. std::make_unique<CAnimation>("CRCOMBAT"),
  44. std::make_unique<CAnimation>("CRDEFLT"),
  45. std::make_unique<CAnimation>("CRSPELL")
  46. };
  47. for (auto & cursor : cursors)
  48. cursor->preload();
  49. set(Cursor::Map::POINTER);
  50. }
  51. Point CursorHandler::position() const
  52. {
  53. return pos;
  54. }
  55. void CursorHandler::changeGraphic(Cursor::Type type, size_t index)
  56. {
  57. assert(dndObject == nullptr);
  58. if (type == this->type && index == this->frame)
  59. return;
  60. this->type = type;
  61. this->frame = index;
  62. cursor->setImage(getCurrentImage(), getPivotOffset());
  63. }
  64. void CursorHandler::set(Cursor::Default index)
  65. {
  66. changeGraphic(Cursor::Type::DEFAULT, static_cast<size_t>(index));
  67. }
  68. void CursorHandler::set(Cursor::Map index)
  69. {
  70. changeGraphic(Cursor::Type::ADVENTURE, static_cast<size_t>(index));
  71. }
  72. void CursorHandler::set(Cursor::Combat index)
  73. {
  74. changeGraphic(Cursor::Type::COMBAT, static_cast<size_t>(index));
  75. }
  76. void CursorHandler::set(Cursor::Spellcast index)
  77. {
  78. //Note: this is animated cursor, ignore specified frame and only change type
  79. changeGraphic(Cursor::Type::SPELLBOOK, frame);
  80. }
  81. void CursorHandler::dragAndDropCursor(std::shared_ptr<IImage> image)
  82. {
  83. dndObject = image;
  84. cursor->setImage(getCurrentImage(), getPivotOffset());
  85. }
  86. void CursorHandler::dragAndDropCursor (std::string path, size_t index)
  87. {
  88. CAnimation anim(path);
  89. anim.load(index);
  90. dragAndDropCursor(anim.getImage(index));
  91. }
  92. void CursorHandler::cursorMove(const int & x, const int & y)
  93. {
  94. pos.x = x;
  95. pos.y = y;
  96. cursor->setCursorPosition(pos);
  97. }
  98. Point CursorHandler::getPivotOffsetDefault(size_t index)
  99. {
  100. return {0, 0};
  101. }
  102. Point CursorHandler::getPivotOffsetMap(size_t index)
  103. {
  104. static const std::array<Point, 43> offsets = {{
  105. { 0, 0}, // POINTER = 0,
  106. { 0, 0}, // HOURGLASS = 1,
  107. { 12, 10}, // HERO = 2,
  108. { 12, 12}, // TOWN = 3,
  109. { 15, 13}, // T1_MOVE = 4,
  110. { 13, 13}, // T1_ATTACK = 5,
  111. { 16, 32}, // T1_SAIL = 6,
  112. { 13, 20}, // T1_DISEMBARK = 7,
  113. { 8, 9}, // T1_EXCHANGE = 8,
  114. { 14, 16}, // T1_VISIT = 9,
  115. { 15, 13}, // T2_MOVE = 10,
  116. { 13, 13}, // T2_ATTACK = 11,
  117. { 16, 32}, // T2_SAIL = 12,
  118. { 13, 20}, // T2_DISEMBARK = 13,
  119. { 8, 9}, // T2_EXCHANGE = 14,
  120. { 14, 16}, // T2_VISIT = 15,
  121. { 15, 13}, // T3_MOVE = 16,
  122. { 13, 13}, // T3_ATTACK = 17,
  123. { 16, 32}, // T3_SAIL = 18,
  124. { 13, 20}, // T3_DISEMBARK = 19,
  125. { 8, 9}, // T3_EXCHANGE = 20,
  126. { 14, 16}, // T3_VISIT = 21,
  127. { 15, 13}, // T4_MOVE = 22,
  128. { 13, 13}, // T4_ATTACK = 23,
  129. { 16, 32}, // T4_SAIL = 24,
  130. { 13, 20}, // T4_DISEMBARK = 25,
  131. { 8, 9}, // T4_EXCHANGE = 26,
  132. { 14, 16}, // T4_VISIT = 27,
  133. { 16, 32}, // T1_SAIL_VISIT = 28,
  134. { 16, 32}, // T2_SAIL_VISIT = 29,
  135. { 16, 32}, // T3_SAIL_VISIT = 30,
  136. { 16, 32}, // T4_SAIL_VISIT = 31,
  137. { 6, 1}, // SCROLL_NORTH = 32,
  138. { 16, 2}, // SCROLL_NORTHEAST = 33,
  139. { 21, 6}, // SCROLL_EAST = 34,
  140. { 16, 16}, // SCROLL_SOUTHEAST = 35,
  141. { 6, 21}, // SCROLL_SOUTH = 36,
  142. { 1, 16}, // SCROLL_SOUTHWEST = 37,
  143. { 1, 5}, // SCROLL_WEST = 38,
  144. { 2, 1}, // SCROLL_NORTHWEST = 39,
  145. { 0, 0}, // POINTER_COPY = 40,
  146. { 14, 16}, // TELEPORT = 41,
  147. { 20, 20}, // SCUTTLE_BOAT = 42
  148. }};
  149. assert(offsets.size() == size_t(Cursor::Map::COUNT)); //Invalid number of pivot offsets for cursor
  150. assert(index < offsets.size());
  151. return offsets[index];
  152. }
  153. Point CursorHandler::getPivotOffsetCombat(size_t index)
  154. {
  155. static const std::array<Point, 20> offsets = {{
  156. { 12, 12 }, // BLOCKED = 0,
  157. { 10, 14 }, // MOVE = 1,
  158. { 14, 14 }, // FLY = 2,
  159. { 12, 12 }, // SHOOT = 3,
  160. { 12, 12 }, // HERO = 4,
  161. { 8, 12 }, // QUERY = 5,
  162. { 0, 0 }, // POINTER = 6,
  163. { 21, 0 }, // HIT_NORTHEAST = 7,
  164. { 31, 5 }, // HIT_EAST = 8,
  165. { 21, 21 }, // HIT_SOUTHEAST = 9,
  166. { 0, 21 }, // HIT_SOUTHWEST = 10,
  167. { 0, 5 }, // HIT_WEST = 11,
  168. { 0, 0 }, // HIT_NORTHWEST = 12,
  169. { 6, 0 }, // HIT_NORTH = 13,
  170. { 6, 31 }, // HIT_SOUTH = 14,
  171. { 14, 0 }, // SHOOT_PENALTY = 15,
  172. { 12, 12 }, // SHOOT_CATAPULT = 16,
  173. { 12, 12 }, // HEAL = 17,
  174. { 12, 12 }, // SACRIFICE = 18,
  175. { 14, 20 }, // TELEPORT = 19
  176. }};
  177. assert(offsets.size() == size_t(Cursor::Combat::COUNT)); //Invalid number of pivot offsets for cursor
  178. assert(index < offsets.size());
  179. return offsets[index];
  180. }
  181. Point CursorHandler::getPivotOffsetSpellcast()
  182. {
  183. return { 18, 28};
  184. }
  185. Point CursorHandler::getPivotOffset()
  186. {
  187. if (dndObject)
  188. return dndObject->dimensions() / 2;
  189. switch (type) {
  190. case Cursor::Type::ADVENTURE: return getPivotOffsetMap(frame);
  191. case Cursor::Type::COMBAT: return getPivotOffsetCombat(frame);
  192. case Cursor::Type::DEFAULT: return getPivotOffsetDefault(frame);
  193. case Cursor::Type::SPELLBOOK: return getPivotOffsetSpellcast();
  194. };
  195. assert(0);
  196. return {0, 0};
  197. }
  198. std::shared_ptr<IImage> CursorHandler::getCurrentImage()
  199. {
  200. if (dndObject)
  201. return dndObject;
  202. return cursors[static_cast<size_t>(type)]->getImage(frame);
  203. }
  204. void CursorHandler::centerCursor()
  205. {
  206. Point screenSize {screen->w, screen->h};
  207. pos = screenSize / 2 - getPivotOffset();
  208. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  209. SDL_WarpMouse(pos.x, pos.y);
  210. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  211. cursor->setCursorPosition(pos);
  212. }
  213. void CursorHandler::updateSpellcastCursor()
  214. {
  215. static const float frameDisplayDuration = 0.1f;
  216. frameTime += GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  217. size_t newFrame = frame;
  218. while (frameTime >= frameDisplayDuration)
  219. {
  220. frameTime -= frameDisplayDuration;
  221. newFrame++;
  222. }
  223. auto & animation = cursors.at(static_cast<size_t>(type));
  224. while (newFrame >= animation->size())
  225. newFrame -= animation->size();
  226. changeGraphic(Cursor::Type::SPELLBOOK, newFrame);
  227. }
  228. void CursorHandler::render()
  229. {
  230. if(!showing)
  231. return;
  232. if (type == Cursor::Type::SPELLBOOK)
  233. updateSpellcastCursor();
  234. cursor->render();
  235. }
  236. void CursorHandler::hide()
  237. {
  238. if (!showing)
  239. return;
  240. showing = false;
  241. cursor->setVisible(false);
  242. }
  243. void CursorHandler::show()
  244. {
  245. if (showing)
  246. return;
  247. showing = true;
  248. cursor->setVisible(true);
  249. }
  250. void CursorSoftware::render()
  251. {
  252. //texture must be updated in the main (renderer) thread, but changes to cursor type may come from other threads
  253. if (needUpdate)
  254. updateTexture();
  255. Point renderPos = pos - pivot;
  256. SDL_Rect destRect;
  257. destRect.x = renderPos.x;
  258. destRect.y = renderPos.y;
  259. destRect.w = 40;
  260. destRect.h = 40;
  261. SDL_RenderCopy(mainRenderer, cursorTexture, nullptr, &destRect);
  262. }
  263. void CursorSoftware::createTexture(const Point & dimensions)
  264. {
  265. if(cursorTexture)
  266. SDL_DestroyTexture(cursorTexture);
  267. if (cursorSurface)
  268. SDL_FreeSurface(cursorSurface);
  269. cursorSurface = CSDL_Ext::newSurface(dimensions.x, dimensions.y);
  270. cursorTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
  271. SDL_SetSurfaceBlendMode(cursorSurface, SDL_BLENDMODE_NONE);
  272. SDL_SetTextureBlendMode(cursorTexture, SDL_BLENDMODE_BLEND);
  273. }
  274. void CursorSoftware::updateTexture()
  275. {
  276. Point dimensions(-1, -1);
  277. if (!cursorSurface || Point(cursorSurface->w, cursorSurface->h) != cursorImage->dimensions())
  278. createTexture(cursorImage->dimensions());
  279. Uint32 fillColor = SDL_MapRGBA(cursorSurface->format, 0, 0, 0, 0);
  280. CSDL_Ext::fillRect(cursorSurface, nullptr, fillColor);
  281. cursorImage->draw(cursorSurface);
  282. SDL_UpdateTexture(cursorTexture, NULL, cursorSurface->pixels, cursorSurface->pitch);
  283. needUpdate = false;
  284. }
  285. void CursorSoftware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  286. {
  287. assert(image != nullptr);
  288. cursorImage = image;
  289. pivot = pivotOffset;
  290. needUpdate = true;
  291. }
  292. void CursorSoftware::setCursorPosition( const Point & newPos )
  293. {
  294. pos = newPos;
  295. }
  296. void CursorSoftware::setVisible(bool on)
  297. {
  298. visible = on;
  299. }
  300. CursorSoftware::CursorSoftware():
  301. cursorTexture(nullptr),
  302. cursorSurface(nullptr),
  303. needUpdate(false),
  304. visible(false),
  305. pivot(0,0)
  306. {
  307. SDL_ShowCursor(SDL_DISABLE);
  308. }
  309. CursorSoftware::~CursorSoftware()
  310. {
  311. if(cursorTexture)
  312. SDL_DestroyTexture(cursorTexture);
  313. if (cursorSurface)
  314. SDL_FreeSurface(cursorSurface);
  315. }
  316. CursorHardware::CursorHardware():
  317. cursor(nullptr)
  318. {
  319. SDL_ShowCursor(SDL_DISABLE);
  320. }
  321. CursorHardware::~CursorHardware()
  322. {
  323. if(cursor)
  324. SDL_FreeCursor(cursor);
  325. }
  326. void CursorHardware::setVisible(bool on)
  327. {
  328. if (on)
  329. SDL_ShowCursor(SDL_ENABLE);
  330. else
  331. SDL_ShowCursor(SDL_DISABLE);
  332. }
  333. void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  334. {
  335. auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y);
  336. Uint32 fillColor = SDL_MapRGBA(cursorSurface->format, 0, 0, 0, 0);
  337. CSDL_Ext::fillRect(cursorSurface, nullptr, fillColor);
  338. image->draw(cursorSurface);
  339. auto oldCursor = cursor;
  340. cursor = SDL_CreateColorCursor(cursorSurface, pivotOffset.x, pivotOffset.y);
  341. if (!cursor)
  342. logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
  343. SDL_FreeSurface(cursorSurface);
  344. SDL_SetCursor(cursor);
  345. if (oldCursor)
  346. SDL_FreeCursor(oldCursor);
  347. }
  348. void CursorHardware::setCursorPosition( const Point & newPos )
  349. {
  350. //no-op
  351. }
  352. void CursorHardware::render()
  353. {
  354. //no-op
  355. }