Browse Source

Added editor for PropertyChanged trigger

Kenric Nugteren 9 months ago
parent
commit
43a5c36c8c

+ 6 - 0
prs.shared/Grids/EventEditor/EventTriggerGrid.cs

@@ -62,6 +62,12 @@ public class EventTriggerGrid<TEvent, TDataModel> : DynamicItemsListGrid<EventTr
         menu.IsOpen = true;
     }
 
+    protected override void DoEdit()
+    {
+        var item = LoadItem(SelectedRows.First());
+        EditTrigger(item.Trigger);
+    }
+
     private void MenuAdd_Click(Type type)
     {
         if (type.IsGenericType)

+ 174 - 0
prs.shared/Grids/EventEditor/TriggerEditors.cs

@@ -0,0 +1,174 @@
+using InABox.Core;
+using InABox.DynamicGrid;
+using InABox.WPF;
+using PRS.Shared.Events;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+
+namespace PRS.Shared.Grids.EventEditor;
+
+public interface IEventTriggerEditor
+{
+    bool Edit(IEventTrigger trigger);
+}
+
+public interface IEventTriggerEditor<TTrigger> : IEventTriggerEditor
+    where TTrigger : IEventTrigger
+{
+    bool Edit(TTrigger trigger);
+
+    bool IEventTriggerEditor.Edit(IEventTrigger trigger) => Edit((TTrigger)trigger);
+}
+
+public static class EventTriggerEditors
+{
+    private static Dictionary<Type, Type>? _triggerEditors;
+    private static Dictionary<Type, Type> GetTriggerEditors()
+    {
+        if(_triggerEditors is null)
+        {
+            _triggerEditors = new Dictionary<Type, Type>();
+            foreach(var type in CoreUtils.TypeList(x => true))
+            {
+                if (type.GetInterfaceDefinition(typeof(IEventTriggerEditor<>)) is Type editorInterface)
+                {
+                    var triggerType = editorInterface.GenericTypeArguments[0];
+                    triggerType = triggerType.IsGenericType ? triggerType.GetGenericTypeDefinition() : triggerType;
+                    _triggerEditors[triggerType] = type;
+                }
+            }
+        }
+        return _triggerEditors;
+    }
+
+    public static Type? GetTriggerEditorType(Type triggerType)
+    {
+        var genericTriggerType = triggerType.IsGenericType ? triggerType.GetGenericTypeDefinition() : triggerType;
+        if(GetTriggerEditors().GetValueOrDefault(genericTriggerType) is Type editorType)
+        {
+            return editorType.IsGenericType ? editorType.MakeGenericType(triggerType.GenericTypeArguments) : editorType;
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+    public static bool EditTrigger<TEvent, TDataModel>(IEventTrigger<TEvent, TDataModel> trigger)
+        where TEvent : IEvent<TDataModel>
+        where TDataModel : IEventDataModel
+    {
+        var editorType = GetTriggerEditorType(trigger.GetType());
+        if (editorType is null) return false;
+
+        var editor = (Activator.CreateInstance(editorType) as IEventTriggerEditor)!;
+        return editor.Edit(trigger);
+    }
+}
+
+public class PropertyChangedSaveEventTriggerEditor<T> : Grid, IEventTriggerEditor<PropertyChangedSaveEventTrigger<T>>
+    where T : Entity
+{
+    public bool Edit(PropertyChangedSaveEventTrigger<T> trigger)
+    {
+        var properties = DatabaseSchema.Properties(typeof(T))
+            .Where(x => x.IsSerializable)
+            .Select(x => x.Name)
+            .ToList();
+        properties.Sort();
+        if(DynamicGridColumnNameSelectorGrid.SelectColumnName(typeof(T), properties, out var value))
+        {
+            trigger.TriggerProperty = DatabaseSchema.Property(typeof(T), value);
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+}
+// public class PropertyChangedSaveEventTriggerEditor<T> : Grid, IEventTriggerEditor<PropertyChangedSaveEventTrigger<T>>
+//     where T : Entity
+// {
+//     public PropertyChangedSaveEventTrigger<T> EventTrigger { get; set; }
+// 
+//     private readonly string[] ColumnNames;
+// 
+//     private Button PropertyButton;
+// 
+//     public PropertyChangedSaveEventTriggerEditor(PropertyChangedSaveEventTrigger<T> eventTrigger)
+//     {
+//         EventTrigger = eventTrigger;
+// 
+//         this.AddColumn(GridUnitType.Auto);
+//         this.AddColumn(GridUnitType.Auto);
+//         this.AddColumn(GridUnitType.Auto);
+//         this.AddColumn(GridUnitType.Star);
+//         this.AddColumn(GridUnitType.Auto);
+//         this.AddColumn(GridUnitType.Star);
+//         this.AddRow(GridUnitType.Auto);
+// 
+//         this.AddChild(new Label
+//         {
+//             Content = "Property:",
+//             VerticalAlignment = VerticalAlignment.Center,
+//         }, 0, 0);
+// 
+//         PropertyButton = new Button
+//         {
+//             Content = eventTrigger.TriggerProperty?.Name ?? "",
+//             Padding = new(5),
+//             Margin = new(5, 0, 0, 0),
+//             HorizontalContentAlignment = HorizontalAlignment.Left,
+//             Background = new LinearGradientBrush(new GradientStopCollection()
+//             {
+//                 new(Color.FromRgb(0xF0, 0xF0, 0xF0), 0.0),
+//                 new(Color.FromRgb(0xE5, 0xE5, 0xE5), 1.0),
+//             })
+//             {
+//                 StartPoint = new(0, 0),
+//                 EndPoint = new(0, 1),
+//             },
+//             BorderBrush = Color.FromRgb(0xAC, 0xAC, 0xAC).ToBrush(),
+//             MinWidth = 100
+//         };
+//         PropertyButton.Click += PropertyButton_Click;
+// 
+//         var properties = DatabaseSchema.Properties(typeof(T))
+//             .Where(x => x.IsSerializable)
+//             .Select(x => x.Name)
+//             .ToList();
+//         properties.Sort();
+//         ColumnNames = properties.ToArray();
+//         this.AddChild(PropertyButton, 0, 1);
+// 
+//         this.AddChild(new Label
+//         {
+//             Content = "Old Value:",
+//             Margin = new(5, 0, 0, 0),
+//             VerticalAlignment = VerticalAlignment.Center,
+//         }, 0, 2);
+// 
+//         this.AddChild(new Label
+//         {
+//             Content = "New Value:",
+//             Margin = new(5, 0, 0, 0),
+//             VerticalAlignment = VerticalAlignment.Center,
+//         }, 0, 4);
+//     }
+// 
+//     private void PropertyButton_Click(object sender, RoutedEventArgs e)
+//     {
+//         if(DynamicGridColumnNameSelectorGrid.SelectColumnName(typeof(T), ColumnNames, out var value))
+//         {
+//             EventTrigger.TriggerProperty = DatabaseSchema.Property(typeof(T), value);
+//             PropertyButton.Content = value;
+//         }
+//     }
+// }