Sprite.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2008-2017 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. Sprite(Context* context);
  34. /// Destruct.
  35. virtual ~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. virtual bool IsWithinScissor(const IntRect& currentScissor) override;
  40. /// Update and return screen position.
  41. virtual const IntVector2& GetScreenPosition() const override;
  42. /// Return UI rendering batches.
  43. virtual void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor) override;
  44. /// React to position change.
  45. virtual void OnPositionSet(const IntVector2& newPosition) override;
  46. /// Convert screen coordinates to element coordinates.
  47. virtual IntVector2 ScreenToElement(const IntVector2& screenPosition) override;
  48. /// Convert element coordinates to screen coordinates.
  49. virtual IntVector2 ElementToScreen(const IntVector2& position) override;
  50. /// Set floating point position.
  51. void SetPosition(const Vector2& position);
  52. /// Set floating point position.
  53. void SetPosition(float x, float y);
  54. /// Set hotspot for positioning and rotation.
  55. void SetHotSpot(const IntVector2& hotSpot);
  56. /// Set hotspot for positioning and rotation.
  57. void SetHotSpot(int x, int y);
  58. /// Set scale. Scale also affects child sprites.
  59. void SetScale(const Vector2& scale);
  60. /// Set scale. Scale also affects child sprites.
  61. void SetScale(float x, float y);
  62. /// Set uniform scale. Scale also affects child sprites.
  63. void SetScale(float scale);
  64. /// Set rotation angle.
  65. void SetRotation(float angle);
  66. /// Set texture.
  67. void SetTexture(Texture* texture);
  68. /// Set part of texture to use as the image.
  69. void SetImageRect(const IntRect& rect);
  70. /// Use whole texture as the image.
  71. void SetFullImageRect();
  72. /// Set blend mode.
  73. void SetBlendMode(BlendMode mode);
  74. /// Return floating point position.
  75. const Vector2& GetPosition() const { return floatPosition_; }
  76. /// Return hotspot.
  77. const IntVector2& GetHotSpot() const { return hotSpot_; }
  78. /// Return scale.
  79. const Vector2& GetScale() const { return scale_; }
  80. /// Return rotation angle.
  81. float GetRotation() const { return rotation_; }
  82. /// Return texture.
  83. Texture* GetTexture() const { return texture_; }
  84. /// Return image rectangle.
  85. const IntRect& GetImageRect() const { return imageRect_; }
  86. /// Return blend mode.
  87. BlendMode GetBlendMode() const { return blendMode_; }
  88. /// Set texture attribute.
  89. void SetTextureAttr(const ResourceRef& value);
  90. /// Return texture attribute.
  91. ResourceRef GetTextureAttr() const;
  92. /// Update and return rendering transform, also used to transform child sprites.
  93. const Matrix3x4& GetTransform() const;
  94. protected:
  95. /// Floating point position.
  96. Vector2 floatPosition_;
  97. /// Hotspot for positioning and rotation.
  98. IntVector2 hotSpot_;
  99. /// Scale.
  100. Vector2 scale_;
  101. /// Rotation angle.
  102. float rotation_;
  103. /// Texture.
  104. SharedPtr<Texture> texture_;
  105. /// Image rectangle.
  106. IntRect imageRect_;
  107. /// Blend mode flag.
  108. BlendMode blendMode_;
  109. /// Transform matrix.
  110. mutable Matrix3x4 transform_;
  111. };
  112. }