Cursor.h 4.5 KB

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