EventEditor.xaml.cs 6.6 KB

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