Patched bug that caused Undoing MoveBounds operations to sometimes move sprite bounds out of range
This commit is contained in:
+49
-65
@@ -56,8 +56,8 @@ namespace SpriteMapEditor
|
|||||||
|
|
||||||
private Point newPosition;
|
private Point newPosition;
|
||||||
private Point oldPosition;
|
private Point oldPosition;
|
||||||
private int totalDragX;
|
|
||||||
private int totalDragY;
|
private List<Microsoft.Xna.Framework.Rectangle> preDragBounds;
|
||||||
|
|
||||||
private int drawingRectangleOffset
|
private int drawingRectangleOffset
|
||||||
{
|
{
|
||||||
@@ -513,65 +513,42 @@ namespace SpriteMapEditor
|
|||||||
{
|
{
|
||||||
hovering = true;
|
hovering = true;
|
||||||
Cursor = Cursors.SizeAll;
|
Cursor = Cursors.SizeAll;
|
||||||
if (movingWithMouse)
|
|
||||||
{
|
|
||||||
newPosition = mousePosition;
|
|
||||||
newPosition.X /= zoomLevel;
|
|
||||||
newPosition.Y /= zoomLevel;
|
|
||||||
int differenceX = newPosition.X - oldPosition.X;
|
|
||||||
int differenceY = newPosition.Y - oldPosition.Y;
|
|
||||||
|
|
||||||
var groupRect = GetSelectedGroupRectangle();
|
|
||||||
if (groupRect.X + differenceX < 0 ||
|
|
||||||
groupRect.X + groupRect.Width + differenceX > spriteSheet.Width)
|
|
||||||
differenceX = 0;
|
|
||||||
if (groupRect.Y + differenceY < 0 ||
|
|
||||||
groupRect.Y + groupRect.Height + differenceY > spriteSheet.Height)
|
|
||||||
differenceY = 0;
|
|
||||||
|
|
||||||
totalDragX += differenceX;
|
|
||||||
totalDragY += differenceY;
|
|
||||||
|
|
||||||
foreach (SpriteMapRegion spriteToMove in selectedSprites)
|
|
||||||
{
|
|
||||||
var newBounds = spriteToMove.Bounds;
|
|
||||||
newBounds.X += differenceX;
|
|
||||||
newBounds.Y += differenceY;
|
|
||||||
|
|
||||||
newBounds = ClampRectangleToSheet(newBounds);
|
|
||||||
|
|
||||||
spriteToMove.Bounds = newBounds;
|
|
||||||
}
|
|
||||||
oldPosition = newPosition;
|
|
||||||
UpdateHighlightMask();
|
|
||||||
spriteSheetViewer.Refresh();
|
|
||||||
LoadSpriteEditorValues();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!hovering)
|
|
||||||
|
if (movingWithMouse)
|
||||||
|
{
|
||||||
|
newPosition = mousePosition;
|
||||||
|
newPosition.X /= zoomLevel;
|
||||||
|
newPosition.Y /= zoomLevel;
|
||||||
|
int differenceX = newPosition.X - oldPosition.X;
|
||||||
|
int differenceY = newPosition.Y - oldPosition.Y;
|
||||||
|
|
||||||
|
var groupRect = GetSelectedGroupRectangle();
|
||||||
|
if (groupRect.X + differenceX < 0 ||
|
||||||
|
groupRect.X + groupRect.Width + differenceX > spriteSheet.Width)
|
||||||
|
differenceX = 0;
|
||||||
|
if (groupRect.Y + differenceY < 0 ||
|
||||||
|
groupRect.Y + groupRect.Height + differenceY > spriteSheet.Height)
|
||||||
|
differenceY = 0;
|
||||||
|
|
||||||
|
foreach (SpriteMapRegion spriteToMove in selectedSprites)
|
||||||
|
{
|
||||||
|
var newBounds = spriteToMove.Bounds;
|
||||||
|
newBounds.X += differenceX;
|
||||||
|
newBounds.Y += differenceY;
|
||||||
|
spriteToMove.Bounds = newBounds;
|
||||||
|
}
|
||||||
|
oldPosition = newPosition;
|
||||||
|
UpdateHighlightMask();
|
||||||
|
spriteSheetViewer.Refresh();
|
||||||
|
LoadSpriteEditorValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hovering)
|
||||||
Cursor = Cursors.Default;
|
Cursor = Cursors.Default;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Microsoft.Xna.Framework.Rectangle ClampRectangleToSheet(Microsoft.Xna.Framework.Rectangle rect)
|
|
||||||
{
|
|
||||||
var clampedRect = rect;
|
|
||||||
|
|
||||||
//clamp X value
|
|
||||||
if (clampedRect.X < 0)
|
|
||||||
clampedRect.X = 0;
|
|
||||||
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 > spriteSheet.Height)
|
|
||||||
clampedRect.Y = spriteSheet.Height - clampedRect.Height;
|
|
||||||
|
|
||||||
return clampedRect;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void spriteSheetViewer_MouseDown(object sender, MouseEventArgs e)
|
private void spriteSheetViewer_MouseDown(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
if (spriteSheet == null)
|
if (spriteSheet == null)
|
||||||
@@ -584,8 +561,11 @@ namespace SpriteMapEditor
|
|||||||
if (GetDrawingRect(currentSprite.Bounds).Contains(mousePosition))
|
if (GetDrawingRect(currentSprite.Bounds).Contains(mousePosition))
|
||||||
{
|
{
|
||||||
movingWithMouse = true;
|
movingWithMouse = true;
|
||||||
totalDragX = 0;
|
preDragBounds = new List<Microsoft.Xna.Framework.Rectangle>();
|
||||||
totalDragY = 0;
|
foreach (SpriteMapRegion sprite in selectedSprites)
|
||||||
|
{
|
||||||
|
preDragBounds.Add(sprite.Bounds);
|
||||||
|
}
|
||||||
newPosition = mousePosition;
|
newPosition = mousePosition;
|
||||||
newPosition.X /= zoomLevel;
|
newPosition.X /= zoomLevel;
|
||||||
newPosition.Y /= zoomLevel;
|
newPosition.Y /= zoomLevel;
|
||||||
@@ -602,14 +582,18 @@ namespace SpriteMapEditor
|
|||||||
|
|
||||||
private void spriteSheetViewer_MouseUp(object sender, MouseEventArgs e)
|
private void spriteSheetViewer_MouseUp(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
movingWithMouse = false;
|
if (movingWithMouse)
|
||||||
if(totalDragX != 0 || totalDragY != 0)
|
|
||||||
{
|
{
|
||||||
var modification = new SpriteMapModifications.MoveBounds(selectedSprites, totalDragX, totalDragY);
|
movingWithMouse = false;
|
||||||
var selection = SpriteMapModifications.ModHelper.GetSelectionList(spriteList);
|
var totalDragX = selectedSprites[0].Bounds.X - preDragBounds[0].X;
|
||||||
modification.SetPreChangeSelection(selection);
|
var totalDragY = selectedSprites[0].Bounds.Y - preDragBounds[0].Y;
|
||||||
modification.SetPostChangeSelection(selection);
|
if (totalDragX != 0 || totalDragY != 0)
|
||||||
undoHistory.Add(modification);
|
{
|
||||||
|
var modification = new SpriteMapModifications.MoveBounds(selectedSprites, totalDragX, totalDragY, preDragBounds);
|
||||||
|
modification.Undo();
|
||||||
|
SpriteMapModifications.ModHelper.DoModificationWithSelectionTracking(modification, spriteList);
|
||||||
|
undoHistory.Add(modification);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,7 @@
|
|||||||
<Compile Include="SpriteMapModifications\ModifyOrigin.cs" />
|
<Compile Include="SpriteMapModifications\ModifyOrigin.cs" />
|
||||||
<Compile Include="SpriteMapModifications\MoveBounds.cs" />
|
<Compile Include="SpriteMapModifications\MoveBounds.cs" />
|
||||||
<Compile Include="SpriteMapModifications\MoveSpriteListEntry.cs" />
|
<Compile Include="SpriteMapModifications\MoveSpriteListEntry.cs" />
|
||||||
|
<Compile Include="SpriteMapModifications\OriginPreset.cs" />
|
||||||
<Compile Include="SpriteMapModifications\RemoveSprite.cs" />
|
<Compile Include="SpriteMapModifications\RemoveSprite.cs" />
|
||||||
<Compile Include="SpriteMapModifications\RenameSprite.cs" />
|
<Compile Include="SpriteMapModifications\RenameSprite.cs" />
|
||||||
<EmbeddedResource Include="Form1.resx">
|
<EmbeddedResource Include="Form1.resx">
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ namespace SpriteMapEditor.SpriteMapModifications
|
|||||||
}
|
}
|
||||||
changes.Add(change);
|
changes.Add(change);
|
||||||
position = changes.Count - 1;
|
position = changes.Count - 1;
|
||||||
//Console.WriteLine("Available Undo Steps: " + changes.Count + "; Current Undo Position: " + position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Undo()
|
public void Undo()
|
||||||
@@ -51,6 +50,7 @@ namespace SpriteMapEditor.SpriteMapModifications
|
|||||||
{
|
{
|
||||||
if (CanUndo)
|
if (CanUndo)
|
||||||
{
|
{
|
||||||
|
Console.Write("Hist.Pos(" + position + "): ");
|
||||||
ModHelper.UndoAndRestoreSelection(changes[position], listBox);
|
ModHelper.UndoAndRestoreSelection(changes[position], listBox);
|
||||||
position--;
|
position--;
|
||||||
}
|
}
|
||||||
@@ -69,6 +69,7 @@ namespace SpriteMapEditor.SpriteMapModifications
|
|||||||
{
|
{
|
||||||
if (CanRedo)
|
if (CanRedo)
|
||||||
{
|
{
|
||||||
|
Console.Write("Hist.Pos(" + position + "): ");
|
||||||
ModHelper.RedoAndRestoreSelection(changes[position + 1], listBox);
|
ModHelper.RedoAndRestoreSelection(changes[position + 1], listBox);
|
||||||
position++;
|
position++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,18 +16,21 @@ namespace SpriteMapEditor.SpriteMapModifications
|
|||||||
mod.SetPreChangeSelection(GetSelectionList(listBox));
|
mod.SetPreChangeSelection(GetSelectionList(listBox));
|
||||||
mod.Do();
|
mod.Do();
|
||||||
mod.SetPostChangeSelection(GetSelectionList(listBox));
|
mod.SetPostChangeSelection(GetSelectionList(listBox));
|
||||||
|
Console.WriteLine("Perform: " + mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void UndoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
|
public static void UndoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
|
||||||
{
|
{
|
||||||
mod.Undo();
|
mod.Undo();
|
||||||
SelectFromList(listBox, mod.GetPreChangeSelection());
|
SelectFromList(listBox, mod.GetPreChangeSelection());
|
||||||
|
Console.WriteLine("Undo: " + mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RedoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
|
public static void RedoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
|
||||||
{
|
{
|
||||||
mod.Do();
|
mod.Do();
|
||||||
SelectFromList(listBox, mod.GetPostChangeSelection());
|
SelectFromList(listBox, mod.GetPostChangeSelection());
|
||||||
|
Console.WriteLine("Redo: " + mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<int> GetSelectionList(ListBox listBox)
|
public static List<int> GetSelectionList(ListBox listBox)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using QURO;
|
using QURO;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
|
||||||
namespace SpriteMapEditor.SpriteMapModifications
|
namespace SpriteMapEditor.SpriteMapModifications
|
||||||
{
|
{
|
||||||
@@ -13,15 +14,17 @@ namespace SpriteMapEditor.SpriteMapModifications
|
|||||||
|
|
||||||
private readonly int xDiff;
|
private readonly int xDiff;
|
||||||
private readonly int yDiff;
|
private readonly int yDiff;
|
||||||
|
private readonly List<Rectangle> oldBoundsList;
|
||||||
|
|
||||||
private List<int> preChangeSelection;
|
private List<int> preChangeSelection;
|
||||||
private List<int> postChangeSelection;
|
private List<int> postChangeSelection;
|
||||||
|
|
||||||
public MoveBounds(List<SpriteMapRegion> spritesToMove, int xDifference, int yDifference)
|
public MoveBounds(List<SpriteMapRegion> spritesToMove, int xDifference, int yDifference, List<Rectangle> preDragBounds = null)
|
||||||
{
|
{
|
||||||
sprites = spritesToMove.ToList();
|
sprites = spritesToMove.ToList();
|
||||||
xDiff = xDifference;
|
xDiff = xDifference;
|
||||||
yDiff = yDifference;
|
yDiff = yDifference;
|
||||||
|
oldBoundsList = preDragBounds.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Do()
|
public void Do()
|
||||||
@@ -36,12 +39,22 @@ namespace SpriteMapEditor.SpriteMapModifications
|
|||||||
}
|
}
|
||||||
public void Undo()
|
public void Undo()
|
||||||
{
|
{
|
||||||
foreach (SpriteMapRegion sprite in sprites)
|
if (oldBoundsList != null)
|
||||||
{
|
{
|
||||||
var newBounds = sprite.Bounds;
|
for(int index = 0; index < sprites.Count; index++)
|
||||||
newBounds.X -= xDiff;
|
{
|
||||||
newBounds.Y -= yDiff;
|
sprites[index].Bounds = oldBoundsList[index];
|
||||||
sprite.Bounds = newBounds;
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (SpriteMapRegion sprite in sprites)
|
||||||
|
{
|
||||||
|
var newBounds = sprite.Bounds;
|
||||||
|
newBounds.X -= xDiff;
|
||||||
|
newBounds.Y -= yDiff;
|
||||||
|
sprite.Bounds = newBounds;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +77,7 @@ namespace SpriteMapEditor.SpriteMapModifications
|
|||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return "Move Sprite Bounds";
|
return "Move Sprite Bounds: (" + xDiff + ", " + yDiff + "), New Position of "+ sprites[0].Name +": (" + (sprites[0].Bounds.X) + ", " + (sprites[0].Bounds.Y) + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SpriteMapEditor.SpriteMapModifications
|
||||||
|
{
|
||||||
|
public enum OriginPreset
|
||||||
|
{
|
||||||
|
TopLeft,
|
||||||
|
Top,
|
||||||
|
TopRight,
|
||||||
|
Left,
|
||||||
|
Center,
|
||||||
|
Right,
|
||||||
|
BottomLeft,
|
||||||
|
Bottom,
|
||||||
|
BottomRight
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user