2
0

CursorHandler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "GameEngine.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. #include "../../lib/json/JsonUtils.h"
  22. std::unique_ptr<ICursor> CursorHandler::createCursor()
  23. {
  24. #if defined(VCMI_MOBILE) || defined(VCMI_PORTMASTER)
  25. if (settings["general"]["userRelativePointer"].Bool())
  26. return std::make_unique<CursorSoftware>();
  27. #endif
  28. if (settings["video"]["cursor"].String() == "hardware")
  29. return std::make_unique<CursorHardware>();
  30. assert(settings["video"]["cursor"].String() == "software");
  31. return std::make_unique<CursorSoftware>();
  32. }
  33. CursorHandler::CursorHandler()
  34. : cursor(createCursor())
  35. , frameTime(0.f)
  36. , showing(false)
  37. , pos(0,0)
  38. , dndObject(nullptr)
  39. {
  40. showType = dynamic_cast<CursorSoftware *>(cursor.get()) ? Cursor::ShowType::SOFTWARE : Cursor::ShowType::HARDWARE;
  41. }
  42. CursorHandler::~CursorHandler() = default;
  43. void CursorHandler::init()
  44. {
  45. JsonNode cursorConfig = JsonUtils::assembleFromFiles("config/cursors.json");
  46. std::vector<AnimationPath> animations;
  47. for (const auto & cursorEntry : cursorConfig.Struct())
  48. {
  49. CursorParameters parameters;
  50. parameters.cursorID = cursorEntry.first;
  51. parameters.image = ImagePath::fromJson(cursorEntry.second["image"]);
  52. parameters.animation = AnimationPath::fromJson(cursorEntry.second["animation"]);
  53. parameters.animationFrameIndex = cursorEntry.second["frame"].Integer();
  54. parameters.isAnimated = cursorEntry.second["animated"].Bool();
  55. parameters.pivot.x = cursorEntry.second["pivotX"].Integer();
  56. parameters.pivot.y = cursorEntry.second["pivotY"].Integer();
  57. cursors.push_back(parameters);
  58. }
  59. set(Cursor::Map::POINTER);
  60. }
  61. void CursorHandler::set(const std::string & index)
  62. {
  63. assert(dndObject == nullptr);
  64. if (index == currentCursorID)
  65. return;
  66. currentCursorID = index;
  67. currentCursorIndex = 0;
  68. frameTime = 0;
  69. for (size_t i = 0; i < cursors.size(); ++i)
  70. {
  71. if (cursors[i].cursorID == index)
  72. {
  73. currentCursorIndex = i;
  74. break;
  75. }
  76. }
  77. const auto & currentCursor = cursors.at(currentCursorIndex);
  78. if (currentCursor.image.empty())
  79. {
  80. if (!loadedAnimations.count(currentCursor.animation))
  81. loadedAnimations[currentCursor.animation] = ENGINE->renderHandler().loadAnimation(currentCursor.animation, EImageBlitMode::COLORKEY);
  82. if (currentCursor.isAnimated)
  83. cursorImage = loadedAnimations[currentCursor.animation]->getImage(0);
  84. else
  85. cursorImage = loadedAnimations[currentCursor.animation]->getImage(currentCursor.animationFrameIndex);
  86. }
  87. else
  88. {
  89. if (!loadedImages.count(currentCursor.image))
  90. loadedImages[currentCursor.image] = ENGINE->renderHandler().loadImage(currentCursor.image, EImageBlitMode::COLORKEY);
  91. cursorImage = loadedImages[currentCursor.image];
  92. }
  93. cursor->setImage(getCurrentImage(), getPivotOffset());
  94. }
  95. void CursorHandler::set(Cursor::Map index)
  96. {
  97. constexpr std::array mapCursorNames =
  98. {
  99. "mapPointer",
  100. "mapHourglass",
  101. "mapHero",
  102. "mapTown",
  103. "mapTurn1Move",
  104. "mapTurn1Attack",
  105. "mapTurn1Sail",
  106. "mapTurn1Disembark",
  107. "mapTurn1Exchange",
  108. "mapTurn1Visit",
  109. "mapTurn2Move",
  110. "mapTurn2Attack",
  111. "mapTurn2Sail",
  112. "mapTurn2Disembark",
  113. "mapTurn2Exchange",
  114. "mapTurn2Visit",
  115. "mapTurn3Move",
  116. "mapTurn3Attack",
  117. "mapTurn3Sail",
  118. "mapTurn3Disembark",
  119. "mapTurn3Exchange",
  120. "mapTurn3Visit",
  121. "mapTurn4Move",
  122. "mapTurn4Attack",
  123. "mapTurn4Sail",
  124. "mapTurn4Disembark",
  125. "mapTurn4Exchange",
  126. "mapTurn4Visit",
  127. "mapTurn1SailVisit",
  128. "mapTurn2SailVisit",
  129. "mapTurn3SailVisit",
  130. "mapTurn4SailVisit",
  131. "mapScrollNorth",
  132. "mapScrollNorthEast",
  133. "mapScrollEast",
  134. "mapScrollSouthEast",
  135. "mapScrollSouth",
  136. "mapScrollSouthWest",
  137. "mapScrollWest",
  138. "mapScrollNorthWest",
  139. "UNUSED",
  140. "mapDimensionDoor",
  141. "mapScuttleBoat"
  142. };
  143. set(mapCursorNames.at(static_cast<int>(index)));
  144. }
  145. void CursorHandler::set(Cursor::Combat index)
  146. {
  147. constexpr std::array combatCursorNames =
  148. {
  149. "combatBlocked",
  150. "combatMove",
  151. "combatFly",
  152. "combatShoot",
  153. "combatHero",
  154. "combatQuery",
  155. "combatPointer",
  156. "combatHitNorthEast",
  157. "combatHitEast",
  158. "combatHitSouthEast",
  159. "combatHitSouthWest",
  160. "combatHitWest",
  161. "combatHitNorthWest",
  162. "combatHitNorth",
  163. "combatHitSouth",
  164. "combatShootPenalty",
  165. "combatShootCatapult",
  166. "combatHeal",
  167. "combatSacrifice",
  168. "combatTeleport"
  169. };
  170. set(combatCursorNames.at(static_cast<int>(index)));
  171. }
  172. void CursorHandler::set(Cursor::Spellcast index)
  173. {
  174. //Note: this is animated cursor, ignore requested frame and only change type
  175. set("castSpell");
  176. }
  177. void CursorHandler::dragAndDropCursor(std::shared_ptr<IImage> image)
  178. {
  179. dndObject = image;
  180. cursor->setImage(getCurrentImage(), getPivotOffset());
  181. }
  182. void CursorHandler::dragAndDropCursor (const AnimationPath & path, size_t index)
  183. {
  184. auto anim = ENGINE->renderHandler().loadAnimation(path, EImageBlitMode::COLORKEY);
  185. dragAndDropCursor(anim->getImage(index));
  186. }
  187. void CursorHandler::cursorMove(const int & x, const int & y)
  188. {
  189. pos.x = x;
  190. pos.y = y;
  191. cursor->setCursorPosition(pos);
  192. }
  193. Point CursorHandler::getPivotOffset()
  194. {
  195. if (dndObject)
  196. return dndObject->dimensions() / 2;
  197. return cursors.at(currentCursorIndex).pivot;
  198. }
  199. std::shared_ptr<IImage> CursorHandler::getCurrentImage()
  200. {
  201. if (dndObject)
  202. return dndObject;
  203. return cursorImage;
  204. }
  205. void CursorHandler::updateAnimatedCursor()
  206. {
  207. static const float frameDisplayDuration = 0.1f; // H3 uses 100 ms per frame
  208. frameTime += ENGINE->framerate().getElapsedMilliseconds() / 1000.f;
  209. int32_t newFrame = currentFrame;
  210. const auto & animationName = cursors.at(currentCursorIndex).animation;
  211. const auto & animation = loadedAnimations.at(animationName);
  212. while (frameTime >= frameDisplayDuration)
  213. {
  214. frameTime -= frameDisplayDuration;
  215. newFrame++;
  216. }
  217. while (newFrame >= animation->size())
  218. newFrame -= animation->size();
  219. currentFrame = newFrame;
  220. cursorImage = animation->getImage(currentFrame);
  221. cursor->setImage(getCurrentImage(), getPivotOffset());
  222. }
  223. void CursorHandler::render()
  224. {
  225. cursor->render();
  226. }
  227. void CursorHandler::update()
  228. {
  229. if(!showing)
  230. return;
  231. if (cursors.at(currentCursorIndex).isAnimated)
  232. updateAnimatedCursor();
  233. cursor->update();
  234. }
  235. void CursorHandler::hide()
  236. {
  237. if (!showing)
  238. return;
  239. showing = false;
  240. cursor->setVisible(false);
  241. }
  242. void CursorHandler::show()
  243. {
  244. if (showing)
  245. return;
  246. showing = true;
  247. cursor->setVisible(true);
  248. }
  249. Cursor::ShowType CursorHandler::getShowType() const
  250. {
  251. return showType;
  252. }
  253. void CursorHandler::changeCursor(Cursor::ShowType newShowType)
  254. {
  255. if(newShowType == showType)
  256. return;
  257. switch(newShowType)
  258. {
  259. case Cursor::ShowType::SOFTWARE:
  260. cursor.reset(new CursorSoftware());
  261. showType = Cursor::ShowType::SOFTWARE;
  262. cursor->setImage(getCurrentImage(), getPivotOffset());
  263. break;
  264. case Cursor::ShowType::HARDWARE:
  265. cursor.reset(new CursorHardware());
  266. showType = Cursor::ShowType::HARDWARE;
  267. cursor->setImage(getCurrentImage(), getPivotOffset());
  268. break;
  269. }
  270. }
  271. void CursorHandler::onScreenResize()
  272. {
  273. cursor->setImage(getCurrentImage(), getPivotOffset());
  274. }