From 9392124317491f2d59df3f07ce90a739945ac3a4 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Tue, 15 Jan 2019 19:34:59 -0600 Subject: [PATCH] began work on skin manager with basics done and untested. --- RecrownedAthenaeum/RecrownedAthenaeum.csproj | 1 + RecrownedAthenaeum/UI/Skin/SkinManager.cs | 104 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 RecrownedAthenaeum/UI/Skin/SkinManager.cs diff --git a/RecrownedAthenaeum/RecrownedAthenaeum.csproj b/RecrownedAthenaeum/RecrownedAthenaeum.csproj index 84589a6..169aaaf 100644 --- a/RecrownedAthenaeum/RecrownedAthenaeum.csproj +++ b/RecrownedAthenaeum/RecrownedAthenaeum.csproj @@ -92,6 +92,7 @@ + diff --git a/RecrownedAthenaeum/UI/Skin/SkinManager.cs b/RecrownedAthenaeum/UI/Skin/SkinManager.cs new file mode 100644 index 0000000..645eed2 --- /dev/null +++ b/RecrownedAthenaeum/UI/Skin/SkinManager.cs @@ -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 +{ + /// + /// Manages reference to default and loading of custom skins. + /// + public class SkinManager + { + private const string EXTENSION = ".rbskin"; + /// + /// The directory that contains the skins. + /// + public string skinsDirectory; + List skinPaths = new List(); + + /// + /// Updates list of skins by searching recursively in the set . + /// + public void SearchForSkins() + { + skinPaths.Clear(); + skinPaths = RecursiveSkinSearch(skinsDirectory); + } + + /// + /// Loads skin data if extension is valid. + /// + /// the path to load from. + /// A that holds all the information and some metadata for the loaded skin. + public SkinData LoadSkinData(string path) + { + if (path.ToLower().EndsWith(EXTENSION)) + { + return JsonConvert.DeserializeObject(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) + "\"."); + } + + /// + /// Generate skin from skin data. + /// + /// The skin data to generate skin from. + /// The path pointing the ".rbskin" file. + /// The graphics device to generate the texture. + /// The skin generated from the skin data. + public Skin GenerateSkinFromData(SkinData skinData, string path, GraphicsDevice graphicsDevice) + { + TextureAtlasDataReader textureAtlasDataReader = new TextureAtlasDataReader(); + DirectoryInfo parentFolder = Directory.GetParent(path); + TextureAtlasData atlasData = JsonConvert.DeserializeObject(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 RecursiveSkinSearch(string path) + { + string[] files = Directory.GetFiles(path); + string[] folders = Directory.GetDirectories(path); + List skins = new List(); + 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; + } + } +}