CursorHandler.h 4.3 KB

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