CursorHandler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * CCursorHandler.h, 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. #pragma once
  11. #include "../../lib/Point.h"
  12. #include "../../lib/filesystem/ResourcePath.h"
  13. class ICursor;
  14. class IImage;
  15. class CAnimation;
  16. namespace Cursor
  17. {
  18. enum class Type : int8_t {
  19. ADVENTURE, // set of various cursors for adventure map
  20. COMBAT, // set of various cursors for combat
  21. SPELLBOOK // animated cursor for spellcasting
  22. };
  23. enum class ShowType : int8_t {
  24. SOFTWARE,
  25. HARDWARE
  26. };
  27. enum class Combat : int8_t {
  28. BLOCKED = 0,
  29. MOVE = 1,
  30. FLY = 2,
  31. SHOOT = 3,
  32. HERO = 4,
  33. QUERY = 5,
  34. POINTER = 6,
  35. HIT_NORTHEAST = 7,
  36. HIT_EAST = 8,
  37. HIT_SOUTHEAST = 9,
  38. HIT_SOUTHWEST = 10,
  39. HIT_WEST = 11,
  40. HIT_NORTHWEST = 12,
  41. HIT_NORTH = 13,
  42. HIT_SOUTH = 14,
  43. SHOOT_PENALTY = 15,
  44. SHOOT_CATAPULT = 16,
  45. HEAL = 17,
  46. SACRIFICE = 18,
  47. TELEPORT = 19,
  48. COUNT
  49. };
  50. enum class Map : int8_t {
  51. POINTER = 0,
  52. HOURGLASS = 1,
  53. HERO = 2,
  54. TOWN = 3,
  55. T1_MOVE = 4,
  56. T1_ATTACK = 5,
  57. T1_SAIL = 6,
  58. T1_DISEMBARK = 7,
  59. T1_EXCHANGE = 8,
  60. T1_VISIT = 9,
  61. T2_MOVE = 10,
  62. T2_ATTACK = 11,
  63. T2_SAIL = 12,
  64. T2_DISEMBARK = 13,
  65. T2_EXCHANGE = 14,
  66. T2_VISIT = 15,
  67. T3_MOVE = 16,
  68. T3_ATTACK = 17,
  69. T3_SAIL = 18,
  70. T3_DISEMBARK = 19,
  71. T3_EXCHANGE = 20,
  72. T3_VISIT = 21,
  73. T4_MOVE = 22,
  74. T4_ATTACK = 23,
  75. T4_SAIL = 24,
  76. T4_DISEMBARK = 25,
  77. T4_EXCHANGE = 26,
  78. T4_VISIT = 27,
  79. T1_SAIL_VISIT = 28,
  80. T2_SAIL_VISIT = 29,
  81. T3_SAIL_VISIT = 30,
  82. T4_SAIL_VISIT = 31,
  83. SCROLL_NORTH = 32,
  84. SCROLL_NORTHEAST = 33,
  85. SCROLL_EAST = 34,
  86. SCROLL_SOUTHEAST = 35,
  87. SCROLL_SOUTH = 36,
  88. SCROLL_SOUTHWEST = 37,
  89. SCROLL_WEST = 38,
  90. SCROLL_NORTHWEST = 39,
  91. //POINTER_COPY = 40, // probably unused
  92. TELEPORT = 41,
  93. SCUTTLE_BOAT = 42,
  94. COUNT
  95. };
  96. enum class Spellcast : int8_t {
  97. SPELL = 0,
  98. };
  99. }
  100. /// handles mouse cursor
  101. class CursorHandler final
  102. {
  103. struct CursorParameters
  104. {
  105. std::string cursorID;
  106. ImagePath image;
  107. AnimationPath animation;
  108. Point pivot;
  109. int animationFrameIndex;
  110. bool isAnimated;
  111. };
  112. std::vector<CursorParameters> cursors;
  113. std::map<AnimationPath, std::shared_ptr<CAnimation>> loadedAnimations;
  114. std::map<ImagePath, std::shared_ptr<IImage>> loadedImages;
  115. std::shared_ptr<IImage> dndObject; //if set, overrides currentCursor
  116. std::shared_ptr<IImage> cursorImage; //if set, overrides currentCursor
  117. /// Current cursor
  118. std::string currentCursorID;
  119. Point pos;
  120. float frameTime;
  121. int32_t currentCursorIndex;
  122. int32_t currentFrame;
  123. Cursor::ShowType showType;
  124. bool showing;
  125. void updateAnimatedCursor();
  126. std::unique_ptr<ICursor> cursor;
  127. static std::unique_ptr<ICursor> createCursor();
  128. public:
  129. CursorHandler();
  130. ~CursorHandler();
  131. void init();
  132. /// Replaces the cursor with a custom image.
  133. /// @param image Image to replace cursor with or nullptr to use the normal cursor.
  134. void dragAndDropCursor(std::shared_ptr<IImage> image);
  135. void dragAndDropCursor(const AnimationPath & path, size_t index);
  136. /// Changes cursor to specified index
  137. void set(Cursor::Map index);
  138. void set(Cursor::Combat index);
  139. void set(Cursor::Spellcast index);
  140. void set(const std::string & index);
  141. std::shared_ptr<IImage> getCurrentImage();
  142. Point getPivotOffset();
  143. void render();
  144. void update();
  145. void hide();
  146. void show();
  147. void onScreenResize();
  148. /// change cursor's positions to (x, y)
  149. void cursorMove(const int & x, const int & y);
  150. Cursor::ShowType getShowType() const;
  151. void changeCursor(Cursor::ShowType showType);
  152. };