CreateEntityActionEditor.cs 4.1 KB

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