Sprite2D.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "../Resource/Resource.h"
  24. namespace Urho3D
  25. {
  26. class SpriteSheet2D;
  27. class Texture2D;
  28. /// Sprite.
  29. class URHO3D_API Sprite2D : public Resource
  30. {
  31. URHO3D_OBJECT(Sprite2D, Resource);
  32. public:
  33. /// Construct.
  34. explicit Sprite2D(Context* context);
  35. /// Destruct.
  36. ~Sprite2D() override;
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  40. bool BeginLoad(Deserializer& source) override;
  41. /// Finish resource loading. Always called from the main thread. Return true if successful.
  42. bool EndLoad() override;
  43. /// Set texture.
  44. /// @property
  45. void SetTexture(Texture2D* texture);
  46. /// Set rectangle.
  47. /// @property
  48. void SetRectangle(const IntRect& rectangle);
  49. /// Set hot spot.
  50. /// @property
  51. void SetHotSpot(const Vector2& hotSpot);
  52. /// Set offset.
  53. /// @property
  54. void SetOffset(const IntVector2& offset);
  55. /// Set texture edge offset in pixels. This affects the left/right and top/bottom edges equally to prevent edge sampling artifacts. Default 0.
  56. /// @property
  57. void SetTextureEdgeOffset(float offset);
  58. /// Set sprite sheet.
  59. void SetSpriteSheet(SpriteSheet2D* spriteSheet);
  60. /// Return texture.
  61. /// @property
  62. Texture2D* GetTexture() const { return texture_; }
  63. /// Return rectangle.
  64. /// @property
  65. const IntRect& GetRectangle() const { return rectangle_; }
  66. /// Return hot spot.
  67. /// @property
  68. const Vector2& GetHotSpot() const { return hotSpot_; }
  69. /// Return offset.
  70. /// @property
  71. const IntVector2& GetOffset() const { return offset_; }
  72. /// Return texture edge offset.
  73. /// @property
  74. float GetTextureEdgeOffset() const { return edgeOffset_; }
  75. /// Return sprite sheet.
  76. SpriteSheet2D* GetSpriteSheet() const { return spriteSheet_; }
  77. /// Return draw rectangle.
  78. bool GetDrawRectangle(Rect& rect, bool flipX = false, bool flipY = false) const;
  79. /// Return draw rectangle with custom hot spot.
  80. bool GetDrawRectangle(Rect& rect, const Vector2& hotSpot, bool flipX = false, bool flipY = false) const;
  81. /// Return texture rectangle.
  82. bool GetTextureRectangle(Rect& rect, bool flipX = false, bool flipY = false) const;
  83. /// Save sprite to ResourceRef.
  84. static ResourceRef SaveToResourceRef(Sprite2D* sprite);
  85. /// Load sprite from ResourceRef.
  86. static Sprite2D* LoadFromResourceRef(Object* object, const ResourceRef& value);
  87. private:
  88. /// Texture.
  89. SharedPtr<Texture2D> texture_;
  90. /// Rectangle.
  91. IntRect rectangle_;
  92. /// Hot spot.
  93. Vector2 hotSpot_;
  94. /// Offset (for trimmed sprite).
  95. IntVector2 offset_;
  96. /// Sprite sheet.
  97. WeakPtr<SpriteSheet2D> spriteSheet_;
  98. /// Texture used while loading.
  99. SharedPtr<Texture2D> loadTexture_;
  100. /// Offset to fix texture edge bleeding.
  101. float edgeOffset_;
  102. };
  103. }