CursorHandler.cpp 10.0 KB

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