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 { private readonly Action _undoAction; private readonly Action _redoAction; private readonly T _oldValue; private readonly T _newValue; public UndoRedoCommand(T oldValue, T newValue, Action undoAction, Action 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> _undoStack = new Stack>(); private static readonly Stack> _redoStack = new Stack>(); public static void AddUndoRedoSupport(this List 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(v => propertyDescriptor.SetValue(obj, v)); var redoAction = new Action(v => propertyDescriptor.SetValue(obj, v)); var command = new UndoRedoCommand(oldValue, newValue, undoAction, redoAction); _undoStack.Push(command); _redoStack.Clear(); } }; } public static void AddCommand(UndoRedoCommand 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(v => propertyDescriptor.SetValue(obj, v)); var redoAction = new Action(v => propertyDescriptor.SetValue(obj, v)); var command = new UndoRedoCommand(oldValue, newValue, undoAction, redoAction); AddCommand(command); } //else if(o is List list && list != null) //{ // List< Action > list_undoAction=new List>(); // List< Action > list_redoAction=new List>(); // List< PropertyDescriptor> list_properties= new List< PropertyDescriptor>(); // foreach ( var item in list) // { // var propertyDescriptor = TypeDescriptor.GetProperties(item)[PropertyDescriptorName]; // var undoAction0 = new Action(v => propertyDescriptor.SetValue(item, v)); // var redoAction0 = new Action(v => propertyDescriptor.SetValue(item, v)); // list_properties.Add(propertyDescriptor); // list_undoAction.Add(undoAction0); // list_redoAction.Add(redoAction0); // } // var undoAction = new Action(v => // { // for (int i = 0; i < list_properties.Count; i++) // { // propertyDescriptor.SetValue(item, v) // } // }); // var command = new UndoRedoCommand(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(); } } }