using System; using SpellBoundAR.SavedAssetsSystem; using SpellBoundAR.SaveSystem; using UnityEngine; namespace SpellBoundAR.CustomTextures { [Serializable] public class SavedCustomTexture : ISavedCustomTexture { public event Action OnSaved; public event Action OnDeleted; public static event Action OnAnySaved; public static event Action OnAnyDeleted; public static event Action OnAnyTexture2DChanged; public static event Action OnAnyNameChanged; public static event Action OnAnyTimestampChanged; private const string Texture2DFile = "Texture2D.png"; private const string NameFile = "Name.txt"; private const string TimestampFile = "Timestamp.txt"; [SerializeField] private SavedTexture2D savedTexture2D; [SerializeField] private SavedString savedName; [SerializeField] private SavedString savedTimestamp; public PathData PathData { get; private set; } public Texture2D Texture2D { get => savedTexture2D.Texture2D; set => savedTexture2D.Texture2D = value; } public string Name { get => savedName.Value; set => savedName.Value = value; } public string Timestamp { get => savedTimestamp.Value; set => savedTimestamp.Value = value; } public virtual bool Saved => savedTexture2D.Saved || savedName.Saved || savedTimestamp.Saved; public virtual void Save() { savedTexture2D.Save(); savedName.Save(); savedTimestamp.Save(); OnSaved?.Invoke(); OnAnySaved?.Invoke(this); } public void Delete() { SaveSystem.SaveSystem.DeleteDirectory(PathData.Directory); OnDeleted?.Invoke(); OnAnyDeleted?.Invoke(this); } public Sprite CreateSprite() { Texture2D texture2D = Texture2D; if (!texture2D) return null; Rect rect = new Rect(0, 0, texture2D.width, texture2D.height); Vector2 pivot = new Vector2(.5f, .5f); Sprite sprite = Sprite.Create(texture2D, rect, pivot); sprite.name = PathData.Directory; return sprite; } public SavedCustomTexture(string directory, string name, Texture2D texture2D) { PathData = new PathData(1, directory, name, FileType.PNG); savedTexture2D = new (PathData.Directory, Texture2DFile, texture2D, () => OnAnyTexture2DChanged?.Invoke(this)); savedName = new (PathData.Directory, NameFile, name, () => OnAnyNameChanged?.Invoke(this)); savedTimestamp = new(PathData.Directory, TimestampFile, DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt"), () => OnAnyTimestampChanged?.Invoke(this)); } public SavedCustomTexture(string directory, string name, int width, int height) { PathData = new PathData(1, directory, name, FileType.PNG); Texture2D texture2D = new Texture2D(width, height); savedTexture2D = new (PathData.Directory, Texture2DFile, texture2D, () => OnAnyTexture2DChanged?.Invoke(this)); savedName = new (PathData.Directory, NameFile, PathData.FileName, () => OnAnyNameChanged?.Invoke(this)); savedTimestamp = new(PathData.Directory, TimestampFile, DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt"), () => OnAnyTimestampChanged?.Invoke(this)); } } }