CursorHandler.h 4.3 KB

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