began work on skin manager with basics done and untested.
This commit is contained in:
parent
a27df5a287
commit
9392124317
@ -92,6 +92,7 @@
|
|||||||
<Compile Include="UI\Skin\Definitions\TextSkinDefinition.cs" />
|
<Compile Include="UI\Skin\Definitions\TextSkinDefinition.cs" />
|
||||||
<Compile Include="UI\Skin\Skin.cs" />
|
<Compile Include="UI\Skin\Skin.cs" />
|
||||||
<Compile Include="Serializable\SkinData.cs" />
|
<Compile Include="Serializable\SkinData.cs" />
|
||||||
|
<Compile Include="UI\Skin\SkinManager.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
|
104
RecrownedAthenaeum/UI/Skin/SkinManager.cs
Normal file
104
RecrownedAthenaeum/UI/Skin/SkinManager.cs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using RecrownedAthenaeum.Pipeline;
|
||||||
|
using RecrownedAthenaeum.Serializable;
|
||||||
|
using RecrownedAthenaeum.SpecialTypes;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace RecrownedAthenaeum.UI.Skin
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Manages reference to default and loading of custom skins.
|
||||||
|
/// </summary>
|
||||||
|
public class SkinManager
|
||||||
|
{
|
||||||
|
private const string EXTENSION = ".rbskin";
|
||||||
|
/// <summary>
|
||||||
|
/// The directory that contains the skins.
|
||||||
|
/// </summary>
|
||||||
|
public string skinsDirectory;
|
||||||
|
List<string> skinPaths = new List<string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates list of skins by searching recursively in the set <see cref="skinsDirectory"/>.
|
||||||
|
/// </summary>
|
||||||
|
public void SearchForSkins()
|
||||||
|
{
|
||||||
|
skinPaths.Clear();
|
||||||
|
skinPaths = RecursiveSkinSearch(skinsDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// Generate skin from skin data.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="skinData">The skin data to generate skin from.</param>
|
||||||
|
/// <param name="path">The path pointing the ".rbskin" file.</param>
|
||||||
|
/// <param name="graphicsDevice">The graphics device to generate the texture.</param>
|
||||||
|
/// <returns>The skin generated from the skin data.</returns>
|
||||||
|
public Skin GenerateSkinFromData(SkinData skinData, string path, GraphicsDevice graphicsDevice)
|
||||||
|
{
|
||||||
|
TextureAtlasDataReader textureAtlasDataReader = new TextureAtlasDataReader();
|
||||||
|
DirectoryInfo parentFolder = Directory.GetParent(path);
|
||||||
|
TextureAtlasData atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(parentFolder.FullName + "/" + skinData.nameOfTextureAtlas);
|
||||||
|
Texture2D atlasTexture;
|
||||||
|
using (FileStream stream = new FileStream(parentFolder.FullName + "/" + atlasData.textureName, FileMode.Open))
|
||||||
|
{
|
||||||
|
atlasTexture = Texture2D.FromStream(graphicsDevice, stream);
|
||||||
|
}
|
||||||
|
TextureAtlas.Region[] regions = textureAtlasDataReader.GenerateAtlasRegionsFromData(atlasData, atlasTexture);
|
||||||
|
|
||||||
|
Skin skin = new Skin(new TextureAtlas(atlasTexture, regions));
|
||||||
|
|
||||||
|
for (int i = 0; i < skinData.colors.Length; i++)
|
||||||
|
{
|
||||||
|
SkinData.ColorData colorData = skinData.colors[i];
|
||||||
|
skin.colors.Add(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user