CursorHandler.cpp 7.4 KB

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