Skip to content

Commit

Permalink
Move INotifyProperty in its own assembly
Browse files Browse the repository at this point in the history
+ fix bug with TMPSceneAnimation previews
  • Loading branch information
Luca3317 committed Sep 3, 2024
1 parent 070dd51 commit a22fb73
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Package/Editor/TMPEffects.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"GUID:96c64e576ede81e46ad482a315c680f9",
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:4bd7a8fb744c5fd43886f1e1d2e6bde3",
"GUID:bc50dcdb89ada6249b1b82bd70623a05"
"GUID:bc50dcdb89ada6249b1b82bd70623a05",
"GUID:3af83a76aff21f94faea8408f8eb05d3"
],
"includePlatforms": [
"Editor"
Expand Down
4 changes: 2 additions & 2 deletions Package/Runtime/Components/TMPAnimator/AnimationDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AnimationDatabase(TDB database, SerializedObservableDictionary<string, TS
customAnimations = new Dictionary<string, ITMPAnimation>();

if (database != null) database.ObjectChanged += RaiseObjectChanged;
if (sceneAnimations != null) sceneAnimations.PropertyChanged += RaiseObjectChanged;
if (sceneAnimations != null) sceneAnimations.ObjectChanged += RaiseObjectChanged;
}

public void AddAnimation(string key, ITMPAnimation animation)
Expand Down Expand Up @@ -78,7 +78,7 @@ public void Dispose()
if (disposed) return;
disposed = true;
if (database != null) database.ObjectChanged -= RaiseObjectChanged;
if (sceneAnimations != null) sceneAnimations.PropertyChanged -= RaiseObjectChanged;
if (sceneAnimations != null) sceneAnimations.ObjectChanged -= RaiseObjectChanged;
database = null;
sceneAnimations = null;
customAnimations = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@ namespace TMPEffects.TMPSceneAnimations
/// <summary>
/// Base class for all SceneAnimations.
/// </summary>
public abstract class TMPSceneAnimationBase : MonoBehaviour, ITMPAnimation, INotifyPropertyChanged
public abstract class TMPSceneAnimationBase : MonoBehaviour, ITMPAnimation, INotifyObjectChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public abstract void Animate(CharData cData, IAnimationContext context);
public abstract object GetNewCustomData();

public abstract void SetParameters(object customData, IDictionary<string, string> parameters);
public abstract bool ValidateParameters(IDictionary<string, string> parameters);


public event ObjectChangedEventHandler ObjectChanged;

protected virtual void OnValidate()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(""));
RaiseObjectChanged();
}

protected virtual void OnDestroy()
{
RaiseObjectChanged();
}

protected void RaiseObjectChanged()
{
ObjectChanged?.Invoke(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "TMPEffects.NotifyObjectChanged",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using TMPEffects.ObjectChanged;
using UnityEditor;
using UnityEngine;

Expand All @@ -10,8 +11,8 @@ internal class SerializedObservableDictionaryDrawer : PropertyDrawer
{
public const string KeyName = nameof(SerializedKeyValuePair<int, int>.Key);
public const string ValueName = nameof(SerializedKeyValuePair<int, int>.Value);
public const string SerializedListName = nameof(SerializedObservableDictionary<int, INotifyPropertyChanged>._serializedList);
public const string LookupTableName = nameof(SerializedObservableDictionary<int, INotifyPropertyChanged>.LookupTable);
public const string SerializedListName = nameof(SerializedObservableDictionary<int, INotifyObjectChanged>._serializedList);
public const string LookupTableName = nameof(SerializedObservableDictionary<int, INotifyObjectChanged>.LookupTable);

public const int TopHeaderClipHeight = 20;
public const int TopHeaderHeight = 19;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "TMPEffects.SerializedCollections.Editor",
"rootNamespace": "",
"references": [
"GUID:bc50dcdb89ada6249b1b82bd70623a05"
"GUID:bc50dcdb89ada6249b1b82bd70623a05",
"GUID:3af83a76aff21f94faea8408f8eb05d3"
],
"includePlatforms": [
"Editor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using TMPEffects.ObjectChanged;
using UnityEngine;

namespace TMPEffects.SerializedCollections
Expand Down Expand Up @@ -68,7 +69,7 @@ internal interface ISerializedDictionary<TKey, TValue> : IDictionary<TKey, TValu
}

[System.Serializable]
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INotifyPropertyChanged, IDisposable where TValue : INotifyPropertyChanged
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INotifyObjectChanged, IDisposable where TValue : INotifyObjectChanged
{
protected bool mayRaise = true;
private Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
Expand All @@ -91,15 +92,15 @@ public TValue this[TKey key]

public bool IsReadOnly => ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).IsReadOnly;

public event PropertyChangedEventHandler PropertyChanged;
public event ObjectChangedEventHandler ObjectChanged;

public void Add(TKey key, TValue value)
{
_dictionary.Add(key, value);
RaisePropertyChanged();
if (value != null)
{
value.PropertyChanged += RaisePropertyChanged;
value.ObjectChanged += RaisePropertyChanged;
}
}

Expand All @@ -108,15 +109,15 @@ public void Add(KeyValuePair<TKey, TValue> item)
((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Add(item);
RaisePropertyChanged();
if (item.Value != null)
item.Value.PropertyChanged += RaisePropertyChanged;
item.Value.ObjectChanged += RaisePropertyChanged;
}

public void Clear()
{
foreach (var kvp in _dictionary)
{
if (kvp.Value != null)
kvp.Value.PropertyChanged -= RaisePropertyChanged;
kvp.Value.ObjectChanged -= RaisePropertyChanged;
}

_dictionary.Clear();
Expand Down Expand Up @@ -147,7 +148,7 @@ public bool Remove(TKey key)
{
if (_dictionary.ContainsKey(key))
{
_dictionary[key].PropertyChanged -= RaisePropertyChanged;
_dictionary[key].ObjectChanged -= RaisePropertyChanged;
if (!_dictionary.Remove(key))
{
throw new System.InvalidOperationException("Failed to remove key despite it being present?");
Expand All @@ -163,7 +164,7 @@ public bool Remove(KeyValuePair<TKey, TValue> item)
{
if (((IDictionary<TKey, TValue>)_dictionary).Contains(item))
{
_dictionary[item.Key].PropertyChanged -= RaisePropertyChanged;
_dictionary[item.Key].ObjectChanged -= RaisePropertyChanged;
if (!_dictionary.Remove(item.Key))
{
throw new System.InvalidOperationException("Failed to remove key despite it being present?");
Expand Down Expand Up @@ -192,17 +193,17 @@ public void Dispose()

protected void RaisePropertyChanged()
{
if (mayRaise) PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("dictionary"));
if (mayRaise) ObjectChanged?.Invoke(this);
}

protected void RaisePropertyChanged(object sender, PropertyChangedEventArgs args)
protected void RaisePropertyChanged(object sender)
{
if (mayRaise) PropertyChanged?.Invoke(this, args);
if (mayRaise) ObjectChanged?.Invoke(this);
}
}

[System.Serializable]
public partial class SerializedObservableDictionary<TKey, TValue> : ObservableDictionary<TKey, TValue>, ISerializedDictionary<TKey, TValue>, ISerializationCallbackReceiver where TValue : INotifyPropertyChanged
public partial class SerializedObservableDictionary<TKey, TValue> : ObservableDictionary<TKey, TValue>, ISerializedDictionary<TKey, TValue>, ISerializationCallbackReceiver where TValue : INotifyObjectChanged
{
public List<SerializedKeyValuePair<TKey, TValue>> SerializedList
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "TMPEffects.SerializedCollections",
"rootNamespace": "",
"references": [],
"references": ["GUID:3af83a76aff21f94faea8408f8eb05d3"],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
Expand Down
3 changes: 2 additions & 1 deletion Package/Runtime/TMPEffects.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"rootNamespace": "",
"references": [
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:bc50dcdb89ada6249b1b82bd70623a05"
"GUID:bc50dcdb89ada6249b1b82bd70623a05",
"GUID:3af83a76aff21f94faea8408f8eb05d3"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
3 changes: 2 additions & 1 deletion Package/Tests/TMPEffects.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"Unity.PerformanceTesting.Editor",
"TMPEffects",
"Unity.TextMeshPro",
"TMPEffects.Editor"
"TMPEffects.Editor",
"TMPEffects.NotifyObjectChanged"
],
"includePlatforms": [
"Editor"
Expand Down

0 comments on commit a22fb73

Please sign in to comment.