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 oldPosition;
|
||||
private int totalDragX;
|
||||
private int totalDragY;
|
||||
|
||||
private List<Microsoft.Xna.Framework.Rectangle> preDragBounds;
|
||||
|
||||
private int drawingRectangleOffset
|
||||
{
|
||||
@@ -513,65 +513,42 @@ namespace SpriteMapEditor
|
||||
{
|
||||
hovering = true;
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (spriteSheet == null)
|
||||
@@ -584,8 +561,11 @@ namespace SpriteMapEditor
|
||||
if (GetDrawingRect(currentSprite.Bounds).Contains(mousePosition))
|
||||
{
|
||||
movingWithMouse = true;
|
||||
totalDragX = 0;
|
||||
totalDragY = 0;
|
||||
preDragBounds = new List<Microsoft.Xna.Framework.Rectangle>();
|
||||
foreach (SpriteMapRegion sprite in selectedSprites)
|
||||
{
|
||||
preDragBounds.Add(sprite.Bounds);
|
||||
}
|
||||
newPosition = mousePosition;
|
||||
newPosition.X /= zoomLevel;
|
||||
newPosition.Y /= zoomLevel;
|
||||
@@ -602,14 +582,18 @@ namespace SpriteMapEditor
|
||||
|
||||
private void spriteSheetViewer_MouseUp(object sender, MouseEventArgs e)
|
||||
{
|
||||
movingWithMouse = false;
|
||||
if(totalDragX != 0 || totalDragY != 0)
|
||||
if (movingWithMouse)
|
||||
{
|
||||
var modification = new SpriteMapModifications.MoveBounds(selectedSprites, totalDragX, totalDragY);
|
||||
var selection = SpriteMapModifications.ModHelper.GetSelectionList(spriteList);
|
||||
modification.SetPreChangeSelection(selection);
|
||||
modification.SetPostChangeSelection(selection);
|
||||
undoHistory.Add(modification);
|
||||
movingWithMouse = false;
|
||||
var totalDragX = selectedSprites[0].Bounds.X - preDragBounds[0].X;
|
||||
var totalDragY = selectedSprites[0].Bounds.Y - preDragBounds[0].Y;
|
||||
if (totalDragX != 0 || totalDragY != 0)
|
||||
{
|
||||
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\MoveBounds.cs" />
|
||||
<Compile Include="SpriteMapModifications\MoveSpriteListEntry.cs" />
|
||||
<Compile Include="SpriteMapModifications\OriginPreset.cs" />
|
||||
<Compile Include="SpriteMapModifications\RemoveSprite.cs" />
|
||||
<Compile Include="SpriteMapModifications\RenameSprite.cs" />
|
||||
<EmbeddedResource Include="Form1.resx">
|
||||
|
||||
@@ -35,7 +35,6 @@ namespace SpriteMapEditor.SpriteMapModifications
|
||||
}
|
||||
changes.Add(change);
|
||||
position = changes.Count - 1;
|
||||
//Console.WriteLine("Available Undo Steps: " + changes.Count + "; Current Undo Position: " + position);
|
||||
}
|
||||
|
||||
public void Undo()
|
||||
@@ -51,6 +50,7 @@ namespace SpriteMapEditor.SpriteMapModifications
|
||||
{
|
||||
if (CanUndo)
|
||||
{
|
||||
Console.Write("Hist.Pos(" + position + "): ");
|
||||
ModHelper.UndoAndRestoreSelection(changes[position], listBox);
|
||||
position--;
|
||||
}
|
||||
@@ -69,6 +69,7 @@ namespace SpriteMapEditor.SpriteMapModifications
|
||||
{
|
||||
if (CanRedo)
|
||||
{
|
||||
Console.Write("Hist.Pos(" + position + "): ");
|
||||
ModHelper.RedoAndRestoreSelection(changes[position + 1], listBox);
|
||||
position++;
|
||||
}
|
||||
|
||||
@@ -16,18 +16,21 @@ namespace SpriteMapEditor.SpriteMapModifications
|
||||
mod.SetPreChangeSelection(GetSelectionList(listBox));
|
||||
mod.Do();
|
||||
mod.SetPostChangeSelection(GetSelectionList(listBox));
|
||||
Console.WriteLine("Perform: " + mod);
|
||||
}
|
||||
|
||||
public static void UndoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
|
||||
{
|
||||
mod.Undo();
|
||||
SelectFromList(listBox, mod.GetPreChangeSelection());
|
||||
Console.WriteLine("Undo: " + mod);
|
||||
}
|
||||
|
||||
public static void RedoAndRestoreSelection(SpriteMapModifications.ISpriteMapModification mod, ListBox listBox)
|
||||
{
|
||||
mod.Do();
|
||||
SelectFromList(listBox, mod.GetPostChangeSelection());
|
||||
Console.WriteLine("Redo: " + mod);
|
||||
}
|
||||
|
||||
public static List<int> GetSelectionList(ListBox listBox)
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using QURO;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace SpriteMapEditor.SpriteMapModifications
|
||||
{
|
||||
@@ -13,15 +14,17 @@ namespace SpriteMapEditor.SpriteMapModifications
|
||||
|
||||
private readonly int xDiff;
|
||||
private readonly int yDiff;
|
||||
private readonly List<Rectangle> oldBoundsList;
|
||||
|
||||
private List<int> preChangeSelection;
|
||||
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();
|
||||
xDiff = xDifference;
|
||||
yDiff = yDifference;
|
||||
oldBoundsList = preDragBounds.ToList();
|
||||
}
|
||||
|
||||
public void Do()
|
||||
@@ -36,12 +39,22 @@ namespace SpriteMapEditor.SpriteMapModifications
|
||||
}
|
||||
public void Undo()
|
||||
{
|
||||
foreach (SpriteMapRegion sprite in sprites)
|
||||
if (oldBoundsList != null)
|
||||
{
|
||||
var newBounds = sprite.Bounds;
|
||||
newBounds.X -= xDiff;
|
||||
newBounds.Y -= yDiff;
|
||||
sprite.Bounds = newBounds;
|
||||
for(int index = 0; index < sprites.Count; index++)
|
||||
{
|
||||
sprites[index].Bounds = oldBoundsList[index];
|
||||
}
|
||||
}
|
||||
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()
|
||||
{
|
||||
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