CursorHandler.cpp 7.4 KB

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