Added: SelectionState; New Undoable Mods: AddAnimation, RemoveAnimation, AddFrame, RemoveFrame, RemoveSprite

This commit is contained in:
quinnvoker
2018-12-26 21:07:21 -08:00
parent 2e72c46b7b
commit aaa80bd654
16 changed files with 761 additions and 120 deletions
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using QURO;
using QURO.Animation;
using System.Windows.Forms;
namespace AnimationEditor.Modifications
{
class AddAnimation : IModification
{
private SelectionState preChangeSelection;
private SelectionState postChangeSelection;
private BindingList<Animation> animations;
private ComboBox animationBox;
private Animation animationToAdd;
public AddAnimation(BindingList<Animation> animList, ComboBox animBox, Animation anim = null)
{
animations = animList;
animationBox = animBox;
if (anim != null)
animationToAdd = anim;
else animationToAdd = new Animation();
}
public void Do()
{
animations.Add(animationToAdd);
animationBox.SelectedIndex = animations.Count - 1;
}
public void Undo()
{
animations.RemoveAt(animations.Count - 1);
}
public SelectionState GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(SelectionState currentSelection)
{
preChangeSelection = currentSelection;
}
public SelectionState GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(SelectionState currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Add Animation";
}
}
}
+98
View File
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using QURO;
using QURO.Animation;
using System.Windows.Forms;
namespace AnimationEditor.Modifications
{
class AddFrame : IModification
{
private SelectionState preChangeSelection;
private SelectionState postChangeSelection;
private BindingList<Frame> frames;
private ListBox frameListBox;
private Frame frameToAdd;
public AddFrame(BindingList<Frame> frameList, ListBox frameBox, Frame frame = null)
{
frames = frameList;
frameListBox = frameBox;
if (frame != null)
frameToAdd = frame;
else frameToAdd = new Frame();
}
public void Do()
{
int newSelectedIndex;
//if only frame in animation is uninitiated and input is not empty, clear the list
if (frameToAdd.Name != "empty" &&
frames.Count == 1 &&
frames[0].Sprites.Count == 1 &&
frames[0].Sprites[0].Name == "empty")
{
frames.Clear();
}
if (frameListBox.SelectedItem == null || frameListBox.SelectedIndex == frames.Count - 1)
{
frames.Add(frameToAdd);
newSelectedIndex = frames.Count - 1;
}
else
{
frames.Insert(frameListBox.SelectedIndex + 1, frameToAdd);
newSelectedIndex = frameListBox.SelectedIndex + 1;
}
ModHelper.SetSingleSelection(frameListBox, newSelectedIndex);
}
public void Undo()
{
if(frames.Count < 2)
{
frames.Clear();
frames.Add(new Frame());
}
else if(postChangeSelection!= null)
{
int addedFrameIndex = postChangeSelection.SelectedFrames[0];
frames.RemoveAt(addedFrameIndex);
if(addedFrameIndex > 0)
{
ModHelper.SetSingleSelection(frameListBox, addedFrameIndex - 1);
}
}
}
public SelectionState GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(SelectionState currentSelection)
{
preChangeSelection = currentSelection;
}
public SelectionState GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(SelectionState currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Add Frame";
}
}
}
+94
View File
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnimationEditor.Modifications
{
class History
{
private List<IModification> 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<IModification>();
}
public void Add(IModification change)
{
if (position < changes.Count - 1)
{
changes.RemoveRange(position + 1, changes.Count - 1 - position);
}
changes.Add(change);
position = changes.Count - 1;
}
public void Undo()
{
if (CanUndo)
{
changes[position].Undo();
position--;
}
}
public void Undo(ComboBox animBox, ListBox frameBox, ListBox spriteBox)
{
if (CanUndo)
{
Console.Write("Hist.Pos(" + position + "): ");
ModHelper.UndoAndRestoreSelection(changes[position], animBox, frameBox, spriteBox);
position--;
}
}
public void Redo()
{
if (CanRedo)
{
changes[position + 1].Do();
position++;
}
}
public void Redo(ComboBox animBox, ListBox frameBox, ListBox spriteBox)
{
if (CanRedo)
{
Console.Write("Hist.Pos(" + position + "): ");
ModHelper.RedoAndRestoreSelection(changes[position + 1], animBox, frameBox, spriteBox);
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];
}
}
}
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnimationEditor.Modifications
{
public interface IModification
{
void Do();
void Undo();
SelectionState GetPreChangeSelection();
void SetPreChangeSelection(SelectionState currentSelection);
SelectionState GetPostChangeSelection();
void SetPostChangeSelection(SelectionState currentSelection);
}
}
@@ -0,0 +1,59 @@
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 AnimationEditor.Modifications
{
public static class ModHelper
{
public static void DoModificationWithSelectionTracking(IModification mod, ComboBox animBox, ListBox frameBox, ListBox frameSpriteBox)
{
mod.SetPreChangeSelection(new SelectionState(animBox, frameBox, frameSpriteBox));
mod.Do();
mod.SetPostChangeSelection(new SelectionState (animBox, frameBox, frameSpriteBox));
Console.WriteLine("Perform: " + mod);
}
public static void UndoAndRestoreSelection(IModification mod, ComboBox animBox, ListBox frameBox, ListBox frameSpriteBox)
{
mod.Undo();
RestoreSelection(mod.GetPreChangeSelection(), animBox, frameBox, frameSpriteBox);
Console.WriteLine("Undo: " + mod);
}
public static void RedoAndRestoreSelection(IModification mod, ComboBox animBox, ListBox frameBox, ListBox frameSpriteBox)
{
mod.Do();
RestoreSelection(mod.GetPostChangeSelection(), animBox, frameBox, frameSpriteBox);
Console.WriteLine("Redo: " + mod);
}
public static void RestoreSelection(SelectionState selection, ComboBox animBox, ListBox frameBox, ListBox frameSpriteBox)
{
animBox.SelectedIndex = selection.SelectedAnimation;
frameBox.SelectedIndices.Clear();
foreach(int index in selection.SelectedFrames)
{
frameBox.SelectedIndex = index;
}
frameSpriteBox.SelectedIndices.Clear();
foreach(int index in selection.SelectedFrameSprites)
{
frameSpriteBox.SelectedIndex = index;
}
}
public static void SetSingleSelection(ListBox lb, int index)
{
lb.SelectedIndices.Clear();
lb.SelectedIndex = index;
}
}
}
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using QURO;
using QURO.Animation;
using System.Windows.Forms;
namespace AnimationEditor.Modifications
{
class RemoveAnimation : IModification
{
private SelectionState preChangeSelection;
private SelectionState postChangeSelection;
private BindingList<Animation> animations;
private ComboBox animationBox;
private int removeIndex;
private Animation removedAnim;
public RemoveAnimation(BindingList<Animation> anims, ComboBox animBox, int index)
{
animations = anims;
animationBox = animBox;
removeIndex = index;
}
public void Do()
{
if (removeIndex > -1 && removeIndex < animations.Count)
{
removedAnim = animations[removeIndex];
if (animations.Count == 1)
{
animations[removeIndex] = new Animation();
}
else
{
animations.RemoveAt(removeIndex);
if (animationBox.SelectedIndex > animations.Count - 1)
{
animationBox.SelectedIndex--;
}
}
}
}
public void Undo()
{
if(removedAnim != null)
{
if (animations.Count == 1 && removeIndex == 0 && animations[0].Frames.Length == 1 && animations[0].Frames[0].Sprites[0].Bounds == Rectangle.Empty)
{
animations[0] = removedAnim;
}
else
animations.Insert(removeIndex, removedAnim);
}
}
public SelectionState GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(SelectionState currentSelection)
{
preChangeSelection = currentSelection;
}
public SelectionState GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(SelectionState currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Remove Animation";
}
}
}
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using QURO;
using QURO.Animation;
using System.Windows.Forms;
namespace AnimationEditor.Modifications
{
class RemoveFrame : IModification
{
private SelectionState preChangeSelection;
private SelectionState postChangeSelection;
private BindingList<Frame> frames;
private ListBox frameListBox;
private int removeIndex;
private Frame removedFrame;
public RemoveFrame(BindingList<Frame> frameList, ListBox frameBox, int index)
{
frames = frameList;
frameListBox = frameBox;
removeIndex = index;
}
public void Do()
{
if(removeIndex > -1 && removeIndex < frames.Count)
{
removedFrame = frames[removeIndex];
if(frames.Count == 1)
{
frames.Clear();
frames.Add(new Frame());
}
else
frames.RemoveAt(removeIndex);
if (frames.Count > -1 && frameListBox.SelectedIndex > frames.Count - 1)
{
ModHelper.SetSingleSelection(frameListBox, frames.Count - 1);
}
}
}
public void Undo()
{
if(removedFrame != null)
{
if(frames.Count == 1 && removeIndex == 0)
{
frames.Clear();
frames.Add(removedFrame);
}
else
frames.Insert(removeIndex, removedFrame);
}
}
public SelectionState GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(SelectionState currentSelection)
{
preChangeSelection = currentSelection;
}
public SelectionState GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(SelectionState currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Remove Sprite";
}
}
}
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using QURO;
using QURO.Animation;
using System.Windows.Forms;
namespace AnimationEditor.Modifications
{
class RemoveSprite : IModification
{
private SelectionState preChangeSelection;
private SelectionState postChangeSelection;
private BindingList<Sprite> sprites;
private ListBox spriteListBox;
private int removeIndex;
private Sprite removedSprite;
public RemoveSprite(BindingList<Sprite> spriteList, ListBox spriteBox, int index)
{
sprites = spriteList;
spriteListBox = spriteBox;
removeIndex = index;
}
public void Do()
{
if(removeIndex > -1 && removeIndex < sprites.Count)
{
removedSprite = sprites[removeIndex];
if(sprites.Count == 1)
{
sprites.Clear();
}
else
sprites.RemoveAt(removeIndex);
if (sprites.Count > -1 && spriteListBox.SelectedIndex > sprites.Count - 1)
{
ModHelper.SetSingleSelection(spriteListBox, sprites.Count - 1);
}
}
}
public void Undo()
{
if(removedSprite != null)
{
sprites.Insert(removeIndex, removedSprite);
}
}
public SelectionState GetPreChangeSelection()
{
return preChangeSelection;
}
public void SetPreChangeSelection(SelectionState currentSelection)
{
preChangeSelection = currentSelection;
}
public SelectionState GetPostChangeSelection()
{
return postChangeSelection;
}
public void SetPostChangeSelection(SelectionState currentSelection)
{
postChangeSelection = currentSelection;
}
public override string ToString()
{
return "Remove Frame";
}
}
}
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnimationEditor.Modifications
{
public class SelectionState
{
public int SelectedAnimation { get; set; }
public List<int> SelectedFrames { get; set; }
public List<int> SelectedFrameSprites { get; set; }
public SelectionState(ComboBox animationComboBox, ListBox frameListBox, ListBox frameSpriteListBox)
{
SelectedAnimation = animationComboBox.SelectedIndex;
SelectedFrames = frameListBox.SelectedIndices.Cast<int>().ToList();
SelectedFrameSprites = frameSpriteListBox.SelectedIndices.Cast<int>().ToList();
}
}
}