Cursor.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../GraphicsAPI/Texture.h"
  6. #include "../Resource/Image.h"
  7. #include "../UI/BorderImage.h"
  8. struct SDL_Cursor;
  9. namespace Urho3D
  10. {
  11. /// %Cursor shapes recognized by the UI subsystem.
  12. enum CursorShape
  13. {
  14. CS_NORMAL = 0,
  15. CS_IBEAM,
  16. CS_CROSS,
  17. CS_RESIZEVERTICAL,
  18. CS_RESIZEDIAGONAL_TOPRIGHT,
  19. CS_RESIZEHORIZONTAL,
  20. CS_RESIZEDIAGONAL_TOPLEFT,
  21. CS_RESIZE_ALL,
  22. CS_ACCEPTDROP,
  23. CS_REJECTDROP,
  24. CS_BUSY,
  25. CS_BUSY_ARROW,
  26. CS_MAX_SHAPES
  27. };
  28. /// %Cursor image and hotspot information.
  29. struct URHO3D_API CursorShapeInfo
  30. {
  31. /// Construct with defaults.
  32. CursorShapeInfo() :
  33. imageRect_(IntRect::ZERO),
  34. hotSpot_(IntVector2::ZERO),
  35. osCursor_(nullptr),
  36. systemDefined_(false),
  37. systemCursor_(-1)
  38. {
  39. }
  40. /// Construct with system cursor.
  41. explicit CursorShapeInfo(int systemCursor) :
  42. imageRect_(IntRect::ZERO),
  43. hotSpot_(IntVector2::ZERO),
  44. osCursor_(nullptr),
  45. systemDefined_(false),
  46. systemCursor_(systemCursor)
  47. {
  48. }
  49. /// Image.
  50. SharedPtr<Image> image_;
  51. /// Texture.
  52. SharedPtr<Texture> texture_;
  53. /// Image rectangle.
  54. IntRect imageRect_;
  55. /// Hotspot coordinates.
  56. IntVector2 hotSpot_;
  57. /// OS cursor.
  58. SDL_Cursor* osCursor_;
  59. /// Whether the OS cursor is system defined.
  60. bool systemDefined_;
  61. /// System cursor index.
  62. int systemCursor_;
  63. };
  64. /// Mouse cursor %UI element.
  65. class URHO3D_API Cursor : public BorderImage
  66. {
  67. URHO3D_OBJECT(Cursor, BorderImage);
  68. public:
  69. /// Construct.
  70. explicit Cursor(Context* context);
  71. /// Destruct.
  72. ~Cursor() override;
  73. /// Register object factory.
  74. /// @nobind
  75. static void RegisterObject(Context* context);
  76. /// Return UI rendering batches.
  77. void GetBatches(Vector<UIBatch>& batches, Vector<float>& vertexData, const IntRect& currentScissor) override;
  78. /// Define a shape.
  79. void DefineShape(const String& shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot);
  80. /// Define a shape.
  81. void DefineShape(CursorShape shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot);
  82. /// Set current shape.
  83. /// @property
  84. void SetShape(const String& shape);
  85. /// Set current shape.
  86. void SetShape(CursorShape shape);
  87. /// Set whether to use system default shapes. Is only possible when the OS mouse cursor has been set visible from the Input subsystem.
  88. /// @property
  89. void SetUseSystemShapes(bool enable);
  90. /// Get current shape.
  91. /// @property
  92. const String& GetShape() const { return shape_; }
  93. /// Return whether is using system default shapes.
  94. /// @property
  95. bool GetUseSystemShapes() const { return useSystemShapes_; }
  96. /// Set shapes attribute.
  97. void SetShapesAttr(const VariantVector& value);
  98. /// Return shapes attribute.
  99. VariantVector GetShapesAttr() const;
  100. /// Apply pending OS cursor shape. Called by UI. No-op when the OS mouse pointer is not used.
  101. void ApplyOSCursorShape();
  102. protected:
  103. /// Handle operating system mouse cursor visibility change event.
  104. void HandleMouseVisibleChanged(StringHash eventType, VariantMap& eventData);
  105. /// Current shape definition.
  106. String shape_;
  107. /// Shape definitions.
  108. HashMap<String, CursorShapeInfo> shapeInfos_;
  109. /// Use system default shapes flag.
  110. bool useSystemShapes_;
  111. /// OS cursor shape needs update flag.
  112. bool osShapeDirty_;
  113. };
  114. }