Adding initial sprite database and sprite entry form

This commit is contained in:
Tim Young 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
}
}

276
SpriteLibrary/SpriteEntryForm.Designer.cs generated Normal file
View File

@ -0,0 +1,276 @@
namespace SpriteLibrary
{
partial class SpriteEntryForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pbImageField = new System.Windows.Forms.PictureBox();
this.cbStartingImage = new System.Windows.Forms.ComboBox();
this.lblStartingImage = new System.Windows.Forms.Label();
this.lblChosenArea = new System.Windows.Forms.Label();
this.tbNumFrames = new System.Windows.Forms.TextBox();
this.lblFrames = new System.Windows.Forms.Label();
this.tbSpriteName = new System.Windows.Forms.TextBox();
this.lblSpriteName = new System.Windows.Forms.Label();
this.btnDone = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnApply = new System.Windows.Forms.Button();
this.btnNew = new System.Windows.Forms.Button();
this.tbAmimationSpeed = new System.Windows.Forms.TextBox();
this.lblAnimationSpeed = new System.Windows.Forms.Label();
this.lblDefaultSize = new System.Windows.Forms.Label();
this.tbDefaultSize = new System.Windows.Forms.TextBox();
this.btnBack = new System.Windows.Forms.Button();
this.btnFwd = new System.Windows.Forms.Button();
this.lblCountSprites = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pbImageField)).BeginInit();
this.SuspendLayout();
//
// pbImageField
//
this.pbImageField.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.pbImageField.Location = new System.Drawing.Point(5, 10);
this.pbImageField.Name = "pbImageField";
this.pbImageField.Size = new System.Drawing.Size(213, 253);
this.pbImageField.TabIndex = 0;
this.pbImageField.TabStop = false;
//
// cbStartingImage
//
this.cbStartingImage.FormattingEnabled = true;
this.cbStartingImage.Location = new System.Drawing.Point(349, 40);
this.cbStartingImage.Name = "cbStartingImage";
this.cbStartingImage.Size = new System.Drawing.Size(131, 24);
this.cbStartingImage.TabIndex = 1;
this.cbStartingImage.SelectedIndexChanged += new System.EventHandler(this.cbStartingImage_SelectedIndexChanged);
//
// lblStartingImage
//
this.lblStartingImage.AutoSize = true;
this.lblStartingImage.Location = new System.Drawing.Point(244, 43);
this.lblStartingImage.Name = "lblStartingImage";
this.lblStartingImage.Size = new System.Drawing.Size(99, 17);
this.lblStartingImage.TabIndex = 2;
this.lblStartingImage.Text = "Starting Image";
//
// lblChosenArea
//
this.lblChosenArea.AutoSize = true;
this.lblChosenArea.Location = new System.Drawing.Point(350, 77);
this.lblChosenArea.Name = "lblChosenArea";
this.lblChosenArea.Size = new System.Drawing.Size(46, 17);
this.lblChosenArea.TabIndex = 3;
this.lblChosenArea.Text = "label2";
//
// tbNumFrames
//
this.tbNumFrames.Location = new System.Drawing.Point(349, 109);
this.tbNumFrames.Name = "tbNumFrames";
this.tbNumFrames.Size = new System.Drawing.Size(47, 22);
this.tbNumFrames.TabIndex = 4;
//
// lblFrames
//
this.lblFrames.AutoSize = true;
this.lblFrames.Location = new System.Drawing.Point(288, 109);
this.lblFrames.Name = "lblFrames";
this.lblFrames.Size = new System.Drawing.Size(55, 17);
this.lblFrames.TabIndex = 5;
this.lblFrames.Text = "Frames";
//
// tbSpriteName
//
this.tbSpriteName.Location = new System.Drawing.Point(349, 10);
this.tbSpriteName.Name = "tbSpriteName";
this.tbSpriteName.Size = new System.Drawing.Size(100, 22);
this.tbSpriteName.TabIndex = 6;
//
// lblSpriteName
//
this.lblSpriteName.AutoSize = true;
this.lblSpriteName.Location = new System.Drawing.Point(251, 14);
this.lblSpriteName.Name = "lblSpriteName";
this.lblSpriteName.Size = new System.Drawing.Size(86, 17);
this.lblSpriteName.TabIndex = 7;
this.lblSpriteName.Text = "Sprite Name";
//
// btnDone
//
this.btnDone.Location = new System.Drawing.Point(434, 259);
this.btnDone.Name = "btnDone";
this.btnDone.Size = new System.Drawing.Size(75, 23);
this.btnDone.TabIndex = 8;
this.btnDone.Text = "Done";
this.btnDone.UseVisualStyleBackColor = true;
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(353, 259);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 9;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
//
// btnApply
//
this.btnApply.Location = new System.Drawing.Point(434, 230);
this.btnApply.Name = "btnApply";
this.btnApply.Size = new System.Drawing.Size(75, 23);
this.btnApply.TabIndex = 10;
this.btnApply.Text = "Apply";
this.btnApply.UseVisualStyleBackColor = true;
//
// btnNew
//
this.btnNew.Location = new System.Drawing.Point(353, 230);
this.btnNew.Name = "btnNew";
this.btnNew.Size = new System.Drawing.Size(75, 23);
this.btnNew.TabIndex = 11;
this.btnNew.Text = "New";
this.btnNew.UseVisualStyleBackColor = true;
//
// tbAmimationSpeed
//
this.tbAmimationSpeed.Location = new System.Drawing.Point(349, 145);
this.tbAmimationSpeed.Name = "tbAmimationSpeed";
this.tbAmimationSpeed.Size = new System.Drawing.Size(100, 22);
this.tbAmimationSpeed.TabIndex = 12;
//
// lblAnimationSpeed
//
this.lblAnimationSpeed.AutoSize = true;
this.lblAnimationSpeed.Location = new System.Drawing.Point(228, 148);
this.lblAnimationSpeed.Name = "lblAnimationSpeed";
this.lblAnimationSpeed.Size = new System.Drawing.Size(115, 17);
this.lblAnimationSpeed.TabIndex = 13;
this.lblAnimationSpeed.Text = "Animation Speed";
//
// lblDefaultSize
//
this.lblDefaultSize.AutoSize = true;
this.lblDefaultSize.Location = new System.Drawing.Point(261, 188);
this.lblDefaultSize.Name = "lblDefaultSize";
this.lblDefaultSize.Size = new System.Drawing.Size(84, 17);
this.lblDefaultSize.TabIndex = 14;
this.lblDefaultSize.Text = "Default Size";
//
// tbDefaultSize
//
this.tbDefaultSize.Location = new System.Drawing.Point(349, 185);
this.tbDefaultSize.Name = "tbDefaultSize";
this.tbDefaultSize.Size = new System.Drawing.Size(100, 22);
this.tbDefaultSize.TabIndex = 15;
//
// btnBack
//
this.btnBack.Location = new System.Drawing.Point(43, 269);
this.btnBack.Name = "btnBack";
this.btnBack.Size = new System.Drawing.Size(29, 23);
this.btnBack.TabIndex = 16;
this.btnBack.Text = "<";
this.btnBack.UseVisualStyleBackColor = true;
//
// btnFwd
//
this.btnFwd.Location = new System.Drawing.Point(125, 269);
this.btnFwd.Name = "btnFwd";
this.btnFwd.Size = new System.Drawing.Size(29, 23);
this.btnFwd.TabIndex = 17;
this.btnFwd.Text = ">";
this.btnFwd.UseVisualStyleBackColor = true;
//
// lblCountSprites
//
this.lblCountSprites.AutoSize = true;
this.lblCountSprites.Location = new System.Drawing.Point(89, 272);
this.lblCountSprites.Name = "lblCountSprites";
this.lblCountSprites.Size = new System.Drawing.Size(16, 17);
this.lblCountSprites.TabIndex = 18;
this.lblCountSprites.Text = "0";
//
// SpriteEntryForm
//
this.AcceptButton = this.btnApply;
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(524, 294);
this.Controls.Add(this.lblCountSprites);
this.Controls.Add(this.btnFwd);
this.Controls.Add(this.btnBack);
this.Controls.Add(this.tbDefaultSize);
this.Controls.Add(this.lblDefaultSize);
this.Controls.Add(this.lblAnimationSpeed);
this.Controls.Add(this.tbAmimationSpeed);
this.Controls.Add(this.btnNew);
this.Controls.Add(this.btnApply);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnDone);
this.Controls.Add(this.lblSpriteName);
this.Controls.Add(this.tbSpriteName);
this.Controls.Add(this.lblFrames);
this.Controls.Add(this.tbNumFrames);
this.Controls.Add(this.lblChosenArea);
this.Controls.Add(this.lblStartingImage);
this.Controls.Add(this.cbStartingImage);
this.Controls.Add(this.pbImageField);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SpriteEntryForm";
this.Text = "SpriteEntryForm";
((System.ComponentModel.ISupportInitialize)(this.pbImageField)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pbImageField;
private System.Windows.Forms.ComboBox cbStartingImage;
private System.Windows.Forms.Label lblStartingImage;
private System.Windows.Forms.Label lblChosenArea;
private System.Windows.Forms.TextBox tbNumFrames;
private System.Windows.Forms.Label lblFrames;
private System.Windows.Forms.TextBox tbSpriteName;
private System.Windows.Forms.Label lblSpriteName;
private System.Windows.Forms.Button btnDone;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnApply;
private System.Windows.Forms.Button btnNew;
private System.Windows.Forms.TextBox tbAmimationSpeed;
private System.Windows.Forms.Label lblAnimationSpeed;
private System.Windows.Forms.Label lblDefaultSize;
private System.Windows.Forms.TextBox tbDefaultSize;
private System.Windows.Forms.Button btnBack;
private System.Windows.Forms.Button btnFwd;
private System.Windows.Forms.Label lblCountSprites;
}
}

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using System.Windows;
using System.Collections;
namespace SpriteLibrary
{
public partial class SpriteEntryForm : Form
{
SpriteController MyController;
ResourceManager myResources = null;
public SpriteEntryForm(ResourceManager theResourceManager)
{
InitializeComponent();
myResources = theResourceManager;
LocalSetup();
}
public void LocalSetup()
{
pbImageField.BackgroundImageLayout = ImageLayout.Stretch;
MyController = new SpriteController(pbImageField);
PopulateMenu();
}
public void PopulateMenu()
{
ResourceManager rm = Properties.Resources.ResourceManager;
if (myResources != null) rm = myResources;
PopulateMenu(rm);
}
public void PopulateMenu(ResourceManager rm)
{
if (myResources == null) myResources = rm;
ResourceSet RS = rm.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
cbStartingImage.Items.Clear();
foreach (DictionaryEntry entry in RS)
{
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
if (resource is Image)
{
cbStartingImage.Items.Add(resourceKey);
}
}
cbStartingImage.SelectedIndex = 0;
}
private void cbStartingImage_SelectedIndexChanged(object sender, EventArgs e)
{
ResourceManager rm;
if (myResources != null) rm = myResources;
else rm = Properties.Resources.ResourceManager;
//We have a selected item
if (cbStartingImage.SelectedIndex >= 0)
{
//Load in a new image into our background
Image NewImage = (Bitmap)rm.GetObject(cbStartingImage.SelectedItem.ToString());
if (NewImage != null)
{
MyController.ReplaceOriginalImage(new Bitmap(NewImage));
pbImageField.BackgroundImage = new Bitmap(NewImage);
pbImageField.Invalidate();
}
}
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace SpriteLibrary
{
internal class SpriteInfo
{
public string SpriteName = "";
public Point StartPoint = new Point(-1, -1);
public string ImageName = "";
public int Width = -1;
public int Height = -1;
public int AnimSpeed = 200;
public int NumAnimations = 1;
public int ViewPercent = 100; //The percent size of the sprite. 100 is full. 50 is half-size
}
}

View File

@ -62,6 +62,14 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sprite.cs" />
<Compile Include="SpriteController.cs" />
<Compile Include="SpriteDatabase.cs" />
<Compile Include="SpriteEntryForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="SpriteEntryForm.Designer.cs">
<DependentUpon>SpriteEntryForm.cs</DependentUpon>
</Compile>
<Compile Include="SpriteInfo.cs" />
<Compile Include="SpritePayload.cs" />
</ItemGroup>
<ItemGroup>