Skip to content

Commit

Permalink
[Conditions editor] Condition type 31 (OBJECT_ENTRY) entry parameter …
Browse files Browse the repository at this point in the history
…can be chosen from the list
  • Loading branch information
BAndysc committed Jun 21, 2021
1 parent 7859874 commit 7afcb45
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 7 deletions.
18 changes: 18 additions & 0 deletions WDE.Conditions/ConditionsModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
using Prism.Events;
using Prism.Ioc;
using WDE.Common.Events;
using WDE.Common.Parameters;
using WDE.Conditions.ViewModels;
using WDE.Module;
using WDE.Parameters;

namespace WDE.Conditions
{
public class ConditionsModule : ModuleBase
{
public override void OnInitialized(IContainerProvider containerProvider)
{
containerProvider.Resolve<IEventAggregator>()
.GetEvent<AllModulesLoaded>()
.Subscribe(() =>
{
var factory = containerProvider.Resolve<IParameterFactory>();
factory.Register("ConditionObjectEntryParameter", new ConditionObjectEntryParameter(factory));
},
ThreadOption.PublisherThread,
true);
}
}
}
2 changes: 1 addition & 1 deletion WDE.Conditions/SmartData/conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@
}
},
{
"type": "Parameter",
"type": "ConditionObjectEntryParameter",
"name": "Entry",
"description": "Creature or gameobject entry"
},
Expand Down
62 changes: 62 additions & 0 deletions WDE.Conditions/ViewModels/ConditionObjectEntryParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using WDE.Common.Database;
using WDE.Common.Parameters;

namespace WDE.Conditions.ViewModels
{
public class ConditionObjectEntryParameter : IContextualParameter<long, ConditionViewModel>
{
private readonly IParameterFactory parameterFactory;
public bool HasItems => true;

public ConditionObjectEntryParameter(IParameterFactory parameterFactory)
{
this.parameterFactory = parameterFactory;
}

public string ToString(long value)
{
return value.ToString();
}

public Dictionary<long, SelectOption>? Items { get; }

public Dictionary<long, SelectOption>? ItemsForContext(ConditionViewModel context)
{
LazyLoad();

if (context.ConditionValue1.Value == 3)
return creatureParameter!.Items;

if (context.ConditionValue1.Value == 5)
return gameobjectParameter!.Items;

return null;
}

private void LazyLoad()
{
if (creatureParameter == null || gameobjectParameter == null)
{
creatureParameter = parameterFactory.Factory("CreatureParameter");
gameobjectParameter = parameterFactory.Factory("GameobjectParameter");
}
}

private IParameter<long>? creatureParameter;
private IParameter<long>? gameobjectParameter;

public string ToString(long value, ConditionViewModel context)
{
LazyLoad();

if (context.ConditionValue1.Value == 3)
return creatureParameter!.ToString(value);

if (context.ConditionValue1.Value == 5)
return gameobjectParameter!.ToString(value);

return ToString(value);
}
}
}
4 changes: 2 additions & 2 deletions WDE.Conditions/ViewModels/ConditionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace WDE.Conditions.ViewModels
{
internal class ConditionViewModel : BindableBase
public class ConditionViewModel : BindableBase
{
public static int ParametersCount => 3;
private ParameterValueHolder<long>[] parameters = new ParameterValueHolder<long>[ParametersCount];
Expand All @@ -30,7 +30,7 @@ internal class ConditionViewModel : BindableBase
public ConditionViewModel(IParameter<long> targets)
{
for (int i = 0; i < ParametersCount; ++i)
parameters[i] = new ParameterValueHolder<long>(Parameter.Instance, 0);
parameters[i] = new ConstContextParameterValueHolder<long, ConditionViewModel>(Parameter.Instance, 0, this);
ConditionTarget = new ParameterValueHolder<long>("Target", targets, 0);
ConditionTarget.IsUsed = targets.HasItems && targets.Items!.Count > 1;

Expand Down
2 changes: 1 addition & 1 deletion WDE.Conditions/ViewModels/ConditionsEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ConditionsEditorViewModel(
if (!prh.HasItems)
return;
var newItem = await itemFromListProvider.GetItemFromList(prh.Parameter.Items, prh.Parameter is FlagParameter, prh.Value);
var newItem = await itemFromListProvider.GetItemFromList(prh.Items, prh.Parameter is FlagParameter, prh.Value);
if (newItem.HasValue)
prh.Value = newItem.Value;
});
Expand Down
51 changes: 48 additions & 3 deletions WDE.Parameters/Models/ParameterValueHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IParameterValueHolder
{
}

public sealed class ParameterValueHolder<T> : IParameterValueHolder, INotifyPropertyChanged where T : notnull
public class ParameterValueHolder<T> : IParameterValueHolder, INotifyPropertyChanged where T : notnull
{
private T value;
[NotNull]
Expand Down Expand Up @@ -68,7 +68,9 @@ public string Name
}
}

public bool HasItems => Parameter.HasItems;
public virtual bool HasItems => Parameter.HasItems;

public virtual Dictionary<T, SelectOption>? Items => Parameter.Items;

public string String => ToString();

Expand Down Expand Up @@ -96,7 +98,7 @@ public ParameterValueHolder(IParameter<T> parameter, T value)
this.value = value;
this.parameter = parameter;
}

public ParameterValueHolder(string name, IParameter<T> parameter, T value)
{
this.name = name;
Expand All @@ -123,4 +125,47 @@ private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public class ConstContextParameterValueHolder<T, R> : ParameterValueHolder<T> where T : notnull
{
private R? constContext;
private bool inToString;

public override Dictionary<T, SelectOption>? Items
{
get
{
if (constContext != null && Parameter is IContextualParameter<T, R> contextual)
{
return contextual.ItemsForContext(constContext);
}

return Parameter.Items;
}
}

public override string ToString()
{
if (inToString || constContext == null)
return base.ToString();

inToString = true;
var ret = ToString(constContext);
inToString = false;
return ret;
}

public ConstContextParameterValueHolder(IParameter<T> parameter, T value, R context) : base(parameter, value)
{
constContext = context;
}

public ConstContextParameterValueHolder(IParameter<T> parameter, T value) : base(parameter, value)
{
}

public ConstContextParameterValueHolder(string name, IParameter<T> parameter, T value) : base(name, parameter, value)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public interface IContextualParameter<T, R> : IParameter<T> where T : notnull
{
string ToString(T value, R context);
System.Type ContextType => typeof(R);
Dictionary<T, SelectOption>? ItemsForContext(R context) => null;
}
}

0 comments on commit 7afcb45

Please sign in to comment.