Skip to content

Commit

Permalink
Add import functionality to SpawnableTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
rvost committed Oct 20, 2022
1 parent 2cfb373 commit d51e6f5
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 9 deletions.
25 changes: 25 additions & 0 deletions src/DayzServerTools.Application/Stores/PresetImportStore.cs
Original file line number Diff line number Diff line change
@@ -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<SpawnableItem> _target;

public PresetImportStore(ObservableCollection<SpawnableItem> target)
{
_target = target;
}

public void Accept(IEnumerable<string> classnames)
{
var total = classnames.Count();
var items = classnames.Select(name =>
new SpawnableItem(name, Math.Round(1.0 / total, 2))
);
_target.AddRange(items);
}
}
Original file line number Diff line number Diff line change
@@ -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<SpawnablePresetViewModel> _target;

public SpawnableTypePresetsImportStore(ObservableCollection<SpawnablePresetViewModel> target)
{
_target = target;
}

public void Accept(IEnumerable<string> 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);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
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;

public class SpawnablePresetViewModel:ObservableObject
{
private readonly SpawnablePreset _model;
private readonly IDialogFactory _dialogFactory;

public SpawnablePreset Model => _model;
public string Preset
Expand All @@ -31,8 +36,20 @@ public double Chance
public SpawnableItem DefaultItem => _model.Items.FirstOrDefault();
public ObservableCollection<SpawnableItem> Items => _model.Items;

public RelayCommand ImportClassnamesCommand { get; }

public SpawnablePresetViewModel(SpawnablePreset model)
{
_model = model;
_dialogFactory = Ioc.Default.GetRequiredService<IDialogFactory>();

ImportClassnamesCommand = new RelayCommand(ImportClassnames);
}

protected void ImportClassnames()
{
var dialog = _dialogFactory.CreateClassnameImportDialog();
dialog.Store = new PresetImportStore(Items);
dialog.ShowDialog();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,21 +54,50 @@ public string Tag
}
public ObservableCollection<SpawnablePresetViewModel> Cargo { get; } = new();
public ObservableCollection<SpawnablePresetViewModel> Attachments { get; } = new();
public IEnumerable<string> AvailableCargoPresets => _workspace.AvailableCargoPresets;
public IEnumerable<string> AvailableAttachmentsPresets => _workspace.AvailableAttachmentsPresets;

public IRelayCommand<PresetType> AddNewPresetCommand { get; }
public IRelayCommand<PresetType> ImportClassnamesAsPresetsCommand { get; }

public SpawnableTypeViewModel(SpawnableType model)
{
_model = model;
_workspace = Ioc.Default.GetRequiredService<WorkspaceViewModel>();
_dialogFactory = Ioc.Default.GetRequiredService<IDialogFactory>();

Cargo.AddRange(_model.Cargo.Select(preset => new SpawnablePresetViewModel(preset)));
Attachments.AddRange(_model.Attachments.Select(preset => new SpawnablePresetViewModel(preset)));

AddNewPresetCommand = new RelayCommand<PresetType>(AddNewPreset);
ImportClassnamesAsPresetsCommand = new RelayCommand<PresetType>(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<SpawnablePreset> target =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public partial class SpawnableTypesViewModel : ProjectFileViewModel<SpawnableTyp
[ObservableProperty]
private WorkspaceViewModel workspace;
[ObservableProperty]
private SpawnableTypeViewModel selectedItem;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(ExportToNewFileCommand))]
private IList selectedItems;

Expand Down
13 changes: 11 additions & 2 deletions src/DayzServerTools.Windows/Controls/SpawnablePresetsGrid.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
</DataTemplate>
</UserControl.Resources>
<Grid>
<DataGrid ItemsSource="{Binding ElementName=root, Path=ItemsSource}" AutoGenerateColumns="False">
<DataGrid ItemsSource="{Binding ElementName=root, Path=ItemsSource}"
SelectedItem="{Binding ElementName=root, Path=SelectedItem}"
VirtualizingPanel.ScrollUnit="Pixel"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Chance" Binding="{Binding Chance}"/>
<DataGridTemplateColumn Header="Preset">
Expand Down Expand Up @@ -103,7 +106,13 @@
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Items}" IsEnabled="{Binding ItemsSpecified}"/>
<DataGrid ItemsSource="{Binding Items}" IsEnabled="{Binding ItemsSpecified}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Chance" Binding="{Binding Chance}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ public IEnumerable<string> 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<SpawnablePresetViewModel>), typeof(SpawnablePresetsGrid), new PropertyMetadata(default));

public static readonly DependencyProperty AvailablePresetsProperty =
DependencyProperty.Register("AvailablePresets", typeof(IEnumerable<string>), typeof(SpawnablePresetsGrid), new PropertyMetadata(default));

public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(SpawnablePresetsGrid), new PropertyMetadata(default));

public SpawnablePresetsGrid()
{
Expand Down
20 changes: 20 additions & 0 deletions src/DayzServerTools.Windows/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,26 @@
<fluent:RibbonGroupBox Header="Edit">
<fluent:Button Header="Add Type" Icon="{StaticResource AddSpawnabeTypeVecIcon}"
Command="{Binding ActiveFile.AddSpawnableTypeCommand}"/>
<fluent:Button Header="Add Cargo Preset" Icon="{StaticResource AddVecIcon}"
Size="Middle"
Command="{Binding ActiveFile.SelectedItem.AddNewPresetCommand}"
CommandParameter="{x:Static viewmodels:PresetType.Cargo}"/>
<fluent:Button Header="Add Attachments Preset" Icon="{StaticResource AddVecIcon}"
Size="Middle"
Command="{Binding ActiveFile.SelectedItem.AddNewPresetCommand}"
CommandParameter="{x:Static viewmodels:PresetType.Attachments}"/>
</fluent:RibbonGroupBox>
<fluent:RibbonGroupBox Header="Import">
<fluent:Button Header="Classnames as Cargo Presets" Icon="{StaticResource ImportVecIcon}"
Size="Middle"
Command="{Binding ActiveFile.SelectedItem.ImportClassnamesAsPresetsCommand}"
CommandParameter="{x:Static viewmodels:PresetType.Cargo}"/>
<fluent:Button Header="Classnames as Attachments Preset" Icon="{StaticResource ImportVecIcon}"
Size="Middle"
Command="{Binding ActiveFile.SelectedItem.ImportClassnamesAsPresetsCommand}"
CommandParameter="{x:Static viewmodels:PresetType.Attachments}"/>
<fluent:Button Header="Classnames to Preset" Icon="{StaticResource ImportVecIcon}"
Command="{Binding ActiveFile.SelectedItem.SelectedPreset.ImportClassnamesCommand}"/>
</fluent:RibbonGroupBox>
<fluent:RibbonGroupBox Header="Export">
<fluent:Button Header="To New File" Icon="{StaticResource NewSpawnableTypesVecIcon}"
Expand Down
7 changes: 4 additions & 3 deletions src/DayzServerTools.Windows/Views/SpawnableTypesView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<controls:BindableMultiSelectDataGrid Grid.Column="0" AutoGenerateColumns="False"
x:Name="datagrid" ItemsSource="{Binding Spawnables}"
x:Name="datagrid" ItemsSource="{Binding Spawnables}"
SelectedItem="{Binding SelectedItem}"
SelectedItems="{Binding SelectedItems, Mode=OneWayToSource}">
<DataGrid.ContextMenu>
<ContextMenu>
Expand Down Expand Up @@ -69,11 +70,11 @@
HorizontalAlignment="Center"
FontSize="16"/>
<GroupBox Grid.Row="1" Header="Cargo">
<controls:SpawnablePresetsGrid ItemsSource="{Binding Cargo}"
<controls:SpawnablePresetsGrid ItemsSource="{Binding Cargo}" SelectedItem="{Binding SelectedPreset, Mode=OneWayToSource}"
AvailablePresets="{Binding DataContext.AvailableCargoPresets, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>
</GroupBox>
<GroupBox Grid.Row="2" Header="Attachments">
<controls:SpawnablePresetsGrid ItemsSource="{Binding Attachments}"
<controls:SpawnablePresetsGrid ItemsSource="{Binding Attachments}" SelectedItem="{Binding SelectedPreset, Mode=OneWayToSource}"
AvailablePresets="{Binding DataContext.AvailableAttachmentsPresets, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>
</GroupBox>
</Grid>
Expand Down

0 comments on commit d51e6f5

Please sign in to comment.