Skip to content

Commit

Permalink
Added SerializedProperty.PropertyChanged event, called recursively up…
Browse files Browse the repository at this point in the history
… the parents
  • Loading branch information
michaelsakharov committed Mar 19, 2024
1 parent 15a1e22 commit 9de3af2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Prowl.Runtime/Serializer/SerializedProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,24 @@ public enum PropertyType
Compound,
}

public class PropertyChangeEventArgs : EventArgs
{
public SerializedProperty Property { get; }
public object? OldValue { get; }
public object? NewValue { get; }

public PropertyChangeEventArgs(SerializedProperty property, object? oldValue, object? newValue)
{
Property = property;
OldValue = oldValue;
NewValue = newValue;
}
}

public sealed partial class SerializedProperty
{
public event EventHandler<PropertyChangeEventArgs>? PropertyChanged;

private object? _value;
public object? Value { get { return _value; } private set { Set(value); } }

Expand Down Expand Up @@ -90,6 +106,12 @@ public SerializedProperty Clone()
return new(TagType, Value);
}

private void OnPropertyChanged(PropertyChangeEventArgs e)
{
PropertyChanged?.Invoke(this, e);
Parent?.OnPropertyChanged(e);
}

#region Shortcuts

/// <summary>
Expand Down Expand Up @@ -145,6 +167,8 @@ public void Set(object value)
PropertyType.Bool => (bool)value,
_ => throw new InvalidOperationException("Cannot set value of " + TagType.ToString())
};

OnPropertyChanged(new PropertyChangeEventArgs(this, old, value));
}

/// <summary> Returns the value of this tag, cast as a bool. </summary>
Expand Down

0 comments on commit 9de3af2

Please sign in to comment.