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 { /// /// Interaction logic for RoleEditor.xaml /// public partial class RoleEditor : Window { public RoleEditor() { InitializeComponent(); DataContext = Items; } public ObservableCollection Items { get; } = new ObservableCollection(); 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 : ViewModel, IDictionaryItem { object IDictionaryItem.Key { get; set; } object IDictionaryItem.Value { get; set; } public TKey Key { get; set; } public TValue Value { get; set; } 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 l = Items.Where(i => i.Value.Equals(BulkPromote)).Select(i => (ulong)i.Key).ToList(); var w = new CollectionEditor(); w.Edit((ICollection)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(m, BulkPromote)); } } }