// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. #pragma once #include "Engine/Core/ISerializable.h" #include "Engine/Core/Types/String.h" #include "Engine/Core/Math/Rectangle.h" #include "Engine/Content/BinaryAsset.h" #include "Engine/Content/AssetReference.h" #include "Engine/Graphics/Textures/TextureBase.h" class SpriteAtlas; class GPUTexture; /// /// Contains information about single atlas slot with sprite texture. /// API_STRUCT() struct Sprite { DECLARE_SCRIPTING_TYPE_MINIMAL(Sprite); /// /// The normalized area of the sprite in the atlas (in range [0;1]). /// API_FIELD() Rectangle Area; /// /// The sprite name. /// API_FIELD() String Name; }; /// /// Handle to sprite atlas slot with a single sprite texture. /// API_STRUCT() struct FLAXENGINE_API SpriteHandle : ISerializable { API_AUTO_SERIALIZATION(); DECLARE_SCRIPTING_TYPE_MINIMAL(SpriteHandle); /// /// Invalid sprite handle. /// static const SpriteHandle Invalid; /// /// The parent atlas. /// API_FIELD() AssetReference Atlas; /// /// The atlas sprites array index. /// API_FIELD() int32 Index; /// /// Initializes a new instance of the struct. /// SpriteHandle() { Index = -1; } /// /// Initializes a new instance of the struct. /// /// The sprite atlas. /// The sprite slot index. SpriteHandle(SpriteAtlas* atlas, int32 index) : Atlas(atlas) { Index = index; } /// /// Tries to get sprite info. /// /// The result. /// True if data is valid, otherwise false. bool GetSprite(Sprite* result) const; /// /// Returns true if sprite is valid. /// /// True if this sprite handle is valid, otherwise false. bool IsValid() const; /// /// Gets the sprite atlas texture. /// /// The texture object. GPUTexture* GetAtlasTexture() const; }; /// /// Sprite atlas asset that contains collection of sprites combined into a single texture. /// /// API_CLASS(NoSpawn) class FLAXENGINE_API SpriteAtlas : public TextureBase { DECLARE_BINARY_ASSET_HEADER(SpriteAtlas, TexturesSerializedVersion); public: /// /// List with all tiles in the sprite atlas. /// API_FIELD() Array Sprites; public: /// /// Gets the sprites count. /// API_PROPERTY() int32 GetSpritesCount() const; /// /// Gets the sprite data. /// /// The index. /// The sprite data. API_FUNCTION() Sprite GetSprite(int32 index) const; /// /// Sets the sprite data. /// /// The index. /// The sprite data. API_FUNCTION() void SetSprite(int32 index, API_PARAM(Ref) const Sprite& value); /// /// Finds the sprite by the name. /// /// The name. /// The sprite handle. API_FUNCTION() SpriteHandle FindSprite(const StringView& name) const; /// /// Adds the sprite. /// /// The sprite. /// The sprite handle. API_FUNCTION() SpriteHandle AddSprite(const Sprite& sprite); /// /// Removes the sprite. /// /// The sprite index. API_FUNCTION() void RemoveSprite(int32 index); #if USE_EDITOR /// /// Save the sprites (texture content won't be modified). /// /// True if cannot save, otherwise false. API_FUNCTION() bool SaveSprites(); #endif protected: bool LoadSprites(ReadStream& stream); protected: // [BinaryAsset] LoadResult load() override; void unload(bool isReloading) override; AssetChunksFlag getChunksToPreload() const override; };