277 lines
11 KiB
C#
277 lines
11 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;
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
|
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;
|
|
|
|
bool WeAreDragging = false;
|
|
Point DragStart = new Point(-1, -1);
|
|
Rectangle ChosenArea = new Rectangle(1,1,100,100);
|
|
|
|
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;
|
|
pbImageField.BackgroundImage = new Bitmap(600, 800);
|
|
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);
|
|
}
|
|
UpdateChosenAreaLabel();
|
|
ResumeLayout();
|
|
}
|
|
|
|
private void UpdateChosenAreaLabel()
|
|
{
|
|
lblChosenArea.Text = ChosenArea.X + "," + ChosenArea.Y + "," + ChosenArea.Width + "," + ChosenArea.Height;
|
|
UpdateHighlightBox();
|
|
}
|
|
|
|
private void UpdateHighlightBox()
|
|
{
|
|
int transparency = 50;
|
|
Image NewFrontImage = new Bitmap(pbImageField.BackgroundImage.Width, pbImageField.BackgroundImage.Height);
|
|
Color FillColor = Color.Green;
|
|
Brush brush = new SolidBrush(Color.FromArgb(transparency, FillColor.R, FillColor.G, FillColor.B));
|
|
Brush nobrush = new SolidBrush(Color.FromArgb(0,0,0,0));
|
|
using (Graphics G = Graphics.FromImage(NewFrontImage))
|
|
{
|
|
G.FillRectangle(brush, 0,0,NewFrontImage.Width,NewFrontImage.Height);
|
|
GraphicsPath path = new GraphicsPath();
|
|
path.AddRectangle(ChosenArea);
|
|
G.SetClip(path);
|
|
G.Clear(Color.Transparent);
|
|
G.ResetClip();
|
|
// G.FillRectangle(nobrush, ChosenArea);
|
|
|
|
}
|
|
pbImageField.Image = NewFrontImage;
|
|
pbImageField.SizeMode = PictureBoxSizeMode.StretchImage;
|
|
pbImageField.Invalidate();
|
|
}
|
|
|
|
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();
|
|
//lblChosenArea.Text = AI.Width + "x" + AI.Height;
|
|
ChosenArea = new Rectangle(AI.StartPoint.X, AI.StartPoint.Y, AI.Width, AI.Height);
|
|
UpdateChosenAreaLabel();
|
|
//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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Given two locations that we have clicked on, find the area we have selected
|
|
/// </summary>
|
|
/// <param name="Start"></param>
|
|
/// <param name="End"></param>
|
|
/// <returns></returns>
|
|
private Rectangle AreaFromGridPoints(Point Start, Point End)
|
|
{
|
|
//Get the points translated from locations on the picturebox
|
|
Point OneImagePoint = MyController.ReturnPointAdjustedForImage(Start);
|
|
Point TwoImagePoint = MyController.ReturnPointAdjustedForImage(End);
|
|
//Now, shrink them to figure out which grid points we have chosen
|
|
Point OneGridPoint = new Point(OneImagePoint.X / SnapGridSize.Width, OneImagePoint.Y / SnapGridSize.Height);
|
|
Point TwoGridPoint = new Point(TwoImagePoint.X / SnapGridSize.Width, TwoImagePoint.Y / SnapGridSize.Height);
|
|
//Find the top-left point and the bottom-right point
|
|
Point StartGridPoint = new Point(Math.Min(OneGridPoint.X, TwoGridPoint.X), Math.Min(OneGridPoint.Y, TwoGridPoint.Y));
|
|
Point EndGridPoint = new Point(Math.Max(OneGridPoint.X, TwoGridPoint.X), Math.Max(OneGridPoint.Y, TwoGridPoint.Y));
|
|
//Translate them back into points on the image
|
|
Point ReturnSPoint = new Point(StartGridPoint.X * SnapGridSize.Width, StartGridPoint.Y * SnapGridSize.Height);
|
|
Point ReturnEPoint = new Point((EndGridPoint.X +1) * SnapGridSize.Width, (EndGridPoint.Y +1) * SnapGridSize.Height);
|
|
//Change it into a rectangle and return it
|
|
Rectangle ReturnRec = new Rectangle(ReturnSPoint.X, ReturnSPoint.Y, ReturnEPoint.X - ReturnSPoint.X, ReturnEPoint.Y - ReturnSPoint.Y);
|
|
return ReturnRec;
|
|
}
|
|
|
|
private void SpriteEntryForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
myDatabase.Save(); //try saving the file
|
|
}
|
|
|
|
private void pbImageField_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
//If we are dragging, process the dragging
|
|
if (WeAreDragging)
|
|
{
|
|
ChosenArea = AreaFromGridPoints(DragStart, e.Location);
|
|
UpdateChosenAreaLabel();
|
|
}
|
|
}
|
|
|
|
private void pbImageField_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
//When the mouse goes down, we note that we are trying to drag
|
|
WeAreDragging = true;
|
|
DragStart = e.Location;
|
|
}
|
|
|
|
private void pbImageField_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
//When the mouse goes up, stop dragging and update
|
|
if(WeAreDragging)
|
|
{
|
|
ChosenArea = AreaFromGridPoints(DragStart, e.Location);
|
|
UpdateChosenAreaLabel();
|
|
}
|
|
WeAreDragging = false;
|
|
|
|
}
|
|
}
|
|
}
|