SpriteLibrary/SpriteLibrary/SpriteEntryForm.cs
2017-09-17 08:13:04 -05:00

184 lines
6.6 KiB
C#

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.Collections;
namespace SpriteLibrary
{
internal partial class SpriteEntryForm : Form
{
SpriteController MyController;
ResourceManager myResources = null;
List<SpriteInfo> SpriteInformation = new List<SpriteInfo>();
SpriteInfo TempInformation = null;
Size SnapGridSize = new Size(5,5);
SpriteDatabase myDatabase = null;
int CurrentSIIndex = -1; //The information item we are editing. -1 means it is a new one.
int CurrentSIAnimation = -1;
internal SpriteEntryForm(SpriteDatabase theDatabase, List<SpriteInfo> ListToWorkOn, Size GridSize)
{
InitializeComponent();
myDatabase = theDatabase;
myResources = myDatabase.GetResourceManager();
SnapGridSize = GridSize;
LocalSetup();
SpriteInformation.AddRange(ListToWorkOn);
}
private void LocalSetup()
{
pbImageField.BackgroundImageLayout = ImageLayout.Stretch;
MyController = new SpriteController(pbImageField);
PopulateMenu();
UpdateMenu();
SpriteInformationToForm();
UpdateMenu();
}
internal List<SpriteInfo> GetUpdatedList()
{
return SpriteInformation;
}
private void PopulateMenu()
{
ResourceManager rm = myResources;
PopulateMenu(rm);
}
private 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 UpdateMenu()
{
SuspendLayout();
lblCountSprites.Text = SpriteInformation.Count.ToString();
if (TempInformation == null) SetUpEmptyInfo();
//Put in numbers into the combo-box of which frame to base ourselves off of
cbAnimation.Items.Clear();
for(int i =0; i < TempInformation.Animations.Count; i++)
{
cbAnimation.Items.Add(i.ToString());
}
if (CurrentSIAnimation == 0)
{
rbFromImage.Checked = true;
panelRadioButtons.Visible = false;
}
else
panelRadioButtons.Visible = true;
if(rbFromImage.Checked)
{
if(!TCTabPages.TabPages.Contains(tpFromImage))
TCTabPages.TabPages.Add(tpFromImage);
if (TCTabPages.TabPages.Contains(tpMirrorRotate))
TCTabPages.TabPages.Remove(tpMirrorRotate);
}
else
{
if (TCTabPages.TabPages.Contains(tpFromImage))
TCTabPages.TabPages.Remove(tpFromImage);
if (!TCTabPages.TabPages.Contains(tpMirrorRotate))
TCTabPages.TabPages.Add(tpMirrorRotate);
}
ResumeLayout();
}
private void SetUpEmptyInfo()
{
TempInformation = new SpriteInfo();
TempInformation.SpriteName = "";
TempInformation.ViewPercent = 100;
AnimationInfo AI = new AnimationInfo();
AI.AnimSpeed = 200;
AI.FieldsToUse = AnimationType.SpriteDefinition;
AI.Height = 100;
AI.Width = 100;
AI.StartPoint = new Point(0, 0);
TempInformation.Animations.Add(AI);
}
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();
}
}
}
/// <summary>
/// Take the values stored in TempInformation and push it out to our form
/// </summary>
private void SpriteInformationToForm()
{
if (TempInformation == null) return;
//For the main sprite information
tbSpriteName.Text = TempInformation.SpriteName;
tbDefaultSize.Text = TempInformation.ViewPercent.ToString();
//From the current animation
AnimationInfo AI = null;
if (CurrentSIAnimation < 0) CurrentSIAnimation = 0;
if (CurrentSIAnimation >= TempInformation.Animations.Count) CurrentSIAnimation = TempInformation.Animations.Count -1;
if (CurrentSIAnimation < TempInformation.Animations.Count)
{
AI = TempInformation.Animations[CurrentSIAnimation];
tbAmimationSpeed.Text = AI.AnimSpeed.ToString();
cbStartingImage.Text = AI.ImageName;
cbMirrorH.Checked = AI.MirrorHorizontally;
cbMirrorV.Checked = AI.MirrorVertically;
cbAnimation.Text = AI.AnimationToUse.ToString();
tbNumFrames.Text = AI.NumFrames.ToString();
//Radio buttons
if (AI.FieldsToUse == AnimationType.SpriteDefinition) rbFromImage.Checked = true;
if (AI.FieldsToUse == AnimationType.Mirror) rbMirror.Checked = true;
if (AI.FieldsToUse == AnimationType.Rotation) rbRotation.Checked = true;
}
}
private void SpriteEntryForm_FormClosing(object sender, FormClosingEventArgs e)
{
myDatabase.Save(); //try saving the file
}
}
}