Cursor.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. /// \file
  23. #pragma once
  24. #include "../Graphics/Texture.h"
  25. #include "../Resource/Image.h"
  26. #include "../UI/BorderImage.h"
  27. struct SDL_Cursor;
  28. namespace Urho3D
  29. {
  30. /// %Cursor shapes recognized by the UI subsystem.
  31. enum CursorShape
  32. {
  33. CS_NORMAL = 0,
  34. CS_IBEAM,
  35. CS_CROSS,
  36. CS_RESIZEVERTICAL,
  37. CS_RESIZEDIAGONAL_TOPRIGHT,
  38. CS_RESIZEHORIZONTAL,
  39. CS_RESIZEDIAGONAL_TOPLEFT,
  40. CS_RESIZE_ALL,
  41. CS_ACCEPTDROP,
  42. CS_REJECTDROP,
  43. CS_BUSY,
  44. CS_BUSY_ARROW,
  45. CS_MAX_SHAPES
  46. };
  47. /// %Cursor image and hotspot information.
  48. struct URHO3D_API CursorShapeInfo
  49. {
  50. /// Construct with defaults.
  51. CursorShapeInfo() :
  52. imageRect_(IntRect::ZERO),
  53. hotSpot_(IntVector2::ZERO),
  54. osCursor_(nullptr),
  55. systemDefined_(false),
  56. systemCursor_(-1)
  57. {
  58. }
  59. /// Construct with system cursor.
  60. explicit CursorShapeInfo(int systemCursor) :
  61. imageRect_(IntRect::ZERO),
  62. hotSpot_(IntVector2::ZERO),
  63. osCursor_(nullptr),
  64. systemDefined_(false),
  65. systemCursor_(systemCursor)
  66. {
  67. }
  68. /// Image.
  69. SharedPtr<Image> image_;
  70. /// Texture.
  71. SharedPtr<Texture> texture_;
  72. /// Image rectangle.
  73. IntRect imageRect_;
  74. /// Hotspot coordinates.
  75. IntVector2 hotSpot_;
  76. /// OS cursor.
  77. SDL_Cursor* osCursor_;
  78. /// Whether the OS cursor is system defined.
  79. bool systemDefined_;
  80. /// System cursor index.
  81. int systemCursor_;
  82. };
  83. /// Mouse cursor %UI element.
  84. class URHO3D_API Cursor : public BorderImage
  85. {
  86. URHO3D_OBJECT(Cursor, BorderImage);
  87. public:
  88. /// Construct.
  89. explicit Cursor(Context* context);
  90. /// Destruct.
  91. ~Cursor() override;
  92. /// Register object factory.
  93. static void RegisterObject(Context* context);
  94. /// Return UI rendering batches.
  95. void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor) override;
  96. /// Define a shape.
  97. void DefineShape(const String& shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot);
  98. /// Define a shape.
  99. void DefineShape(CursorShape shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot);
  100. /// Set current shape.
  101. /// @property
  102. void SetShape(const String& shape);
  103. /// Set current shape.
  104. void SetShape(CursorShape shape);
  105. /// Set whether to use system default shapes. Is only possible when the OS mouse cursor has been set visible from the Input subsystem.
  106. /// @property
  107. void SetUseSystemShapes(bool enable);
  108. /// Get current shape.
  109. /// @property
  110. const String& GetShape() const { return shape_; }
  111. /// Return whether is using system default shapes.
  112. /// @property
  113. bool GetUseSystemShapes() const { return useSystemShapes_; }
  114. /// Set shapes attribute.
  115. void SetShapesAttr(const VariantVector& value);
  116. /// Return shapes attribute.
  117. VariantVector GetShapesAttr() const;
  118. /// Apply pending OS cursor shape. Called by UI. No-op when the OS mouse pointer is not used.
  119. void ApplyOSCursorShape();
  120. protected:
  121. /// Handle operating system mouse cursor visibility change event.
  122. void HandleMouseVisibleChanged(StringHash eventType, VariantMap& eventData);
  123. /// Current shape definition.
  124. String shape_;
  125. /// Shape definitions.
  126. HashMap<String, CursorShapeInfo> shapeInfos_;
  127. /// Use system default shapes flag.
  128. bool useSystemShapes_;
  129. /// OS cursor shape needs update flag.
  130. bool osShapeDirty_;
  131. };
  132. }