From d51e6f592b111a3777daeed17816e875025bbfa4 Mon Sep 17 00:00:00 2001 From: rvost Date: Thu, 20 Oct 2022 19:13:10 +0300 Subject: [PATCH] Add import functionality to SpawnableTypes --- .../Stores/PresetImportStore.cs | 25 ++++++++++ .../Stores/SpawnableTypePresetsImportStore.cs | 29 +++++++++++ .../ViewModels/SpawnablePresetViewModel.cs | 17 +++++++ .../ViewModels/SpawnableTypeViewModel.cs | 50 +++++++++++++++++-- .../ViewModels/SpawnableTypesViewModel.cs | 2 + .../Controls/SpawnablePresetsGrid.xaml | 13 ++++- .../Controls/SpawnablePresetsGrid.xaml.cs | 9 ++++ .../Views/MainWindow.xaml | 20 ++++++++ .../Views/SpawnableTypesView.xaml | 7 +-- 9 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 src/DayzServerTools.Application/Stores/PresetImportStore.cs create mode 100644 src/DayzServerTools.Application/Stores/SpawnableTypePresetsImportStore.cs diff --git a/src/DayzServerTools.Application/Stores/PresetImportStore.cs b/src/DayzServerTools.Application/Stores/PresetImportStore.cs new file mode 100644 index 0000000..7718136 --- /dev/null +++ b/src/DayzServerTools.Application/Stores/PresetImportStore.cs @@ -0,0 +1,25 @@ +using System.Collections.ObjectModel; + +using DayzServerTools.Application.Extensions; +using DayzServerTools.Library.Xml; + +namespace DayzServerTools.Application.Stores; + +public class PresetImportStore : IClassnameImportStore +{ + private readonly ObservableCollection _target; + + public PresetImportStore(ObservableCollection target) + { + _target = target; + } + + public void Accept(IEnumerable classnames) + { + var total = classnames.Count(); + var items = classnames.Select(name => + new SpawnableItem(name, Math.Round(1.0 / total, 2)) + ); + _target.AddRange(items); + } +} diff --git a/src/DayzServerTools.Application/Stores/SpawnableTypePresetsImportStore.cs b/src/DayzServerTools.Application/Stores/SpawnableTypePresetsImportStore.cs new file mode 100644 index 0000000..2fb81fd --- /dev/null +++ b/src/DayzServerTools.Application/Stores/SpawnableTypePresetsImportStore.cs @@ -0,0 +1,29 @@ +using System.Collections.ObjectModel; + +using DayzServerTools.Application.Extensions; +using DayzServerTools.Application.ViewModels; +using DayzServerTools.Library.Xml; + +namespace DayzServerTools.Application.Stores; + +public class SpawnableTypePresetsImportStore : IClassnameImportStore +{ + private readonly ObservableCollection _target; + + public SpawnableTypePresetsImportStore(ObservableCollection target) + { + _target = target; + } + + public void Accept(IEnumerable classnames) + { + var items = classnames.Select(name => new SpawnableItem(name, 1)); + var presetVMs = items.Select(item => + { + var preset = new SpawnablePreset() { Chance = 1 }; + preset.Items.Add(item); + return new SpawnablePresetViewModel(preset); + }); + _target.AddRange(presetVMs); + } +} \ No newline at end of file diff --git a/src/DayzServerTools.Application/ViewModels/SpawnablePresetViewModel.cs b/src/DayzServerTools.Application/ViewModels/SpawnablePresetViewModel.cs index 19b027a..e297d9a 100644 --- a/src/DayzServerTools.Application/ViewModels/SpawnablePresetViewModel.cs +++ b/src/DayzServerTools.Application/ViewModels/SpawnablePresetViewModel.cs @@ -1,7 +1,11 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.DependencyInjection; +using CommunityToolkit.Mvvm.Input; +using DayzServerTools.Application.Services; +using DayzServerTools.Application.Stores; using DayzServerTools.Library.Xml; namespace DayzServerTools.Application.ViewModels; @@ -9,6 +13,7 @@ namespace DayzServerTools.Application.ViewModels; public class SpawnablePresetViewModel:ObservableObject { private readonly SpawnablePreset _model; + private readonly IDialogFactory _dialogFactory; public SpawnablePreset Model => _model; public string Preset @@ -31,8 +36,20 @@ public double Chance public SpawnableItem DefaultItem => _model.Items.FirstOrDefault(); public ObservableCollection Items => _model.Items; + public RelayCommand ImportClassnamesCommand { get; } + public SpawnablePresetViewModel(SpawnablePreset model) { _model = model; + _dialogFactory = Ioc.Default.GetRequiredService(); + + ImportClassnamesCommand = new RelayCommand(ImportClassnames); + } + + protected void ImportClassnames() + { + var dialog = _dialogFactory.CreateClassnameImportDialog(); + dialog.Store = new PresetImportStore(Items); + dialog.ShowDialog(); } } diff --git a/src/DayzServerTools.Application/ViewModels/SpawnableTypeViewModel.cs b/src/DayzServerTools.Application/ViewModels/SpawnableTypeViewModel.cs index a8690d8..7847d31 100644 --- a/src/DayzServerTools.Application/ViewModels/SpawnableTypeViewModel.cs +++ b/src/DayzServerTools.Application/ViewModels/SpawnableTypeViewModel.cs @@ -3,15 +3,28 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.DependencyInjection; +using CommunityToolkit.Mvvm.Input; + using DayzServerTools.Application.Extensions; +using DayzServerTools.Application.Services; +using DayzServerTools.Application.Stores; using DayzServerTools.Library.Xml; namespace DayzServerTools.Application.ViewModels; +public enum PresetType +{ + Cargo, + Attachments +} + public partial class SpawnableTypeViewModel : ObservableObject { private readonly SpawnableType _model; - private readonly WorkspaceViewModel _workspace; + private readonly IDialogFactory _dialogFactory; + + [ObservableProperty] + private SpawnablePresetViewModel selectedPreset; public SpawnableType Model => _model; public string Name @@ -41,21 +54,50 @@ public string Tag } public ObservableCollection Cargo { get; } = new(); public ObservableCollection Attachments { get; } = new(); - public IEnumerable AvailableCargoPresets => _workspace.AvailableCargoPresets; - public IEnumerable AvailableAttachmentsPresets => _workspace.AvailableAttachmentsPresets; + + public IRelayCommand AddNewPresetCommand { get; } + public IRelayCommand ImportClassnamesAsPresetsCommand { get; } public SpawnableTypeViewModel(SpawnableType model) { _model = model; - _workspace = Ioc.Default.GetRequiredService(); + _dialogFactory = Ioc.Default.GetRequiredService(); Cargo.AddRange(_model.Cargo.Select(preset => new SpawnablePresetViewModel(preset))); Attachments.AddRange(_model.Attachments.Select(preset => new SpawnablePresetViewModel(preset))); + AddNewPresetCommand = new RelayCommand(AddNewPreset); + ImportClassnamesAsPresetsCommand = new RelayCommand(ImportClassnamesAsPresets); + Cargo.CollectionChanged += OnPresetsCollectionChanged; Attachments.CollectionChanged += OnPresetsCollectionChanged; } + protected void AddNewPreset(PresetType type) + { + var newPreset = new SpawnablePresetViewModel(new SpawnablePreset()); + + switch (type) + { + case PresetType.Cargo: + Cargo.Add(newPreset); + break; + case PresetType.Attachments: + Attachments.Add(newPreset); + break; + default: + break; + } + } + protected void ImportClassnamesAsPresets(PresetType type) + { + var target = type == PresetType.Cargo ? Cargo : Attachments; + + var dialog = _dialogFactory.CreateClassnameImportDialog(); + dialog.Store = new SpawnableTypePresetsImportStore(target); + dialog.ShowDialog(); + } + private void OnPresetsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { ObservableCollection target = diff --git a/src/DayzServerTools.Application/ViewModels/SpawnableTypesViewModel.cs b/src/DayzServerTools.Application/ViewModels/SpawnableTypesViewModel.cs index 72f2de6..270c463 100644 --- a/src/DayzServerTools.Application/ViewModels/SpawnableTypesViewModel.cs +++ b/src/DayzServerTools.Application/ViewModels/SpawnableTypesViewModel.cs @@ -18,6 +18,8 @@ public partial class SpawnableTypesViewModel : ProjectFileViewModel - + @@ -103,7 +106,13 @@ - + + + + + + diff --git a/src/DayzServerTools.Windows/Controls/SpawnablePresetsGrid.xaml.cs b/src/DayzServerTools.Windows/Controls/SpawnablePresetsGrid.xaml.cs index 9a0bb55..ea47e1e 100644 --- a/src/DayzServerTools.Windows/Controls/SpawnablePresetsGrid.xaml.cs +++ b/src/DayzServerTools.Windows/Controls/SpawnablePresetsGrid.xaml.cs @@ -34,11 +34,20 @@ public IEnumerable AvailablePresets set { SetValue(AvailablePresetsProperty, value); } } + public object SelectedItem + { + get { return GetValue(SelectedItemProperty); } + set { SetValue(SelectedItemProperty, value); } + } + public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(ObservableCollection), typeof(SpawnablePresetsGrid), new PropertyMetadata(default)); public static readonly DependencyProperty AvailablePresetsProperty = DependencyProperty.Register("AvailablePresets", typeof(IEnumerable), typeof(SpawnablePresetsGrid), new PropertyMetadata(default)); + + public static readonly DependencyProperty SelectedItemProperty = + DependencyProperty.Register("SelectedItem", typeof(object), typeof(SpawnablePresetsGrid), new PropertyMetadata(default)); public SpawnablePresetsGrid() { diff --git a/src/DayzServerTools.Windows/Views/MainWindow.xaml b/src/DayzServerTools.Windows/Views/MainWindow.xaml index de345a1..36c9ea9 100644 --- a/src/DayzServerTools.Windows/Views/MainWindow.xaml +++ b/src/DayzServerTools.Windows/Views/MainWindow.xaml @@ -420,6 +420,26 @@ + + + + + + + @@ -69,11 +70,11 @@ HorizontalAlignment="Center" FontSize="16"/> - -