using InABox.Core; using InABox.DynamicGrid; using InABox.Wpf; using InABox.WPF; using PRS.Shared.Events; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace PRS.Shared.Grids.EventEditor; public class CreateEntitySaveEventActionEditor : IEventActionEditor>, INotifyPropertyChanged where T : Entity, new() { private CreateEntitySaveEventAction Action = null!; private PropertyInitializerGrid Grid = null!; private Button ScriptButton = null!; public Type? SelectedType { get => Action.EntityType; set { Action.EntityType = value; Grid.EntityType = value; OnPropertyChanged(); DoChanged(); } } private bool _changed; public bool Changed { get => _changed; set { _changed = value; OnPropertyChanged(); } } private void DoChanged() { Changed = true; } public bool Edit(CreateEntitySaveEventAction action, IEventDataModelDefinition dataModelDefinition) { Action = action; var grid = new Grid(); grid.AddColumn(GridUnitType.Auto); grid.AddColumn(GridUnitType.Auto); grid.AddColumn(GridUnitType.Star); grid.AddColumn(GridUnitType.Auto); grid.AddRow(GridUnitType.Auto); grid.AddRow(GridUnitType.Star); grid.AddChild(new Label { Content = "Entity Type:", VerticalAlignment = VerticalAlignment.Center }, 0, 0); var box = new ComboBox { Margin = new(5, 0, 0, 0), Padding = new(5), MinWidth = 100, DisplayMemberPath = "Name" }; var entities = CoreUtils.Entities.Where(x => !x.IsGenericType && x.IsSubclassOf(typeof(Entity))).ToArray(); entities.SortBy(x => x.Name); box.ItemsSource = entities; box.Bind(ComboBox.SelectedValueProperty, this, x => x.SelectedType); ScriptButton = new Button { Padding = new(5), Content = "Script" }; ScriptButton.Bind(Button.IsEnabledProperty, this, x => x.SelectedType, converter: new FuncConverter(x => x is not null)); ScriptButton.Click += ScriptButton_Click; Grid = new() { EntityType = SelectedType, DataModelDefinition = dataModelDefinition, Margin = new(0, 5, 0, 0) }; Grid.Items = action.Initializers.Select(x => new PropertyInitializerEditItem { PropertyInitializer = x }).ToList(); Grid.Refresh(true, true); Grid.OnChanged += Grid_OnChanged; grid.AddChild(box, 0, 1); grid.AddChild(ScriptButton, 0, 3); grid.AddChild(Grid, 1, 0, colSpan: 4); var dlg = new DynamicContentDialog(grid) { Title = "Edit Create Entity Action", WindowStartupLocation = WindowStartupLocation.CenterScreen }; dlg.Bind(DynamicContentDialog.CanSaveProperty, this, x => x.Changed); if(dlg.ShowDialog() == true) { action.Initializers = Grid.Items.Select(x => x.PropertyInitializer).ToList(); return true; } else { return false; } } private void ScriptButton_Click(object sender, RoutedEventArgs e) { var window = new ScriptEditorWindow(Action.Script ?? Action.DefaultScript(), scriptTitle: "Edit Create Entity Action"); if(window.ShowDialog() == true) { Action.Script = window.Script; DoChanged(); } } private void Grid_OnChanged(object? sender, EventArgs e) { DoChanged(); } public event PropertyChangedEventHandler? PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new(propertyName)); } }