diff --git a/SpriteMapEditor/Form1.Designer.cs b/SpriteMapEditor/Form1.Designer.cs index 92968c6..d924ac3 100644 --- a/SpriteMapEditor/Form1.Designer.cs +++ b/SpriteMapEditor/Form1.Designer.cs @@ -70,6 +70,9 @@ this.highlightCheckBox = new System.Windows.Forms.CheckBox(); this.outlineTimer = new System.Windows.Forms.Timer(this.components); 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.spritePanel.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.originYPosBox)).BeginInit(); @@ -367,7 +370,8 @@ this.spriteNameBox.Name = "spriteNameBox"; this.spriteNameBox.Size = new System.Drawing.Size(144, 20); 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 // @@ -429,7 +433,8 @@ // menuStrip1 // 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.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(691, 24); @@ -457,6 +462,7 @@ // // loadSpriteMapToolStripMenuItem // + this.loadSpriteMapToolStripMenuItem.Enabled = false; this.loadSpriteMapToolStripMenuItem.Name = "loadSpriteMapToolStripMenuItem"; this.loadSpriteMapToolStripMenuItem.Size = new System.Drawing.Size(183, 22); this.loadSpriteMapToolStripMenuItem.Text = "Load Sprite Map..."; @@ -469,6 +475,7 @@ // // saveSpriteMapToolStripMenuItem // + this.saveSpriteMapToolStripMenuItem.Enabled = false; this.saveSpriteMapToolStripMenuItem.Name = "saveSpriteMapToolStripMenuItem"; this.saveSpriteMapToolStripMenuItem.Size = new System.Drawing.Size(183, 22); this.saveSpriteMapToolStripMenuItem.Text = "Save Sprite Map"; @@ -476,6 +483,7 @@ // // saveSpriteMapAsToolStripMenuItem // + this.saveSpriteMapAsToolStripMenuItem.Enabled = false; this.saveSpriteMapAsToolStripMenuItem.Name = "saveSpriteMapAsToolStripMenuItem"; this.saveSpriteMapAsToolStripMenuItem.Size = new System.Drawing.Size(183, 22); this.saveSpriteMapAsToolStripMenuItem.Text = "Save Sprite Map As..."; @@ -517,6 +525,29 @@ this.spriteSheetViewer.MouseMove += new System.Windows.Forms.MouseEventHandler(this.spriteSheetViewer_MouseMove); 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 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -531,8 +562,11 @@ this.Controls.Add(this.spritePanel); this.Controls.Add(this.spriteListPanel); this.Controls.Add(this.menuStrip1); + this.KeyPreview = true; this.Name = "Form1"; 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.PerformLayout(); this.spritePanel.ResumeLayout(false); @@ -595,6 +629,9 @@ private System.Windows.Forms.CheckBox editingOriginCheckBox; private System.Windows.Forms.CheckBox highlightCheckBox; private System.Windows.Forms.Timer outlineTimer; + private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem undoToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem redoToolStripMenuItem; } } diff --git a/SpriteMapEditor/Form1.cs b/SpriteMapEditor/Form1.cs index ab4b2a9..7ab3f4b 100644 --- a/SpriteMapEditor/Form1.cs +++ b/SpriteMapEditor/Form1.cs @@ -13,13 +13,14 @@ using Microsoft.Xna; using System.IO; using QURO; using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate; -using SpriteMapEditor.SpriteMapModifications; namespace SpriteMapEditor { public partial class Form1 : Form { + private SpriteMapModifications.History undoHistory; + private BindingList sprites; private List selectedSprites; @@ -45,6 +46,8 @@ namespace SpriteMapEditor Image originPointer_black; Image originPointer_white; + Image spriteSheet; + Pen blackPen; Pen whitePen; @@ -53,6 +56,8 @@ namespace SpriteMapEditor private Point newPosition; private Point oldPosition; + private int totalDragX; + private int totalDragY; private int drawingRectangleOffset { @@ -70,6 +75,8 @@ namespace SpriteMapEditor InitializeComponent(); FormBorderStyle = FormBorderStyle.FixedSingle; + undoHistory = new SpriteMapModifications.History(); + spriteSheetViewer.Width = 1; spriteSheetViewer.Height = 1; @@ -96,18 +103,26 @@ namespace SpriteMapEditor { if(loadSpriteSheetDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { - spriteSheetViewer.Image = Image.FromFile(loadSpriteSheetDialog.FileName); + spriteSheet = Image.FromFile(loadSpriteSheetDialog.FileName); + spriteSheetViewer.Image = spriteSheet; } + EnableLoadAndSaveAsSpriteMap(); InitializeMask(); } + private void EnableLoadAndSaveAsSpriteMap() + { + loadSpriteMapToolStripMenuItem.Enabled = true; + saveSpriteMapAsToolStripMenuItem.Enabled = true; + } + private void spriteSheetViewer_Paint(object sender, PaintEventArgs e) { - if (spriteSheetViewer.Image != null) + if (spriteSheet != null) { - spriteSheetViewer.Width = spriteSheetViewer.Image.Width * zoomLevel; - spriteSheetViewer.Height = spriteSheetViewer.Image.Height * zoomLevel; + spriteSheetViewer.Width = spriteSheet.Width * zoomLevel; + spriteSheetViewer.Height = spriteSheet.Height * zoomLevel; if (highlight) { @@ -190,8 +205,8 @@ namespace SpriteMapEditor { if (editingOrigin) { - var spriteSheet = (Bitmap)spriteSheetViewer.Image; - var colorAtOrigin = spriteSheet.GetPixel((int)(sprite.Bounds.X + sprite.Origin.X), + var spriteSheetBitmap = (Bitmap)spriteSheet; + var colorAtOrigin = spriteSheetBitmap.GetPixel((int)(sprite.Bounds.X + sprite.Origin.X), (int)(sprite.Bounds.Y + sprite.Origin.Y)); var highContrastPointer = originPointer_black; @@ -232,7 +247,7 @@ namespace SpriteMapEditor { 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 currentX = 0; currentX < highlightMask.Width; currentX++) @@ -244,7 +259,8 @@ namespace SpriteMapEditor } else { - highlightMask.CopyBitmap(baseMask); + highlightMask.Dispose(); + highlightMask = new DirectBitmap(baseMask); } } @@ -280,12 +296,9 @@ namespace SpriteMapEditor { if (loadingSprite) return; - foreach (SpriteMapRegion currentSprite in selectedSprites) - { - var newBounds = currentSprite.Bounds; - newBounds.X = (int)spriteXPosBox.Value; - currentSprite.Bounds = newBounds; - } + var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, x: (int)spriteXPosBox.Value); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); UpdateHighlightMask(); spriteSheetViewer.Refresh(); } @@ -294,12 +307,9 @@ namespace SpriteMapEditor { if (loadingSprite) return; - foreach (SpriteMapRegion currentSprite in selectedSprites) - { - var newBounds = currentSprite.Bounds; - newBounds.Y = (int)spriteYPosBox.Value; - currentSprite.Bounds = newBounds; - } + var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, y: (int)spriteYPosBox.Value); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); UpdateHighlightMask(); spriteSheetViewer.Refresh(); } @@ -308,12 +318,9 @@ namespace SpriteMapEditor { if (loadingSprite) return; - foreach (SpriteMapRegion currentSprite in selectedSprites) - { - var newBounds = currentSprite.Bounds; - newBounds.Width = (int)spriteWidthBox.Value; - currentSprite.Bounds = newBounds; - } + var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, width: (int)spriteWidthBox.Value); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); UpdateHighlightMask(); spriteSheetViewer.Refresh(); } @@ -322,33 +329,35 @@ namespace SpriteMapEditor { if (loadingSprite) return; - foreach (SpriteMapRegion currentSprite in selectedSprites) - { - var newBounds = currentSprite.Bounds; - newBounds.Height = (int)spriteHeightBox.Value; - currentSprite.Bounds = newBounds; - } + var modification = new SpriteMapModifications.ModifyBounds(selectedSprites, height: (int)spriteHeightBox.Value); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); UpdateHighlightMask(); 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) { - 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) { - if (spriteList.SelectedIndex > -1 && spriteList.SelectedIndex < sprites.Count) - sprites.RemoveAt(spriteList.SelectedIndex); + int indexToRemove = 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) @@ -484,12 +493,14 @@ namespace SpriteMapEditor } spriteList.DataSource = sprites; + + saveSpriteMapToolStripMenuItem.Enabled = true; } } private void spriteSheetViewer_MouseMove(object sender, MouseEventArgs e) { - if(spriteSheetViewer.Image == null) + if(spriteSheet == null) return; Point mousePosition = new Point(e.X, e.Y); @@ -512,12 +523,15 @@ namespace SpriteMapEditor var groupRect = GetSelectedGroupRectangle(); if (groupRect.X + differenceX < 0 || - groupRect.X + groupRect.Width + differenceX > spriteSheetViewer.Image.Width) + groupRect.X + groupRect.Width + differenceX > spriteSheet.Width) differenceX = 0; if (groupRect.Y + differenceY < 0 || - groupRect.Y + groupRect.Height + differenceY > spriteSheetViewer.Image.Height) + groupRect.Y + groupRect.Height + differenceY > spriteSheet.Height) differenceY = 0; + totalDragX += differenceX; + totalDragY += differenceY; + foreach (SpriteMapRegion spriteToMove in selectedSprites) { var newBounds = spriteToMove.Bounds; @@ -547,20 +561,20 @@ namespace SpriteMapEditor //clamp X value if (clampedRect.X < 0) clampedRect.X = 0; - else if (clampedRect.X + clampedRect.Width > spriteSheetViewer.Image.Width) - clampedRect.X = spriteSheetViewer.Image.Width - clampedRect.Width; + else if (clampedRect.X + clampedRect.Width > spriteSheet.Width) + clampedRect.X = spriteSheet.Width - clampedRect.Width; //clamp Y value if (clampedRect.Y < 0) clampedRect.Y = 0; - else if (clampedRect.Y + clampedRect.Height > spriteSheetViewer.Image.Height) - clampedRect.Y = spriteSheetViewer.Image.Height - clampedRect.Height; + else if (clampedRect.Y + clampedRect.Height > spriteSheet.Height) + clampedRect.Y = spriteSheet.Height - clampedRect.Height; return clampedRect; } private void spriteSheetViewer_MouseDown(object sender, MouseEventArgs e) { - if (spriteSheetViewer.Image == null) + if (spriteSheet == null) return; Point mousePosition = new Point(e.X, e.Y); @@ -570,6 +584,8 @@ namespace SpriteMapEditor if (GetDrawingRect(currentSprite.Bounds).Contains(mousePosition)) { movingWithMouse = true; + totalDragX = 0; + totalDragY = 0; newPosition = mousePosition; newPosition.X /= zoomLevel; newPosition.Y /= zoomLevel; @@ -587,18 +603,23 @@ namespace SpriteMapEditor private void spriteSheetViewer_MouseUp(object sender, MouseEventArgs e) { 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) { if (loadingSprite) return; - foreach (SpriteMapRegion currentSprite in selectedSprites) - { - var newOrigin = currentSprite.Origin; - newOrigin.X = (int)originXPosBox.Value; - currentSprite.Origin = newOrigin; - } + var modification = new SpriteMapModifications.ModifyOrigin(selectedSprites, x: (int)originXPosBox.Value); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); spriteSheetViewer.Refresh(); } @@ -606,12 +627,9 @@ namespace SpriteMapEditor { if (loadingSprite) return; - foreach (SpriteMapRegion currentSprite in selectedSprites) - { - var newOrigin = currentSprite.Origin; - newOrigin.Y = (int)originYPosBox.Value; - currentSprite.Origin = newOrigin; - } + var modification = new SpriteMapModifications.ModifyOrigin(selectedSprites, y: (int)originYPosBox.Value); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); spriteSheetViewer.Refresh(); } @@ -619,9 +637,9 @@ namespace SpriteMapEditor { if(spriteList.SelectedIndex < sprites.Count - 1) { - var spriteBelow = sprites[spriteList.SelectedIndex + 1]; - sprites[spriteList.SelectedIndex + 1] = selectedSprites[0]; - sprites[spriteList.SelectedIndex] = spriteBelow; + var modification = new SpriteMapModifications.MoveSpriteListEntry(sprites, spriteList.SelectedIndex, 1); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); int newPosition = spriteList.SelectedIndex + 1; spriteList.SelectedIndices.Clear(); spriteList.SelectedIndex = newPosition; @@ -632,9 +650,9 @@ namespace SpriteMapEditor { if(spriteList.SelectedIndex > 0) { - var spriteAbove = sprites[spriteList.SelectedIndex - 1]; - sprites[spriteList.SelectedIndex - 1] = selectedSprites[0]; - sprites[spriteList.SelectedIndex] = spriteAbove; + var modification = new SpriteMapModifications.MoveSpriteListEntry(sprites, spriteList.SelectedIndex, -1); + SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList); + undoHistory.Add(modification); int newPosition = spriteList.SelectedIndex - 1; spriteList.SelectedIndices.Clear(); spriteList.SelectedIndex = newPosition; @@ -681,5 +699,64 @@ namespace SpriteMapEditor whitePen.DashOffset = 0; 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); + } } } diff --git a/SpriteMapEditor/SpriteMapEditor.csproj b/SpriteMapEditor/SpriteMapEditor.csproj index 1a01be6..e607c7a 100644 --- a/SpriteMapEditor/SpriteMapEditor.csproj +++ b/SpriteMapEditor/SpriteMapEditor.csproj @@ -58,7 +58,9 @@ Form1.cs + + @@ -70,6 +72,7 @@ + Form1.cs diff --git a/SpriteMapEditor/SpriteMapModifications/AddSprite.cs b/SpriteMapEditor/SpriteMapModifications/AddSprite.cs index eaff42c..84b5552 100644 --- a/SpriteMapEditor/SpriteMapModifications/AddSprite.cs +++ b/SpriteMapEditor/SpriteMapModifications/AddSprite.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using System.ComponentModel; using QURO; +using System.Windows.Forms; namespace SpriteMapEditor.SpriteMapModifications { @@ -14,9 +15,13 @@ namespace SpriteMapEditor.SpriteMapModifications private readonly SpriteMapRegion selectedSprite; + private List preChangeSelection; + private List postChangeSelection; + public AddSprite(BindingList spriteList, SpriteMapRegion selected = null) { allSprites = spriteList; + selectedSprite = selected; } @@ -32,5 +37,28 @@ namespace SpriteMapEditor.SpriteMapModifications { allSprites.RemoveAt(allSprites.Count - 1); } + + public List GetPreChangeSelection() + { + return preChangeSelection; + } + public void SetPreChangeSelection(List currentSelection) + { + preChangeSelection = currentSelection; + } + public List GetPostChangeSelection() + { + return postChangeSelection; + } + public void SetPostChangeSelection(List currentSelection) + { + postChangeSelection = currentSelection; + } + + + public override string ToString() + { + return "Add Sprite"; + } } } diff --git a/SpriteMapEditor/SpriteMapModifications/History.cs b/SpriteMapEditor/SpriteMapModifications/History.cs new file mode 100644 index 0000000..4afe256 --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/History.cs @@ -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 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(); + } + + 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]; + } + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs b/SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs index 84f7315..ed0e9d6 100644 --- a/SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs +++ b/SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs @@ -1,14 +1,19 @@ using System; using System.Collections.Generic; +using System.Windows.Forms; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpriteMapEditor.SpriteMapModifications { - interface ISpriteMapModification + public interface ISpriteMapModification { void Do(); void Undo(); + List GetPreChangeSelection(); + void SetPreChangeSelection(List currentSelection); + List GetPostChangeSelection(); + void SetPostChangeSelection(List currentSelection); } } diff --git a/SpriteMapEditor/SpriteMapModifications/ModHelper.cs b/SpriteMapEditor/SpriteMapModifications/ModHelper.cs new file mode 100644 index 0000000..7f9f26c --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/ModHelper.cs @@ -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 GetSelectionList(ListBox listBox) + { + List result = new List(); + foreach(int currentIndex in listBox.SelectedIndices) + { + result.Add(currentIndex); + } + return result; + } + + public static void SelectFromList(ListBox listBox, List newSelection) + { + listBox.SelectedIndices.Clear(); + var source = (BindingList)listBox.DataSource; + foreach (int currentIndex in newSelection) + { + if(currentIndex < source.Count) + listBox.SelectedIndex = currentIndex; + } + } + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs b/SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs index d039562..7e76231 100644 --- a/SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs +++ b/SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using QURO; using Microsoft.Xna.Framework; +using System.Windows.Forms; namespace SpriteMapEditor.SpriteMapModifications { @@ -19,9 +20,12 @@ namespace SpriteMapEditor.SpriteMapModifications private readonly List oldBounds; + private List preChangeSelection; + private List postChangeSelection; + public ModifyBounds(List spritesToModify, int x = -1, int y = -1, int width = -1, int height = -1) { - sprites = spritesToModify; + sprites = spritesToModify.ToList(); newXPosition = x; newYPosition = y; newWidth = width; @@ -61,5 +65,27 @@ namespace SpriteMapEditor.SpriteMapModifications sprites[index].Bounds = oldBounds[index]; } } + + public List GetPreChangeSelection() + { + return preChangeSelection; + } + public void SetPreChangeSelection(List currentSelection) + { + preChangeSelection = currentSelection; + } + public List GetPostChangeSelection() + { + return postChangeSelection; + } + public void SetPostChangeSelection(List currentSelection) + { + postChangeSelection = currentSelection; + } + + public override string ToString() + { + return "Modify Sprite Bounds"; + } } } diff --git a/SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs b/SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs index a35d0bd..64a3d40 100644 --- a/SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs +++ b/SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs @@ -17,9 +17,12 @@ namespace SpriteMapEditor.SpriteMapModifications private readonly List oldOrigins; - public ModifyOrigin(List spritesToEditOrigin, int x = -1, int y = -1) + private List preChangeSelection; + private List postChangeSelection; + + public ModifyOrigin(List spritesToEditOrigin, int x = -1, int y = -1) { - sprites = spritesToEditOrigin; + sprites = spritesToEditOrigin.ToList(); newOriginX = x; newOriginY = y; @@ -52,5 +55,27 @@ namespace SpriteMapEditor.SpriteMapModifications sprites[index].Origin = oldOrigins[index]; } } + + public List GetPreChangeSelection() + { + return preChangeSelection; + } + public void SetPreChangeSelection(List currentSelection) + { + preChangeSelection = currentSelection; + } + public List GetPostChangeSelection() + { + return postChangeSelection; + } + public void SetPostChangeSelection(List currentSelection) + { + postChangeSelection = currentSelection; + } + + public override string ToString() + { + return "Modify Sprite Origin"; + } } } diff --git a/SpriteMapEditor/SpriteMapModifications/MoveBounds.cs b/SpriteMapEditor/SpriteMapModifications/MoveBounds.cs index 31c6b66..9138f2f 100644 --- a/SpriteMapEditor/SpriteMapModifications/MoveBounds.cs +++ b/SpriteMapEditor/SpriteMapModifications/MoveBounds.cs @@ -14,9 +14,12 @@ namespace SpriteMapEditor.SpriteMapModifications private readonly int xDiff; private readonly int yDiff; + private List preChangeSelection; + private List postChangeSelection; + public MoveBounds(List spritesToMove, int xDifference, int yDifference) { - sprites = spritesToMove; + sprites = spritesToMove.ToList(); xDiff = xDifference; yDiff = yDifference; } @@ -41,5 +44,27 @@ namespace SpriteMapEditor.SpriteMapModifications sprite.Bounds = newBounds; } } + + public List GetPreChangeSelection() + { + return preChangeSelection; + } + public void SetPreChangeSelection(List currentSelection) + { + preChangeSelection = currentSelection; + } + public List GetPostChangeSelection() + { + return postChangeSelection; + } + public void SetPostChangeSelection(List currentSelection) + { + postChangeSelection = currentSelection; + } + + public override string ToString() + { + return "Move Sprite Bounds"; + } } } diff --git a/SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs b/SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs index 3175bdf..c5beaab 100644 --- a/SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs +++ b/SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs @@ -15,6 +15,9 @@ namespace SpriteMapEditor.SpriteMapModifications private readonly int selectedIndex; private readonly int dir; + private List preChangeSelection; + private List postChangeSelection; + public MoveSpriteListEntry(BindingList spriteList, int selection, int direction) { allSprites = spriteList; @@ -25,15 +28,37 @@ namespace SpriteMapEditor.SpriteMapModifications public void Do() { var movingSprite = allSprites[selectedIndex]; - allSprites.RemoveAt(selectedIndex); - allSprites.Insert(selectedIndex + dir, movingSprite); + allSprites[selectedIndex] = allSprites[selectedIndex + dir]; + allSprites[selectedIndex + dir] = movingSprite; } public void Undo() { var movingSprite = allSprites[selectedIndex + dir]; - allSprites.RemoveAt(selectedIndex + dir); - allSprites.Insert(selectedIndex, movingSprite); + allSprites[selectedIndex + dir] = allSprites[selectedIndex]; + allSprites[selectedIndex] = movingSprite; + } + + public List GetPreChangeSelection() + { + return preChangeSelection; + } + public void SetPreChangeSelection(List currentSelection) + { + preChangeSelection = currentSelection; + } + public List GetPostChangeSelection() + { + return postChangeSelection; + } + public void SetPostChangeSelection(List currentSelection) + { + postChangeSelection = currentSelection; + } + + public override string ToString() + { + return "Move Sprite List Entry"; } } } diff --git a/SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs b/SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs index dda5f19..458bae6 100644 --- a/SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs +++ b/SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs @@ -16,6 +16,9 @@ namespace SpriteMapEditor.SpriteMapModifications private readonly SpriteMapRegion removedSprite; + private List preChangeSelection; + private List postChangeSelection; + public RemoveSprite(BindingList spriteList, int indexToRemove) { allSprites = spriteList; @@ -32,5 +35,27 @@ namespace SpriteMapEditor.SpriteMapModifications { allSprites.Insert(index, removedSprite); } + + public List GetPreChangeSelection() + { + return preChangeSelection; + } + public void SetPreChangeSelection(List currentSelection) + { + preChangeSelection = currentSelection; + } + public List GetPostChangeSelection() + { + return postChangeSelection; + } + public void SetPostChangeSelection(List currentSelection) + { + postChangeSelection = currentSelection; + } + + public override string ToString() + { + return "Remove Sprite"; + } } } diff --git a/SpriteMapEditor/SpriteMapModifications/RenameSprite.cs b/SpriteMapEditor/SpriteMapModifications/RenameSprite.cs new file mode 100644 index 0000000..c729eb8 --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/RenameSprite.cs @@ -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 preChangeSelection; + private List 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 GetPreChangeSelection() + { + return preChangeSelection; + } + public void SetPreChangeSelection(List currentSelection) + { + preChangeSelection = currentSelection; + } + public List GetPostChangeSelection() + { + return postChangeSelection; + } + public void SetPostChangeSelection(List currentSelection) + { + postChangeSelection = currentSelection; + } + + public override string ToString() + { + return "Rename Sprite"; + } + } +}