using System.Collections.Generic;
namespace PaintDotNet.Annotation
{
///
/// 管理类,可用于撤销、恢复
///
public class UndoManager
{
///
/// 每个画布对应的标注、测量、视场、其它工具的集合
///
GraphicsList graphicsList;
///
/// 命令的集合
///
List historyList;
///
/// 命令位置标记
///
int nextUndo;
public UndoManager(GraphicsList graphicsList)
{
this.graphicsList = graphicsList;
ClearHistory();
}
public void RemoveCommandFromHistory(DrawObject o)
{
}
///
/// Return true if Undo operation is available
///
public bool CanUndo
{
get
{
// If the NextUndo pointer is -1, no commands to undo
if (nextUndo < 0 ||
nextUndo > historyList.Count - 1) // precaution
{
return false;
}
return true;
}
}
///
/// Return true if Redo operation is available
///
public bool CanRedo
{
get
{
// If the NextUndo pointer points to the last item, no commands to redo
if (nextUndo == historyList.Count - 1)
{
return false;
}
return true;
}
}
///
/// Clear History
///
public void ClearHistory()
{
historyList = new List();
nextUndo = -1;
}
///
/// Add new command to history.
/// Called by client after executing some action.
///
///
public void AddCommandToHistory(Command.Command command)
{
// Purge history list
this.TrimHistoryList();
// Add command and increment undo counter
historyList.Add(command);
nextUndo++;
}
///
/// Undo
///
public void Undo()
{
if (!CanUndo)
{
return;
}
// Get the Command object to be undone
Command.Command command = historyList[nextUndo];
// Execute the Command object's undo method
command.Undo(graphicsList);
// Move the pointer up one item
nextUndo--;
}
///
/// Redo
///
public void Redo()
{
if (!CanRedo)
{
return;
}
// Get the Command object to redo
int itemToRedo = nextUndo + 1;
Command.Command command = historyList[itemToRedo];
// Execute the Command object
command.Redo(graphicsList);
// Move the undo pointer down one item
nextUndo++;
}
private void TrimHistoryList()
{
// We can redo any undone command until we execute a new
// command. The new command takes us off in a new direction,
// which means we can no longer redo previously undone actions.
// So, we purge all undone commands from the history list.*/
// Exit if no items in History list
if (historyList.Count == 0)
{
return;
}
// Exit if NextUndo points to last item on the list
if (nextUndo == historyList.Count - 1)
{
return;
}
// Purge all items below the NextUndo pointer
for (int i = historyList.Count - 1; i > nextUndo; i--)
{
historyList.RemoveAt(i);
}
}
}
}