TriggerEditors.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using InABox.WPF;
  4. using PRS.Shared.Events;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. namespace PRS.Shared.Grids.EventEditor;
  14. public interface IEventTriggerEditor
  15. {
  16. bool Edit(IEventTrigger trigger);
  17. }
  18. public interface IEventTriggerEditor<TTrigger> : IEventTriggerEditor
  19. where TTrigger : IEventTrigger
  20. {
  21. bool Edit(TTrigger trigger);
  22. bool IEventTriggerEditor.Edit(IEventTrigger trigger) => Edit((TTrigger)trigger);
  23. }
  24. public static class EventTriggerEditors
  25. {
  26. private static Dictionary<Type, Type>? _triggerEditors;
  27. private static Dictionary<Type, Type> GetTriggerEditors()
  28. {
  29. if(_triggerEditors is null)
  30. {
  31. _triggerEditors = new Dictionary<Type, Type>();
  32. foreach(var type in CoreUtils.TypeList(x => true))
  33. {
  34. if (type.GetInterfaceDefinition(typeof(IEventTriggerEditor<>)) is Type editorInterface)
  35. {
  36. var triggerType = editorInterface.GenericTypeArguments[0];
  37. triggerType = triggerType.IsGenericType ? triggerType.GetGenericTypeDefinition() : triggerType;
  38. _triggerEditors[triggerType] = type;
  39. }
  40. }
  41. }
  42. return _triggerEditors;
  43. }
  44. public static Type? GetTriggerEditorType(Type triggerType)
  45. {
  46. var genericTriggerType = triggerType.IsGenericType ? triggerType.GetGenericTypeDefinition() : triggerType;
  47. if(GetTriggerEditors().GetValueOrDefault(genericTriggerType) is Type editorType)
  48. {
  49. return editorType.IsGenericType ? editorType.MakeGenericType(triggerType.GenericTypeArguments) : editorType;
  50. }
  51. else
  52. {
  53. return null;
  54. }
  55. }
  56. public static bool EditTrigger<TEvent, TDataModel>(IEventTrigger<TEvent, TDataModel> trigger)
  57. where TEvent : IEvent<TDataModel>
  58. where TDataModel : IEventDataModel
  59. {
  60. var editorType = GetTriggerEditorType(trigger.GetType());
  61. if (editorType is null) return false;
  62. var editor = (Activator.CreateInstance(editorType) as IEventTriggerEditor)!;
  63. return editor.Edit(trigger);
  64. }
  65. }
  66. public class PropertyChangedSaveEventTriggerEditor<T> : Grid, IEventTriggerEditor<PropertyChangedSaveEventTrigger<T>>
  67. where T : Entity
  68. {
  69. public bool Edit(PropertyChangedSaveEventTrigger<T> trigger)
  70. {
  71. var properties = DatabaseSchema.Properties(typeof(T))
  72. .Where(x => x.IsSerializable)
  73. .Select(x => x.Name)
  74. .ToList();
  75. properties.Sort();
  76. if(DynamicGridColumnNameSelectorGrid.SelectColumnName(typeof(T), properties, out var value))
  77. {
  78. trigger.TriggerProperty = DatabaseSchema.Property(typeof(T), value);
  79. return true;
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. }
  87. // public class PropertyChangedSaveEventTriggerEditor<T> : Grid, IEventTriggerEditor<PropertyChangedSaveEventTrigger<T>>
  88. // where T : Entity
  89. // {
  90. // public PropertyChangedSaveEventTrigger<T> EventTrigger { get; set; }
  91. //
  92. // private readonly string[] ColumnNames;
  93. //
  94. // private Button PropertyButton;
  95. //
  96. // public PropertyChangedSaveEventTriggerEditor(PropertyChangedSaveEventTrigger<T> eventTrigger)
  97. // {
  98. // EventTrigger = eventTrigger;
  99. //
  100. // this.AddColumn(GridUnitType.Auto);
  101. // this.AddColumn(GridUnitType.Auto);
  102. // this.AddColumn(GridUnitType.Auto);
  103. // this.AddColumn(GridUnitType.Star);
  104. // this.AddColumn(GridUnitType.Auto);
  105. // this.AddColumn(GridUnitType.Star);
  106. // this.AddRow(GridUnitType.Auto);
  107. //
  108. // this.AddChild(new Label
  109. // {
  110. // Content = "Property:",
  111. // VerticalAlignment = VerticalAlignment.Center,
  112. // }, 0, 0);
  113. //
  114. // PropertyButton = new Button
  115. // {
  116. // Content = eventTrigger.TriggerProperty?.Name ?? "",
  117. // Padding = new(5),
  118. // Margin = new(5, 0, 0, 0),
  119. // HorizontalContentAlignment = HorizontalAlignment.Left,
  120. // Background = new LinearGradientBrush(new GradientStopCollection()
  121. // {
  122. // new(Color.FromRgb(0xF0, 0xF0, 0xF0), 0.0),
  123. // new(Color.FromRgb(0xE5, 0xE5, 0xE5), 1.0),
  124. // })
  125. // {
  126. // StartPoint = new(0, 0),
  127. // EndPoint = new(0, 1),
  128. // },
  129. // BorderBrush = Color.FromRgb(0xAC, 0xAC, 0xAC).ToBrush(),
  130. // MinWidth = 100
  131. // };
  132. // PropertyButton.Click += PropertyButton_Click;
  133. //
  134. // var properties = DatabaseSchema.Properties(typeof(T))
  135. // .Where(x => x.IsSerializable)
  136. // .Select(x => x.Name)
  137. // .ToList();
  138. // properties.Sort();
  139. // ColumnNames = properties.ToArray();
  140. // this.AddChild(PropertyButton, 0, 1);
  141. //
  142. // this.AddChild(new Label
  143. // {
  144. // Content = "Old Value:",
  145. // Margin = new(5, 0, 0, 0),
  146. // VerticalAlignment = VerticalAlignment.Center,
  147. // }, 0, 2);
  148. //
  149. // this.AddChild(new Label
  150. // {
  151. // Content = "New Value:",
  152. // Margin = new(5, 0, 0, 0),
  153. // VerticalAlignment = VerticalAlignment.Center,
  154. // }, 0, 4);
  155. // }
  156. //
  157. // private void PropertyButton_Click(object sender, RoutedEventArgs e)
  158. // {
  159. // if(DynamicGridColumnNameSelectorGrid.SelectColumnName(typeof(T), ColumnNames, out var value))
  160. // {
  161. // EventTrigger.TriggerProperty = DatabaseSchema.Property(typeof(T), value);
  162. // PropertyButton.Content = value;
  163. // }
  164. // }
  165. // }