Cursor.h 3.9 KB

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