qinjie
2023-12-19 15d15d24fbccb9b70a305b46b71453b2ab1a720e
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Hydro.Inp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Hydro.MapView.Common
{
    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<IBaseViewModel> list, PropertyGrid propertyGrid)
        {
            propertyGrid.PropertyValueChanged += (sender, e) =>
            {
                if (propertyGrid.SelectedObject is IBaseViewModel 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();
        }
        private static List<IBaseViewModel> getObj(object o)
        {
            var list = o as List<IBaseViewModel>;
            if (list == null)
            {
                if (o is List<NodeViewModel> list_nodes) list = new List<IBaseViewModel>(list_nodes);
                if (o is List<LinkViewModel> list_links) list = new List<IBaseViewModel>(list_links);
            }
            return list;
        }
        public static void AddCommand(object o, string PropertyDescriptorName, dynamic oldValue, dynamic newValue)
        {
            switch (PropertyDescriptorName)
            {
                case "Add":
                    {
 
                        var net = o as MapViewNetWork;
                        var undoAction = new Action<object>(v => net.Remove(newValue));
                        var redoAction = new Action<object>(v => net.Add(newValue));
                        //object oldValue=propertyDescriptor.GetValue(obj);
                        var command = new UndoRedoCommand<object>(oldValue, newValue, undoAction, redoAction);
                        AddCommand(command);
                    }
                    return;
                case "Remove":
                    {
                        var net = o as MapViewNetWork;
                        var undoAction = new Action<object>(v => net.Add(newValue));
                        var redoAction = new Action<object>(v => net.Remove(newValue));
                        //object oldValue=propertyDescriptor.GetValue(obj);
                        var command = new UndoRedoCommand<object>(oldValue, newValue, undoAction, redoAction);
                        AddCommand(command);
                    }
                    return;
                case "Map":
                    {
                        var mv = o as MapDimensions;
                        var undoAction = new Action<object>(v =>
                        {
                            mv.Center = oldValue.Center;
                            mv.zoom = oldValue.zoom;
                            mv.rotation = oldValue.rotation;
                            mv.rotationF = oldValue.rotationF;
 
                        });
                        var redoAction = new Action<object>(v =>
                        {
                            mv.Center = newValue.Center;
                            mv.zoom = newValue.zoom;
                            mv.rotation = newValue.rotation;
                            mv.rotationF = newValue.rotationF;
 
                        });
                        //object oldValue=propertyDescriptor.GetValue(obj);
                        var command = new UndoRedoCommand<object>(oldValue, newValue, undoAction, redoAction);
                        AddCommand(command);
                    }
                    return;
            }
 
 
            if (o is IBaseViewModel obj && obj != null)
            {
                var propertyDescriptor = TypeDescriptor.GetProperties(obj)[PropertyDescriptorName];
                if (propertyDescriptor == null) return;
                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<IBaseViewModel> || o is List<NodeViewModel>)
            {
                var list = getObj(o);
                //List<Action<object>> list_undoAction = new List<Action<object>>();
                //List<Action<object>> list_redoAction = new List<Action<object>>();
                List<UndoRedoCommand<object>> list_command = new List<UndoRedoCommand<object>>();
                PropertyDescriptor propertyDescriptor = null;
                //List<object> oldValues = oldValue as List<object>;
                //List<object> newValues = newValue as List<object>;
                for (int i = 0; i < list.Count; i++)
                {
                    var item = list[i];
                    if (propertyDescriptor == null) propertyDescriptor = TypeDescriptor.GetProperties(item)[PropertyDescriptorName];
                    if (propertyDescriptor == null) continue;
                    IBaseViewModel obj0 = (IBaseViewModel)item;
                    var undoAction0 = new Action<object>(v => propertyDescriptor.SetValue(obj0, v));
                    var redoAction0 = new Action<object>(v => propertyDescriptor.SetValue(obj0, v));
                    //object oldValue = propertyDescriptor.GetValue(obj0);
 
                    var command0 = new UndoRedoCommand<object>(oldValue[i], newValue[i], undoAction0, redoAction0);
                    list_command.Add(command0);
                    //list_undoAction.Add(undoAction0);
                    //list_redoAction.Add(redoAction0);
 
 
                }
 
                var undoAction = new Action<object>(v =>
                {
                    for (int i = 0; i < list_command.Count; i++)
                    {
                        list_command[i].Undo();
                    }
                });
 
                var redoAction = new Action<object>(v =>
                {
                    for (int i = 0; i < list_command.Count; i++)
                    {
                        list_command[i].Redo();
                    }
                });
                var command = new UndoRedoCommand<object>(null, null, undoAction, redoAction);
                AddCommand(command);
 
            }
 
        }
 
        public static void Undo()
        {
            if (_undoStack.Count > 0)
            {
                var command = _undoStack.Pop();
                command.Undo();
                _redoStack.Push(command);
            }
 
        }
 
        public static void Redo()
        {
            if (_redoStack.Count > 0)
            {
                var command = _redoStack.Pop();
                command.Redo();
                _undoStack.Push(command);
            }
 
        }
    }
}