CursorHandler.cpp 9.5 KB

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