Skip to content

Commit

Permalink
Add items import from classnames
Browse files Browse the repository at this point in the history
* Import items to types.xml from list of class names;
* Import items to trader config from list of class names
  • Loading branch information
rvost committed Oct 11, 2022
1 parent 2e013a4 commit 7eff985
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using DayzServerTools.Application.Stores;

namespace DayzServerTools.Application.Models;

public interface IClassnameImportDialog
{
IClassnameImportStore Store { get; set; }
bool? ShowDialog();
}
1 change: 1 addition & 0 deletions src/DayzServerTools.Application/Services/IDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface IDialogFactory
IFileDialog CreateOpenFileDialog();
IFileDialog CreateSaveFileDialog();
IExportDialog CreateExportDialog();
IClassnameImportDialog CreateClassnameImportDialog();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using DayzServerTools.Application.Extensions;
using DayzServerTools.Application.ViewModels;
using DayzServerTools.Library.Trader;
using DayzServerTools.Library.Xml;

namespace DayzServerTools.Application.Stores;

public interface IClassnameImportStore
{
void Accept(IEnumerable<string> classnames);
}

public class ItemTypesClassnameImportStore : IClassnameImportStore
{
private readonly ItemTypesViewModel _viewModel;

public ItemTypesClassnameImportStore(ItemTypesViewModel viewModel)
{
_viewModel = viewModel;
}

public void Accept(IEnumerable<string> classnames)
{
var itemTypes = classnames.Select(name => new ItemType() { Name = name });
_viewModel.CopyItemTypes(itemTypes);
}
}

public class TraderCategoryClassnameImportStore : IClassnameImportStore
{
private readonly TraderCategoryViewModel _viewModel;

public TraderCategoryClassnameImportStore(TraderCategoryViewModel viewModel)
{
_viewModel = viewModel;
}

public void Accept(IEnumerable<string> classnames)
{
var traderItems = classnames.Select(name => new TraderItem() { Name = name });
var viewModels = traderItems.Select(item => new TraderItemViewModel(item));

_viewModel.Items.AddRange(viewModels);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

using DayzServerTools.Application.Stores;

namespace DayzServerTools.Application.ViewModels;

public partial class ClassnamesImportViewModel : ObservableObject
{
private readonly IClassnameImportStore _importStore;
[ObservableProperty]
private string rawInput = "";
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(ImportCommand))]
private IEnumerable<string> classnames = new List<string>();

public IRelayCommand<IEnumerable<string>> ImportCommand { get; }
public IRelayCommand ParseCommand { get; }
public event EventHandler CloseRequested;

public ClassnamesImportViewModel(IClassnameImportStore importStore)
{
_importStore = importStore;

ImportCommand = new RelayCommand<IEnumerable<string>>(Import, CanImport);
ParseCommand = new RelayCommand(Parse);
}

protected bool CanImport(IEnumerable<string> classnames)
=> classnames?.Any() ?? false;
protected void Import(IEnumerable<string> classnames)
{
_importStore.Accept(classnames);
CloseRequested?.Invoke(this, EventArgs.Empty);
}

protected void Parse()
{
if (!string.IsNullOrWhiteSpace(rawInput))
{
var regex = new Regex(@"\w+");
Classnames = regex.Matches(RawInput).Select(m => m.Value).ToList();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public partial class ItemTypesViewModel : ProjectFileViewModel<ItemTypes>, IDisp
public IRelayCommand<float?> AdjustRestockCommand { get; }
public IRelayCommand ExportToNewFileCommand { get; }
public IRelayCommand ExportToTraderCommand { get; }
public IRelayCommand ClassnamesImportCommand { get; }
public IRelayCommand<VanillaFlag> SetCategoryCommand { get; }
public IRelayCommand<UserDefinableFlag> AddValueFlagCommand { get; }
public IRelayCommand<UserDefinableFlag> AddUsageFlagCommand { get; }
Expand All @@ -66,6 +67,7 @@ public ItemTypesViewModel(IDialogFactory dialogFactory) : base(dialogFactory)
AdjustRestockCommand = new RelayCommand<float?>(AdjustRestock, (param) => CanExecuteBatchCommand());
ExportToNewFileCommand = new RelayCommand<object>(ExportToNewFile, CanExecuteExportCommand);
ExportToTraderCommand = new RelayCommand<object>(ExportToTrader, CanExecuteExportCommand);
ClassnamesImportCommand = new RelayCommand(ClassnamesImport);
SetCategoryCommand = new RelayCommand<VanillaFlag>(SetCategory, (param) => CanExecuteBatchCommand());
AddValueFlagCommand = new RelayCommand<UserDefinableFlag>(AddValueFlag, (param) => CanExecuteBatchCommand());
AddUsageFlagCommand = new RelayCommand<UserDefinableFlag>(AddUsageFlag, (param) => CanExecuteBatchCommand());
Expand Down Expand Up @@ -149,6 +151,12 @@ protected void ExportToTrader(object cmdParam)
dialog.Store = new ItemTypesToTraderExportStore(items);
dialog.ShowDialog();
}
protected void ClassnamesImport()
{
var dialog = _dialogFactory.CreateClassnameImportDialog();
dialog.Store = new ItemTypesClassnameImportStore(this);
dialog.ShowDialog();
}
protected void SetCategory(VanillaFlag category)
{
if (category == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public string Name
}
public ObservableCollection<TraderItemViewModel> Items { get; }

public IRelayCommand ClassnamesImportCommand { get; }
public IRelayCommand CopyItemsCommand { get; }
public IRelayCommand MoveItemsCommand { get; }
public IRelayCommand ProhibitSellingCommand { get; }
Expand All @@ -46,6 +47,7 @@ public TraderCategoryViewModel(TraderCategory model)
this.model = model;
Items = new(model.TraderItems.Select(m => new TraderItemViewModel(m)));

ClassnamesImportCommand = new RelayCommand(ClassnamesImport);
CopyItemsCommand = new RelayCommand(CopyItems, () => CanExecuteBatchCommand());
MoveItemsCommand = new RelayCommand(MoveItems, () => CanExecuteBatchCommand());
ProhibitBuyingCommand = new RelayCommand(ProhibitBuying, () => CanExecuteBatchCommand());
Expand Down Expand Up @@ -81,6 +83,12 @@ private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEven
public TraderCategoryViewModel() : this(new TraderCategory()) { }

protected bool CanExecuteBatchCommand() => SelectedItems is not null;
protected void ClassnamesImport()
{
var dialog = _dialogFactory.CreateClassnameImportDialog();
dialog.Store = new TraderCategoryClassnameImportStore(this);
dialog.ShowDialog();
}
protected void CopyItems()
{
if (SelectedItems is null)
Expand Down
18 changes: 18 additions & 0 deletions src/DayzServerTools.Windows/Models/WindowsClassnameImportDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using DayzServerTools.Application.Models;
using DayzServerTools.Application.Stores;
using DayzServerTools.Application.ViewModels;
using DayzServerTools.Windows.Views;

namespace DayzServerTools.Windows.Models;

internal class WindowsClassnameImportDialog : IClassnameImportDialog
{
public IClassnameImportStore Store { get; set; }

public bool? ShowDialog()
{
var vm = new ClassnamesImportViewModel(Store);
var window = new ClassnamesImportView(vm);
return window.ShowDialog();
}
}
25 changes: 25 additions & 0 deletions src/DayzServerTools.Windows/Resources/IconsDictionary.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,31 @@
</Rectangle>
</Viewbox>

<Viewbox x:Key="ImportVecIcon" x:Shared="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
<Rectangle Width="16 " Height="16">
<Rectangle.Resources>
<SolidColorBrush x:Key="canvas" Opacity="0" />
<SolidColorBrush x:Key="light-blue" Color="#005dba" Opacity="1" />
</Rectangle.Resources>
<Rectangle.Fill>
<DrawingBrush Stretch="None">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup x:Name="canvas">
<GeometryDrawing Brush="{DynamicResource canvas}" Geometry="F1M16,16H0V0H16Z" />
</DrawingGroup>
<DrawingGroup x:Name="level_1">
<GeometryDrawing Brush="{DynamicResource light-blue}" Geometry="F1M10.354,14.354l-.708-.708L12.293,11H5.5a4.5,4.5,0,0,1,0-9V3a3.5,3.5,0,0,0,0,7h6.793L9.646,7.354l.708-.708,3.5,3.5v.708Z" />
</DrawingGroup>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
</Viewbox>

<Viewbox x:Key="NewFlagsVecIcon" x:Shared="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
Expand Down
3 changes: 3 additions & 0 deletions src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ internal class WindowsDialogFactory : IDialogFactory
{
private string defaultFilter = "XML Files (*.xml)|*.xml";

public IClassnameImportDialog CreateClassnameImportDialog()
=> new WindowsClassnameImportDialog();

public IExportDialog CreateExportDialog()
=> new WindowsExportDialog();

Expand Down
31 changes: 31 additions & 0 deletions src/DayzServerTools.Windows/Views/ClassnamesImportView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Window x:Class="DayzServerTools.Windows.Views.ClassnamesImportView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DayzServerTools.Windows.Views"
mc:Ignorable="d"
Title="Import items from class names"
WindowStartupLocation="CenterScreen"
WindowStyle="ToolWindow"
Height="450" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Header="Paste class names:">
<TextBox Text="{Binding RawInput}" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
AcceptsReturn="True"/>
</GroupBox>
<Button Grid.Row="1" Content="Parse" Command="{Binding ParseCommand}"/>
<GroupBox Grid.Row="2" Header="Items to be imported:">
<ListView ItemsSource="{Binding Classnames}"/>
</GroupBox>
<Button Grid.Row="3" Content="Import" Command="{Binding ImportCommand}"
CommandParameter="{Binding Classnames}"/>
</Grid>
</Window>
39 changes: 39 additions & 0 deletions src/DayzServerTools.Windows/Views/ClassnamesImportView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using DayzServerTools.Application.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DayzServerTools.Windows.Views
{
/// <summary>
/// Interaction logic for ClassnamesImportView.xaml
/// </summary>
public partial class ClassnamesImportView : Window
{
private readonly ClassnamesImportViewModel _model;
public ClassnamesImportView(ClassnamesImportViewModel model)
{
InitializeComponent();
_model = model;
DataContext = _model;
_model.CloseRequested += OnCloseRequested;
}

private void OnCloseRequested(object sender, EventArgs e)
{
DialogResult = true;
_model.CloseRequested -= OnCloseRequested;
Close();
}
}
}
6 changes: 6 additions & 0 deletions src/DayzServerTools.Windows/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@
<fluent:RibbonGroupBox Header="Edit">
<fluent:Button Header="Add New Item" Icon="{StaticResource AddVecIcon}"
Command="{Binding ActiveFile.AddEmptyItemCommand}"/>
<fluent:Button Header="Import Classnames" Icon="{StaticResource ImportVecIcon}"
Command="{Binding ActiveFile.ClassnamesImportCommand}"/>
</fluent:RibbonGroupBox>
<fluent:RibbonGroupBox Header="Export">
<fluent:Button Header="To New types.xml" Icon="{StaticResource NewItemTypesVecIcon}"
Expand Down Expand Up @@ -355,6 +357,10 @@
Command="{Binding ActiveFile.SelectedTrader.RemoveCategoryCommand}"
CommandParameter="{x:Null}"/>
</fluent:RibbonGroupBox>
<fluent:RibbonGroupBox Header="Items">
<fluent:Button Header="Import Classnames" Icon="{StaticResource ImportVecIcon}"
Command="{Binding ActiveFile.SelectedTrader.SelectedCategory.ClassnamesImportCommand}"/>
</fluent:RibbonGroupBox>
<fluent:RibbonGroupBox Header="Items Quantity">
<fluent:ComboBox x:Name="quantityModifierComboBox" Header="Quantity"
ItemsSource="{Binding ActiveFile.AvailableModifiers}"
Expand Down

0 comments on commit 7eff985

Please sign in to comment.