CursorHandler.cpp 7.8 KB

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