| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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);
- }
- }
- public class ScriptSaveEventActionEditor<T> : IEventActionEditor<ScriptSaveEventAction<T>>
- where T : Entity
- {
- public bool Edit(ScriptSaveEventAction<T> action, IEventDataModelDefinition dataModelDefinition)
- {
- var window = new ScriptEditorWindow(action.Script ?? action.DefaultScript(), scriptTitle: "Edit Custom Script Action");
- if(window.ShowDialog() == true)
- {
- action.Script = window.Script;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
|