Sprite.h 5.0 KB

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