-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathEditableList.cs
273 lines (228 loc) · 9.19 KB
/
EditableList.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
namespace TextAdventures.Quest
{
public class EditableList<T> : IEditableList<T>, IDataWrapper, INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event EventHandler<EditableListUpdatedEventArgs<T>> Added;
public event EventHandler<EditableListUpdatedEventArgs<T>> Removed;
// currently unused
public event EventHandler<DataWrapperUpdatedEventArgs> UnderlyingValueUpdated
{
add { }
remove { }
}
#region Static DataWrapper
private static EditableDataWrapper<QuestList<T>, EditableList<T>> s_wrapper;
static EditableList()
{
s_wrapper = new EditableDataWrapper<QuestList<T>, EditableList<T>>(GetNewInstance);
}
public static EditableList<T> GetInstance(EditorController controller, QuestList<T> list)
{
return s_wrapper.GetInstance(controller, list);
}
private static EditableList<T> GetNewInstance(EditorController controller, QuestList<T> list)
{
return new EditableList<T>(controller, list);
}
public static void Clear()
{
s_wrapper.Clear();
}
#endregion
private QuestList<T> m_source;
private Dictionary<string, IEditableListItem<T>> m_wrappedItems = new Dictionary<string, IEditableListItem<T>>();
private List<IEditableListItem<T>> m_wrappedItemsList = new List<IEditableListItem<T>>();
private List<string> m_wrappedItemKeys = new List<string>();
private int m_nextId = 0;
private EditorController m_controller;
public EditableList(EditorController controller, QuestList<T> source)
{
m_controller = controller;
m_source = source;
m_source.Added += m_source_Added;
m_source.Removed += m_source_Removed;
PopulateWrappedItems();
}
public IDictionary<string, IEditableListItem<T>> Items
{
get { return m_wrappedItems; }
}
private void PopulateWrappedItems()
{
m_wrappedItems.Clear();
m_wrappedItemsList.Clear();
m_wrappedItemKeys.Clear();
int index = 0;
foreach (var item in m_source)
{
AddWrappedItem(item, EditorUpdateSource.System, index);
index++;
}
}
public void Add(T item)
{
string undoEntry = null;
if (typeof(T) == typeof(string))
{
undoEntry = string.Format("Add '{0}'", item as string);
}
if (undoEntry == null)
{
throw new InvalidOperationException("Unknown list type");
}
m_controller.WorldModel.UndoLogger.StartTransaction(undoEntry);
AddInternal(item);
m_controller.WorldModel.UndoLogger.EndTransaction();
}
internal void AddInternal(T item)
{
m_source.Add(item, UpdateSource.User);
}
private void AddWrappedItem(T item, EditorUpdateSource source, int index)
{
string key = GetUniqueId();
IEditableListItem<T> wrappedValue = new EditableListItem<T>(key, item);
m_wrappedItems.Add(key, wrappedValue);
m_wrappedItemsList.Insert(index, wrappedValue);
m_wrappedItemKeys.Insert(index, key);
if (Added != null) Added(this, new EditableListUpdatedEventArgs<T> { UpdatedItem = wrappedValue, Index = index, Source = source });
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, wrappedValue, index));
}
private string GetUniqueId()
{
m_nextId++;
return "k" + m_nextId.ToString();
}
public void Remove(params string[] keys)
{
string undoEntry = null;
if (typeof(T) == typeof(string))
{
undoEntry = string.Format("Remove items from list");
}
if (undoEntry == null)
{
throw new InvalidOperationException("Unknown list type");
}
m_controller.WorldModel.UndoLogger.StartTransaction(undoEntry);
foreach (string key in keys)
{
m_source.RemoveByIndex(m_wrappedItemKeys.IndexOf(key), UpdateSource.User);
}
m_controller.WorldModel.UndoLogger.EndTransaction();
}
public void Update(int index, T item)
{
string undoEntry = null;
if (typeof(T) == typeof(string))
{
undoEntry = string.Format("Update '{0}'", item as string);
}
if (undoEntry == null)
{
throw new InvalidOperationException("Unknown list type");
}
m_controller.WorldModel.UndoLogger.StartTransaction(undoEntry);
m_source.Remove(m_source[index], UpdateSource.User, index);
m_source.Add(item, UpdateSource.User, index);
m_controller.WorldModel.UndoLogger.EndTransaction();
}
public void Update(string key, T item)
{
Update(m_wrappedItemKeys.IndexOf(key), item);
}
private void RemoveWrappedItem(IEditableListItem<T> item, EditorUpdateSource source, int index)
{
m_wrappedItems.Remove(item.Key);
m_wrappedItemsList.Remove(item);
m_wrappedItemKeys.Remove(item.Key);
if (Removed != null) Removed(this, new EditableListUpdatedEventArgs<T> { UpdatedItem = item, Index = index, Source = source });
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
}
void m_source_Added(object sender, QuestListUpdatedEventArgs<T> e)
{
AddWrappedItem(e.UpdatedItem, (EditorUpdateSource)e.Source, e.Index);
}
void m_source_Removed(object sender, QuestListUpdatedEventArgs<T> e)
{
RemoveWrappedItem(m_wrappedItems[m_wrappedItemKeys[e.Index]], (EditorUpdateSource)e.Source, e.Index);
}
public object GetUnderlyingValue()
{
return m_source;
}
public IEnumerable<KeyValuePair<string, string>> DisplayItems
{
get
{
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (var item in m_wrappedItems)
{
// TO DO: We will need some kind of projection function for non-string T's
result.Add(item.Key, item.Value.Value as string);
}
return result;
}
}
public ValidationResult CanAdd(T item)
{
// Commented this section out as it is valid to have the same item multiple times in a list,
// for example for walkthroughs.
//if (m_source.Contains(item))
//{
// return new ValidationResult { Valid = false, Message = ValidationMessage.ItemAlreadyExists };
//}
return new ValidationResult { Valid = true };
}
public string DisplayString()
{
throw new NotImplementedException();
}
public bool Locked
{
get
{
return m_source.Locked;
}
}
public IEditableList<T> Clone(string parent, string attribute)
{
IEditableList<T> result;
m_controller.WorldModel.UndoLogger.StartTransaction(string.Format("Copy '{0}' {1}", parent, attribute));
result = CloneInternal(m_controller.WorldModel.Elements.Get(parent), attribute);
m_controller.WorldModel.UndoLogger.EndTransaction();
return result;
}
private IEditableList<T> CloneInternal(Element parent, string attribute)
{
QuestList<T> newSource = (QuestList<T>)m_source.Clone();
newSource.Locked = false;
parent.Fields.Set(attribute, newSource);
newSource = (QuestList<T>)parent.Fields.Get(attribute);
return EditableList<T>.GetNewInstance(m_controller, newSource);
}
public IEnumerator GetEnumerator()
{
return m_wrappedItemsList.GetEnumerator();
}
public string Owner
{
get
{
if (m_source.Owner == null) return null;
return m_source.Owner.Name;
}
}
public IEnumerable<IEditableListItem<T>> ItemsList
{
get { return m_wrappedItemsList; }
}
}
}