diff --git a/AnimationEditor/AnimationEditor.csproj b/AnimationEditor/AnimationEditor.csproj
index 657515d..ec58e90 100644
--- a/AnimationEditor/AnimationEditor.csproj
+++ b/AnimationEditor/AnimationEditor.csproj
@@ -93,6 +93,7 @@
+
diff --git a/AnimationEditor/AnimationPreview.cs b/AnimationEditor/AnimationPreview.cs
index 46f54b2..e22d047 100644
--- a/AnimationEditor/AnimationPreview.cs
+++ b/AnimationEditor/AnimationPreview.cs
@@ -36,6 +36,8 @@ namespace AnimationEditor
private MouseState mState;
private MouseState mState_old;
+ private Vector2 totalDragDist;
+
private Vector2 centerPoint
{
get { return new Vector2((Editor.graphics.PresentationParameters.BackBufferWidth / 2), (Editor.graphics.PresentationParameters.BackBufferHeight / 2)); }
@@ -54,16 +56,29 @@ namespace AnimationEditor
}
}
- private EventHandler onSpriteMoved;
- public event EventHandler SpriteMoved
+ private EventHandler onSpriteDragUpdate;
+ public event EventHandler SpriteDragUpdate
{
add
{
- onSpriteMoved += value;
+ onSpriteDragUpdate += value;
}
remove
{
- onSpriteMoved -= value;
+ onSpriteDragUpdate -= value;
+ }
+ }
+
+ private EventHandler onSpriteDragCompleted;
+ public event EventHandler SpriteDragCompleted
+ {
+ add
+ {
+ onSpriteDragCompleted += value;
+ }
+ remove
+ {
+ onSpriteDragCompleted -= value;
}
}
@@ -180,10 +195,23 @@ namespace AnimationEditor
}
if (mState.LeftButton == ButtonState.Pressed && mState.RightButton != ButtonState.Pressed && mState_old.LeftButton != ButtonState.Pressed)
+ {
dragging = hovering;
+ if (dragging)
+ {
+ totalDragDist = Vector2.Zero;
+ }
+ }
if (mState.LeftButton != ButtonState.Pressed)
+ {
+ if(dragging && totalDragDist != Vector2.Zero)
+ {
+ OnSpriteDragCompleted(new Modifications.SpriteDragArgs(totalDragDist));
+
+ }
dragging = false;
+ }
if (dragging)
{
@@ -194,11 +222,12 @@ namespace AnimationEditor
if (distMoved != Vector2.Zero)
{
+ totalDragDist += distMoved;
foreach(Sprite spr in EditSprites)
{
spr.Offset += distMoved;
}
- OnSpriteMoved(new EventArgs());
+ OnSpriteDragUpdate(new EventArgs());
}
}
}
@@ -231,9 +260,14 @@ namespace AnimationEditor
}
}
- protected virtual void OnSpriteMoved(EventArgs e)
+ protected virtual void OnSpriteDragCompleted(EventArgs e)
+ {
+ onSpriteDragCompleted?.Invoke(this, e);
+ }
+
+ protected virtual void OnSpriteDragUpdate(EventArgs e)
{
- onSpriteMoved?.Invoke(this, e);
+ onSpriteDragUpdate?.Invoke(this, e);
}
protected virtual void OnFrameChanged(EventArgs e)
diff --git a/AnimationEditor/Form1.cs b/AnimationEditor/Form1.cs
index 2b390c3..cb584e1 100644
--- a/AnimationEditor/Form1.cs
+++ b/AnimationEditor/Form1.cs
@@ -57,7 +57,17 @@ namespace AnimationEditor
importDelayBox.Value = 0;
animationPreview.FrameChanged += animationPreview_FrameChanged;
animationPreview.AnimationEnded += animationPreview_AnimationEnded;
- animationPreview.SpriteMoved += animationPreview_SpriteMoved;
+ animationPreview.SpriteDragUpdate += animationPreview_SpriteDragUpdate;
+ animationPreview.SpriteDragCompleted += animationPreview_SpriteDragComplete;
+ }
+
+ private void animationPreview_SpriteDragComplete(object sender, EventArgs e)
+ {
+ var args = e as SpriteDragArgs;
+ var mod = new MoveSprite(frameSprites, frameSpriteListBox, args.DragDistance);
+ mod.Undo();
+ ModHelper.DoModificationWithSelectionTracking(mod, animationBox, frameListBox, frameSpriteListBox);
+ undoHistory.Add(mod);
}
private void frameListBox_SetSingleSelection(int index)
@@ -116,6 +126,7 @@ namespace AnimationEditor
{
if(frames?.Count > 0)
{
+ //update currentAnimation's data
if(currentAnimation == null)
currentAnimation = new Animation("unnamed", frames.ToArray(), true);
else
@@ -124,13 +135,19 @@ namespace AnimationEditor
currentAnimation.Frames = frames.ToArray();
currentAnimation.IsLooping = animationLoopCheckBox.Checked;
}
+ //ensure frameTrackBar length matches current animation
+ frameTrackBar.Maximum = currentAnimation.Frames.Count() - 1;
+ //send currentAnimation to animationPreview
if (animationPreview.PreviewSprite == null)
animationPreview.PreviewSprite = new AnimatedSprite(currentAnimation);
else
animationPreview.PreviewSprite.CurrentAnimation = currentAnimation;
- frameTrackBar.Maximum = currentAnimation.Frames.Count() - 1;
- if(frameListBox.SelectedIndices.Count < 2 && frameListBox.SelectedIndex != frameTrackBar.Value)
+ //restore currently viewed frame
+ if (frameListBox.SelectedIndices.Count < 2)
frameListBox_SetSingleSelection(frameTrackBar.Value);
+ else
+ frameListBox.SelectedIndex = frameTrackBar.Value;
+ //if animations list is empty, send created currentAnimation to it
if(animations.Count < 1)
{
animations.Add(currentAnimation);
@@ -722,7 +739,7 @@ namespace AnimationEditor
}
}
- private void animationPreview_SpriteMoved(object sender, EventArgs e)
+ private void animationPreview_SpriteDragUpdate(object sender, EventArgs e)
{
if(frameSpriteListBox.SelectedIndices.Count == 1)
ReadSpriteOffset();
diff --git a/AnimationEditor/Modifications/MoveSprite.cs b/AnimationEditor/Modifications/MoveSprite.cs
index 8a8ca96..871ae4a 100644
--- a/AnimationEditor/Modifications/MoveSprite.cs
+++ b/AnimationEditor/Modifications/MoveSprite.cs
@@ -19,7 +19,6 @@ namespace AnimationEditor.Modifications
private BindingList sprites;
private ListBox listBox;
private List indices;
- private Dictionary originalPositions;
private Vector2 dist;
public MoveSprite(BindingList spriteList, ListBox spriteListBox, Vector2 distance)
@@ -27,12 +26,6 @@ namespace AnimationEditor.Modifications
sprites = spriteList;
listBox = spriteListBox;
indices = new List();
- originalPositions = new Dictionary();
- foreach (int i in spriteListBox.SelectedIndices)
- {
- indices.Add(i);
- originalPositions.Add(i, sprites[i].Offset);
- }
dist = distance;
}
@@ -48,7 +41,7 @@ namespace AnimationEditor.Modifications
{
foreach (int index in indices)
{
- sprites[index].Offset = originalPositions[index];
+ sprites[index].Offset -= dist;
}
}
diff --git a/AnimationEditor/Modifications/ReorderSprite.cs b/AnimationEditor/Modifications/ReorderSprite.cs
index c9aa3c7..7f2c564 100644
--- a/AnimationEditor/Modifications/ReorderSprite.cs
+++ b/AnimationEditor/Modifications/ReorderSprite.cs
@@ -65,7 +65,7 @@ namespace AnimationEditor.Modifications
public override string ToString()
{
- return "Move Sprite";
+ return "Reorder Sprite";
}
}
}
diff --git a/AnimationEditor/Modifications/SpriteDragArgs.cs b/AnimationEditor/Modifications/SpriteDragArgs.cs
new file mode 100644
index 0000000..40fa62f
--- /dev/null
+++ b/AnimationEditor/Modifications/SpriteDragArgs.cs
@@ -0,0 +1,19 @@
+using System;
+using Microsoft.Xna.Framework;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnimationEditor.Modifications
+{
+ class SpriteDragArgs : EventArgs
+ {
+ public Vector2 DragDistance { get; private set; }
+
+ public SpriteDragArgs(Vector2 drag)
+ {
+ DragDistance = drag;
+ }
+ }
+}