recrownedgtk/RecrownedAthenaeum/UI/Skin/SkinManager.cs

216 lines
8.1 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json;
using RecrownedAthenaeum.Pipeline;
using RecrownedAthenaeum.Data;
using RecrownedAthenaeum.SpecialTypes;
using System;
using System.Collections.Generic;
using System.IO;
2019-01-21 00:05:40 +00:00
using System.Threading;
namespace RecrownedAthenaeum.UI.Skin
{
2019-01-21 00:05:40 +00:00
/// <summary>
/// Called when the skin manager has completed a async action.
/// </summary>
/// <param name="actionCompleted">The completed action.</param>
public delegate void AsyncComplete(SkinManager.Action actionCompleted);
/// <summary>
/// Manages reference to default and loading of custom skins.
/// </summary>
public class SkinManager
{
private const string EXTENSION = ".rbskin";
private readonly MergedSkin mergedSkin = new MergedSkin();
2019-01-21 00:05:40 +00:00
private Thread thread;
private Action action;
private GraphicsDevice graphicsDevice;
private string selectedSkinPath;
private SkinData skinDataToUse;
/// <summary>
/// The list of paths for all found skins by <see cref="SearchSkinDirectory"/>. May be null.
/// </summary>
public volatile List<string> skinPaths;
/// <summary>
/// The event that is called when a state changes.
/// </summary>
public event AsyncComplete AsyncCompleteEvent;
/// <summary>
/// The various possible states a skin manager could be in.
/// </summary>
public enum Action
{
/// <summary>
/// After a search has completed.
/// </summary>
SEARCH,
/// <summary>
/// Having the skin generated to be in a useable state.
/// </summary>
GENERATE
}
/// <summary>
/// the skin that favors the selected skin, but still has a fallback to the default skin in case anything is missing.
/// </summary>
public ISkin Skin { get { return mergedSkin; } }
/// <summary>
/// The user selected skin.
/// </summary>
public ISkin SelectedSkin { get { return mergedSkin.mainSkin; } set { mergedSkin.mainSkin = value; } }
/// <summary>
/// The fallback skin in case the selected skin doesn't cover a specific definition or color.
/// </summary>
public ISkin BaseSkin { get { return mergedSkin.alternateSkin; } set { mergedSkin.alternateSkin = value; } }
/// <summary>
/// The directory that contains the skins.
/// </summary>
public string skinsDirectory;
/// <summary>
2019-01-21 00:05:40 +00:00
/// Performs a recursive asynchronous search of the directory given in a path set by <see cref="skinsDirectory"/>.
/// </summary>
2019-01-21 00:05:40 +00:00
public void SearchSkinDirectory()
{
2019-01-21 00:05:40 +00:00
action = Action.SEARCH;
AttemptAsync();
}
/// <summary>
/// Loads skin data if extension is valid.
/// </summary>
/// <param name="path">the path to load from.</param>
/// <returns>A <see cref="SkinData"/> that holds all the information and some metadata for the loaded skin.</returns>
public SkinData LoadSkinData(string path)
{
if (path.ToLower().EndsWith(EXTENSION))
{
return JsonConvert.DeserializeObject<SkinData>(File.ReadAllText(path));
}
throw new ArgumentException("The path given does not point to a file with the required extension \"" + EXTENSION + "\" rather, has \"" + Path.GetExtension(path) + "\".");
}
/// <summary>
2019-01-21 00:05:40 +00:00
/// Generates a skin asynchronously.
/// </summary>
2019-01-21 00:05:40 +00:00
/// <param name="graphicsDevice">Graphics device to use for texture creation.</param>
/// <param name="skinData">The data to generate from.</param>
/// <param name="path">The path pointing to the file with the extension "<see cref="EXTENSION"/>".</param>
2019-01-21 00:05:40 +00:00
public void GenerateSkin(GraphicsDevice graphicsDevice, SkinData skinData, string path)
{
action = Action.GENERATE;
this.graphicsDevice = graphicsDevice;
this.selectedSkinPath = path;
this.skinDataToUse = skinData;
AttemptAsync();
}
private void AttemptAsync()
{
if (thread != null && thread.IsAlive) throw new InvalidOperationException("Already performing task: " + action);
thread = new Thread(ThreadStart);
}
private void ThreadStart()
{
switch (action)
{
case Action.SEARCH:
SearchForSkins();
OnAsyncComplete(action);
break;
case Action.GENERATE:
SelectedSkin = GenerateSkinFromData(skinDataToUse, selectedSkinPath, graphicsDevice);
OnAsyncComplete(action);
break;
}
}
private void SearchForSkins()
{
skinPaths.Clear();
skinPaths = RecursiveSkinSearch(skinsDirectory);
}
private Skin GenerateSkinFromData(SkinData skinData, string path, GraphicsDevice graphicsDevice)
{
TextureAtlasDataReader textureAtlasDataReader = new TextureAtlasDataReader();
FileInfo[] skinFiles = Directory.GetParent(path).GetFiles();
Dictionary<string, string> fileNameWithPath = new Dictionary<string, string>();
for (int i = 0; i < skinFiles.Length; i++)
{
fileNameWithPath.Add(skinFiles[i].Name, skinFiles[i].FullName);
}
TextureAtlasData atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(fileNameWithPath[skinData.nameOfTextureAtlas]);
Texture2D atlasTexture;
using (FileStream stream = new FileStream(fileNameWithPath[atlasData.textureName], FileMode.Open))
{
atlasTexture = Texture2D.FromStream(graphicsDevice, stream);
}
TextureAtlas.Region[] regions = textureAtlasDataReader.GenerateAtlasRegionsFromData(atlasData, atlasTexture);
TextureAtlas textureAtlas = new TextureAtlas(atlasTexture, regions);
Texture2D cursorTexture;
if (Path.HasExtension(skinData.cursorTextureName) && fileNameWithPath.ContainsKey(skinData.cursorTextureName))
{
using (FileStream stream = new FileStream(fileNameWithPath[skinData.cursorTextureName], FileMode.Open))
{
cursorTexture = Texture2D.FromStream(graphicsDevice, stream);
}
2019-01-21 00:05:40 +00:00
}
else
{
cursorTexture = textureAtlas[skinData.cursorTextureName].AsTexture2D(graphicsDevice);
}
Skin skin = new Skin(new TextureAtlas(atlasTexture, regions), cursorTexture);
for (int i = 0; i < skinData.colors.Length; i++)
{
SkinData.ColorData colorData = skinData.colors[i];
skin.AddColor(colorData.name, new Color(colorData.r, colorData.g, colorData.b, colorData.a));
}
for (int i = 0; i < skinData.definitions.Length; i++)
{
SkinData.DefinitionData definitionData = skinData.definitions[i];
skin.AddDefinition(definitionData.name, definitionData.skin);
}
return skin;
}
private List<string> RecursiveSkinSearch(string path)
{
string[] files = Directory.GetFiles(path);
string[] folders = Directory.GetDirectories(path);
List<string> skins = new List<string>();
for (int i = 0; i < files.Length; i++)
{
if (files[i].ToLower().EndsWith(EXTENSION))
{
skins.Add(files[i]);
}
}
for (int i = 0; i < folders.Length; i++)
{
skins.AddRange(RecursiveSkinSearch(folders[i]));
}
return skins;
}
2019-01-21 00:05:40 +00:00
private void OnAsyncComplete(Action newState)
{
AsyncCompleteEvent?.Invoke(newState);
}
}
}