Sprite2D.h 3.9 KB

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