using System.Collections.Generic; using System.Linq; using UnityEditor.U2D.Aseprite.Common; using UnityEngine; using UnityEngine.Assertions; using UnityEditor.U2D.Sprites; using UnityEngine.U2D; namespace UnityEditor.U2D.Aseprite { internal abstract class AsepriteDataProvider { public AsepriteImporter dataProvider; } internal class SpriteBoneDataProvider : AsepriteDataProvider, ISpriteBoneDataProvider { public List GetBones(GUID guid) { var sprite = ((SpriteMetaData)dataProvider.GetSpriteData(guid)); Assert.IsNotNull(sprite, string.Format("Sprite not found for GUID:{0}", guid.ToString())); return sprite.spriteBone != null ? sprite.spriteBone.ToList() : new List(); } public void SetBones(GUID guid, List bones) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).spriteBone = bones; } } internal class TextureDataProvider : AsepriteDataProvider, ITextureDataProvider { Texture2D m_ReadableTexture; Texture2D m_OriginalTexture; AsepriteImporter textureImporter { get { return (AsepriteImporter)dataProvider.targetObject; } } public Texture2D texture { get { if (m_OriginalTexture == null) m_OriginalTexture = AssetDatabase.LoadAssetAtPath(textureImporter.assetPath); return m_OriginalTexture; } } public Texture2D previewTexture { get { return texture; } } public Texture2D GetReadableTexture2D() { if (m_ReadableTexture == null) { m_ReadableTexture = InternalEditorBridge.CreateTemporaryDuplicate(texture, texture.width, texture.height); if (m_ReadableTexture != null) m_ReadableTexture.filterMode = texture.filterMode; } return m_ReadableTexture; } public void GetTextureActualWidthAndHeight(out int width, out int height) { width = dataProvider.textureActualWidth; height = dataProvider.textureActualHeight; } } internal class SecondaryTextureDataProvider : AsepriteDataProvider, ISecondaryTextureDataProvider { public SecondarySpriteTexture[] textures { get { return dataProvider.secondaryTextures; } set { dataProvider.secondaryTextures = value; } } } internal class SpriteOutlineDataProvider : AsepriteDataProvider, ISpriteOutlineDataProvider { public List GetOutlines(GUID guid) { var sprite = ((SpriteMetaData)dataProvider.GetSpriteData(guid)); Assert.IsNotNull(sprite, string.Format("Sprite not found for GUID:{0}", guid.ToString())); var outline = sprite.spriteOutline; if (outline != null) return outline.Select(x => x.outline).ToList(); return new List(); } public void SetOutlines(GUID guid, List data) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).spriteOutline = data.Select(x => new SpriteOutline() {outline = x}).ToList(); } public float GetTessellationDetail(GUID guid) { return ((SpriteMetaData)dataProvider.GetSpriteData(guid)).tessellationDetail; } public void SetTessellationDetail(GUID guid, float value) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).tessellationDetail = value; } } internal class SpritePhysicsOutlineProvider : AsepriteDataProvider, ISpritePhysicsOutlineDataProvider { public List GetOutlines(GUID guid) { var sprite = ((SpriteMetaData)dataProvider.GetSpriteData(guid)); Assert.IsNotNull(sprite, string.Format("Sprite not found for GUID:{0}", guid.ToString())); var outline = sprite.spritePhysicsOutline; if (outline != null) return outline.Select(x => x.outline).ToList(); return new List(); } public void SetOutlines(GUID guid, List data) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).spritePhysicsOutline = data.Select(x => new SpriteOutline() { outline = x }).ToList(); } public float GetTessellationDetail(GUID guid) { return ((SpriteMetaData)dataProvider.GetSpriteData(guid)).tessellationDetail; } public void SetTessellationDetail(GUID guid, float value) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).tessellationDetail = value; } } internal class SpriteMeshDataProvider : AsepriteDataProvider, ISpriteMeshDataProvider { public Vertex2DMetaData[] GetVertices(GUID guid) { var sprite = ((SpriteMetaData)dataProvider.GetSpriteData(guid)); Assert.IsNotNull(sprite, string.Format("Sprite not found for GUID:{0}", guid.ToString())); var v = sprite.vertices; if (v != null) return v.ToArray(); return new Vertex2DMetaData[0]; } public void SetVertices(GUID guid, Vertex2DMetaData[] vertices) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).vertices = vertices.ToList(); } public int[] GetIndices(GUID guid) { var sprite = ((SpriteMetaData)dataProvider.GetSpriteData(guid)); Assert.IsNotNull(sprite, string.Format("Sprite not found for GUID:{0}", guid.ToString())); var v = sprite.indices; if (v != null) return v; return new int[0]; } public void SetIndices(GUID guid, int[] indices) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).indices = indices; } public Vector2Int[] GetEdges(GUID guid) { var sprite = ((SpriteMetaData)dataProvider.GetSpriteData(guid)); Assert.IsNotNull(sprite, string.Format("Sprite not found for GUID:{0}", guid.ToString())); var v = sprite.edges; if (v != null) return v; return new Vector2Int[0]; } public void SetEdges(GUID guid, Vector2Int[] edges) { var sprite = dataProvider.GetSpriteData(guid); if (sprite != null) ((SpriteMetaData)sprite).edges = edges; } } }