using Newtonsoft.Json.Linq;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using System.Xml.Linq;
|
|
namespace CloudWaterNetwork
|
{
|
public class UndoRedoCommand<T>
|
{
|
private readonly Action<T> _undoAction;
|
private readonly Action<T> _redoAction;
|
private readonly T _oldValue;
|
private readonly T _newValue;
|
|
public UndoRedoCommand(T oldValue, T newValue, Action<T> undoAction, Action<T> redoAction)
|
{
|
_oldValue = oldValue;
|
_newValue = newValue;
|
_undoAction = undoAction;
|
_redoAction = redoAction;
|
}
|
|
public void Undo()
|
{
|
_undoAction?.Invoke(_oldValue);
|
}
|
|
public void Redo()
|
{
|
_redoAction?.Invoke(_newValue);
|
}
|
}
|
|
public static class MapObjectExtensions
|
{
|
private static readonly Stack<UndoRedoCommand<object>> _undoStack = new Stack<UndoRedoCommand<object>>();
|
private static readonly Stack<UndoRedoCommand<object>> _redoStack = new Stack<UndoRedoCommand<object>>();
|
|
public static void AddUndoRedoSupport(this List<MapObject> list, PropertyGrid propertyGrid)
|
{
|
propertyGrid.PropertyValueChanged += (sender, e) =>
|
{
|
if (propertyGrid.SelectedObject is MapObject obj && obj != null)
|
{
|
var propertyDescriptor = TypeDescriptor.GetProperties(obj)[e.ChangedItem.PropertyDescriptor.Name];
|
var oldValue = e.OldValue;//propertyDescriptor.GetValue(obj);
|
var newValue = e.ChangedItem.Value;
|
|
var undoAction = new Action<object>(v => propertyDescriptor.SetValue(obj, v));
|
var redoAction = new Action<object>(v => propertyDescriptor.SetValue(obj, v));
|
|
|
var command = new UndoRedoCommand<object>(oldValue, newValue, undoAction, redoAction);
|
|
_undoStack.Push(command);
|
_redoStack.Clear();
|
}
|
};
|
}
|
public static void AddCommand(UndoRedoCommand<object> command)
|
{
|
_undoStack.Push(command);
|
_redoStack.Clear();
|
}
|
public static void AddCommand(object o,string PropertyDescriptorName,object oldValue,object newValue)
|
{
|
if (o is MapObject obj && obj != null)
|
{
|
var propertyDescriptor = TypeDescriptor.GetProperties(obj)[PropertyDescriptorName];
|
|
|
var undoAction = new Action<object>(v => propertyDescriptor.SetValue(obj, v));
|
var redoAction = new Action<object>(v => propertyDescriptor.SetValue(obj, v));
|
var command = new UndoRedoCommand<object>(oldValue, newValue, undoAction, redoAction);
|
AddCommand(command);
|
}
|
//else if(o is List<MapObject> list && list != null)
|
//{
|
// List< Action<object> > list_undoAction=new List<Action<object>>();
|
// List< Action<object> > list_redoAction=new List<Action<object>>();
|
// List< PropertyDescriptor> list_properties= new List< PropertyDescriptor>();
|
// foreach ( var item in list)
|
// {
|
// var propertyDescriptor = TypeDescriptor.GetProperties(item)[PropertyDescriptorName];
|
|
|
// var undoAction0 = new Action<object>(v => propertyDescriptor.SetValue(item, v));
|
// var redoAction0 = new Action<object>(v => propertyDescriptor.SetValue(item, v));
|
// list_properties.Add(propertyDescriptor);
|
// list_undoAction.Add(undoAction0);
|
// list_redoAction.Add(redoAction0);
|
|
|
// }
|
|
// var undoAction = new Action<object>(v =>
|
// {
|
// for (int i = 0; i < list_properties.Count; i++)
|
// {
|
// propertyDescriptor.SetValue(item, v)
|
// }
|
// });
|
// var command = new UndoRedoCommand<object>(oldValue, newValue, undoAction, redoAction);
|
// AddCommand(command);
|
|
//}
|
|
}
|
|
public static void Undo()
|
{
|
if (_undoStack.Count > 0)
|
{
|
var command = _undoStack.Pop();
|
command.Undo();
|
_redoStack.Push(command);
|
}
|
Global.map.SetInvalidated();
|
Global.PropertyForm.propertyGrid.Refresh();
|
}
|
|
public static void Redo()
|
{
|
if (_redoStack.Count > 0)
|
{
|
var command = _redoStack.Pop();
|
command.Redo();
|
_undoStack.Push(command);
|
}
|
Global.map.SetInvalidated();
|
Global.PropertyForm.propertyGrid.Refresh();
|
}
|
}
|
|
|
}
|