123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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;
- // }
- // }
- // }
|