CCursorHandler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 CIntObject;
  12. class CAnimImage;
  13. struct SDL_Surface;
  14. struct SDL_Texture;
  15. struct Point;
  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. INVALID = -1,
  31. BLOCKED = 0,
  32. MOVE = 1,
  33. FLY = 2,
  34. SHOOT = 3,
  35. HERO = 4,
  36. QUERY = 5,
  37. POINTER = 6,
  38. HIT_NORTHEAST = 7,
  39. HIT_EAST = 8,
  40. HIT_SOUTHEAST = 9,
  41. HIT_SOUTHWEST = 10,
  42. HIT_WEST = 11,
  43. HIT_NORTHWEST = 12,
  44. HIT_NORTH = 13,
  45. HIT_SOUTH = 14,
  46. SHOOT_PENALTY = 15,
  47. SHOOT_CATAPULT = 16,
  48. HEAL = 17,
  49. SACRIFICE = 18,
  50. TELEPORT = 19
  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. };
  97. enum class Spellcast {
  98. SPELL = 0,
  99. };
  100. }
  101. /// handles mouse cursor
  102. class CCursorHandler final
  103. {
  104. bool needUpdate;
  105. SDL_Texture * cursorLayer;
  106. SDL_Surface * buffer;
  107. CAnimImage * currentCursor;
  108. std::unique_ptr<CAnimImage> dndObject; //if set, overrides currentCursor
  109. std::array<std::unique_ptr<CAnimImage>, 4> cursors;
  110. bool showing;
  111. void clearBuffer();
  112. void updateBuffer(CIntObject * payload);
  113. void replaceBuffer(CIntObject * payload);
  114. void shiftPos( int &x, int &y );
  115. void updateTexture();
  116. /// Current cursor
  117. Cursor::Type type;
  118. size_t frame;
  119. float frameTime;
  120. void changeGraphic(Cursor::Type type, size_t index);
  121. /// position of cursor
  122. int xpos, ypos;
  123. public:
  124. CCursorHandler();
  125. ~CCursorHandler();
  126. /**
  127. * Replaces the cursor with a custom image.
  128. *
  129. * @param image Image to replace cursor with or nullptr to use the normal
  130. * cursor. CursorHandler takes ownership of object
  131. */
  132. void dragAndDropCursor (std::unique_ptr<CAnimImage> image);
  133. /// Returns current position of the cursor
  134. Point position() const;
  135. /// Changes cursor to specified index
  136. void set(Cursor::Default index);
  137. void set(Cursor::Map index);
  138. void set(Cursor::Combat index);
  139. void set(Cursor::Spellcast index);
  140. /// Returns current index of cursor
  141. template<typename Index>
  142. Index get()
  143. {
  144. assert((std::is_same<Index, Cursor::Default>::value )|| type != Cursor::Type::DEFAULT );
  145. assert((std::is_same<Index, Cursor::Map>::value )|| type != Cursor::Type::ADVENTURE );
  146. assert((std::is_same<Index, Cursor::Combat>::value )|| type != Cursor::Type::COMBAT );
  147. assert((std::is_same<Index, Cursor::Spellcast>::value )|| type != Cursor::Type::SPELLBOOK );
  148. return static_cast<Index>(frame);
  149. }
  150. void render();
  151. void hide() { showing=false; };
  152. void show() { showing=true; };
  153. /// change cursor's positions to (x, y)
  154. void cursorMove(const int & x, const int & y);
  155. /// Move cursor to screen center
  156. void centerCursor();
  157. };