CursorHandler.h 4.4 KB

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