CursorHandler.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. SPELLBOOK // animated cursor for spellcasting
  22. };
  23. enum class ShowType {
  24. SOFTWARE,
  25. HARDWARE
  26. };
  27. enum class Combat {
  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 {
  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 {
  97. SPELL = 0,
  98. };
  99. }
  100. /// handles mouse cursor
  101. class CursorHandler final
  102. {
  103. std::shared_ptr<IImage> dndObject; //if set, overrides currentCursor
  104. std::array<std::shared_ptr<CAnimation>, 4> cursors;
  105. bool showing;
  106. /// Current cursor
  107. Cursor::Type type;
  108. Cursor::ShowType showType;
  109. size_t frame;
  110. float frameTime;
  111. Point pos;
  112. void changeGraphic(Cursor::Type type, size_t index);
  113. Point getPivotOffset();
  114. void updateSpellcastCursor();
  115. std::shared_ptr<IImage> getCurrentImage();
  116. std::unique_ptr<ICursor> cursor;
  117. static std::unique_ptr<ICursor> createCursor();
  118. public:
  119. CursorHandler();
  120. ~CursorHandler();
  121. void init();
  122. /// Replaces the cursor with a custom image.
  123. /// @param image Image to replace cursor with or nullptr to use the normal cursor.
  124. void dragAndDropCursor(std::shared_ptr<IImage> image);
  125. void dragAndDropCursor(const AnimationPath & path, size_t index);
  126. /// Changes cursor to specified index
  127. void set(Cursor::Map index);
  128. void set(Cursor::Combat index);
  129. void set(Cursor::Spellcast index);
  130. /// Returns current index of cursor
  131. template<typename Index>
  132. std::optional<Index> get()
  133. {
  134. bool typeValid = true;
  135. typeValid &= (std::is_same<Index, Cursor::Map>::value )|| type != Cursor::Type::ADVENTURE;
  136. typeValid &= (std::is_same<Index, Cursor::Combat>::value )|| type != Cursor::Type::COMBAT;
  137. typeValid &= (std::is_same<Index, Cursor::Spellcast>::value )|| type != Cursor::Type::SPELLBOOK;
  138. if (typeValid)
  139. return static_cast<Index>(frame);
  140. return std::nullopt;
  141. }
  142. Point getPivotOffsetSpellcast();
  143. Point getPivotOffsetMap(size_t index);
  144. Point getPivotOffsetCombat(size_t index);
  145. void render();
  146. void update();
  147. void hide();
  148. void show();
  149. void onScreenResize();
  150. /// change cursor's positions to (x, y)
  151. void cursorMove(const int & x, const int & y);
  152. Cursor::ShowType getShowType() const;
  153. void changeCursor(Cursor::ShowType showType);
  154. };