Cursor.h 4.6 KB

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