Sprite.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "Matrix3x4.h"
  24. #include "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. OBJECT(Sprite);
  31. public:
  32. /// Construct.
  33. Sprite(Context* context);
  34. /// Destruct.
  35. virtual ~Sprite();
  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);
  40. /// Update and return screen position.
  41. virtual const IntVector2& GetScreenPosition() const;
  42. /// Return UI rendering batches.
  43. virtual void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  44. /// React to position change.
  45. virtual void OnPositionSet();
  46. /// Set floating point position.
  47. void SetPosition(const Vector2& position);
  48. /// Set floating point position.
  49. void SetPosition(float x, float y);
  50. /// Set hotspot for positioning and rotation.
  51. void SetHotSpot(const IntVector2& hotSpot);
  52. /// Set hotspot for positioning and rotation.
  53. void SetHotSpot(int x, int y);
  54. /// Set scale. Scale also affects child sprites.
  55. void SetScale(const Vector2& scale);
  56. /// Set scale. Scale also affects child sprites.
  57. void SetScale(float x, float y);
  58. /// Set uniform scale. Scale also affects child sprites.
  59. void SetScale(float scale);
  60. /// Set rotation angle.
  61. void SetRotation(float angle);
  62. /// Set texture.
  63. void SetTexture(Texture* texture);
  64. /// Set part of texture to use as the image.
  65. void SetImageRect(const IntRect& rect);
  66. /// Use whole texture as the image.
  67. void SetFullImageRect();
  68. /// Set blend mode.
  69. void SetBlendMode(BlendMode mode);
  70. /// Return floating point position.
  71. const Vector2& GetPosition() const { return floatPosition_; }
  72. /// Return hotspot.
  73. const IntVector2& GetHotSpot() const { return hotSpot_; }
  74. /// Return scale.
  75. const Vector2& GetScale() const { return scale_; }
  76. /// Return rotation angle.
  77. float GetRotation() const { return rotation_; }
  78. /// Return texture.
  79. Texture* GetTexture() const { return texture_; }
  80. /// Return image rectangle.
  81. const IntRect& GetImageRect() const { return imageRect_; }
  82. /// Return blend mode.
  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. }