Initial Commit

This commit is contained in:
Ben
2015-06-04 18:15:54 +01:00
commit 446fe51843
125 changed files with 10792 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
using System.Runtime.CompilerServices;
using Filtration.Annotations;
using GalaSoft.MvvmLight;
namespace Filtration.ViewModels
{
internal class FiltrationViewModelBase : ViewModelBase
{
/// This gives us the ReSharper option to transform an autoproperty into a property with change notification
/// Also leverages .net 4.5 callermembername attribute
[NotifyPropertyChangedInvocator]
protected override void RaisePropertyChanged([CallerMemberName]string property = "")
{
base.RaisePropertyChanged(property);
}
}
}

View File

@@ -0,0 +1,16 @@
using System.ComponentModel;
using Filtration.Models;
using GalaSoft.MvvmLight.CommandWpf;
namespace Filtration.ViewModels
{
internal interface ILootFilterBlockViewModel
{
void Initialise(LootFilterBlock lootFilterBlock);
bool IsDirty { get; set; }
LootFilterBlock Block { get; }
RelayCommand CopyBlockCommand { get; }
string BlockDescription { get; set; }
event PropertyChangedEventHandler PropertyChanged;
}
}

View File

@@ -0,0 +1,8 @@
namespace Filtration.ViewModels
{
internal interface ILootFilterBlockViewModelFactory
{
ILootFilterBlockViewModel Create();
void Release(ILootFilterBlockViewModel lootFilterBlockViewModel);
}
}

View File

@@ -0,0 +1,20 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using Filtration.Models;
namespace Filtration.ViewModels
{
internal interface ILootFilterScriptViewModel
{
ObservableCollection<ILootFilterBlockViewModel> LootFilterBlockViewModels { get; }
LootFilterScript Script { get; }
bool IsDirty { get; }
string Description { get; set; }
string Filename { get; }
string DisplayName { get; }
string Filepath { get; }
void Initialise(LootFilterScript lootFilterScript);
void RemoveDirtyFlag();
event PropertyChangedEventHandler PropertyChanged;
}
}

View File

@@ -0,0 +1,8 @@
namespace Filtration.ViewModels
{
internal interface ILootFilterScriptViewModelFactory
{
ILootFilterScriptViewModel Create();
void Release(ILootFilterScriptViewModel lootFilterScriptViewModel);
}
}

View File

@@ -0,0 +1,6 @@
namespace Filtration.ViewModels
{
internal interface IMainWindowViewModel
{
}
}

View File

@@ -0,0 +1,288 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Filtration.Models;
using Filtration.Models.BlockItemTypes;
using Filtration.Services;
using Filtration.Translators;
using GalaSoft.MvvmLight.CommandWpf;
namespace Filtration.ViewModels
{
internal class LootFilterBlockViewModel : FiltrationViewModelBase, ILootFilterBlockViewModel
{
private readonly ILootFilterBlockTranslator _translator;
private readonly IStaticDataService _staticDataService;
private readonly MediaPlayer _mediaPlayer = new MediaPlayer();
private bool _displaySettingsPopupOpen;
public LootFilterBlockViewModel(ILootFilterBlockTranslator translator, IStaticDataService staticDataService)
{
_translator = translator;
_staticDataService = staticDataService;
CopyBlockCommand = new RelayCommand(OnCopyConditionCommand);
AddFilterBlockItemCommand = new RelayCommand<Type>(OnAddFilterBlockItemCommand);
AddAudioVisualBlockItemCommand = new RelayCommand<Type>(OnAddAudioVisualBlockItemCommand);
RemoveFilterBlockItemCommand = new RelayCommand<ILootFilterBlockItem>(OnRemoveFilterBlockItemCommand);
RemoveAudioVisualBlockItemCommand = new RelayCommand<ILootFilterBlockItem>(OnRemoveAudioVisualBlockItemCommand);
PlaySoundCommand = new RelayCommand(OnPlaySoundCommand, () => HasSound);
}
public void Initialise(LootFilterBlock lootFilterBlock)
{
if (lootFilterBlock == null)
{
throw new ArgumentNullException("lootFilterBlock");
}
Block = lootFilterBlock;
lootFilterBlock.BlockItems.CollectionChanged += OnBlockItemsCollectionChanged;
foreach (var blockItem in lootFilterBlock.BlockItems.OfType<IAudioVisualBlockItem>())
{
blockItem.PropertyChanged += OnAudioVisualBlockItemChanged;
}
}
public RelayCommand CopyBlockCommand { get; private set; }
public RelayCommand<Type> AddFilterBlockItemCommand { get; private set; }
public RelayCommand<Type> AddAudioVisualBlockItemCommand { get; private set; }
public RelayCommand<ILootFilterBlockItem> RemoveFilterBlockItemCommand { get; private set; }
public RelayCommand<ILootFilterBlockItem> RemoveAudioVisualBlockItemCommand { get; private set; }
public RelayCommand PlaySoundCommand { get; private set; }
public LootFilterBlock Block { get; private set; }
public bool IsDirty { get; set; }
public ObservableCollection<ILootFilterBlockItem> FilterBlockItems
{
get { return Block.BlockItems; }
}
public IEnumerable<ILootFilterBlockItem> SummaryBlockItems
{
get { return Block.BlockItems.Where(b => !(b is IAudioVisualBlockItem)); }
}
public IEnumerable<ILootFilterBlockItem> AudioVisualBlockItems
{
get { return Block.BlockItems.Where(b => b is IAudioVisualBlockItem); }
}
public bool DisplaySettingsPopupOpen
{
get { return _displaySettingsPopupOpen; }
set
{
_displaySettingsPopupOpen = value;
RaisePropertyChanged();
}
}
public IEnumerable<string> AutoCompleteItemClasses
{
get { return _staticDataService.ItemClasses; }
}
public IEnumerable<string> AutoCompleteItemBaseTypes
{
get { return _staticDataService.ItemBaseTypes; }
}
public List<int> SoundsAvailable
{
get
{
return new List<int>
{
1,
2,
3,
4,
5,
6,
7,
8,
9
};
}
}
public List<Type> BlockItemTypesAvailable
{
get
{
return new List<Type>
{
typeof (ItemLevelBlockItem),
typeof (DropLevelBlockItem),
typeof (QualityBlockItem),
typeof (RarityBlockItem),
typeof (SocketsBlockItem),
typeof (LinkedSocketsBlockItem),
typeof (WidthBlockItem),
typeof (HeightBlockItem),
typeof (SocketGroupBlockItem),
typeof (ClassBlockItem),
typeof (BaseTypeBlockItem)
};
}
}
public List<Type> AudioVisualBlockItemTypesAvailable
{
get
{
return new List<Type>
{
typeof (TextColorBlockItem),
typeof (BackgroundColorBlockItem),
typeof (BorderColorBlockItem),
typeof (FontSizeBlockItem),
typeof (SoundBlockItem)
};
}
}
public string BlockDescription
{
get
{
return Block.Description;
}
set
{
Block.Description = value;
RaisePropertyChanged();
}
}
public bool HasTextColor
{
get { return FilterBlockItems.Count(b => b is TextColorBlockItem) > 0; }
}
public Color DisplayTextColor
{
get
{
return HasTextColor
? FilterBlockItems.OfType<TextColorBlockItem>().First().Color
: new Color { A = 255, R = 255, G = 255, B = 255 };
}
}
public bool HasBackgroundColor
{
get { return FilterBlockItems.Count(b => b is BackgroundColorBlockItem) > 0; }
}
public Color DisplayBackgroundColor
{
get
{
return HasBackgroundColor
? FilterBlockItems.OfType<BackgroundColorBlockItem>().First().Color
: new Color { A = 255, R = 0, G = 0, B = 0 };
}
}
public bool HasBorderColor
{
get { return FilterBlockItems.Count(b => b is BorderColorBlockItem) > 0; }
}
public Color DisplayBorderColor
{
get
{
return HasBorderColor
? FilterBlockItems.OfType<BorderColorBlockItem>().First().Color
: new Color { A = 255, R = 0, G = 0, B = 0 };
}
}
public bool HasFontSize
{
get { return FilterBlockItems.Count(b => b is FontSizeBlockItem) > 0; }
}
public bool HasSound
{
get { return FilterBlockItems.Count(b => b is SoundBlockItem) > 0; }
}
private void OnCopyConditionCommand()
{
Clipboard.SetText(_translator.TranslateLootFilterBlockToString(Block));
}
private void OnAddFilterBlockItemCommand(Type blockItemType)
{
if (!AddBlockItemAllowed(blockItemType)) return;
var newBlockItem = (ILootFilterBlockItem) Activator.CreateInstance(blockItemType);
FilterBlockItems.Add(newBlockItem);
IsDirty = true;
}
private void OnRemoveFilterBlockItemCommand(ILootFilterBlockItem blockItem)
{
FilterBlockItems.Remove(blockItem);
IsDirty = true;
}
private void OnAddAudioVisualBlockItemCommand(Type blockItemType)
{
if (!AddBlockItemAllowed(blockItemType)) return;
var newBlockItem = (ILootFilterBlockItem) Activator.CreateInstance(blockItemType);
newBlockItem.PropertyChanged += OnAudioVisualBlockItemChanged;
FilterBlockItems.Add(newBlockItem);
OnAudioVisualBlockItemChanged(null, null);
IsDirty = true;
}
private void OnRemoveAudioVisualBlockItemCommand(ILootFilterBlockItem blockItem)
{
blockItem.PropertyChanged -= OnAudioVisualBlockItemChanged;
FilterBlockItems.Remove(blockItem);
OnAudioVisualBlockItemChanged(null, null);
IsDirty = true;
}
private bool AddBlockItemAllowed(Type type)
{
var blockItem = (ILootFilterBlockItem)Activator.CreateInstance(type);
var blockCount = FilterBlockItems.Count(b => b.GetType() == type);
return blockCount < blockItem.MaximumAllowed;
}
private void OnPlaySoundCommand()
{
var soundUri = "Resources/AlertSound" + FilterBlockItems.OfType<SoundBlockItem>().First().Value + ".wav";
_mediaPlayer.Open(new Uri(soundUri, UriKind.Relative));
_mediaPlayer.Play();
}
private void OnAudioVisualBlockItemChanged(object sender, EventArgs e)
{
RaisePropertyChanged("DisplayTextColor");
RaisePropertyChanged("DisplayBackgroundColor");
RaisePropertyChanged("DisplayBorderColor");
RaisePropertyChanged("HasSound");
}
private void OnBlockItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
RaisePropertyChanged("SummaryBlockItems");
RaisePropertyChanged("AudioVisualBlockItems");
}
}
}

View File

@@ -0,0 +1,100 @@
using System.Collections.Generic;
using System.Windows.Media;
using Filtration.Enums;
using Filtration.Models;
namespace Filtration.ViewModels
{
internal class LootFilterScriptViewModela : ILootFilterScriptViewModel
{
private readonly ILootFilterConditionViewModelFactory _lootFilterConditionViewModelFactory;
public LootFilterScriptViewModela(ILootFilterConditionViewModelFactory lootFilterConditionViewModelFactory)
{
_lootFilterConditionViewModelFactory = lootFilterConditionViewModelFactory;
LootFilterConditionViewModels = new List<ILootFilterConditionViewModel>();
var testCondition = new LootFilterCondition
{
BackgroundColor = Colors.DarkCyan,
TextColor = Colors.White,
BorderColor = Colors.Red,
Sockets =
new NumericFilterPredicate(FilterPredicateOperator.LessThanOrEqual, 5),
LinkedSockets =
new NumericFilterPredicate(FilterPredicateOperator.Equal, 2),
ItemRarity = new NumericFilterPredicate(FilterPredicateOperator.GreaterThan, (int) ItemRarity.Magic),
FontSize = 10,
Quality =
new NumericFilterPredicate(FilterPredicateOperator.GreaterThanOrEqual, 15),
ItemLevel =
new NumericFilterPredicate(FilterPredicateOperator.GreaterThan, 50),
DropLevel = new NumericFilterPredicate(),
FilterDescription = "My Wicked Filter"
};
var testCondition2 = new LootFilterCondition
{
BackgroundColor = Colors.Beige,
TextColor = Colors.Blue,
BorderColor = Colors.Black,
Sockets =
new NumericFilterPredicate(FilterPredicateOperator.LessThan, 4),
LinkedSockets =
new NumericFilterPredicate(FilterPredicateOperator.GreaterThanOrEqual, 3),
FontSize = 12,
Quality =
new NumericFilterPredicate(FilterPredicateOperator.GreaterThanOrEqual, 15),
ItemLevel =
new NumericFilterPredicate(FilterPredicateOperator.Equal, 32),
DropLevel = new NumericFilterPredicate(FilterPredicateOperator.GreaterThanOrEqual, 85),
FilterDescription = "This is a test filter"
};
var testCondition3 = new LootFilterCondition
{
BackgroundColor = Colors.Beige,
TextColor = new Color { A = 128, R = 0, G = 0, B = 255},
BorderColor = Colors.Black,
Sockets =
new NumericFilterPredicate(FilterPredicateOperator.LessThan, 4),
LinkedSockets =
new NumericFilterPredicate(FilterPredicateOperator.GreaterThanOrEqual, 3),
FontSize = 12,
Quality =
new NumericFilterPredicate(FilterPredicateOperator.GreaterThanOrEqual, 15),
ItemLevel =
new NumericFilterPredicate(FilterPredicateOperator.Equal, 32),
DropLevel = new NumericFilterPredicate(FilterPredicateOperator.GreaterThanOrEqual, 85),
FilterDescription = "This is a test filter"
};
var testConditionFilter = _lootFilterConditionViewModelFactory.Create();
var testConditionFilter2 = _lootFilterConditionViewModelFactory.Create();
var testConditionFilter3 = _lootFilterConditionViewModelFactory.Create();
testConditionFilter.Initialise(testCondition);
testConditionFilter2.Initialise(testCondition2);
testConditionFilter3.Initialise(testCondition3);
testConditionFilter.Classes.Add("Test Class 1");
testConditionFilter.Classes.Add("Test Class 2");
testConditionFilter.Classes.Add("Test Class 3");
testConditionFilter.Classes.Add("Test Class 4");
testConditionFilter.Classes.Add("Test Class 5");
testConditionFilter.Classes.Add("Test Class 6");
testConditionFilter.Classes.Add("Test Class 7");
testConditionFilter.Classes.Add("Test Class 8");
testConditionFilter.Classes.Add("Test Class 9");
testConditionFilter.Classes.Add("Test Class 10");
testConditionFilter.BaseTypes.Add("Test Base Type 1");
testConditionFilter.BaseTypes.Add("Test Base Type 2");
LootFilterConditionViewModels.Add(testConditionFilter);
LootFilterConditionViewModels.Add(testConditionFilter2);
LootFilterConditionViewModels.Add(testConditionFilter3);
}
public List<ILootFilterConditionViewModel> LootFilterConditionViewModels { get; private set; }
}
}

View File

@@ -0,0 +1,225 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using Filtration.Models;
using GalaSoft.MvvmLight.CommandWpf;
namespace Filtration.ViewModels
{
internal class LootFilterScriptViewModel : FiltrationViewModelBase, ILootFilterScriptViewModel
{
private readonly ILootFilterBlockViewModelFactory _lootFilterBlockViewModelFactory;
private bool _isDirty;
public LootFilterScriptViewModel(ILootFilterBlockViewModelFactory lootFilterBlockViewModelFactory )
{
DeleteBlockCommand = new RelayCommand(OnDeleteBlock, () => SelectedBlockViewModel != null);
MoveBlockToTopCommand = new RelayCommand(OnMoveBlockToTopCommand, () => SelectedBlockViewModel != null);
MoveBlockUpCommand = new RelayCommand(OnMoveBlockUpCommand, () => SelectedBlockViewModel != null);
MoveBlockDownCommand = new RelayCommand(OnMoveBlockDownCommand, () => SelectedBlockViewModel != null);
MoveBlockToBottomCommand = new RelayCommand(OnMoveBlockToBottomCommand, () => SelectedBlockViewModel != null);
AddBlockAboveCommand = new RelayCommand(OnAddBlockAboveCommand, () => SelectedBlockViewModel != null || LootFilterBlockViewModels.Count == 0);
AddBlockBelowCommand = new RelayCommand(OnAddBlockBelowCommand, () => SelectedBlockViewModel != null);
AddSectionAboveCommand = new RelayCommand(OnAddSectionAboveCommand, () => SelectedBlockViewModel != null);
_lootFilterBlockViewModelFactory = lootFilterBlockViewModelFactory;
LootFilterBlockViewModels = new ObservableCollection<ILootFilterBlockViewModel>();
}
public RelayCommand DeleteBlockCommand { get; private set; }
public RelayCommand MoveBlockToTopCommand { get; private set; }
public RelayCommand MoveBlockUpCommand { get; private set; }
public RelayCommand MoveBlockDownCommand { get; private set; }
public RelayCommand MoveBlockToBottomCommand { get; private set; }
public RelayCommand AddBlockAboveCommand { get; private set; }
public RelayCommand AddBlockBelowCommand { get; private set; }
public RelayCommand AddSectionAboveCommand { get; private set; }
public ObservableCollection<ILootFilterBlockViewModel> LootFilterBlockViewModels { get; private set; }
public string Description
{
get { return Script.Description; }
set
{
Script.Description = value;
_isDirty = true;
RaisePropertyChanged();
}
}
public LootFilterBlockViewModel SelectedBlockViewModel { get; set; }
public LootFilterScript Script { get; private set; }
public bool IsDirty
{
get { return _isDirty || HasDirtyChildren; }
set { _isDirty = value; }
}
private bool HasDirtyChildren
{
get { return LootFilterBlockViewModels.Any(vm => vm.IsDirty); }
}
private void CleanChildren()
{
foreach (var vm in LootFilterBlockViewModels)
{
vm.IsDirty = false;
}
}
public void RemoveDirtyFlag()
{
CleanChildren();
IsDirty = false;
}
public string DisplayName
{
get { return !string.IsNullOrEmpty(Filename) ? Filename : Description; }
}
public string Filename
{
get { return Path.GetFileName(Script.FilePath); }
}
public string Filepath
{
get { return Script.FilePath; }
}
public void Initialise(LootFilterScript lootFilterScript)
{
LootFilterBlockViewModels.Clear();
Script = lootFilterScript;
foreach (var block in Script.LootFilterBlocks)
{
var vm = _lootFilterBlockViewModelFactory.Create();
vm.Initialise(block);
LootFilterBlockViewModels.Add(vm);
}
}
private void OnMoveBlockToTopCommand()
{
var currentIndex = LootFilterBlockViewModels.IndexOf(SelectedBlockViewModel);
if (currentIndex > 0)
{
var block = SelectedBlockViewModel.Block;
Script.LootFilterBlocks.Remove(block);
Script.LootFilterBlocks.Insert(0, block);
LootFilterBlockViewModels.Move(currentIndex, 0);
_isDirty = true;
}
}
private void OnMoveBlockUpCommand()
{
var currentIndex = LootFilterBlockViewModels.IndexOf(SelectedBlockViewModel);
if (currentIndex > 0)
{
var block = SelectedBlockViewModel.Block;
var blockPos = Script.LootFilterBlocks.IndexOf(block);
Script.LootFilterBlocks.RemoveAt(blockPos);
Script.LootFilterBlocks.Insert(blockPos - 1, block);
LootFilterBlockViewModels.Move(currentIndex, currentIndex - 1);
_isDirty = true;
}
}
private void OnMoveBlockDownCommand()
{
var currentIndex = LootFilterBlockViewModels.IndexOf(SelectedBlockViewModel);
if (currentIndex < LootFilterBlockViewModels.Count - 1)
{
var block = SelectedBlockViewModel.Block;
var blockPos = Script.LootFilterBlocks.IndexOf(block);
Script.LootFilterBlocks.RemoveAt(blockPos);
Script.LootFilterBlocks.Insert(blockPos + 1, block);
LootFilterBlockViewModels.Move(currentIndex, currentIndex + 1);
_isDirty = true;
}
}
private void OnMoveBlockToBottomCommand()
{
var currentIndex = LootFilterBlockViewModels.IndexOf(SelectedBlockViewModel);
if (currentIndex < LootFilterBlockViewModels.Count - 1)
{
var block = SelectedBlockViewModel.Block;
Script.LootFilterBlocks.Remove(block);
Script.LootFilterBlocks.Add(block);
LootFilterBlockViewModels.Move(currentIndex, LootFilterBlockViewModels.Count - 1);
_isDirty = true;
}
}
private void OnAddBlockAboveCommand()
{
var vm = _lootFilterBlockViewModelFactory.Create();
var newBlock = new LootFilterBlock();
vm.Initialise(newBlock);
if (LootFilterBlockViewModels.Count > 0)
{
Script.LootFilterBlocks.Insert(Script.LootFilterBlocks.IndexOf(SelectedBlockViewModel.Block), newBlock);
LootFilterBlockViewModels.Insert(LootFilterBlockViewModels.IndexOf(SelectedBlockViewModel), vm);
}
else
{
Script.LootFilterBlocks.Add(newBlock);
LootFilterBlockViewModels.Add(vm);
}
_isDirty = true;
}
private void OnAddBlockBelowCommand()
{
var vm = _lootFilterBlockViewModelFactory.Create();
var newBlock = new LootFilterBlock();
vm.Initialise(newBlock);
Script.LootFilterBlocks.Insert(Script.LootFilterBlocks.IndexOf(SelectedBlockViewModel.Block) + 1, newBlock);
LootFilterBlockViewModels.Insert(LootFilterBlockViewModels.IndexOf(SelectedBlockViewModel) + 1, vm);
_isDirty = true;
}
private void OnAddSectionAboveCommand()
{
var vm = _lootFilterBlockViewModelFactory.Create();
var newSection = new LootFilterSection { Description = "New Section" };
vm.Initialise(newSection);
Script.LootFilterBlocks.Insert(Script.LootFilterBlocks.IndexOf(SelectedBlockViewModel.Block), newSection);
LootFilterBlockViewModels.Insert(LootFilterBlockViewModels.IndexOf(SelectedBlockViewModel), vm);
_isDirty = true;
}
private void OnDeleteBlock()
{
var result = MessageBox.Show("Are you sure you wish to delete this block?", "Delete Confirmation",
MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
Script.LootFilterBlocks.Remove(SelectedBlockViewModel.Block);
LootFilterBlockViewModels.Remove(SelectedBlockViewModel);
_isDirty = true;
}
SelectedBlockViewModel = null;
}
}
}

View File

@@ -0,0 +1,244 @@
using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Castle.Core;
using Filtration.Models;
using Filtration.Services;
using Filtration.Translators;
using GalaSoft.MvvmLight.CommandWpf;
using Clipboard = System.Windows.Clipboard;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
namespace Filtration.ViewModels
{
internal class MainWindowViewModel : FiltrationViewModelBase, IMainWindowViewModel
{
private LootFilterScript _loadedScript;
private readonly ILootFilterScriptViewModelFactory _lootFilterScriptViewModelFactory;
private readonly ILootFilterPersistenceService _persistenceService;
private readonly ILootFilterScriptTranslator _lootFilterScriptTranslator;
private ILootFilterScriptViewModel _currentScriptViewModel;
private readonly ObservableCollection<ILootFilterScriptViewModel> _scriptViewModels;
public MainWindowViewModel(ILootFilterScriptViewModelFactory lootFilterScriptViewModelFactory,
ILootFilterPersistenceService persistenceService, ILootFilterScriptTranslator lootFilterScriptTranslator)
{
_lootFilterScriptViewModelFactory = lootFilterScriptViewModelFactory;
_persistenceService = persistenceService;
_lootFilterScriptTranslator = lootFilterScriptTranslator;
_scriptViewModels = new ObservableCollection<ILootFilterScriptViewModel>();
OpenScriptCommand = new RelayCommand(OnOpenScriptCommand);
SaveScriptCommand = new RelayCommand(OnSaveScriptCommand, () => CurrentScriptViewModel != null);
SaveScriptAsCommand = new RelayCommand(OnSaveScriptAsCommand, () => CurrentScriptViewModel != null);
CopyScriptCommand = new RelayCommand(OnCopyScriptCommand, () => CurrentScriptViewModel != null);
NewScriptCommand = new RelayCommand(OnNewScriptCommand);
CloseScriptCommand = new RelayCommand<ILootFilterScriptViewModel>(OnCloseScriptCommand, v => CurrentScriptViewModel != null);
LoadScriptFromFile("C:\\ThioleLootFilter.txt");
SetLootFilterScriptDirectory();
}
public RelayCommand OpenScriptCommand { get; private set; }
public RelayCommand SaveScriptCommand { get; private set; }
public RelayCommand SaveScriptAsCommand { get; private set; }
public RelayCommand CopyScriptCommand { get; private set; }
public RelayCommand NewScriptCommand { get; private set; }
public RelayCommand<ILootFilterScriptViewModel> CloseScriptCommand { get; private set; }
public ObservableCollection<ILootFilterScriptViewModel> ScriptViewModels
{
get { return _scriptViewModels; }
}
[DoNotWire]
public ILootFilterScriptViewModel CurrentScriptViewModel
{
get { return _currentScriptViewModel; }
set
{
_currentScriptViewModel = value;
RaisePropertyChanged();
SaveScriptCommand.RaiseCanExecuteChanged();
SaveScriptAsCommand.RaiseCanExecuteChanged();
}
}
private void OnOpenScriptCommand()
{
var openFileDialog = new OpenFileDialog
{
Filter = "Filter Files (*.filter)|*.filter|All Files (*.*)|*.*",
InitialDirectory = _persistenceService.LootFilterScriptDirectory
};
if (openFileDialog.ShowDialog() != true) return;
LoadScriptFromFile(openFileDialog.FileName);
}
private void LoadScriptFromFile(string path)
{
try
{
_loadedScript = _persistenceService.LoadLootFilterScript(path);
}
catch (Exception e)
{
MessageBox.Show(@"Error loading filter script - " + e.Message, @"Script Load Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
var newViewModel = _lootFilterScriptViewModelFactory.Create();
newViewModel.Initialise(_loadedScript);
ScriptViewModels.Add(newViewModel);
CurrentScriptViewModel = newViewModel;
}
private void SetLootFilterScriptDirectory()
{
var defaultDir = _persistenceService.DefaultPathOfExileDirectory();
if (!string.IsNullOrEmpty(defaultDir))
{
_persistenceService.LootFilterScriptDirectory = defaultDir;
}
else
{
var dlg = new FolderBrowserDialog
{
Description = @"Select your Path of Exile data directory, usually in Documents\My Games",
ShowNewFolderButton = false
};
var result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
_persistenceService.LootFilterScriptDirectory = dlg.SelectedPath;
}
}
}
private void OnSaveScriptCommand()
{
if (!ValidateScript()) return;
if (string.IsNullOrEmpty(CurrentScriptViewModel.Script.FilePath))
{
OnSaveScriptAsCommand();
return;
}
try
{
_persistenceService.SaveLootFilterScript(CurrentScriptViewModel.Script);
CurrentScriptViewModel.RemoveDirtyFlag();
}
catch (Exception e)
{
MessageBox.Show(@"Error saving filter file - " + e.Message, @"Save Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void OnSaveScriptAsCommand()
{
if (!ValidateScript()) return;
var saveDialog = new SaveFileDialog
{
DefaultExt = ".filter",
Filter = @"Filter Files (*.filter)|*.filter|All Files (*.*)|*.*",
InitialDirectory = _persistenceService.LootFilterScriptDirectory
};
var result = saveDialog.ShowDialog();
if (result != DialogResult.OK) return;
var previousFilePath = CurrentScriptViewModel.Script.FilePath;
try
{
CurrentScriptViewModel.Script.FilePath = saveDialog.FileName;
_persistenceService.SaveLootFilterScript(CurrentScriptViewModel.Script);
CurrentScriptViewModel.RemoveDirtyFlag();
}
catch (Exception e)
{
MessageBox.Show(@"Error saving filter file - " + e.Message, @"Save Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
CurrentScriptViewModel.Script.FilePath = previousFilePath;
}
}
private bool ValidateScript()
{
var result = CurrentScriptViewModel.Script.Validate();
if (result.Count == 0) return true;
var failures = string.Empty;
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (string failure in result)
{
failures += failure + Environment.NewLine;
}
MessageBox.Show(@"The following script validation errors occurred:" + Environment.NewLine + failures,
@"Script Validation Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
private void OnCopyScriptCommand()
{
Clipboard.SetText(_lootFilterScriptTranslator.TranslateLootFilterScriptToString(_loadedScript));
}
private void OnNewScriptCommand()
{
var newScript = new LootFilterScript();
var newViewModel = _lootFilterScriptViewModelFactory.Create();
newViewModel.Initialise(newScript);
newViewModel.Description = "New Script";
ScriptViewModels.Add(newViewModel);
CurrentScriptViewModel = newViewModel;
}
private void OnCloseScriptCommand(ILootFilterScriptViewModel scriptViewModel)
{
CurrentScriptViewModel = scriptViewModel;
if (!CurrentScriptViewModel.IsDirty)
{
ScriptViewModels.Remove(CurrentScriptViewModel);
}
else
{
var result = MessageBox.Show(@"Want to save your changes to this script?",
@"Filtration", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (result)
{
case DialogResult.Yes:
{
OnSaveScriptCommand();
ScriptViewModels.Remove(CurrentScriptViewModel);
break;
}
case DialogResult.No:
{
ScriptViewModels.Remove(CurrentScriptViewModel);
break;
}
case DialogResult.Cancel:
{
break;
}
}
}
}
}
}