-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathRoleEditor.xaml.cs
More file actions
123 lines (105 loc) · 3.69 KB
/
RoleEditor.xaml.cs
File metadata and controls
123 lines (105 loc) · 3.69 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Torch.Server.Managers;
using Torch.Views;
using VRage.Game.ModAPI;
namespace Torch.Server.Views
{
/// <summary>
/// Interaction logic for RoleEditor.xaml
/// </summary>
public partial class RoleEditor : Window
{
public RoleEditor()
{
InitializeComponent();
DataContext = Items;
}
public ObservableCollection<IDictionaryItem> Items { get; } = new ObservableCollection<IDictionaryItem>();
private Type _itemType;
private Action _commitChanges;
public MyPromoteLevel BulkPromote { get; set; } = MyPromoteLevel.Scripter;
public void Edit(IDictionary dict)
{
Items.Clear();
var dictType = dict.GetType();
_itemType = typeof(DictionaryItem<,>).MakeGenericType(dictType.GenericTypeArguments[0], dictType.GenericTypeArguments[1]);
foreach (var key in dict.Keys)
{
Items.Add((IDictionaryItem)Activator.CreateInstance(_itemType, key, dict[key]));
}
ItemGrid.ItemsSource = Items;
_commitChanges = () =>
{
dict.Clear();
foreach (var item in Items)
{
dict[item.Key] = item.Value;
}
};
Show();
}
private void Cancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void Ok_OnClick(object sender, RoutedEventArgs e)
{
_commitChanges?.Invoke();
Close();
}
public interface IDictionaryItem
{
object Key { get; set; }
object Value { get; set; }
}
public class DictionaryItem<TKey, TValue> : ViewModel, IDictionaryItem
{
private TKey _key;
private TValue _value;
object IDictionaryItem.Key { get => _key; set => SetValue(ref _key, (TKey)value); }
object IDictionaryItem.Value { get => _value; set => SetValue(ref _value, (TValue)value); }
public TKey Key { get => _key; set => SetValue(ref _key, value); }
public TValue Value { get => _value; set => SetValue(ref _value, value); }
public DictionaryItem()
{
_key = default(TKey);
_value = default(TValue);
}
public DictionaryItem(TKey key, TValue value)
{
_key = key;
_value = value;
}
}
private void AddNew_OnClick(object sender, RoutedEventArgs e)
{
Items.Add((IDictionaryItem)Activator.CreateInstance(_itemType));
}
private void BulkEdit(object sender, RoutedEventArgs e)
{
List<ulong> l = Items.Where(i => i.Value.Equals(BulkPromote)).Select(i => (ulong)i.Key).ToList();
var w = new CollectionEditor();
w.Edit((ICollection<ulong>)l, "Bulk edit");
var r = Items.Where(j => j.Value.Equals(BulkPromote) || l.Contains((ulong)j.Key)).ToList();
foreach (var k in r)
Items.Remove(k);
foreach (var m in l)
Items.Add(new DictionaryItem<ulong, MyPromoteLevel>(m, BulkPromote));
}
}
}