Refactored script to text translation

This commit is contained in:
Ben 2015-06-23 22:21:10 +01:00
parent 29ed02e172
commit 1be6fe9e7b
13 changed files with 76 additions and 141 deletions

View File

@ -1186,6 +1186,7 @@ namespace Filtration.Tests.Translators
Assert.AreEqual(expectedResult, result);
}
[Ignore("This test fails after the refactoring to use OutputText in the translator, but this situation shouldn't ever occur anyway")]
[Test]
public void TranslateItemFilterBlockToString_MultipleFontSize_UsesFirstFontSize()
{

View File

@ -153,7 +153,6 @@
<Compile Include="Models\ItemFilterScript.cs" />
<Compile Include="Models\ItemFilterSection.cs" />
<Compile Include="Models\NumericFilterPredicate.cs" />
<Compile Include="Models\BlockItemBaseTypes\SocketGroupBlockItemBase.cs" />
<Compile Include="Models\ReplaceColorsParameterSet.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Repositories\ItemFilterScriptRepository.cs" />

View File

@ -1,5 +1,6 @@
using System.Windows.Media;
using Filtration.Enums;
using Filtration.Extensions;
namespace Filtration.Models.BlockItemBaseTypes
{
@ -25,6 +26,11 @@ namespace Filtration.Models.BlockItemBaseTypes
}
}
public override string OutputText
{
get { return Action.GetAttributeDescription(); }
}
public override string PrefixText
{
get { return string.Empty; }

View File

@ -8,6 +8,7 @@ namespace Filtration.Models.BlockItemBaseTypes
abstract class BlockItemBase : IItemFilterBlockItem
{
public abstract string PrefixText { get; }
public abstract string OutputText { get; }
public abstract int MaximumAllowed { get; }
public abstract string DisplayHeading { get; }
public abstract string SummaryText { get; }

View File

@ -15,6 +15,15 @@ namespace Filtration.Models.BlockItemBaseTypes
Color = color;
}
public override string OutputText
{
get
{
return PrefixText + " " + +Color.R + " " + Color.G + " "
+ Color.B + (Color.A < 255 ? " " + Color.A : string.Empty);
}
}
public override string SummaryText
{
get { return string.Empty; }

View File

@ -17,6 +17,11 @@ namespace Filtration.Models.BlockItemBaseTypes
SecondValue = secondValue;
}
public override string OutputText
{
get { return PrefixText + " " + Value + " " + SecondValue; }
}
public override string SummaryText { get { return string.Empty; } }
public override Color SummaryBackgroundColor { get { return Colors.Transparent; } }
public override Color SummaryTextColor { get { return Colors.Transparent; } }

View File

@ -15,6 +15,11 @@ namespace Filtration.Models.BlockItemBaseTypes
Value = value;
}
public override string OutputText
{
get { return PrefixText + " " + Value; }
}
public override string SummaryText { get { return string.Empty; } }
public override Color SummaryBackgroundColor { get { return Colors.Transparent; } }
public override Color SummaryTextColor { get { return Colors.Transparent; } }

View File

@ -1,5 +1,6 @@
using System;
using Filtration.Enums;
using Filtration.Extensions;
namespace Filtration.Models.BlockItemBaseTypes
{
@ -19,6 +20,15 @@ namespace Filtration.Models.BlockItemBaseTypes
FilterPredicate.PropertyChanged += OnFilterPredicateChanged;
}
public override string OutputText
{
get
{
return PrefixText + " " + FilterPredicate.PredicateOperator.GetAttributeDescription() +
" " + FilterPredicate.PredicateOperand;
}
}
public abstract int Minimum { get; }
public abstract int Maximum { get; }

View File

@ -1,22 +0,0 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Filtration.Enums;
namespace Filtration.Models.BlockItemBaseTypes
{
internal abstract class SocketGroupBlockItemBase : BlockItemBase
{
protected SocketGroupBlockItemBase()
{
SocketColorGroups = new ObservableCollection<List<SocketColor>>();
SocketColorGroups.CollectionChanged += OnSocketColorGroupsCollectionChanged;
}
public ObservableCollection<List<SocketColor>> SocketColorGroups { get; private set; }
private void OnSocketColorGroupsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged("SocketColorGroups");
}
}
}

View File

@ -1,5 +1,7 @@
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
namespace Filtration.Models.BlockItemBaseTypes
{
@ -11,6 +13,22 @@ namespace Filtration.Models.BlockItemBaseTypes
Items.CollectionChanged += OnItemsCollectionChanged;
}
public override string OutputText
{
get
{
var enumerable = Items as IList<string> ?? Items.ToList();
if (enumerable.Count > 0)
{
return PrefixText + " " +
string.Format("\"{0}\"",
string.Join("\" \"", enumerable.ToArray()));
}
return string.Empty;
}
}
public ObservableCollection<string> Items { get; protected set; }
private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)

View File

@ -21,6 +21,18 @@ namespace Filtration.Models.BlockItemTypes
get { return "Rarity"; }
}
public override string OutputText
{
get
{
return PrefixText + " " + FilterPredicate.PredicateOperator
.GetAttributeDescription() +
" " +
((ItemRarity) FilterPredicate.PredicateOperand)
.GetAttributeDescription();
}
}
public override int MaximumAllowed
{
get { return 2; }

View File

@ -6,11 +6,12 @@ namespace Filtration.Models
internal interface IItemFilterBlockItem : INotifyPropertyChanged
{
string PrefixText { get; }
int MaximumAllowed { get; }
string OutputText { get; }
string DisplayHeading { get; }
string SummaryText { get; }
Color SummaryBackgroundColor { get; }
Color SummaryTextColor { get; }
int MaximumAllowed { get; }
int SortOrder { get; }
}
}

View File

@ -330,126 +330,16 @@ namespace Filtration.Translators
outputString += " # " + block.BlockGroup;
}
// This could be refactored to use the upcasted NumericFilterPredicateBlockItem (or even IItemFilterBlockItem) instead
// of the specific downcasts. Leaving it like this currently to preserve sorting since the different
// downcasts have no defined sort order (yet).
foreach (var blockItem in block.BlockItems.OfType<ItemLevelBlockItem>())
{
AddNumericFilterPredicateBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<DropLevelBlockItem>())
{
AddNumericFilterPredicateBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<QualityBlockItem>())
{
AddNumericFilterPredicateBlockItemToString(ref outputString, blockItem);
}
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var blockItem in block.BlockItems.OfType<RarityBlockItem>())
foreach (var blockItem in block.BlockItems.Where(b => b.GetType() != typeof(ActionBlockItem)).OrderBy(b => b.SortOrder))
{
outputString += _newLine + "Rarity " +
blockItem.FilterPredicate.PredicateOperator
.GetAttributeDescription() +
" " +
((ItemRarity) blockItem.FilterPredicate.PredicateOperand)
.GetAttributeDescription();
}
foreach (var blockItem in block.BlockItems.OfType<ClassBlockItem>())
{
AddStringListBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<BaseTypeBlockItem>())
{
AddStringListBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<SocketsBlockItem>())
{
AddNumericFilterPredicateBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<LinkedSocketsBlockItem>())
{
AddNumericFilterPredicateBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<WidthBlockItem>())
{
AddNumericFilterPredicateBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<HeightBlockItem>())
{
AddNumericFilterPredicateBlockItemToString(ref outputString, blockItem);
}
foreach (var blockItem in block.BlockItems.OfType<SocketGroupBlockItem>())
{
AddStringListBlockItemToString(ref outputString, blockItem);
}
if (block.BlockItems.Count(b => b is TextColorBlockItem) > 0)
{
// Only add the first TextColorBlockItem type (not that we should ever have more than one).
AddColorBlockItemToString(ref outputString, block.BlockItems.OfType<TextColorBlockItem>().First());
}
if (block.BlockItems.Count(b => b.GetType() == typeof(BackgroundColorBlockItem)) > 0)
{
// Only add the first BackgroundColorBlockItem type (not that we should ever have more than one).
AddColorBlockItemToString(ref outputString, block.BlockItems.OfType<BackgroundColorBlockItem>().First());
}
if (block.BlockItems.Count(b => b.GetType() == typeof(BorderColorBlockItem)) > 0)
{
// Only add the first BorderColorBlockItem (not that we should ever have more than one).
AddColorBlockItemToString(ref outputString, block.BlockItems.OfType<BorderColorBlockItem>().First());
}
if (block.BlockItems.Count(b => b.GetType() == typeof(FontSizeBlockItem)) > 0)
{
outputString += _newLine + "SetFontSize " +
block.BlockItems.OfType<FontSizeBlockItem>().First().Value;
}
if (block.BlockItems.Count(b => b.GetType() == typeof(SoundBlockItem)) > 0)
{
var blockItemValue = block.BlockItems.OfType<SoundBlockItem>().First();
outputString += _newLine + "PlayAlertSound " + blockItemValue.Value + " " + blockItemValue.SecondValue;
if (blockItem.OutputText != string.Empty)
{
outputString += _newLine + blockItem.OutputText;
}
}
return outputString;
}
private void AddNumericFilterPredicateBlockItemToString(ref string targetString, NumericFilterPredicateBlockItem blockItem)
{
targetString += _newLine + blockItem.PrefixText + " " +
blockItem.FilterPredicate.PredicateOperator.GetAttributeDescription() +
" " + blockItem.FilterPredicate.PredicateOperand;
}
private void AddStringListBlockItemToString(ref string targetString, StringListBlockItem blockItem)
{
var enumerable = blockItem.Items as IList<string> ?? blockItem.Items.ToList();
if (enumerable.Count > 0)
{
targetString += _newLine + blockItem.PrefixText + " " +
string.Format("\"{0}\"",
string.Join("\" \"", enumerable.ToArray()));
}
}
private void AddColorBlockItemToString(ref string targetString, ColorBlockItem blockItem)
{
targetString += _newLine + blockItem.PrefixText + " " + blockItem.Color.R + " " + blockItem.Color.G + " "
+ blockItem.Color.B + (blockItem.Color.A < 255 ? " " + blockItem.Color.A : string.Empty);
}
}
}