CCursorHandler.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. namespace ECursor
  16. {
  17. enum ECursorTypes { ADVENTURE, COMBAT, DEFAULT, SPELLBOOK };
  18. enum EBattleCursors { COMBAT_BLOCKED, COMBAT_MOVE, COMBAT_FLY, COMBAT_SHOOT,
  19. COMBAT_HERO, COMBAT_QUERY, COMBAT_POINTER,
  20. //various attack frames
  21. COMBAT_SHOOT_PENALTY = 15, COMBAT_SHOOT_CATAPULT, COMBAT_HEAL,
  22. COMBAT_SACRIFICE, COMBAT_TELEPORT};
  23. }
  24. /// handles mouse cursor
  25. class CCursorHandler final
  26. {
  27. bool needUpdate;
  28. SDL_Texture * cursorLayer;
  29. SDL_Surface * buffer;
  30. CAnimImage * currentCursor;
  31. std::unique_ptr<CAnimImage> dndObject; //if set, overrides currentCursor
  32. std::array<std::unique_ptr<CAnimImage>, 4> cursors;
  33. bool showing;
  34. void clearBuffer();
  35. void updateBuffer(CIntObject * payload);
  36. void replaceBuffer(CIntObject * payload);
  37. void shiftPos( int &x, int &y );
  38. void updateTexture();
  39. public:
  40. /// position of cursor
  41. int xpos, ypos;
  42. /// Current cursor
  43. ECursor::ECursorTypes type;
  44. size_t frame;
  45. /// inits cursorHandler - run only once, it's not memleak-proof (rev 1333)
  46. void initCursor();
  47. /// changes cursor graphic for type type (0 - adventure, 1 - combat, 2 - default, 3 - spellbook) and frame index (not used for type 3)
  48. void changeGraphic(ECursor::ECursorTypes type, int index);
  49. /**
  50. * Replaces the cursor with a custom image.
  51. *
  52. * @param image Image to replace cursor with or nullptr to use the normal
  53. * cursor. CursorHandler takes ownership of object
  54. */
  55. void dragAndDropCursor (std::unique_ptr<CAnimImage> image);
  56. void render();
  57. void hide() { showing=false; };
  58. void show() { showing=true; };
  59. /// change cursor's positions to (x, y)
  60. void cursorMove(const int & x, const int & y);
  61. /// Move cursor to screen center
  62. void centerCursor();
  63. CCursorHandler();
  64. ~CCursorHandler();
  65. };