CursorHandler.cpp 8.2 KB

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