From 92249cac8f9c24602aaae8f44b8eae675f6178ae Mon Sep 17 00:00:00 2001 From: quinnvoker Date: Sat, 1 Dec 2018 14:31:13 -0800 Subject: [PATCH] Added SpriteMapModifications for use in Form1 --- SpriteMapEditor/Form1.cs | 2 +- SpriteMapEditor/SpriteMapEditor.csproj | 7 ++ .../SpriteMapModifications/AddSprite.cs | 36 ++++++++++ .../ISpriteModification.cs | 14 ++++ .../SpriteMapModifications/ModifyBounds.cs | 65 +++++++++++++++++++ .../SpriteMapModifications/ModifyOrigin.cs | 56 ++++++++++++++++ .../SpriteMapModifications/MoveBounds.cs | 45 +++++++++++++ .../MoveSpriteListEntry.cs | 39 +++++++++++ .../SpriteMapModifications/RemoveSprite.cs | 36 ++++++++++ 9 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 SpriteMapEditor/SpriteMapModifications/AddSprite.cs create mode 100644 SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs create mode 100644 SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs create mode 100644 SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs create mode 100644 SpriteMapEditor/SpriteMapModifications/MoveBounds.cs create mode 100644 SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs create mode 100644 SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs diff --git a/SpriteMapEditor/Form1.cs b/SpriteMapEditor/Form1.cs index 8d795b2..ab4b2a9 100644 --- a/SpriteMapEditor/Form1.cs +++ b/SpriteMapEditor/Form1.cs @@ -13,6 +13,7 @@ using Microsoft.Xna; using System.IO; using QURO; using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate; +using SpriteMapEditor.SpriteMapModifications; namespace SpriteMapEditor @@ -23,7 +24,6 @@ namespace SpriteMapEditor private List selectedSprites; private bool highlight = false; - //private SpriteMapRegion currentSprite { get { return sprites[spriteList.SelectedIndices[0]]; } } private int zoomLevel = 3; private bool loadingSprite = false; diff --git a/SpriteMapEditor/SpriteMapEditor.csproj b/SpriteMapEditor/SpriteMapEditor.csproj index 914dae8..1a01be6 100644 --- a/SpriteMapEditor/SpriteMapEditor.csproj +++ b/SpriteMapEditor/SpriteMapEditor.csproj @@ -58,11 +58,18 @@ Form1.cs + + + Component + + + + Form1.cs diff --git a/SpriteMapEditor/SpriteMapModifications/AddSprite.cs b/SpriteMapEditor/SpriteMapModifications/AddSprite.cs new file mode 100644 index 0000000..eaff42c --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/AddSprite.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel; +using QURO; + +namespace SpriteMapEditor.SpriteMapModifications +{ + class AddSprite : ISpriteMapModification + { + private readonly BindingList allSprites; + + private readonly SpriteMapRegion selectedSprite; + + public AddSprite(BindingList spriteList, SpriteMapRegion selected = null) + { + allSprites = spriteList; + selectedSprite = selected; + } + + public void Do() + { + if (selectedSprite != null) + allSprites.Add(new SpriteMapRegion(selectedSprite.Name, selectedSprite.Bounds, selectedSprite.Origin)); + else + allSprites.Add(new SpriteMapRegion()); + } + + public void Undo() + { + allSprites.RemoveAt(allSprites.Count - 1); + } + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs b/SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs new file mode 100644 index 0000000..84f7315 --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/ISpriteModification.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SpriteMapEditor.SpriteMapModifications +{ + interface ISpriteMapModification + { + void Do(); + void Undo(); + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs b/SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs new file mode 100644 index 0000000..d039562 --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/ModifyBounds.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using QURO; +using Microsoft.Xna.Framework; + +namespace SpriteMapEditor.SpriteMapModifications +{ + class ModifyBounds : ISpriteMapModification + { + private readonly List sprites; + + private readonly int newXPosition; + private readonly int newYPosition; + private readonly int newWidth; + private readonly int newHeight; + + private readonly List oldBounds; + + public ModifyBounds(List spritesToModify, int x = -1, int y = -1, int width = -1, int height = -1) + { + sprites = spritesToModify; + newXPosition = x; + newYPosition = y; + newWidth = width; + newHeight = height; + + oldBounds = new List(); + + for(int index = 0; index < sprites.Count; index++) + { + oldBounds.Add(sprites[index].Bounds); + } + } + + public void Do() + { + for (int index = 0; index < sprites.Count; index++) + { + Rectangle newBounds = sprites[index].Bounds; + + if (newXPosition > -1) + newBounds.X = newXPosition; + if (newYPosition > -1) + newBounds.Y = newYPosition; + if (newWidth > -1) + newBounds.Width = newWidth; + if (newHeight > -1) + newBounds.Height = newHeight; + + sprites[index].Bounds = newBounds; + } + } + + public void Undo() + { + for (int index = 0; index < sprites.Count; index++) + { + sprites[index].Bounds = oldBounds[index]; + } + } + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs b/SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs new file mode 100644 index 0000000..a35d0bd --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/ModifyOrigin.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using QURO; +using Microsoft.Xna.Framework; + +namespace SpriteMapEditor.SpriteMapModifications +{ + class ModifyOrigin : ISpriteMapModification + { + private readonly List sprites; + + private readonly int newOriginX; + private readonly int newOriginY; + + private readonly List oldOrigins; + + public ModifyOrigin(List spritesToEditOrigin, int x = -1, int y = -1) + { + sprites = spritesToEditOrigin; + newOriginX = x; + newOriginY = y; + + oldOrigins = new List(); + for(int index = 0; index < sprites.Count; index++) + { + oldOrigins.Add(sprites[index].Origin); + } + } + + public void Do() + { + for (int index = 0; index < sprites.Count; index++) + { + var newOrigin = sprites[index].Origin; + + if (newOriginX > -1) + newOrigin.X = newOriginX; + if (newOriginY > -1) + newOrigin.Y = newOriginY; + + sprites[index].Origin = newOrigin; + } + } + + public void Undo() + { + for (int index = 0; index < sprites.Count; index++) + { + sprites[index].Origin = oldOrigins[index]; + } + } + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/MoveBounds.cs b/SpriteMapEditor/SpriteMapModifications/MoveBounds.cs new file mode 100644 index 0000000..31c6b66 --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/MoveBounds.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using QURO; + +namespace SpriteMapEditor.SpriteMapModifications +{ + class MoveBounds : ISpriteMapModification + { + private readonly List sprites; + + private readonly int xDiff; + private readonly int yDiff; + + public MoveBounds(List spritesToMove, int xDifference, int yDifference) + { + sprites = spritesToMove; + xDiff = xDifference; + yDiff = yDifference; + } + + public void Do() + { + foreach (SpriteMapRegion sprite in sprites) + { + var newBounds = sprite.Bounds; + newBounds.X += xDiff; + newBounds.Y += yDiff; + sprite.Bounds = newBounds; + } + } + public void Undo() + { + foreach (SpriteMapRegion sprite in sprites) + { + var newBounds = sprite.Bounds; + newBounds.X -= xDiff; + newBounds.Y -= yDiff; + sprite.Bounds = newBounds; + } + } + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs b/SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs new file mode 100644 index 0000000..3175bdf --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/MoveSpriteListEntry.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using QURO; +using System.ComponentModel; + +namespace SpriteMapEditor.SpriteMapModifications +{ + class MoveSpriteListEntry : ISpriteMapModification + { + private readonly BindingList allSprites; + + private readonly int selectedIndex; + private readonly int dir; + + public MoveSpriteListEntry(BindingList spriteList, int selection, int direction) + { + allSprites = spriteList; + selectedIndex = selection; + dir = Math.Sign(direction); + } + + public void Do() + { + var movingSprite = allSprites[selectedIndex]; + allSprites.RemoveAt(selectedIndex); + allSprites.Insert(selectedIndex + dir, movingSprite); + } + + public void Undo() + { + var movingSprite = allSprites[selectedIndex + dir]; + allSprites.RemoveAt(selectedIndex + dir); + allSprites.Insert(selectedIndex, movingSprite); + } + } +} diff --git a/SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs b/SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs new file mode 100644 index 0000000..dda5f19 --- /dev/null +++ b/SpriteMapEditor/SpriteMapModifications/RemoveSprite.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using QURO; +using System.ComponentModel; + +namespace SpriteMapEditor.SpriteMapModifications +{ + class RemoveSprite : ISpriteMapModification + { + private readonly BindingList allSprites; + + private readonly int index; + + private readonly SpriteMapRegion removedSprite; + + public RemoveSprite(BindingList spriteList, int indexToRemove) + { + allSprites = spriteList; + index = indexToRemove; + removedSprite = allSprites[index]; + } + + public void Do() + { + allSprites.RemoveAt(index); + } + + public void Undo() + { + allSprites.Insert(index, removedSprite); + } + } +}