Skip to content

Commit

Permalink
Ensured that only instance properties (and no static properties) are …
Browse files Browse the repository at this point in the history
…used in `SimpleEffectDialog` (PintaProject#860)

* Added property name to exception message

* Improved filtering out of fields and properties

* Refactored out method for filtering

* Refactored blend ops list out of effect data, and directly into `CloudsEffect`
  • Loading branch information
Lehonti authored Jun 7, 2024
1 parent 02c1de9 commit e6b1e73
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
33 changes: 14 additions & 19 deletions Pinta.Effects/Effects/CloudsEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public override void LaunchConfiguration ()
// Adapted to 2-D version in C# from 3-D version in Java from http://mrl.nyu.edu/~perlin/noise/
private static readonly ImmutableArray<int> permute_lookup;

public static ReadOnlyDictionary<string, object> BlendOps { get; }

private static readonly string default_blend_op;

static CloudsEffect ()
{
#pragma warning disable format
Expand Down Expand Up @@ -90,6 +94,15 @@ static CloudsEffect ()
permuteLookup[i] = permutationTable[i];
}
permute_lookup = permuteLookup.MoveToImmutable ();

BlendOps =
UserBlendOps.GetAllBlendModeNames ()
.ToDictionary (
o => o,
o => (object) UserBlendOps.GetBlendModeByName (o))
.AsReadOnly ();

default_blend_op = UserBlendOps.GetBlendModeName (Pinta.Core.BlendMode.Normal);
}

private static double Fade (double t)
Expand Down Expand Up @@ -218,7 +231,7 @@ protected override void Render (

g.Clear (r);
g.BlendSurface (src, r);
g.BlendSurface (temp, r.Location (), (BlendMode) CloudsData.BlendOps[Data.BlendMode]);
g.BlendSurface (temp, r.Location (), (BlendMode) BlendOps[Data.BlendMode]);
}
}
#endregion
Expand All @@ -234,24 +247,6 @@ public sealed class CloudsData : EffectData
[Caption ("Power"), MinimumValue (0), MaximumValue (100)]
public int Power { get; set; } = 50;

[Skip]
public static ReadOnlyDictionary<string, object> BlendOps { get; }

[Skip]
private static readonly string default_blend_op;

static CloudsData ()
{
BlendOps =
UserBlendOps.GetAllBlendModeNames ()
.ToDictionary (
o => o,
o => (object) UserBlendOps.GetBlendModeByName (o))
.AsReadOnly ();

default_blend_op = UserBlendOps.GetBlendModeName (Pinta.Core.BlendMode.Normal);
}

[StaticList ("BlendOps")]
public string BlendMode { get; set; } = default_blend_op;

Expand Down
2 changes: 1 addition & 1 deletion Pinta.Gui.Widgets/Dialogs/MemberReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static Action<object, object> CreateSetter (MemberInfo memberInfo)
case FieldInfo f:
return f.SetValue;
case PropertyInfo p:
MethodInfo setter = p.GetSetMethod () ?? throw new ArgumentException ("Property has no 'set' method", nameof (memberInfo));
MethodInfo setter = p.GetSetMethod () ?? throw new ArgumentException ($"Property {p.Name} has no 'set' method", nameof (memberInfo));
return (o, v) => setter.Invoke (o, new[] { v });
default:
throw new ArgumentException ($"Member type {memberInfo.GetType ()} not supported", nameof (memberInfo));
Expand Down
23 changes: 21 additions & 2 deletions Pinta.Gui.Widgets/Dialogs/SimpleEffectDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,32 @@ private void HandleClose ()
effectData
.GetType ()
.GetMembers ()
.Where (m => m is FieldInfo || m is PropertyInfo)
.Where (m => string.Compare (m.Name, nameof (EffectData.IsDefault), true) != 0)
.Where (IsInstanceFieldOrProperty)
.Where (IsCustomProperty)
.Select (CreateSettings)
.Where (settings => !settings.skip)
.Select (settings => GetMemberWidgets (settings, effectData, localizer))
.SelectMany (widgets => widgets);

private bool IsCustomProperty (MemberInfo memberInfo)
{
return string.Compare (memberInfo.Name, nameof (EffectData.IsDefault), true) != 0;
}

private bool IsInstanceFieldOrProperty (MemberInfo memberInfo)
{
switch (memberInfo) {
case FieldInfo fieldInfo:
return !fieldInfo.IsStatic;
case PropertyInfo propertyInfo:
MethodInfo? getter = propertyInfo.GetGetMethod ();
if (getter is null) return false;
return !getter.IsStatic;
default:
return false;
}
}

private sealed record MemberSettings (
MemberReflector reflector,
string caption,
Expand Down

0 comments on commit e6b1e73

Please sign in to comment.