CursorHandlerBase.h 4.4 KB

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