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
@@ -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<int> preChangeSelection;
private List<int> postChangeSelection;
public AddSprite(BindingList<SpriteMapRegion> spriteList, SpriteMapRegion selected = null)
{
allSprites = spriteList;
selectedSprite = selected;
}
@@ -32,5 +37,28 @@ namespace SpriteMapEditor.SpriteMapModifications
{
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.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<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 QURO;
using Microsoft.Xna.Framework;
using System.Windows.Forms;
namespace SpriteMapEditor.SpriteMapModifications
{
@@ -19,9 +20,12 @@ namespace SpriteMapEditor.SpriteMapModifications
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)
{
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<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;
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;
newOriginY = y;
@@ -52,5 +55,27 @@ namespace SpriteMapEditor.SpriteMapModifications
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 yDiff;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public MoveBounds(List<SpriteMapRegion> 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<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 dir;
private List<int> preChangeSelection;
private List<int> postChangeSelection;
public MoveSpriteListEntry(BindingList<SpriteMapRegion> 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<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 List<int> preChangeSelection;
private List<int> postChangeSelection;
public RemoveSprite(BindingList<SpriteMapRegion> spriteList, int indexToRemove)
{
allSprites = spriteList;
@@ -32,5 +35,27 @@ namespace SpriteMapEditor.SpriteMapModifications
{
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";
}
}
}