2
0

CursorHandler.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "CGuiHandler.h"
  13. #include "../renderSDL/CursorSoftware.h"
  14. #include "../renderSDL/CursorHardware.h"
  15. #include "../render/CAnimation.h"
  16. #include "../render/IImage.h"
  17. #include "../renderSDL/SDL_Extensions.h"
  18. #include "../CMT.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include <SDL_render.h>
  21. #include <SDL_events.h>
  22. #ifdef VCMI_APPLE
  23. #include <dispatch/dispatch.h>
  24. #endif
  25. std::unique_ptr<ICursor> CursorHandler::createCursor()
  26. {
  27. if (settings["video"]["cursor"].String() == "auto")
  28. {
  29. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  30. return std::make_unique<CursorSoftware>();
  31. #else
  32. return std::make_unique<CursorHardware>();
  33. #endif
  34. }
  35. if (settings["video"]["cursor"].String() == "hardware")
  36. return std::make_unique<CursorHardware>();
  37. assert(settings["video"]["cursor"].String() == "software");
  38. return std::make_unique<CursorSoftware>();
  39. }
  40. CursorHandler::CursorHandler()
  41. : cursor(createCursor())
  42. , frameTime(0.f)
  43. , showing(false)
  44. , pos(0,0)
  45. {
  46. type = Cursor::Type::DEFAULT;
  47. dndObject = nullptr;
  48. cursors =
  49. {
  50. std::make_unique<CAnimation>("CRADVNTR"),
  51. std::make_unique<CAnimation>("CRCOMBAT"),
  52. std::make_unique<CAnimation>("CRDEFLT"),
  53. std::make_unique<CAnimation>("CRSPELL")
  54. };
  55. for (auto & cursor : cursors)
  56. cursor->preload();
  57. set(Cursor::Map::POINTER);
  58. }
  59. Point CursorHandler::position() const
  60. {
  61. return pos;
  62. }
  63. void CursorHandler::changeGraphic(Cursor::Type type, size_t index)
  64. {
  65. assert(dndObject == nullptr);
  66. if (type == this->type && index == this->frame)
  67. return;
  68. this->type = type;
  69. this->frame = index;
  70. cursor->setImage(getCurrentImage(), getPivotOffset());
  71. }
  72. void CursorHandler::set(Cursor::Default index)
  73. {
  74. changeGraphic(Cursor::Type::DEFAULT, static_cast<size_t>(index));
  75. }
  76. void CursorHandler::set(Cursor::Map index)
  77. {
  78. changeGraphic(Cursor::Type::ADVENTURE, static_cast<size_t>(index));
  79. }
  80. void CursorHandler::set(Cursor::Combat index)
  81. {
  82. changeGraphic(Cursor::Type::COMBAT, static_cast<size_t>(index));
  83. }
  84. void CursorHandler::set(Cursor::Spellcast index)
  85. {
  86. //Note: this is animated cursor, ignore specified frame and only change type
  87. changeGraphic(Cursor::Type::SPELLBOOK, frame);
  88. }
  89. void CursorHandler::dragAndDropCursor(std::shared_ptr<IImage> image)
  90. {
  91. dndObject = image;
  92. cursor->setImage(getCurrentImage(), getPivotOffset());
  93. }
  94. void CursorHandler::dragAndDropCursor (std::string path, size_t index)
  95. {
  96. CAnimation anim(path);
  97. anim.load(index);
  98. dragAndDropCursor(anim.getImage(index));
  99. }
  100. void CursorHandler::cursorMove(const int & x, const int & y)
  101. {
  102. pos.x = x;
  103. pos.y = y;
  104. cursor->setCursorPosition(pos);
  105. }
  106. Point CursorHandler::getPivotOffsetDefault(size_t index)
  107. {
  108. return {0, 0};
  109. }
  110. Point CursorHandler::getPivotOffsetMap(size_t index)
  111. {
  112. static const std::array<Point, 43> offsets = {{
  113. { 0, 0}, // POINTER = 0,
  114. { 0, 0}, // HOURGLASS = 1,
  115. { 12, 10}, // HERO = 2,
  116. { 12, 12}, // TOWN = 3,
  117. { 15, 13}, // T1_MOVE = 4,
  118. { 13, 13}, // T1_ATTACK = 5,
  119. { 16, 32}, // T1_SAIL = 6,
  120. { 13, 20}, // T1_DISEMBARK = 7,
  121. { 8, 9}, // T1_EXCHANGE = 8,
  122. { 14, 16}, // T1_VISIT = 9,
  123. { 15, 13}, // T2_MOVE = 10,
  124. { 13, 13}, // T2_ATTACK = 11,
  125. { 16, 32}, // T2_SAIL = 12,
  126. { 13, 20}, // T2_DISEMBARK = 13,
  127. { 8, 9}, // T2_EXCHANGE = 14,
  128. { 14, 16}, // T2_VISIT = 15,
  129. { 15, 13}, // T3_MOVE = 16,
  130. { 13, 13}, // T3_ATTACK = 17,
  131. { 16, 32}, // T3_SAIL = 18,
  132. { 13, 20}, // T3_DISEMBARK = 19,
  133. { 8, 9}, // T3_EXCHANGE = 20,
  134. { 14, 16}, // T3_VISIT = 21,
  135. { 15, 13}, // T4_MOVE = 22,
  136. { 13, 13}, // T4_ATTACK = 23,
  137. { 16, 32}, // T4_SAIL = 24,
  138. { 13, 20}, // T4_DISEMBARK = 25,
  139. { 8, 9}, // T4_EXCHANGE = 26,
  140. { 14, 16}, // T4_VISIT = 27,
  141. { 16, 32}, // T1_SAIL_VISIT = 28,
  142. { 16, 32}, // T2_SAIL_VISIT = 29,
  143. { 16, 32}, // T3_SAIL_VISIT = 30,
  144. { 16, 32}, // T4_SAIL_VISIT = 31,
  145. { 6, 1}, // SCROLL_NORTH = 32,
  146. { 16, 2}, // SCROLL_NORTHEAST = 33,
  147. { 21, 6}, // SCROLL_EAST = 34,
  148. { 16, 16}, // SCROLL_SOUTHEAST = 35,
  149. { 6, 21}, // SCROLL_SOUTH = 36,
  150. { 1, 16}, // SCROLL_SOUTHWEST = 37,
  151. { 1, 5}, // SCROLL_WEST = 38,
  152. { 2, 1}, // SCROLL_NORTHWEST = 39,
  153. { 0, 0}, // POINTER_COPY = 40,
  154. { 14, 16}, // TELEPORT = 41,
  155. { 20, 20}, // SCUTTLE_BOAT = 42
  156. }};
  157. assert(offsets.size() == size_t(Cursor::Map::COUNT)); //Invalid number of pivot offsets for cursor
  158. assert(index < offsets.size());
  159. return offsets[index];
  160. }
  161. Point CursorHandler::getPivotOffsetCombat(size_t index)
  162. {
  163. static const std::array<Point, 20> offsets = {{
  164. { 12, 12 }, // BLOCKED = 0,
  165. { 10, 14 }, // MOVE = 1,
  166. { 14, 14 }, // FLY = 2,
  167. { 12, 12 }, // SHOOT = 3,
  168. { 12, 12 }, // HERO = 4,
  169. { 8, 12 }, // QUERY = 5,
  170. { 0, 0 }, // POINTER = 6,
  171. { 21, 0 }, // HIT_NORTHEAST = 7,
  172. { 31, 5 }, // HIT_EAST = 8,
  173. { 21, 21 }, // HIT_SOUTHEAST = 9,
  174. { 0, 21 }, // HIT_SOUTHWEST = 10,
  175. { 0, 5 }, // HIT_WEST = 11,
  176. { 0, 0 }, // HIT_NORTHWEST = 12,
  177. { 6, 0 }, // HIT_NORTH = 13,
  178. { 6, 31 }, // HIT_SOUTH = 14,
  179. { 14, 0 }, // SHOOT_PENALTY = 15,
  180. { 12, 12 }, // SHOOT_CATAPULT = 16,
  181. { 12, 12 }, // HEAL = 17,
  182. { 12, 12 }, // SACRIFICE = 18,
  183. { 14, 20 }, // TELEPORT = 19
  184. }};
  185. assert(offsets.size() == size_t(Cursor::Combat::COUNT)); //Invalid number of pivot offsets for cursor
  186. assert(index < offsets.size());
  187. return offsets[index];
  188. }
  189. Point CursorHandler::getPivotOffsetSpellcast()
  190. {
  191. return { 18, 28};
  192. }
  193. Point CursorHandler::getPivotOffset()
  194. {
  195. if (dndObject)
  196. return dndObject->dimensions() / 2;
  197. switch (type) {
  198. case Cursor::Type::ADVENTURE: return getPivotOffsetMap(frame);
  199. case Cursor::Type::COMBAT: return getPivotOffsetCombat(frame);
  200. case Cursor::Type::DEFAULT: return getPivotOffsetDefault(frame);
  201. case Cursor::Type::SPELLBOOK: return getPivotOffsetSpellcast();
  202. };
  203. assert(0);
  204. return {0, 0};
  205. }
  206. std::shared_ptr<IImage> CursorHandler::getCurrentImage()
  207. {
  208. if (dndObject)
  209. return dndObject;
  210. return cursors[static_cast<size_t>(type)]->getImage(frame);
  211. }
  212. void CursorHandler::centerCursor()
  213. {
  214. Point screenSize {screen->w, screen->h};
  215. pos = screenSize / 2 - getPivotOffset();
  216. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  217. GH.moveCursorToPosition(pos);
  218. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  219. cursor->setCursorPosition(pos);
  220. }
  221. void CursorHandler::updateSpellcastCursor()
  222. {
  223. static const float frameDisplayDuration = 0.1f; // H3 uses 100 ms per frame
  224. frameTime += GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  225. size_t newFrame = frame;
  226. while (frameTime >= frameDisplayDuration)
  227. {
  228. frameTime -= frameDisplayDuration;
  229. newFrame++;
  230. }
  231. auto & animation = cursors.at(static_cast<size_t>(type));
  232. while (newFrame >= animation->size())
  233. newFrame -= animation->size();
  234. changeGraphic(Cursor::Type::SPELLBOOK, newFrame);
  235. }
  236. void CursorHandler::render()
  237. {
  238. if(!showing)
  239. return;
  240. if (type == Cursor::Type::SPELLBOOK)
  241. updateSpellcastCursor();
  242. cursor->render();
  243. }
  244. void CursorHandler::hide()
  245. {
  246. if (!showing)
  247. return;
  248. showing = false;
  249. cursor->setVisible(false);
  250. }
  251. void CursorHandler::show()
  252. {
  253. if (showing)
  254. return;
  255. showing = true;
  256. cursor->setVisible(true);
  257. }