CursorHandler.cpp 8.0 KB

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