Adding initial sprite database and sprite entry form

This commit is contained in:
2017-09-15 15:52:59 -05:00
parent 4d49f58809
commit 846d4b569f
5 changed files with 612 additions and 0 deletions

View File

@ -0,0 +1,224 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;
using System.Resources;
using System.IO;
namespace SpriteLibrary
{
internal struct ImageStruct
{
internal Image TheImage;
internal string ImageName;
}
/// <summary>
/// Store of all the types of things in the ADVDemo
/// </summary>
public class SpriteDatabase
{
List<SpriteInfo> SpriteInfoList = new List<SpriteInfo>();
List<ImageStruct> TheImages = new List<ImageStruct>();
ResourceManager myResourceManager = null;
string Filename = "";
public SpriteDatabase(ResourceManager theResourceManager, string filename)
{
myResourceManager = theResourceManager;
Filename = filename;
Load();
}
internal void Load()
{
LoadSpriteInfo();
}
//*******************************
//**** Sprite Info Functions ***
//*******************************
#region SpriteInfo Functions
void LoadSpriteInfo()
{
if (DoesResourceExist(Filename))
{
//This clears out the old list, as it gets replaced.
SpriteInfoList = LoadObjectFromXmlFile<List<SpriteInfo>>(Filename, myResourceManager);
}
else
{
//try loading it from an actual filename
if (File.Exists(Filename))
SpriteInfoList = ReadFromXmlFile<List<SpriteInfo>>(Filename);
}
//If neither works, we end up with an empty file.
//If it fails, SpriteInfoList is null and things explode.
if (SpriteInfoList == null)
SpriteInfoList = new List<SpriteInfo>(); //make an empty one so things do not explode.
}
public List<string> SpriteNames()
{
List<string> theNames = new List<string>();
foreach (SpriteInfo si in SpriteInfoList)
{
theNames.Add(si.SpriteName);
}
return theNames;
}
internal bool DoesResourceExist(string resourcename)
{
if (myResourceManager == null) return false;
if (myResourceManager.GetObject(resourcename) != null)
return true;
return false;
}
public Image GetImageFromName(string Name, bool UseSmartImages)
{
Image MyImage = null;
if (UseSmartImages)
{
foreach (ImageStruct IS in TheImages)
{
if (IS.ImageName.Equals(Name, StringComparison.InvariantCultureIgnoreCase))
{
MyImage = IS.TheImage;
break;
}
}
}
if (MyImage == null)
{
ResourceManager rm = myResourceManager;
MyImage = (Bitmap)rm.GetObject(Name);
if (UseSmartImages)
{
ImageStruct NewIS = new ImageStruct();
NewIS.ImageName = Name;
NewIS.TheImage = MyImage;
TheImages.Add(NewIS);
}
}
return MyImage;
}
public Sprite SmartDuplicateSprite(SpriteController theController, string SpriteName, bool UseSmartImages = true)
{
Sprite DestSprite = theController.DuplicateSprite(SpriteName);
if (DestSprite != null) return DestSprite;
//If it does not exist, make it
foreach (SpriteInfo SI in SpriteInfoList)
{
if (SI.SpriteName == SpriteName)
{
//This is the sprite. Pull out the image resource. then pull out the sprite.
ResourceManager rm = myResourceManager;
Image myImage = (Bitmap)rm.GetObject(SI.ImageName);
if (myImage == null) return null; //break out if we do not have the image defined for this
DestSprite = new Sprite(SI.StartPoint, theController, myImage, SI.Width, SI.Height, SI.AnimSpeed, SI.NumAnimations);
int sizepercent = SI.ViewPercent;
if (sizepercent < 5) sizepercent = 100;
if (sizepercent > 300) sizepercent = 100;
double delta = (double)sizepercent / 100.0; //turn it into a double, and into something we can multiply.
DestSprite.SetSize(new Size((int)(DestSprite.GetSize.Width * delta), (int)(DestSprite.GetSize.Height * delta)));
DestSprite.SetName(SpriteName);
//We have created a new sprite. Now, return a duplicate of that sprite.
return theController.DuplicateSprite(SpriteName);
}
}
return null;
}
#endregion
#region Generic XML Funcs
/// <summary>
/// Load in an XML serialized item from the specified ResourceManager. You will usually make one of these by
/// creating an object and using <see cref="SpriteDatabase.WriteToXmlFile">SpriteDatabase.WriteToXmlFile</see> to
/// save it to a file on your desktop. Then you can drag and drop that file into your project and then use this
/// LoadObjectFromXmlFile function.
/// </summary>
/// <typeparam name="T">The type of object to load. It could be something as simple as an int, a class, or a list of classes.</typeparam>
/// <param name="XMLResourceToLoad">The resource item to load. If you would access it like: properties.resources.myFile,
/// the correct value to put here would be "myFile"</param>
/// <param name="MyManager">The resource manager. Usually Properties.Resources.ResourceManager</param>
/// <returns>An object of the value you specified. Or null if it fails.</returns>
public static T LoadObjectFromXmlFile<T>(string XMLResourceToLoad, ResourceManager MyManager) where T : new()
{
//Load in the sprite data
XmlSerializer serializer = new XmlSerializer(typeof(T));
// Retrieves String and Image resources.
object titem = MyManager.GetObject(XMLResourceToLoad);
byte[] item = (byte[])System.Text.Encoding.UTF8.GetBytes((string)titem);
try
{
return (T)serializer.Deserialize(new MemoryStream(item));
}
finally
{
}
}
/// <summary>
/// Writes the given object instance to an XML file.
/// <para>Only Public properties and variables will be written to the file. These can be any type though, even other classes.</para>
/// <para>If there are public properties/variables that you do not want written to the file, decorate them with the [XmlIgnore] attribute.</para>
/// <para>Object type must have a parameterless constructor.</para>
/// </summary>
/// <typeparam name="T">The type of object being written to the file.</typeparam>
/// <param name="filePath">The file path to write the object instance to.</param>
/// <param name="objectToWrite">The object instance to write to the file.</param>
internal static void WriteToXmlFile<T>(string filePath, T objectToWrite) where T : new()
{
TextWriter writer = null;
try
{
var serializer = new XmlSerializer(typeof(T));
writer = new StreamWriter(filePath);
serializer.Serialize(writer, objectToWrite);
}
finally
{
if (writer != null)
writer.Close();
}
}
/// <summary>
/// Reads an object instance from an XML file.
/// <para>Object type must have a parameterless constructor.</para>
/// </summary>
/// <typeparam name="T">The type of object to read from the file.</typeparam>
/// <param name="filePath">The file path to read the object instance from.</param>
/// <returns>Returns a new instance of the object read from the XML file.</returns>
public static T ReadFromXmlFile<T>(string filePath) where T : new()
{
TextReader reader = null;
try
{
var serializer = new XmlSerializer(typeof(T));
reader = new StreamReader(filePath);
return (T)serializer.Deserialize(reader);
}
finally
{
if (reader != null)
reader.Close();
}
}
#endregion
}
}