diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj
index b5352d4..29d220e 100644
--- a/Torch/Torch.csproj
+++ b/Torch/Torch.csproj
@@ -260,6 +260,9 @@
DictionaryEditor.xaml
+
+ ObjectCollectionEditor.xaml
+
PropertyGrid.xaml
@@ -284,6 +287,10 @@
MSBuild:Compile
Designer
+
+ Designer
+ MSBuild:Compile
+
MSBuild:Compile
Designer
diff --git a/Torch/Views/CollectionEditor.xaml.cs b/Torch/Views/CollectionEditor.xaml.cs
index e1b81f8..d2e3bac 100644
--- a/Torch/Views/CollectionEditor.xaml.cs
+++ b/Torch/Views/CollectionEditor.xaml.cs
@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
+using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
@@ -21,11 +22,33 @@ namespace Torch.Views
///
public partial class CollectionEditor : Window
{
+ private static readonly Dictionary MethodCache = new Dictionary();
+ private static readonly MethodInfo EditMethod;
+
public CollectionEditor()
{
InitializeComponent();
}
+ static CollectionEditor()
+ {
+ var m = typeof(CollectionEditor).GetMethods();
+ EditMethod = m.First(mt => mt.Name == "Edit" && mt.GetGenericArguments().Length == 1);
+ }
+
+ public void Edit(ICollection collection, string name)
+ {
+ var gt = collection.GetType().GenericTypeArguments[0];
+ MethodInfo gm;
+ if (!MethodCache.TryGetValue(gt, out gm))
+ {
+ gm = EditMethod.MakeGenericMethod(gt);
+ MethodCache.Add(gt, gm);
+ }
+
+ gm.Invoke(this, new object[] {collection, name});
+ }
+
public void Edit(ICollection collection, string name)
{
ItemList.Text = string.Join("\r\n", collection.Select(x => x.ToString()));
diff --git a/Torch/Views/ObjectCollectionEditor.xaml b/Torch/Views/ObjectCollectionEditor.xaml
new file mode 100644
index 0000000..72f899f
--- /dev/null
+++ b/Torch/Views/ObjectCollectionEditor.xaml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
diff --git a/Torch/Views/ObjectCollectionEditor.xaml.cs b/Torch/Views/ObjectCollectionEditor.xaml.cs
new file mode 100644
index 0000000..6d21ec9
--- /dev/null
+++ b/Torch/Views/ObjectCollectionEditor.xaml.cs
@@ -0,0 +1,114 @@
+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.Navigation;
+using System.Windows.Shapes;
+using NLog;
+using NLog.Fluent;
+
+namespace Torch.Views
+{
+ ///
+ /// Interaction logic for ObjectCollectionEditor.xaml
+ ///
+ public partial class ObjectCollectionEditor : Window
+ {
+ private static readonly Dictionary MethodCache = new Dictionary();
+ private static readonly MethodInfo EditMethod;
+
+ public ObjectCollectionEditor()
+ {
+ InitializeComponent();
+ }
+
+ static ObjectCollectionEditor()
+ {
+ var m = typeof(ObjectCollectionEditor).GetMethods();
+ EditMethod = m.First(mt => mt.Name == "Edit" && mt.GetGenericArguments().Length == 1);
+ }
+
+ public void Edit(ICollection collection, string title)
+ {
+ if (collection == null)
+ {
+ MessageBox.Show("Cannot load null collection.", "Edit Error");
+ return;
+ }
+
+ var gt = collection.GetType().GenericTypeArguments[0];
+
+ //substitute for 'where T : new()'
+ if (gt.GetConstructor(Type.EmptyTypes) == null)
+ {
+ MessageBox.Show("Unsupported collection type. Type must have paramaterless ctor.", "Edit Error");
+ return;
+ }
+
+ if (!MethodCache.TryGetValue(gt, out MethodInfo gm))
+ {
+ gm = EditMethod.MakeGenericMethod(gt);
+ MethodCache.Add(gt, gm);
+ }
+
+ gm.Invoke(this, new object[] {collection, title});
+ }
+
+ public void Edit(ICollection collection, string title) where T : new()
+ {
+ var oc = collection as ObservableCollection ?? new ObservableCollection(collection);
+
+ AddButton.Click += (sender, args) =>
+ {
+ var t = new T();
+ oc.Add(t);
+ ElementList.SelectedItem = t;
+ };
+
+ RemoveButton.Click += RemoveButton_OnClick;
+ ElementList.SelectionChanged += ElementsList_OnSelected;
+
+ ElementList.ItemsSource = oc;
+
+ Title = title;
+
+ WindowStartupLocation = WindowStartupLocation.CenterOwner;
+ ShowDialog();
+
+ if (!(collection is ObservableCollection))
+ {
+ collection.Clear();
+ foreach (var o in oc)
+ collection.Add(o);
+ }
+ }
+
+ private void RemoveButton_OnClick(object sender, RoutedEventArgs e)
+ {
+ //this is kinda shitty, but item count is normally small, and it prevents CollectionModifiedExceptions
+ var l = (ObservableCollection)ElementList.ItemsSource;
+ var r = new List(ElementList.SelectedItems.Cast());
+ foreach (var item in r)
+ l.Remove(item);
+ if (l.Any())
+ ElementList.SelectedIndex = 0;
+ }
+
+ private void ElementsList_OnSelected(object sender, RoutedEventArgs e)
+ {
+ var item = (sender as ListBox)?.SelectedItem;
+ PGrid.DataContext = item;
+ }
+ }
+}
diff --git a/Torch/Views/PropertyGrid.xaml.cs b/Torch/Views/PropertyGrid.xaml.cs
index 3439a7d..a5eca95 100644
--- a/Torch/Views/PropertyGrid.xaml.cs
+++ b/Torch/Views/PropertyGrid.xaml.cs
@@ -15,6 +15,8 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
+using NLog;
+using NLog.Fluent;
using VRage.Game;
using VRage.Serialization;
@@ -124,6 +126,28 @@ namespace Torch.Views
valueControl = button;
}
+ else if (propertyType.IsGenericType && typeof(ICollection).IsAssignableFrom(propertyType.GetGenericTypeDefinition()))
+ {
+ var button = new Button
+ {
+ Content = "Edit Collection"
+ };
+ button.SetBinding(Button.DataContextProperty, $"{property.Name}");
+
+ var gt = propertyType.GetGenericArguments()[0];
+
+ //TODO: Is this the best option? Probably not
+ if (gt.IsPrimitive || gt == typeof(string))
+ {
+ button.Click += (sender, args) => EditPrimitiveCollection(((Button)sender).DataContext);
+ }
+ else
+ {
+ button.Click += (sender, args) => EditObjectCollection(((Button)sender).DataContext);
+ }
+
+ valueControl = button;
+ }
else
{
valueControl = new TextBox();
@@ -149,6 +173,18 @@ namespace Torch.Views
new DictionaryEditorDialog().Edit(dic);
}
+ private void EditPrimitiveCollection(object collection, string title = "Collection Editor")
+ {
+ var c = (ICollection)collection;
+ new CollectionEditor().Edit(c, title);
+ }
+
+ private void EditObjectCollection(object collection, string title = "Collection Editor")
+ {
+ var c = (ICollection)collection;
+ new ObjectCollectionEditor().Edit(c, title);
+ }
+
private void UpdateFilter(object sender, TextChangedEventArgs e)
{
var filterText = ((TextBox)sender).Text;