using System; using System.Collections.Generic; using System.Reflection; using Amphora; using Azimuth; using Prism; namespace Konis.Testbed { /// /// One property of an emitter def that a tuning panel can show, and mostly change. /// public class TunableProperty { private static readonly object[] NoChoices = new object[0]; private readonly PropertyInfo property; /// /// Wraps , which must have a public getter and setter. /// public TunableProperty(PropertyInfo property, TunableKind kind, TuneEffect effect) { this.property = property ?? throw new ArgumentNullException(nameof(property)); Kind = kind; Effect = effect; Choices = ChoicesFor(property, kind); } private static object[] ChoicesFor(PropertyInfo property, TunableKind kind) { if (kind == TunableKind.Sprite) { var sprites = new object[SpriteChoices.All.Count]; for (int i = 0; i < sprites.Length; i++) { sprites[i] = SpriteChoices.All[i].Def; } return sprites; } if (kind != TunableKind.Choice) { return NoChoices; } Array values = Enum.GetValues(property.PropertyType); var returnMe = new object[values.Length]; values.CopyTo(returnMe, 0); return returnMe; } /// /// Gets the property's name, which is how a def's initializer spells it. /// public string Name => property.Name; /// /// Gets the shape of value the property holds. /// public TunableKind Kind { get; } /// /// Gets what changing the property does to an emitter that is already running. /// public TuneEffect Effect { get; } /// /// Gets the type of value the property holds. /// public Type ValueType => Kind == TunableKind.Gradient ? typeof(Gradient) : property.PropertyType; /// /// Gets the values a or can take, in the /// order a list shows them. It is empty for any other kind. /// public IReadOnlyList Choices { get; } /// /// Returns what a list calls , one of . /// /// A sprite isn't one of the choices. public string LabelOf(object choice) { if (Kind != TunableKind.Sprite) { return choice.ToString(); } SpriteChoice found = SpriteChoices.Find((SpriteDef)choice); if (found == null) { throw new ArgumentException("The sprite isn't one of the choices.", nameof(choice)); } return found.Label; } /// /// Returns the property's value on , boxed. A gradient property gives the gradient /// itself, or null when the def holds none. /// public object Get(EmitterDef def) { object value = property.GetValue(def); return Kind == TunableKind.Gradient ? (value as GradientDef)?.Gradient : value; } /// /// Returns whether 's value for this property can be written as C# source. A computed /// property can't, and neither can a function that is absent or made of code, or a sprite that no static field /// holds. /// public bool HasSource(EmitterDef def) { if (Kind == TunableKind.Computed) { return false; } if (Kind == TunableKind.Sprite) { return SpriteChoices.Find((SpriteDef)Get(def))?.Source != null; } if (Kind == TunableKind.Function) { object function = Get(def); return function != null && !(function is CallbackFunction); } return true; } // A gradient is replaced inside its GradientDef, which stays put so whatever else holds it sees the change. private void Put(EmitterDef def, object value) { if (Kind == TunableKind.Gradient) { ((GradientDef)property.GetValue(def)).Gradient = (Gradient)value; } else { property.SetValue(def, value); } } /// /// Gives the new value unless the def then couldn't work. The old value stays then, and /// the status is with the reason. /// /// The property is . /// The value isn't of the property's type. public Status Set(EmitterDef def, object value) { if (Kind == TunableKind.Computed) { throw new InvalidOperationException($"The {Name} of an emitter can't be changed here."); } if (!ValueType.IsInstanceOfType(value)) { throw new ArgumentException($"The {Name} of an emitter takes a {ValueType.Name}.", nameof(value)); } if (Kind == TunableKind.Choice && !Enum.IsDefined(ValueType, value)) { return TunableFailures.Refused.Fail( $"The {Name} of an emitter, {(int)value}, isn't one of its choices."); } if (Kind == TunableKind.Gradient && property.GetValue(def) == null) { return TunableFailures.Refused.Fail($"The {Name} of an emitter has no gradient to change."); } object before = Get(def); Put(def, value); try { def.Validate(); } catch (InvalidOperationException exception) { Put(def, before); return TunableFailures.Refused.Fail(exception.Message); } return Status.Ok; } } }