Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nicloay
Last active September 3, 2021 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicloay/f0b704e7e70c9d0fd6a165e2cb4966bc to your computer and use it in GitHub Desktop.
Save nicloay/f0b704e7e70c9d0fd6a165e2cb4966bc to your computer and use it in GitHub Desktop.
using System.Collections;
using PaintCraft.Utils;
using UnityEngine;
namespace PaintCraft.Canvas.Configs
{
public class PageConfig : IPageConfig, ISaveUserChanges, ISaveUserIcon, IOutlineTexture, IRegionTexture,
IDefaultIcon, IDownloadIconAsync, IDownloadDataAsync
{
/// <summary>
/// Max icon width or height
/// </summary>
public const int MaxDimSize = 400;
protected readonly PageConfigPathInfo pathInfo;
private readonly Lazy<Texture> defaultIcon;
private Lazy<Texture> outlineTexture;
private Lazy<Texture2D> regionTexture;
public PageConfig(PageConfigPathInfo pathInfo)
{
this.pathInfo = pathInfo;
defaultIcon = new Lazy<Texture>(() => this.pathInfo.OriginalIconPath.GetTexture());
outlineTexture = new Lazy<Texture>(() => this.pathInfo.OutlinePath.GetTexture());
regionTexture = new Lazy<Texture2D>(() => this.pathInfo.RegionPath.GetTexture());
}
public Texture DefaultIcon => defaultIcon.Value;
public virtual IEnumerator DownloadData()
{
yield return CoroutineHolder.instance.StartCoroutine(TextureUtil.DownloadUrlInToTexture(
pathInfo.OutlinePath.FullPath, true,
texture2D => { outlineTexture.ReplaceValue(texture2D); }));
yield return CoroutineHolder.instance.StartCoroutine(TextureUtil.DownloadUrlInToTexture(
pathInfo.RegionPath.FullPath, true,
texture2D => { regionTexture.ReplaceValue(texture2D); }));
}
public IEnumerator DownloadIcon()
{
yield return CoroutineHolder.instance.StartCoroutine(TextureUtil.DownloadUrlInToTexture(
pathInfo.OriginalIconPath.FullPath, true,
texture2D => { defaultIcon.ReplaceValue(texture2D); }));
}
public Texture OutlineTexture => outlineTexture.Value;
public virtual Vector2Int Size => TextureUtil.GetSizeFromFirstNotNullTexture(RegionTexture, OutlineTexture);
public virtual void Dispose()
{
outlineTexture = null;
regionTexture = null;
}
public Texture2D RegionTexture => regionTexture.Value;
public virtual Texture LoadUserChanges()
=> pathInfo.UserChangesPath.GetTexture(Config.SaveUserChangesFormat, Size);
public void SaveUserChanges(RenderTexture texture)
=> texture.SaveRenderTexture(pathInfo.UserChangesPath.FullPath, Config.SaveUserChangesFormat);
public Vector2Int IconSize => Size.ResizeToMaxDimension(MaxDimSize);
public Texture LoadUserIcon() => pathInfo.UserIconPath.GetTexture(Config.SaveIconFormat);
public void SaveUserIcon(RenderTexture texture)
=> texture.SaveRenderTexture(pathInfo.UserIconPath.FullPath, Config.SaveIconFormat);
}
}
using System.IO;
using PaintCraft.Utils;
namespace PaintCraft.Canvas.Configs
{
public class PageConfigPathInfo
{
private const string IconExtension = ".icon.png";
private const string OutlineExtension = ".outline.png";
private const string RegionExtension = ".regions.png";
private const string UserIconName = "icon.png";
private const string UserChangesName = "changes.raw";
private readonly Lazy<StreamingPath> originalIconPath;
private readonly Lazy<StreamingPath> outlinePath;
private readonly Lazy<StreamingPath> regionPath;
private readonly Lazy<PersistentPath> userChangesPath;
private readonly Lazy<PersistentPath> userIconPath;
public PageConfigPathInfo(PagePath pagePath)
{
BaseStreamingPath = pagePath.Path;
outlinePath = new Lazy<StreamingPath>(() => new StreamingPath(BaseStreamingPath + OutlineExtension));
regionPath = new Lazy<StreamingPath>(() => new StreamingPath(BaseStreamingPath + RegionExtension));
originalIconPath = new Lazy<StreamingPath>(() => new StreamingPath(BaseStreamingPath + IconExtension));
var baseLocalVersionPath = pagePath.Path + "/" + pagePath.Version + "/";
userIconPath = new Lazy<PersistentPath>(() =>
new PersistentPath(Path.Combine(pagePath.FullLocalPath, UserIconName)));
userChangesPath = new Lazy<PersistentPath>(() =>
new PersistentPath(Path.Combine(pagePath.FullLocalPath, UserChangesName)));
}
protected string BaseStreamingPath { get; }
public StreamingPath OutlinePath => outlinePath.Value;
public StreamingPath RegionPath => regionPath.Value;
public StreamingPath OriginalIconPath => originalIconPath.Value;
public PersistentPath UserIconPath => userIconPath.Value;
public PersistentPath UserChangesPath => userChangesPath.Value;
}
}
using System.Collections;
using PaintCraft.Utils;
using UnityEngine;
namespace PaintCraft.Canvas.Configs
{
/// <summary>
/// Scriptable object adapter to PageConfig
/// </summary>
[CreateAssetMenu(fileName = "PageConfig", menuName = "PaintCraft/PageConfig")]
public class PageConfigSO : ScriptableObject, IPageConfig, ISaveUserChanges, ISaveUserIcon, IOutlineTexture,
IRegionTexture, IDefaultIcon, IDownloadIconAsync, IDownloadDataAsync
{
[SerializeField] private string streamingAssetPath;
[SerializeField] private int version;
public string StreamingAssetPath => streamingAssetPath;
public int Version => version;
private PageConfig pageConfig;
private PageConfig PageConfig
{
get
{
if (pageConfig == null)
pageConfig = new PageConfig(new PageConfigPathInfo(new PagePath(streamingAssetPath, version)));
return pageConfig;
}
}
public Texture DefaultIcon => pageConfig.DefaultIcon;
public Texture OutlineTexture => PageConfig.OutlineTexture;
public void Dispose()
{
PageConfig.Dispose();
pageConfig = null;
}
public Vector2Int Size => PageConfig.Size;
public Texture2D RegionTexture => PageConfig.RegionTexture;
public Texture LoadUserChanges() => PageConfig.LoadUserChanges();
public void SaveUserChanges(RenderTexture texture) => PageConfig.SaveUserChanges(texture);
public Vector2Int IconSize => PageConfig.IconSize;
public Texture LoadUserIcon() => PageConfig.LoadUserIcon();
public void SaveUserIcon(RenderTexture texture) => PageConfig.SaveUserIcon(texture);
public IEnumerator DownloadIcon() => PageConfig.DownloadIcon();
public IEnumerator DownloadData() => PageConfig.DownloadData();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment