EventEditor.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using AutoProperties;
  2. using Comal.Classes;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using PRS.Shared.Events;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Runtime.CompilerServices;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Shapes;
  23. namespace PRS.Shared;
  24. /// <summary>
  25. /// Interaction logic for EventEditorWIndow.xaml
  26. /// </summary>
  27. public partial class EventEditor : UserControl, INotifyPropertyChanged
  28. {
  29. private EventType? _eventType;
  30. public EventType? EventType
  31. {
  32. get => _eventType;
  33. set
  34. {
  35. _eventType = value;
  36. OnPropertyChanged();
  37. HasType = value.HasValue && EventTypeHasEntityType(value.Value);
  38. DoChanged();
  39. UpdateEventData();
  40. }
  41. }
  42. private Type? _entityType;
  43. public Type? EntityType
  44. {
  45. get => _entityType;
  46. set
  47. {
  48. _entityType = value;
  49. OnPropertyChanged();
  50. UpdateEventData();
  51. }
  52. }
  53. private bool _hasProperties;
  54. public bool HasProperties
  55. {
  56. get => _hasProperties;
  57. set
  58. {
  59. _hasProperties = value;
  60. OnPropertyChanged();
  61. }
  62. }
  63. private bool _hasType;
  64. public bool HasType
  65. {
  66. get => _hasType;
  67. set
  68. {
  69. _hasType = value;
  70. OnPropertyChanged();
  71. }
  72. }
  73. private Type? EventDataType;
  74. private Type? DataModelType;
  75. private IEventEditorControl? _eventEditorControl;
  76. private bool _changed;
  77. public bool Changed
  78. {
  79. get => _changed;
  80. set
  81. {
  82. _changed = value;
  83. OnPropertyChanged();
  84. }
  85. }
  86. public IEventData? Data
  87. {
  88. get => _eventEditorControl?.Data;
  89. set
  90. {
  91. OnPropertyChanged();
  92. if(value is not null && EventType.HasValue)
  93. {
  94. if(EventDataType is null || DataModelType is null)
  95. {
  96. var inter = value.GetType().GetSuperclassDefinition(typeof(EventData<,>))!;
  97. EventDataType = inter.GenericTypeArguments[0];
  98. DataModelType = inter.GenericTypeArguments[1];
  99. if (EventTypeHasEntityType(EventType.Value))
  100. {
  101. EntityType = EventDataType.GenericTypeArguments[0];
  102. }
  103. }
  104. var control = Activator.CreateInstance(typeof(EventEditorControl<,>).MakeGenericType(EventDataType, DataModelType), value);
  105. _eventEditorControl = (control as IEventEditorControl)!;
  106. _eventEditorControl.OnChanged += DoChanged;
  107. Content.Content = control;
  108. Content.Visibility = Visibility.Visible;
  109. Placeholder.Visibility = Visibility.Collapsed;
  110. }
  111. else
  112. {
  113. _eventEditorControl = null;
  114. Content.Content = null;
  115. Content.Visibility = Visibility.Collapsed;
  116. Placeholder.Visibility = Visibility.Visible;
  117. }
  118. DoChanged();
  119. }
  120. }
  121. public EventEditor()
  122. {
  123. InitializeComponent();
  124. EventTypeBox.ItemsSource = Enum.GetValues<EventType>();
  125. var entities = CoreUtils.Entities.Where(x => !x.IsGenericType && x.IsSubclassOf(typeof(Entity))).ToArray();
  126. entities.SortBy(x => x.Name);
  127. EntityTypeBox.ItemsSource = entities;
  128. }
  129. private void DoChanged()
  130. {
  131. Changed = true;
  132. }
  133. private bool EventTypeHasEntityType(EventType eventType)
  134. {
  135. switch (eventType)
  136. {
  137. case Comal.Classes.EventType.AfterSave:
  138. return true;
  139. default:
  140. return false;
  141. }
  142. }
  143. private void UpdateEventData()
  144. {
  145. if (EventType is null || (EventTypeHasEntityType(EventType.Value) && EntityType is null))
  146. {
  147. Data = null;
  148. HasProperties = false;
  149. return;
  150. }
  151. Type eventType;
  152. Type dataModelType;
  153. switch (EventType.Value)
  154. {
  155. case Comal.Classes.EventType.AfterSave:
  156. eventType = typeof(SaveEvent<>).MakeGenericType(EntityType!);
  157. dataModelType = typeof(SaveEventDataModel<>).MakeGenericType(EntityType!);
  158. break;
  159. case Comal.Classes.EventType.Scheduled:
  160. eventType = typeof(ScheduledEvent);
  161. dataModelType = typeof(ScheduledEventDataModel);
  162. break;
  163. default:
  164. Data = null;
  165. HasProperties = false;
  166. return;
  167. }
  168. HasProperties = eventType.HasInterface(typeof(IPropertiesEvent<>));
  169. var ev = Activator.CreateInstance(eventType);
  170. EventDataType = eventType;
  171. DataModelType = dataModelType;
  172. Data = (Activator.CreateInstance(typeof(EventData<,>).MakeGenericType(eventType, dataModelType), ev) as IEventData)!;
  173. }
  174. private void EditButton_Click(object sender, RoutedEventArgs e)
  175. {
  176. if (EventDataType is null || EventDataType.GetInterfaceDefinition(typeof(IPropertiesEvent<>)) is not Type propertiesInterface) return;
  177. var method = GetType().GetMethod(nameof(EditProperties), BindingFlags.Instance | BindingFlags.NonPublic)!
  178. .MakeGenericMethod(propertiesInterface.GenericTypeArguments[0]);
  179. method.Invoke(this, []);
  180. }
  181. private void EditProperties<TProperties>() where TProperties : BaseObject, new()
  182. {
  183. if (Data?.Event is not IPropertiesEvent<TProperties> propertiesEvent) return;
  184. var props = propertiesEvent.GetProperties();
  185. if (DynamicGridUtils.EditEntity(props))
  186. {
  187. propertiesEvent.SetProperties(props);
  188. DoChanged();
  189. }
  190. }
  191. public static bool Run(ref EventType evType, ref IEventData? eventData)
  192. {
  193. var editor = new EventEditor();
  194. editor.EventType = evType;
  195. if(eventData is not null)
  196. {
  197. editor.Data = eventData;
  198. }
  199. editor.Changed = false;
  200. var dialog = new DynamicContentDialog(editor)
  201. {
  202. SizeToContent = SizeToContent.Height,
  203. Title = "Edit Event"
  204. };
  205. dialog.Bind(DynamicContentDialog.CanSaveProperty, editor, x => x.Changed);
  206. if(dialog.ShowDialog() == true)
  207. {
  208. eventData = editor.Data;
  209. evType = editor.EventType ?? default;
  210. return true;
  211. }
  212. else
  213. {
  214. return false;
  215. }
  216. }
  217. }