Skip to content

Commit 2280a9b

Browse files
committed
INotifyPropertyChanged for Bindings, Added Lock class
1 parent 9943f84 commit 2280a9b

12 files changed

+164
-73
lines changed

Runtime/Locks.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.

Runtime/Locks/GameLock.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using Gameframe.ScriptableObjects.Events;
3+
using UnityEngine;
4+
5+
namespace Gameframe.ScriptableObjects.Locks
6+
{
7+
[CreateAssetMenu(menuName=MenuNames.LockMenu+"Lock")]
8+
public class GameLock : ScriptableObject
9+
{
10+
[NonSerialized]
11+
private int lockCount = 0;
12+
13+
public bool Locked => lockCount != 0;
14+
public event Action<bool> OnValueChanged;
15+
16+
[SerializeField]
17+
private GameEvent unlockedEvent;
18+
public GameEvent OnUnlocked
19+
{
20+
get
21+
{
22+
if (unlockedEvent == null)
23+
{
24+
unlockedEvent = CreateInstance<GameEvent>();
25+
}
26+
return unlockedEvent;
27+
}
28+
}
29+
30+
[SerializeField]
31+
private GameEvent lockedEvent;
32+
public GameEvent OnLocked
33+
{
34+
get
35+
{
36+
if (lockedEvent == null)
37+
{
38+
lockedEvent = CreateInstance<GameEvent>();
39+
}
40+
return lockedEvent;
41+
}
42+
}
43+
44+
private void OnEnable()
45+
{
46+
lockCount = 0;
47+
}
48+
49+
public void Lock()
50+
{
51+
lockCount++;
52+
if (lockCount == 1)
53+
{
54+
RaiseValueChanged();
55+
}
56+
}
57+
58+
public void Unlock()
59+
{
60+
lockCount--;
61+
if (lockCount == 0)
62+
{
63+
RaiseValueChanged();
64+
}
65+
}
66+
67+
private void RaiseValueChanged()
68+
{
69+
var value = Locked;
70+
if (value)
71+
{
72+
if (lockedEvent != null)
73+
{
74+
lockedEvent.Raise();
75+
}
76+
}
77+
else
78+
{
79+
if (unlockedEvent != null)
80+
{
81+
unlockedEvent.Raise();
82+
}
83+
}
84+
OnValueChanged?.Invoke(value);
85+
}
86+
87+
}
88+
}
89+
90+

Runtime/Locks/GameLock.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/MenuNames.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
namespace Gameframe.ScriptableObjects
1+
using UnityEditor;
2+
3+
namespace Gameframe.ScriptableObjects
24
{
35
public static class MenuNames
46
{
57
public const string MenuBase = "Gameframe/";
68
public const string EventMenu = MenuBase + "Events/";
9+
public const string LockMenu = MenuBase;
710
public const string RuntimeSetMenu = MenuBase + "RuntimeSets/";
811
public const string Variables = MenuBase + "Variables/";
912
}

Runtime/Variables/BaseVariable.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using Gameframe.ScriptableObjects.Events;
1+
using System;
2+
using System.ComponentModel;
3+
using System.Runtime.CompilerServices;
4+
using Gameframe.ScriptableObjects.Events;
5+
using JetBrains.Annotations;
26
using UnityEngine;
37

48
namespace Gameframe.ScriptableObjects.Variables
59
{
6-
public class BaseVariable : ScriptableObject
10+
public class BaseVariable : ScriptableObject, INotifyPropertyChanged
711
{
812
[SerializeField]
913
protected GameEvent onValueChanged;
@@ -19,6 +23,44 @@ public GameEvent OnValueChanged
1923
return onValueChanged;
2024
}
2125
}
26+
27+
/// <summary>
28+
/// INotifyPropertyChanged interface implemented to support Gameframe.Bindings
29+
/// </summary>
30+
#region INotifyPropertyChanged
31+
32+
public event PropertyChangedEventHandler PropertyChanged;
33+
34+
[NotifyPropertyChangedInvocator]
35+
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
36+
{
37+
try
38+
{
39+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
40+
}
41+
catch (Exception e)
42+
{
43+
Debug.LogException(e,this);
44+
}
45+
if (onValueChanged != null)
46+
{
47+
onValueChanged.Raise();
48+
}
49+
}
50+
51+
#endregion
52+
53+
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
54+
{
55+
if (!Equals(field,value))
56+
{
57+
field = value;
58+
OnPropertyChanged(propertyName);
59+
return true;
60+
}
61+
return false;
62+
}
63+
2264
}
2365
}
2466

Runtime/Variables/ColorVariable.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,7 @@ public class ColorVariable : BaseVariable, IVariable<Color>
1111
public Color Value
1212
{
1313
get => value;
14-
set
15-
{
16-
if (this.value != value)
17-
{
18-
this.value = value;
19-
if (onValueChanged != null)
20-
{
21-
onValueChanged.Raise();
22-
}
23-
}
24-
}
14+
set => SetProperty(ref this.value, value);
2515
}
2616
}
2717
}

Runtime/Variables/FloatVariable.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,7 @@ public class FloatVariable : BaseVariable, IVariable<float>
1010
public float Value
1111
{
1212
get => value;
13-
set
14-
{
15-
if (!Mathf.Approximately(this.value,value) )
16-
{
17-
this.value = value;
18-
if (onValueChanged != null)
19-
{
20-
onValueChanged.Raise();
21-
}
22-
}
23-
}
13+
set => SetProperty(ref this.value, value);
2414
}
2515
}
2616
}

Runtime/Variables/GameObjectVariable.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ public GameObject Value
2121
if (gameObject != value)
2222
{
2323
gameObject = value;
24-
if (onValueChanged != null)
25-
{
26-
onValueChanged.Raise();
27-
}
24+
OnPropertyChanged();
2825
}
2926
}
3027
}

Runtime/Variables/IntVariable.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,7 @@ public class IntVariable : BaseVariable, IVariable<int>
1010
public int Value
1111
{
1212
get => value;
13-
set
14-
{
15-
if ( this.value != value )
16-
{
17-
this.value = value;
18-
if ( onValueChanged != null )
19-
{
20-
onValueChanged.Raise();
21-
}
22-
}
23-
}
13+
set => SetProperty(ref this.value, value);
2414
}
2515
}
2616
}

Runtime/Variables/StringVariable.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,7 @@ public class StringVariable : BaseVariable, IVariable<string>
1010
public string Value
1111
{
1212
get => value;
13-
set
14-
{
15-
if (this.value != value)
16-
{
17-
this.value = value;
18-
if (onValueChanged != null)
19-
{
20-
onValueChanged.Raise();
21-
}
22-
}
23-
}
13+
set => SetProperty(ref this.value, value);
2414
}
2515
}
2616
}

0 commit comments

Comments
 (0)