Sprite.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Matrix3x4.h"
  5. #include "../UI/UIElement.h"
  6. namespace Urho3D
  7. {
  8. /// %UI element which allows sub-pixel positioning and size, as well as rotation. Only other Sprites should be added as child elements.
  9. class URHO3D_API Sprite : public UIElement
  10. {
  11. URHO3D_OBJECT(Sprite, UIElement);
  12. public:
  13. /// Construct.
  14. explicit Sprite(Context* context);
  15. /// Destruct.
  16. ~Sprite() override;
  17. /// Register object factory.
  18. /// @nobind
  19. static void RegisterObject(Context* context);
  20. /// Return whether is visible and inside a scissor rectangle and should be rendered.
  21. bool IsWithinScissor(const IntRect& currentScissor) override;
  22. /// Update and return screen position.
  23. const IntVector2& GetScreenPosition() const override;
  24. /// Return UI rendering batches.
  25. void GetBatches(Vector<UIBatch>& batches, Vector<float>& vertexData, const IntRect& currentScissor) override;
  26. /// React to position change.
  27. void OnPositionSet(const IntVector2& newPosition) override;
  28. /// Convert screen coordinates to element coordinates.
  29. IntVector2 ScreenToElement(const IntVector2& screenPosition) override;
  30. /// Convert element coordinates to screen coordinates.
  31. IntVector2 ElementToScreen(const IntVector2& position) override;
  32. /// Set floating point position.
  33. /// @property
  34. void SetPosition(const Vector2& position);
  35. /// Set floating point position.
  36. void SetPosition(float x, float y);
  37. /// Set hotspot for positioning and rotation.
  38. /// @property
  39. void SetHotSpot(const IntVector2& hotSpot);
  40. /// Set hotspot for positioning and rotation.
  41. void SetHotSpot(int x, int y);
  42. /// Set scale. Scale also affects child sprites.
  43. /// @property
  44. void SetScale(const Vector2& scale);
  45. /// Set scale. Scale also affects child sprites.
  46. void SetScale(float x, float y);
  47. /// Set uniform scale. Scale also affects child sprites.
  48. void SetScale(float scale);
  49. /// Set rotation angle.
  50. /// @property
  51. void SetRotation(float angle);
  52. /// Set texture.
  53. /// @property
  54. void SetTexture(Texture* texture);
  55. /// Set part of texture to use as the image.
  56. /// @property
  57. void SetImageRect(const IntRect& rect);
  58. /// Use whole texture as the image.
  59. void SetFullImageRect();
  60. /// Set blend mode.
  61. /// @property
  62. void SetBlendMode(BlendMode mode);
  63. /// Return floating point position.
  64. /// @property
  65. const Vector2& GetPosition() const { return floatPosition_; }
  66. /// Return hotspot.
  67. /// @property
  68. const IntVector2& GetHotSpot() const { return hotSpot_; }
  69. /// Return scale.
  70. /// @property
  71. const Vector2& GetScale() const { return scale_; }
  72. /// Return rotation angle.
  73. /// @property
  74. float GetRotation() const { return rotation_; }
  75. /// Return texture.
  76. /// @property
  77. Texture* GetTexture() const { return texture_; }
  78. /// Return image rectangle.
  79. /// @property
  80. const IntRect& GetImageRect() const { return imageRect_; }
  81. /// Return blend mode.
  82. /// @property
  83. BlendMode GetBlendMode() const { return blendMode_; }
  84. /// Set texture attribute.
  85. void SetTextureAttr(const ResourceRef& value);
  86. /// Return texture attribute.
  87. ResourceRef GetTextureAttr() const;
  88. /// Update and return rendering transform, also used to transform child sprites.
  89. const Matrix3x4& GetTransform() const;
  90. protected:
  91. /// Floating point position.
  92. Vector2 floatPosition_;
  93. /// Hotspot for positioning and rotation.
  94. IntVector2 hotSpot_;
  95. /// Scale.
  96. Vector2 scale_;
  97. /// Rotation angle.
  98. float rotation_;
  99. /// Texture.
  100. SharedPtr<Texture> texture_;
  101. /// Image rectangle.
  102. IntRect imageRect_;
  103. /// Blend mode flag.
  104. BlendMode blendMode_;
  105. /// Transform matrix.
  106. mutable Matrix3x4 transform_;
  107. };
  108. }