CursorHandler.cpp 7.2 KB

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