tangxu
2024-04-27 d902834f17ff6b07537a7a0921b63cb35274533e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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();
        }
    }
 
 
}