Skip to content

Commit 1a2d5d0

Browse files
committed
Adding comments. Added bindable scriptable object. Added demo package.
1 parent bb0071b commit 1a2d5d0

9 files changed

+101
-29
lines changed

Demo.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Demo/Demo.unitypackage

5.71 KB
Binary file not shown.

Runtime/BindableMonoBehaviour.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
namespace Gameframe.Bindings
77
{
8-
public class BindableMonoBehaviour : MonoBehaviour, INotifyPropertyChanged
8+
/// <summary>
9+
/// MonoBehaviour that provides property changed events for bindings through the INotifyPropertyChanged interface
10+
/// </summary>
11+
public abstract class BindableMonoBehaviour : MonoBehaviour, INotifyPropertyChanged
912
{
1013
public event PropertyChangedEventHandler PropertyChanged;
1114

Runtime/BindableScriptableObject.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Runtime.CompilerServices;
5+
using UnityEngine;
6+
7+
namespace Gameframe.Bindings
8+
{
9+
/// <summary>
10+
/// ScriptableObject that provides property change events for bindings via the INotifyPropertyChanged interface
11+
/// </summary>
12+
public abstract class BindableScriptableObject : ScriptableObject, INotifyPropertyChanged
13+
{
14+
public event PropertyChangedEventHandler PropertyChanged;
15+
16+
protected virtual void OnEnable()
17+
{
18+
//We need to clear out the subscribed bindings between editor play sessions
19+
PropertyChanged = null;
20+
}
21+
22+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
23+
{
24+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
25+
}
26+
27+
/// <summary>
28+
/// Use this inside property setters to raise property changed events which are needed by bindingssss
29+
/// </summary>
30+
/// <param name="field">field to which the value will be set</param>
31+
/// <param name="value">value you want to assign to storage</param>
32+
/// <param name="propertyName">name of the property being set</param>
33+
/// <typeparam name="T">Type of the property being set</typeparam>
34+
/// <returns>True if property was set. False if it was already equal to the given value.</returns>
35+
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null )
36+
{
37+
if (EqualityComparer<T>.Default.Equals(field,value))
38+
{
39+
return false;
40+
}
41+
field = value;
42+
OnPropertyChanged(propertyName);
43+
return true;
44+
}
45+
}
46+
}

Runtime/BindableScriptableObject.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Binding.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
namespace Gameframe.Bindings
77
{
8+
/// <summary>
9+
/// Maintains a one directional binding between two properties
10+
/// Property change events generated by the source will assign and update the target property
11+
/// </summary>
812
public class Binding : IDisposable
913
{
1014
private object _source = null;
@@ -18,8 +22,20 @@ public class Binding : IDisposable
1822

1923
private INotifyPropertyChanged _propertyChangedNotifier;
2024

25+
/// <summary>
26+
/// Error context is passed to any error logs so that errors selected in the console
27+
/// Will direct the user to the associated context object
28+
/// </summary>
2129
public UnityEngine.Object ErrorContext = null;
30+
31+
/// <summary>
32+
/// Changes will only propagate while enabled is true
33+
/// </summary>
2234
public bool Enabled = true;
35+
36+
/// <summary>
37+
/// Converter function will transform values before assigning the target property
38+
/// </summary>
2339
public Func<object, object> Converter = null;
2440

2541
/// <summary>
@@ -59,6 +75,9 @@ public void SetTarget(object dataContext, string path, bool refresh = true)
5975
}
6076
}
6177

78+
/// <summary>
79+
/// Refresh the target value with the value from the source
80+
/// </summary>
6281
public void Refresh()
6382
{
6483
if (_target == null || _source == null || _targetProperty == null || _sourceProperty == null)
@@ -81,6 +100,9 @@ public void Refresh()
81100
}
82101
}
83102

103+
/// <summary>
104+
/// Destroys the binding so changes will no longer propagate
105+
/// </summary>
84106
public void Dispose()
85107
{
86108
SetPropertyChangedNotifier(null);

Runtime/BindingBehaviour.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
namespace Gameframe.Bindings
99
{
10+
/// <summary>
11+
/// Serves as the base class for components that create bindings
12+
/// See TextBinding as an example
13+
/// </summary>
1014
public abstract class BindingBehaviour : MonoBehaviour
1115
{
1216
[SerializeField]
@@ -65,33 +69,6 @@ public virtual void Refresh()
6569
{
6670
_binding?.Refresh();
6771
}
68-
69-
/*private static PropertyInfo GetPropertyInfo(object obj, string path)
70-
{
71-
if (obj == null || path == null)
72-
{
73-
return null;
74-
}
75-
Type type = obj.GetType();
76-
return type.GetProperty(path);
77-
foreach (var property in path.Split('.'))
78-
{
79-
if (obj == null)
80-
{
81-
return null;
82-
}
83-
84-
Type type = obj.GetType();
85-
info = type.GetProperty(property);
86-
if (info == null)
87-
{
88-
return null;
89-
}
90-
91-
obj = info.GetValue(obj, null);
92-
}
93-
return info;
94-
}*/
9572

9673
#if UNITY_EDITOR
9774
protected virtual void OnValidate()
@@ -103,7 +80,6 @@ protected virtual void OnValidate()
10380
}
10481
InitializeBinding();
10582
}
106-
10783
#endif
10884
}
10985
}

Runtime/ComponentBinding.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace Gameframe.Bindings
1010
{
11+
/// <summary>
12+
/// This component will create a binding between any two UnityEngine.Object types
13+
/// </summary>
1114
public class ComponentBinding : BindingBehaviour
1215
{
1316
[SerializeField]

Runtime/TextBinding.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Gameframe.Bindings
55
{
6+
/// <summary>
7+
/// Used to display text via binding
8+
/// </summary>
69
public class TextBinding : BindingBehaviour
710
{
811
[SerializeField]

0 commit comments

Comments
 (0)