CreateEntityActionEditor.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. namespace PRS.Shared.Grids.EventEditor;
  15. public class CreateEntitySaveEventActionEditor<T> : IEventActionEditor<CreateEntitySaveEventAction<T>>, INotifyPropertyChanged
  16. where T : Entity
  17. {
  18. private CreateEntitySaveEventAction<T> Action = null!;
  19. private PropertyInitializerGrid Grid = null!;
  20. public Type? SelectedType
  21. {
  22. get => Action.EntityType;
  23. set
  24. {
  25. Action.EntityType = value;
  26. Grid.EntityType = value;
  27. OnPropertyChanged();
  28. DoChanged();
  29. }
  30. }
  31. private bool _changed;
  32. public bool Changed
  33. {
  34. get => _changed;
  35. set
  36. {
  37. _changed = value;
  38. OnPropertyChanged();
  39. }
  40. }
  41. private void DoChanged()
  42. {
  43. Changed = true;
  44. }
  45. public bool Edit(CreateEntitySaveEventAction<T> action, IEventDataModelDefinition dataModelDefinition)
  46. {
  47. Action = action;
  48. var grid = new Grid();
  49. grid.AddColumn(GridUnitType.Auto);
  50. grid.AddColumn(GridUnitType.Auto);
  51. grid.AddColumn(GridUnitType.Star);
  52. grid.AddRow(GridUnitType.Auto);
  53. grid.AddRow(GridUnitType.Star);
  54. grid.AddChild(new Label
  55. {
  56. Content = "Entity Type:",
  57. VerticalAlignment = VerticalAlignment.Center
  58. }, 0, 0);
  59. var box = new ComboBox
  60. {
  61. Margin = new(5, 0, 0, 0),
  62. Padding = new(5),
  63. MinWidth = 100,
  64. DisplayMemberPath = "Name"
  65. };
  66. var entities = CoreUtils.Entities.Where(x => !x.IsGenericType && x.IsSubclassOf(typeof(Entity))).ToArray();
  67. entities.SortBy(x => x.Name);
  68. box.ItemsSource = entities;
  69. box.Bind(ComboBox.SelectedValueProperty, this, x => x.SelectedType);
  70. Grid = new()
  71. {
  72. EntityType = SelectedType,
  73. DataModelDefinition = dataModelDefinition,
  74. Margin = new(0, 5, 0, 0)
  75. };
  76. Grid.Items = action.Initializers.Select(x => new PropertyInitializerEditItem { PropertyInitializer = x }).ToList();
  77. Grid.Refresh(true, true);
  78. Grid.OnChanged += Grid_OnChanged;
  79. grid.AddChild(box, 0, 1);
  80. grid.AddChild(Grid, 1, 0, colSpan: 3);
  81. var dlg = new DynamicContentDialog(grid)
  82. {
  83. Title = "Edit Create Entity Action",
  84. WindowStartupLocation = WindowStartupLocation.CenterScreen
  85. };
  86. dlg.Bind(DynamicContentDialog.CanSaveProperty, this, x => x.Changed);
  87. if(dlg.ShowDialog() == true)
  88. {
  89. action.Initializers = Grid.Items.Select(x => x.PropertyInitializer).ToList();
  90. return true;
  91. }
  92. else
  93. {
  94. return false;
  95. }
  96. }
  97. private void Grid_OnChanged(object? sender, EventArgs e)
  98. {
  99. DoChanged();
  100. }
  101. public event PropertyChangedEventHandler? PropertyChanged;
  102. private void OnPropertyChanged([CallerMemberName] string propertyName = "")
  103. {
  104. PropertyChanged?.Invoke(this, new(propertyName));
  105. }
  106. }