1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using Microsoft.CodeAnalysis.VisualBasic.Syntax;
- using PRS.Shared.Events;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- 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 IEventActionEditor
- {
- bool Edit(IEventAction trigger, IEventDataModelDefinition dataModelDefinition);
- }
- public interface IEventActionEditor<TAction> : IEventActionEditor
- where TAction : IEventAction
- {
- bool Edit(TAction trigger, IEventDataModelDefinition dataModelDefinition);
- bool IEventActionEditor.Edit(IEventAction trigger, IEventDataModelDefinition dataModelDefinition) => Edit((TAction)trigger, dataModelDefinition);
- }
- public static class EventActionEditors
- {
- private static Dictionary<Type, Type>? _actionEditors;
- private static Dictionary<Type, Type> GetActionEditors()
- {
- if(_actionEditors is null)
- {
- _actionEditors = new Dictionary<Type, Type>();
- foreach(var type in CoreUtils.TypeList(x => true))
- {
- if (type.GetInterfaceDefinition(typeof(IEventActionEditor<>)) is Type editorInterface)
- {
- var actionType = editorInterface.GenericTypeArguments[0];
- actionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType;
- _actionEditors[actionType] = type;
- }
- }
- }
- return _actionEditors;
- }
- public static Type? GetActionEditorType(Type actionType)
- {
- var genericActionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType;
- if(GetActionEditors().GetValueOrDefault(genericActionType) is Type editorType)
- {
- return editorType.IsGenericType ? editorType.MakeGenericType(actionType.GenericTypeArguments) : editorType;
- }
- else
- {
- return null;
- }
- }
- public static bool EditAction<TEvent, TDataModel>(IEventAction<TEvent> action, IEventDataModelDefinition dataModelDefinition)
- where TEvent : IEvent<TDataModel>
- where TDataModel : IEventDataModel
- {
- var editorType = GetActionEditorType(action.GetType());
- if (editorType is null) return true;
- var editor = (Activator.CreateInstance(editorType) as IEventActionEditor)!;
- return editor.Edit(action, dataModelDefinition);
- }
- }
|