CursorHandler.cpp 11 KB

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