Undo and Redo Operations Functional, with some caveats

This commit is contained in:
quinnvoker
2018-12-01 19:41:40 -08:00
parent 92249cac8f
commit 17b1e56180
13 changed files with 563 additions and 81 deletions
+39 -2
View File
@@ -70,6 +70,9 @@
this.highlightCheckBox = new System.Windows.Forms.CheckBox(); this.highlightCheckBox = new System.Windows.Forms.CheckBox();
this.outlineTimer = new System.Windows.Forms.Timer(this.components); this.outlineTimer = new System.Windows.Forms.Timer(this.components);
this.spriteSheetViewer = new SpriteMapEditor.PictureBoxWithInterpolationMode(); this.spriteSheetViewer = new SpriteMapEditor.PictureBoxWithInterpolationMode();
this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.undoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.redoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.spriteListPanel.SuspendLayout(); this.spriteListPanel.SuspendLayout();
this.spritePanel.SuspendLayout(); this.spritePanel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.originYPosBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.originYPosBox)).BeginInit();
@@ -367,7 +370,8 @@
this.spriteNameBox.Name = "spriteNameBox"; this.spriteNameBox.Name = "spriteNameBox";
this.spriteNameBox.Size = new System.Drawing.Size(144, 20); this.spriteNameBox.Size = new System.Drawing.Size(144, 20);
this.spriteNameBox.TabIndex = 0; this.spriteNameBox.TabIndex = 0;
this.spriteNameBox.TextChanged += new System.EventHandler(this.spriteNameBox_TextChanged); this.spriteNameBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.spriteNameBox_KeyPress);
this.spriteNameBox.Leave += new System.EventHandler(this.spriteNameBox_Leave);
// //
// loadSpriteSheetDialog // loadSpriteSheetDialog
// //
@@ -429,7 +433,8 @@
// menuStrip1 // menuStrip1
// //
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem}); this.fileToolStripMenuItem,
this.editToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(691, 24); this.menuStrip1.Size = new System.Drawing.Size(691, 24);
@@ -457,6 +462,7 @@
// //
// loadSpriteMapToolStripMenuItem // loadSpriteMapToolStripMenuItem
// //
this.loadSpriteMapToolStripMenuItem.Enabled = false;
this.loadSpriteMapToolStripMenuItem.Name = "loadSpriteMapToolStripMenuItem"; this.loadSpriteMapToolStripMenuItem.Name = "loadSpriteMapToolStripMenuItem";
this.loadSpriteMapToolStripMenuItem.Size = new System.Drawing.Size(183, 22); this.loadSpriteMapToolStripMenuItem.Size = new System.Drawing.Size(183, 22);
this.loadSpriteMapToolStripMenuItem.Text = "Load Sprite Map..."; this.loadSpriteMapToolStripMenuItem.Text = "Load Sprite Map...";
@@ -469,6 +475,7 @@
// //
// saveSpriteMapToolStripMenuItem // saveSpriteMapToolStripMenuItem
// //
this.saveSpriteMapToolStripMenuItem.Enabled = false;
this.saveSpriteMapToolStripMenuItem.Name = "saveSpriteMapToolStripMenuItem"; this.saveSpriteMapToolStripMenuItem.Name = "saveSpriteMapToolStripMenuItem";
this.saveSpriteMapToolStripMenuItem.Size = new System.Drawing.Size(183, 22); this.saveSpriteMapToolStripMenuItem.Size = new System.Drawing.Size(183, 22);
this.saveSpriteMapToolStripMenuItem.Text = "Save Sprite Map"; this.saveSpriteMapToolStripMenuItem.Text = "Save Sprite Map";
@@ -476,6 +483,7 @@
// //
// saveSpriteMapAsToolStripMenuItem // saveSpriteMapAsToolStripMenuItem
// //
this.saveSpriteMapAsToolStripMenuItem.Enabled = false;
this.saveSpriteMapAsToolStripMenuItem.Name = "saveSpriteMapAsToolStripMenuItem"; this.saveSpriteMapAsToolStripMenuItem.Name = "saveSpriteMapAsToolStripMenuItem";
this.saveSpriteMapAsToolStripMenuItem.Size = new System.Drawing.Size(183, 22); this.saveSpriteMapAsToolStripMenuItem.Size = new System.Drawing.Size(183, 22);
this.saveSpriteMapAsToolStripMenuItem.Text = "Save Sprite Map As..."; this.saveSpriteMapAsToolStripMenuItem.Text = "Save Sprite Map As...";
@@ -517,6 +525,29 @@
this.spriteSheetViewer.MouseMove += new System.Windows.Forms.MouseEventHandler(this.spriteSheetViewer_MouseMove); this.spriteSheetViewer.MouseMove += new System.Windows.Forms.MouseEventHandler(this.spriteSheetViewer_MouseMove);
this.spriteSheetViewer.MouseUp += new System.Windows.Forms.MouseEventHandler(this.spriteSheetViewer_MouseUp); this.spriteSheetViewer.MouseUp += new System.Windows.Forms.MouseEventHandler(this.spriteSheetViewer_MouseUp);
// //
// editToolStripMenuItem
//
this.editToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.undoToolStripMenuItem,
this.redoToolStripMenuItem});
this.editToolStripMenuItem.Name = "editToolStripMenuItem";
this.editToolStripMenuItem.Size = new System.Drawing.Size(39, 20);
this.editToolStripMenuItem.Text = "Edit";
//
// undoToolStripMenuItem
//
this.undoToolStripMenuItem.Name = "undoToolStripMenuItem";
this.undoToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.undoToolStripMenuItem.Text = "Undo";
this.undoToolStripMenuItem.Click += new System.EventHandler(this.undoToolStripMenuItem_Click);
//
// redoToolStripMenuItem
//
this.redoToolStripMenuItem.Name = "redoToolStripMenuItem";
this.redoToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.redoToolStripMenuItem.Text = "Redo";
this.redoToolStripMenuItem.Click += new System.EventHandler(this.redoToolStripMenuItem_Click);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -531,8 +562,11 @@
this.Controls.Add(this.spritePanel); this.Controls.Add(this.spritePanel);
this.Controls.Add(this.spriteListPanel); this.Controls.Add(this.spriteListPanel);
this.Controls.Add(this.menuStrip1); this.Controls.Add(this.menuStrip1);
this.KeyPreview = true;
this.Name = "Form1"; this.Name = "Form1";
this.Text = "QUROGames Sprite Map Editor"; this.Text = "QUROGames Sprite Map Editor";
this.Click += new System.EventHandler(this.Form1_Click);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.spriteListPanel.ResumeLayout(false); this.spriteListPanel.ResumeLayout(false);
this.spriteListPanel.PerformLayout(); this.spriteListPanel.PerformLayout();
this.spritePanel.ResumeLayout(false); this.spritePanel.ResumeLayout(false);
@@ -595,6 +629,9 @@
private System.Windows.Forms.CheckBox editingOriginCheckBox; private System.Windows.Forms.CheckBox editingOriginCheckBox;
private System.Windows.Forms.CheckBox highlightCheckBox; private System.Windows.Forms.CheckBox highlightCheckBox;
private System.Windows.Forms.Timer outlineTimer; private System.Windows.Forms.Timer outlineTimer;
private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem undoToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem redoToolStripMenuItem;
} }
} }
+147 -70
View File
@@ -13,13 +13,14 @@ using Microsoft.Xna;
using System.IO; using System.IO;
using QURO; using QURO;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate; using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using SpriteMapEditor.SpriteMapModifications;
namespace SpriteMapEditor namespace SpriteMapEditor
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
private SpriteMapModifications.History undoHistory;
private BindingList<SpriteMapRegion> sprites; private BindingList<SpriteMapRegion> sprites;
private List<SpriteMapRegion> selectedSprites; private List<SpriteMapRegion> selectedSprites;
@@ -45,6 +46,8 @@ namespace SpriteMapEditor
Image originPointer_black; Image originPointer_black;
Image originPointer_white; Image originPointer_white;
Image spriteSheet;
Pen blackPen; Pen blackPen;
Pen whitePen; Pen whitePen;
@@ -53,6 +56,8 @@ namespace SpriteMapEditor
private Point newPosition; private Point newPosition;
private Point oldPosition; private Point oldPosition;
private int totalDragX;
private int totalDragY;
private int drawingRectangleOffset private int drawingRectangleOffset
{ {
@@ -70,6 +75,8 @@ namespace SpriteMapEditor
InitializeComponent(); InitializeComponent();
FormBorderStyle = FormBorderStyle.FixedSingle; FormBorderStyle = FormBorderStyle.FixedSingle;
undoHistory = new SpriteMapModifications.History();
spriteSheetViewer.Width = 1; spriteSheetViewer.Width = 1;
spriteSheetViewer.Height = 1; spriteSheetViewer.Height = 1;
@@ -96,18 +103,26 @@ namespace SpriteMapEditor
{ {
if(loadSpriteSheetDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) if(loadSpriteSheetDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{ {
spriteSheetViewer.Image = Image.FromFile(loadSpriteSheetDialog.FileName); spriteSheet = Image.FromFile(loadSpriteSheetDialog.FileName);
spriteSheetViewer.Image = spriteSheet;
} }
EnableLoadAndSaveAsSpriteMap();
InitializeMask(); InitializeMask();
} }
private void EnableLoadAndSaveAsSpriteMap()
{
loadSpriteMapToolStripMenuItem.Enabled = true;
saveSpriteMapAsToolStripMenuItem.Enabled = true;
}
private void spriteSheetViewer_Paint(object sender, PaintEventArgs e) private void spriteSheetViewer_Paint(object sender, PaintEventArgs e)
{ {
if (spriteSheetViewer.Image != null) if (spriteSheet != null)
{ {
spriteSheetViewer.Width = spriteSheetViewer.Image.Width * zoomLevel; spriteSheetViewer.Width = spriteSheet.Width * zoomLevel;
spriteSheetViewer.Height = spriteSheetViewer.Image.Height * zoomLevel; spriteSheetViewer.Height = spriteSheet.Height * zoomLevel;
if (highlight) if (highlight)
{ {
@@ -190,8 +205,8 @@ namespace SpriteMapEditor
{ {
if (editingOrigin) if (editingOrigin)
{ {
var spriteSheet = (Bitmap)spriteSheetViewer.Image; var spriteSheetBitmap = (Bitmap)spriteSheet;
var colorAtOrigin = spriteSheet.GetPixel((int)(sprite.Bounds.X + sprite.Origin.X), var colorAtOrigin = spriteSheetBitmap.GetPixel((int)(sprite.Bounds.X + sprite.Origin.X),
(int)(sprite.Bounds.Y + sprite.Origin.Y)); (int)(sprite.Bounds.Y + sprite.Origin.Y));
var highContrastPointer = originPointer_black; var highContrastPointer = originPointer_black;
@@ -232,7 +247,7 @@ namespace SpriteMapEditor
{ {
Color maskColor = Color.FromArgb(128, 32, 32, 32); Color maskColor = Color.FromArgb(128, 32, 32, 32);
highlightMask = new DirectBitmap(spriteSheetViewer.Image.Width, spriteSheetViewer.Image.Height); highlightMask = new DirectBitmap(spriteSheet.Width, spriteSheet.Height);
for (int currentY = 0; currentY < highlightMask.Height; currentY++) for (int currentY = 0; currentY < highlightMask.Height; currentY++)
{ {
for (int currentX = 0; currentX < highlightMask.Width; currentX++) for (int currentX = 0; currentX < highlightMask.Width; currentX++)
@@ -244,7 +259,8 @@ namespace SpriteMapEditor
} }
else else
{ {
highlightMask.CopyBitmap(baseMask); highlightMask.Dispose();
highlightMask = new DirectBitmap(baseMask);
} }
} }
@@ -280,12 +296,9 @@ namespace SpriteMapEditor
{ {
if (loadingSprite) if (loadingSprite)
return; return;
foreach (SpriteMapRegion currentSprite in selectedSprites) var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, x: (int)spriteXPosBox.Value);
{ SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
var newBounds = currentSprite.Bounds; undoHistory.Add(modification);
newBounds.X = (int)spriteXPosBox.Value;
currentSprite.Bounds = newBounds;
}
UpdateHighlightMask(); UpdateHighlightMask();
spriteSheetViewer.Refresh(); spriteSheetViewer.Refresh();
} }
@@ -294,12 +307,9 @@ namespace SpriteMapEditor
{ {
if (loadingSprite) if (loadingSprite)
return; return;
foreach (SpriteMapRegion currentSprite in selectedSprites) var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, y: (int)spriteYPosBox.Value);
{ SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
var newBounds = currentSprite.Bounds; undoHistory.Add(modification);
newBounds.Y = (int)spriteYPosBox.Value;
currentSprite.Bounds = newBounds;
}
UpdateHighlightMask(); UpdateHighlightMask();
spriteSheetViewer.Refresh(); spriteSheetViewer.Refresh();
} }
@@ -308,12 +318,9 @@ namespace SpriteMapEditor
{ {
if (loadingSprite) if (loadingSprite)
return; return;
foreach (SpriteMapRegion currentSprite in selectedSprites) var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, width: (int)spriteWidthBox.Value);
{ SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
var newBounds = currentSprite.Bounds; undoHistory.Add(modification);
newBounds.Width = (int)spriteWidthBox.Value;
currentSprite.Bounds = newBounds;
}
UpdateHighlightMask(); UpdateHighlightMask();
spriteSheetViewer.Refresh(); spriteSheetViewer.Refresh();
} }
@@ -322,33 +329,35 @@ namespace SpriteMapEditor
{ {
if (loadingSprite) if (loadingSprite)
return; return;
foreach (SpriteMapRegion currentSprite in selectedSprites) var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, height: (int)spriteHeightBox.Value);
{ SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
var newBounds = currentSprite.Bounds; undoHistory.Add(modification);
newBounds.Height = (int)spriteHeightBox.Value;
currentSprite.Bounds = newBounds;
}
UpdateHighlightMask(); UpdateHighlightMask();
spriteSheetViewer.Refresh(); spriteSheetViewer.Refresh();
} }
private void spriteNameBox_TextChanged(object sender, EventArgs e)
{
if (loadingSprite)
return;
selectedSprites[0].Name = spriteNameBox.Text;
sprites.ResetBindings();
}
private void addSpriteButton_Click(object sender, EventArgs e) private void addSpriteButton_Click(object sender, EventArgs e)
{ {
sprites.Add(new SpriteMapRegion(selectedSprites[0].Name, selectedSprites[0].Bounds)); var modification = new SpriteMapModifications.AddSprite(sprites, selectedSprites[0]);
SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
undoHistory.Add(modification);
} }
private void removeSpriteButton_Click(object sender, EventArgs e) private void removeSpriteButton_Click(object sender, EventArgs e)
{ {
if (spriteList.SelectedIndex > -1 && spriteList.SelectedIndex < sprites.Count) int indexToRemove = spriteList.SelectedIndex;
sprites.RemoveAt(spriteList.SelectedIndex); if (indexToRemove == sprites.Count - 1)
{
spriteList.SelectedIndices.Clear();
spriteList.SelectedIndex = indexToRemove - 1;
}
if (indexToRemove > -1 && indexToRemove < sprites.Count)
{
var modification = new SpriteMapModifications.RemoveSprite(sprites, indexToRemove);
SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
undoHistory.Add(modification);
}
} }
private void spriteList_SelectedValueChanged(object sender, EventArgs e) private void spriteList_SelectedValueChanged(object sender, EventArgs e)
@@ -484,12 +493,14 @@ namespace SpriteMapEditor
} }
spriteList.DataSource = sprites; spriteList.DataSource = sprites;
saveSpriteMapToolStripMenuItem.Enabled = true;
} }
} }
private void spriteSheetViewer_MouseMove(object sender, MouseEventArgs e) private void spriteSheetViewer_MouseMove(object sender, MouseEventArgs e)
{ {
if(spriteSheetViewer.Image == null) if(spriteSheet == null)
return; return;
Point mousePosition = new Point(e.X, e.Y); Point mousePosition = new Point(e.X, e.Y);
@@ -512,12 +523,15 @@ namespace SpriteMapEditor
var groupRect = GetSelectedGroupRectangle(); var groupRect = GetSelectedGroupRectangle();
if (groupRect.X + differenceX < 0 || if (groupRect.X + differenceX < 0 ||
groupRect.X + groupRect.Width + differenceX > spriteSheetViewer.Image.Width) groupRect.X + groupRect.Width + differenceX > spriteSheet.Width)
differenceX = 0; differenceX = 0;
if (groupRect.Y + differenceY < 0 || if (groupRect.Y + differenceY < 0 ||
groupRect.Y + groupRect.Height + differenceY > spriteSheetViewer.Image.Height) groupRect.Y + groupRect.Height + differenceY > spriteSheet.Height)
differenceY = 0; differenceY = 0;
totalDragX += differenceX;
totalDragY += differenceY;
foreach (SpriteMapRegion spriteToMove in selectedSprites) foreach (SpriteMapRegion spriteToMove in selectedSprites)
{ {
var newBounds = spriteToMove.Bounds; var newBounds = spriteToMove.Bounds;
@@ -547,20 +561,20 @@ namespace SpriteMapEditor
//clamp X value //clamp X value
if (clampedRect.X < 0) if (clampedRect.X < 0)
clampedRect.X = 0; clampedRect.X = 0;
else if (clampedRect.X + clampedRect.Width > spriteSheetViewer.Image.Width) else if (clampedRect.X + clampedRect.Width > spriteSheet.Width)
clampedRect.X = spriteSheetViewer.Image.Width - clampedRect.Width; clampedRect.X = spriteSheet.Width - clampedRect.Width;
//clamp Y value //clamp Y value
if (clampedRect.Y < 0) if (clampedRect.Y < 0)
clampedRect.Y = 0; clampedRect.Y = 0;
else if (clampedRect.Y + clampedRect.Height > spriteSheetViewer.Image.Height) else if (clampedRect.Y + clampedRect.Height > spriteSheet.Height)
clampedRect.Y = spriteSheetViewer.Image.Height - clampedRect.Height; clampedRect.Y = spriteSheet.Height - clampedRect.Height;
return clampedRect; return clampedRect;
} }
private void spriteSheetViewer_MouseDown(object sender, MouseEventArgs e) private void spriteSheetViewer_MouseDown(object sender, MouseEventArgs e)
{ {
if (spriteSheetViewer.Image == null) if (spriteSheet == null)
return; return;
Point mousePosition = new Point(e.X, e.Y); Point mousePosition = new Point(e.X, e.Y);
@@ -570,6 +584,8 @@ namespace SpriteMapEditor
if (GetDrawingRect(currentSprite.Bounds).Contains(mousePosition)) if (GetDrawingRect(currentSprite.Bounds).Contains(mousePosition))
{ {
movingWithMouse = true; movingWithMouse = true;
totalDragX = 0;
totalDragY = 0;
newPosition = mousePosition; newPosition = mousePosition;
newPosition.X /= zoomLevel; newPosition.X /= zoomLevel;
newPosition.Y /= zoomLevel; newPosition.Y /= zoomLevel;
@@ -587,18 +603,23 @@ namespace SpriteMapEditor
private void spriteSheetViewer_MouseUp(object sender, MouseEventArgs e) private void spriteSheetViewer_MouseUp(object sender, MouseEventArgs e)
{ {
movingWithMouse = false; movingWithMouse = false;
if(totalDragX != 0 || totalDragY != 0)
{
var modification = new SpriteMapModifications.MoveBounds(selectedSprites, totalDragX, totalDragY);
var selection = SpriteMapModifications.ModHelper.GetSelectionList(spriteList);
modification.SetPreChangeSelection(selection);
modification.SetPostChangeSelection(selection);
undoHistory.Add(modification);
}
} }
private void originXPosBox_ValueChanged(object sender, EventArgs e) private void originXPosBox_ValueChanged(object sender, EventArgs e)
{ {
if (loadingSprite) if (loadingSprite)
return; return;
foreach (SpriteMapRegion currentSprite in selectedSprites) var modification = new SpriteMapModifications.ModifyOrigin(selectedSprites, x: (int)originXPosBox.Value);
{ SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
var newOrigin = currentSprite.Origin; undoHistory.Add(modification);
newOrigin.X = (int)originXPosBox.Value;
currentSprite.Origin = newOrigin;
}
spriteSheetViewer.Refresh(); spriteSheetViewer.Refresh();
} }
@@ -606,12 +627,9 @@ namespace SpriteMapEditor
{ {
if (loadingSprite) if (loadingSprite)
return; return;
foreach (SpriteMapRegion currentSprite in selectedSprites) var modification = new SpriteMapModifications.ModifyOrigin(selectedSprites, y: (int)originYPosBox.Value);
{ SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
var newOrigin = currentSprite.Origin; undoHistory.Add(modification);
newOrigin.Y = (int)originYPosBox.Value;
currentSprite.Origin = newOrigin;
}
spriteSheetViewer.Refresh(); spriteSheetViewer.Refresh();
} }
@@ -619,9 +637,9 @@ namespace SpriteMapEditor
{ {
if(spriteList.SelectedIndex < sprites.Count - 1) if(spriteList.SelectedIndex < sprites.Count - 1)
{ {
var spriteBelow = sprites[spriteList.SelectedIndex + 1]; var modification = new SpriteMapModifications.MoveSpriteListEntry(sprites, spriteList.SelectedIndex, 1);
sprites[spriteList.SelectedIndex + 1] = selectedSprites[0]; SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
sprites[spriteList.SelectedIndex] = spriteBelow; undoHistory.Add(modification);
int newPosition = spriteList.SelectedIndex + 1; int newPosition = spriteList.SelectedIndex + 1;
spriteList.SelectedIndices.Clear(); spriteList.SelectedIndices.Clear();
spriteList.SelectedIndex = newPosition; spriteList.SelectedIndex = newPosition;
@@ -632,9 +650,9 @@ namespace SpriteMapEditor
{ {
if(spriteList.SelectedIndex > 0) if(spriteList.SelectedIndex > 0)
{ {
var spriteAbove = sprites[spriteList.SelectedIndex - 1]; var modification = new SpriteMapModifications.MoveSpriteListEntry(sprites, spriteList.SelectedIndex, -1);
sprites[spriteList.SelectedIndex - 1] = selectedSprites[0]; SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
sprites[spriteList.SelectedIndex] = spriteAbove; undoHistory.Add(modification);
int newPosition = spriteList.SelectedIndex - 1; int newPosition = spriteList.SelectedIndex - 1;
spriteList.SelectedIndices.Clear(); spriteList.SelectedIndices.Clear();
spriteList.SelectedIndex = newPosition; spriteList.SelectedIndex = newPosition;
@@ -681,5 +699,64 @@ namespace SpriteMapEditor
whitePen.DashOffset = 0; whitePen.DashOffset = 0;
spriteSheetViewer.Refresh(); spriteSheetViewer.Refresh();
} }
private void UpdateSpriteName()
{
var modification = new SpriteMapModifications.RenameSprite(selectedSprites[0], spriteNameBox.Text);
SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
undoHistory.Add(modification);
sprites.ResetBindings();
}
private void spriteNameBox_Leave(object sender, EventArgs e)
{
if (selectedSprites[0].Name != spriteNameBox.Text)
{
UpdateSpriteName();
}
}
private void spriteNameBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return && selectedSprites[0].Name != spriteNameBox.Text)
{
UpdateSpriteName();
}
}
private void Form1_Click(object sender, EventArgs e)
{
if (selectedSprites[0].Name != spriteNameBox.Text)
{
UpdateSpriteName();
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control)
{
if (e.KeyCode == Keys.Z)
{
//Console.WriteLine("Undo Keypress");
undoHistory.Undo(spriteList);
}
else if (e.KeyCode == Keys.Y)
{
//Console.WriteLine("Redo Keypress");
undoHistory.Redo(spriteList);
}
}
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
undoHistory.Undo(spriteList);
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
undoHistory.Redo(spriteList);
}
} }
} }
+3
View File
@@ -58,7 +58,9 @@
<Compile Include="Form1.Designer.cs"> <Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon> <DependentUpon>Form1.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="SpriteMapModifications\ModHelper.cs" />
<Compile Include="SpriteMapModifications\AddSprite.cs" /> <Compile Include="SpriteMapModifications\AddSprite.cs" />
<Compile Include="SpriteMapModifications\History.cs" />
<Compile Include="SpriteMapModifications\ISpriteModification.cs" /> <Compile Include="SpriteMapModifications\ISpriteModification.cs" />
<Compile Include="SpriteMapModifications\ModifyBounds.cs" /> <Compile Include="SpriteMapModifications\ModifyBounds.cs" />
<Compile Include="PictureBoxWithInterpolationMode.cs"> <Compile Include="PictureBoxWithInterpolationMode.cs">
@@ -70,6 +72,7 @@
<Compile Include="SpriteMapModifications\MoveBounds.cs" /> <Compile Include="SpriteMapModifications\MoveBounds.cs" />
<Compile Include="SpriteMapModifications\MoveSpriteListEntry.cs" /> <Compile Include="SpriteMapModifications\MoveSpriteListEntry.cs" />
<Compile Include="SpriteMapModifications\RemoveSprite.cs" /> <Compile Include="SpriteMapModifications\RemoveSprite.cs" />
<Compile Include="SpriteMapModifications\RenameSprite.cs" />
<EmbeddedResource Include="Form1.resx"> <EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon> <DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
@@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.ComponentModel; using System.ComponentModel;
using QURO; using QURO;
using System.Windows.Forms;
namespace SpriteMapEditor.SpriteMapModifications namespace SpriteMapEditor.SpriteMapModifications
{ {
@@ -14,9 +15,13 @@ namespace SpriteMapEditor.SpriteMapModifications
private readonly SpriteMapRegion selectedSprite; private readonly SpriteMapRegion selectedSprite;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public AddSprite(BindingList<SpriteMapRegion> spriteList, SpriteMapRegion selected = null) public AddSprite(BindingList<SpriteMapRegion> spriteList, SpriteMapRegion selected = null)
{ {
allSprites = spriteList; allSprites = spriteList;
selectedSprite = selected; selectedSprite = selected;
} }
@@ -32,5 +37,28 @@ namespace SpriteMapEditor.SpriteMapModifications
{ {
allSprites.RemoveAt(allSprites.Count - 1); allSprites.RemoveAt(allSprites.Count - 1);
} }
public List<int> GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(List<int> currentSelection)
{
preChangeSelection = currentSelection;
}
public List<int> GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(List<int> currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Add Sprite";
}
} }
} }
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpriteMapEditor.SpriteMapModifications
{
class History
{
private List<ISpriteMapModification> changes;
private int position;
public bool CanUndo
{
get { return position >= 0 && changes.Count > 0; }
}
public bool CanRedo
{
get { return position < changes.Count - 1; }
}
public History()
{
changes = new List<ISpriteMapModification>();
}
public void Add(ISpriteMapModification change)
{
if (position < changes.Count - 1)
{
changes.RemoveRange(position + 1, changes.Count - 1 - position);
}
changes.Add(change);
position = changes.Count - 1;
//Console.WriteLine("Available Undo Steps: " + changes.Count + "; Current Undo Position: " + position);
}
public void Undo()
{
if (CanUndo)
{
changes[position].Undo();
position--;
}
}
public void Undo(ListBox listBox)
{
if (CanUndo)
{
ModHelper.UndoAndRestoreSelection(changes[position], listBox);
position--;
}
}
public void Redo()
{
if (CanRedo)
{
changes[position + 1].Do();
position++;
}
}
public void Redo(ListBox listBox)
{
if (CanRedo)
{
ModHelper.RedoAndRestoreSelection(changes[position + 1], listBox);
position++;
}
}
public string NextUndoString()
{
if (!CanUndo)
return "Undo...";
else
return "Undo " + changes[position];
}
public string NextRedoString()
{
if (!CanRedo)
return "Redo...";
else
return "Redo " + changes[position + 1];
}
}
}
@@ -1,14 +1,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SpriteMapEditor.SpriteMapModifications namespace SpriteMapEditor.SpriteMapModifications
{ {
interface ISpriteMapModification public interface ISpriteMapModification
{ {
void Do(); void Do();
void Undo(); void Undo();
List<int> GetPreChangeSelection();
void SetPreChangeSelection(List<int> currentSelection);
List<int> GetPostChangeSelection();
void SetPostChangeSelection(List<int> currentSelection);
} }
} }
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;
using QURO;
namespace SpriteMapEditor.SpriteMapModifications
{
public static class ModHelper
{
public static void DoModificationWithSelectionTracking(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
{
mod.SetPreChangeSelection(GetSelectionList(listBox));
mod.Do();
mod.SetPostChangeSelection(GetSelectionList(listBox));
}
public static void UndoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
{
mod.Undo();
SelectFromList(listBox, mod.GetPreChangeSelection());
}
public static void RedoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
{
mod.Do();
SelectFromList(listBox, mod.GetPostChangeSelection());
}
public static List<int> GetSelectionList(ListBox listBox)
{
List<int> result = new List<int>();
foreach(int currentIndex in listBox.SelectedIndices)
{
result.Add(currentIndex);
}
return result;
}
public static void SelectFromList(ListBox listBox, List<int> newSelection)
{
listBox.SelectedIndices.Clear();
var source = (BindingList<SpriteMapRegion>)listBox.DataSource;
foreach (int currentIndex in newSelection)
{
if(currentIndex < source.Count)
listBox.SelectedIndex = currentIndex;
}
}
}
}
@@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using QURO; using QURO;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System.Windows.Forms;
namespace SpriteMapEditor.SpriteMapModifications namespace SpriteMapEditor.SpriteMapModifications
{ {
@@ -19,9 +20,12 @@ namespace SpriteMapEditor.SpriteMapModifications
private readonly List<Rectangle> oldBounds; private readonly List<Rectangle> oldBounds;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public ModifyBounds(List<SpriteMapRegion> spritesToModify, int x = -1, int y = -1, int width = -1, int height = -1) public ModifyBounds(List<SpriteMapRegion> spritesToModify, int x = -1, int y = -1, int width = -1, int height = -1)
{ {
sprites = spritesToModify; sprites = spritesToModify.ToList();
newXPosition = x; newXPosition = x;
newYPosition = y; newYPosition = y;
newWidth = width; newWidth = width;
@@ -61,5 +65,27 @@ namespace SpriteMapEditor.SpriteMapModifications
sprites[index].Bounds = oldBounds[index]; sprites[index].Bounds = oldBounds[index];
} }
} }
public List<int> GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(List<int> currentSelection)
{
preChangeSelection = currentSelection;
}
public List<int> GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(List<int> currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Modify Sprite Bounds";
}
} }
} }
@@ -17,9 +17,12 @@ namespace SpriteMapEditor.SpriteMapModifications
private readonly List<Vector2> oldOrigins; private readonly List<Vector2> oldOrigins;
public ModifyOrigin(List<SpriteMapRegion> spritesToEditOrigin, int x = -1, int y = -1) private List<int> preChangeSelection;
private List<int> postChangeSelection;
public ModifyOrigin(List<SpriteMapRegion> spritesToEditOrigin, int x = -1, int y = -1)
{ {
sprites = spritesToEditOrigin; sprites = spritesToEditOrigin.ToList();
newOriginX = x; newOriginX = x;
newOriginY = y; newOriginY = y;
@@ -52,5 +55,27 @@ namespace SpriteMapEditor.SpriteMapModifications
sprites[index].Origin = oldOrigins[index]; sprites[index].Origin = oldOrigins[index];
} }
} }
public List<int> GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(List<int> currentSelection)
{
preChangeSelection = currentSelection;
}
public List<int> GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(List<int> currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Modify Sprite Origin";
}
} }
} }
@@ -14,9 +14,12 @@ namespace SpriteMapEditor.SpriteMapModifications
private readonly int xDiff; private readonly int xDiff;
private readonly int yDiff; private readonly int yDiff;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public MoveBounds(List<SpriteMapRegion> spritesToMove, int xDifference, int yDifference) public MoveBounds(List<SpriteMapRegion> spritesToMove, int xDifference, int yDifference)
{ {
sprites = spritesToMove; sprites = spritesToMove.ToList();
xDiff = xDifference; xDiff = xDifference;
yDiff = yDifference; yDiff = yDifference;
} }
@@ -41,5 +44,27 @@ namespace SpriteMapEditor.SpriteMapModifications
sprite.Bounds = newBounds; sprite.Bounds = newBounds;
} }
} }
public List<int> GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(List<int> currentSelection)
{
preChangeSelection = currentSelection;
}
public List<int> GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(List<int> currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Move Sprite Bounds";
}
} }
} }
@@ -15,6 +15,9 @@ namespace SpriteMapEditor.SpriteMapModifications
private readonly int selectedIndex; private readonly int selectedIndex;
private readonly int dir; private readonly int dir;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public MoveSpriteListEntry(BindingList<SpriteMapRegion> spriteList, int selection, int direction) public MoveSpriteListEntry(BindingList<SpriteMapRegion> spriteList, int selection, int direction)
{ {
allSprites = spriteList; allSprites = spriteList;
@@ -25,15 +28,37 @@ namespace SpriteMapEditor.SpriteMapModifications
public void Do() public void Do()
{ {
var movingSprite = allSprites[selectedIndex]; var movingSprite = allSprites[selectedIndex];
allSprites.RemoveAt(selectedIndex); allSprites[selectedIndex] = allSprites[selectedIndex + dir];
allSprites.Insert(selectedIndex + dir, movingSprite); allSprites[selectedIndex + dir] = movingSprite;
} }
public void Undo() public void Undo()
{ {
var movingSprite = allSprites[selectedIndex + dir]; var movingSprite = allSprites[selectedIndex + dir];
allSprites.RemoveAt(selectedIndex + dir); allSprites[selectedIndex + dir] = allSprites[selectedIndex];
allSprites.Insert(selectedIndex, movingSprite); allSprites[selectedIndex] = movingSprite;
}
public List<int> GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(List<int> currentSelection)
{
preChangeSelection = currentSelection;
}
public List<int> GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(List<int> currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Move Sprite List Entry";
} }
} }
} }
@@ -16,6 +16,9 @@ namespace SpriteMapEditor.SpriteMapModifications
private readonly SpriteMapRegion removedSprite; private readonly SpriteMapRegion removedSprite;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public RemoveSprite(BindingList<SpriteMapRegion> spriteList, int indexToRemove) public RemoveSprite(BindingList<SpriteMapRegion> spriteList, int indexToRemove)
{ {
allSprites = spriteList; allSprites = spriteList;
@@ -32,5 +35,27 @@ namespace SpriteMapEditor.SpriteMapModifications
{ {
allSprites.Insert(index, removedSprite); allSprites.Insert(index, removedSprite);
} }
public List<int> GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(List<int> currentSelection)
{
preChangeSelection = currentSelection;
}
public List<int> GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(List<int> currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Remove Sprite";
}
} }
} }
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QURO;
namespace SpriteMapEditor.SpriteMapModifications
{
class RenameSprite : ISpriteMapModification
{
private readonly SpriteMapRegion sprite;
private readonly string newName;
private readonly string oldName;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public RenameSprite(SpriteMapRegion spriteToRename, string name)
{
sprite = spriteToRename;
newName = name;
oldName = sprite.Name;
}
public void Do()
{
sprite.Name = newName;
}
public void Undo()
{
sprite.Name = oldName;
}
public List<int> GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(List<int> currentSelection)
{
preChangeSelection = currentSelection;
}
public List<int> GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(List<int> currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Rename Sprite";
}
}
}