1
0
mirror of synced 2024-12-04 20:08:00 +01:00
Switch-Toolbox/File_Format_Library/GUI/BFLYT/LayoutUndoManager.cs
KillzXGaming 99d0986e87 More layout editor features and fixes.
Add option to create null panes.
Fix pane lookup table to properly adjust when a pane is renamed.
Adjust selection transform for rotation.
Some undo fixes for removed panes.
2019-10-05 21:15:19 -04:00

127 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Syroot.Maths;
namespace LayoutBXLYT
{
public class LayoutUndoManager
{
protected Stack<IRevertAction> undoStack = new Stack<IRevertAction>();
protected Stack<IRevertAction> redoStack = new Stack<IRevertAction>();
public bool HasUndo => undoStack.Count > 0;
public bool HasRedo => redoStack.Count > 0;
public void Undo()
{
if (undoStack.Count > 0)
redoStack.Push(undoStack.Pop().Revert());
}
public void Redo()
{
if (redoStack.Count > 0)
undoStack.Push(redoStack.Pop().Revert());
}
public void AddToUndo(IRevertAction revertable)
{
undoStack.Push(revertable);
redoStack.Clear();
}
public interface IRevertAction
{
IRevertAction Revert();
}
public class UndoActionPaneHide : IRevertAction
{
private bool hidePane = true;
public List<BasePane> targetPanes = new List<BasePane>();
public UndoActionPaneHide(List<BasePane> panes, bool hide = true)
{
targetPanes = panes;
hidePane = hide;
foreach (var pane in targetPanes)
pane.DisplayInEditor = !hidePane;
}
public IRevertAction Revert()
{
foreach (var pane in targetPanes)
pane.DisplayInEditor = hidePane;
return this;
}
}
public class UndoActionPaneDelete : IRevertAction
{
public BxlytHeader layoutFile;
public List<PaneInfo> targetPanes = new List<PaneInfo>();
public UndoActionPaneDelete(List<BasePane> panes, BxlytHeader header)
{
layoutFile = header;
for (int i = 0; i < panes.Count; i++)
targetPanes.Add(new PaneInfo(panes[i], header.PaneLookup[panes[i].Parent.Name]));
}
public IRevertAction Revert()
{
for (int i = 0; i < targetPanes.Count; i++)
layoutFile.AddPane(targetPanes[i].Pane, targetPanes[i].Parent);
return this;
}
public class PaneInfo
{
public BasePane Parent;
public BasePane Pane;
public PaneInfo(BasePane pane, BasePane parent)
{
Pane = pane;
Parent = parent;
}
}
}
public class UndoActionTransform : IRevertAction
{
public Vector3F Translate;
public Vector3F Rotate;
public Vector2F Scale;
public float Width;
public float Height;
public BasePane targetPane;
public UndoActionTransform(BasePane pane)
{
targetPane = pane;
Translate = pane.Translate;
Scale = pane.Scale;
Rotate = pane.Rotate;
Width = pane.Width;
Height = pane.Height;
}
public IRevertAction Revert()
{
targetPane.Translate = Translate;
targetPane.Scale = Scale;
targetPane.Rotate = Rotate;
targetPane.Width = Width;
targetPane.Height = Height;
return this;
}
}
}
}