Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit a25a595

Browse files
Added events for variables when they change
1 parent 23e1d0f commit a25a595

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

Assets/SO Architecture/Editor/Inspectors/BaseVariableEditor.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,17 @@ public override void OnInspectorGUI()
4848
}
4949
protected void DrawValue()
5050
{
51-
string content = "Cannot display value. No PropertyDrawer for (" + Target.Type + ") [" + Target.ToString() + "]";
52-
GenericPropertyDrawer.DrawPropertyDrawer(Target.Type, _valueProperty, new GUIContent(content, content));
51+
using (var scope = new EditorGUI.ChangeCheckScope())
52+
{
53+
string content = "Cannot display value. No PropertyDrawer for (" + Target.Type + ") [" + Target.ToString() + "]";
54+
GenericPropertyDrawer.DrawPropertyDrawer(Target.Type, _valueProperty, new GUIContent(content, content));
55+
56+
if (scope.changed)
57+
{
58+
// Value changed, raise events
59+
Target.Raise();
60+
}
61+
}
5362
}
5463
protected void DrawClampedFields()
5564
{

Assets/SO Architecture/Events/Game Events/GameEventBase.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
public abstract class GameEventBase<T> : GameEventBase, IGameEvent<T>, IStackTraceObject
55
{
66
private readonly List<IGameEventListener<T>> _typedListeners = new List<IGameEventListener<T>>();
7+
private readonly List<System.Action<T>> _typedActions = new List<System.Action<T>>();
78

89
#if UNITY_EDITOR
910
[SerializeField]
@@ -18,6 +19,12 @@ public void Raise(T value)
1819

1920
for (int i = _listeners.Count - 1; i >= 0; i--)
2021
_listeners[i].OnEventRaised();
22+
23+
for (int i = _typedActions.Count - 1; i >= 0; i--)
24+
_typedActions[i](value);
25+
26+
for (int i = _typedActions.Count - 1; i >= 0; i--)
27+
_actions[i]();
2128
}
2229
public void AddListener(IGameEventListener<T> listener)
2330
{
@@ -29,6 +36,16 @@ public void RemoveListener(IGameEventListener<T> listener)
2936
if (_typedListeners.Contains(listener))
3037
_typedListeners.Remove(listener);
3138
}
39+
public void AddListener(System.Action<T> action)
40+
{
41+
if (!_typedActions.Contains(action))
42+
_typedActions.Add(action);
43+
}
44+
public void RemoveListener(System.Action<T> action)
45+
{
46+
if (_typedActions.Contains(action))
47+
_typedActions.Remove(action);
48+
}
3249
public override string ToString()
3350
{
3451
return "GameEventBase<" + typeof(T) + ">";
@@ -37,6 +54,7 @@ public override string ToString()
3754
public abstract class GameEventBase : SOArchitectureBaseObject, IGameEvent, IStackTraceObject
3855
{
3956
protected readonly List<IGameEventListener> _listeners = new List<IGameEventListener>();
57+
protected readonly List<System.Action> _actions = new List<System.Action>();
4058

4159
public List<StackTraceEntry> StackTraces { get { return _stackTraces; } }
4260
private List<StackTraceEntry> _stackTraces = new List<StackTraceEntry>();
@@ -60,6 +78,9 @@ public void Raise()
6078

6179
for (int i = _listeners.Count - 1; i >= 0; i--)
6280
_listeners[i].OnEventRaised();
81+
82+
for (int i = _actions.Count - 1; i >= 0; i--)
83+
_actions[i]();
6384
}
6485
public void AddListener(IGameEventListener listener)
6586
{
@@ -71,4 +92,14 @@ public void RemoveListener(IGameEventListener listener)
7192
if (_listeners.Contains(listener))
7293
_listeners.Remove(listener);
7394
}
95+
public void AddListener(System.Action action)
96+
{
97+
if (!_actions.Contains(action))
98+
_actions.Add(action);
99+
}
100+
public void RemoveListener(System.Action action)
101+
{
102+
if (_actions.Contains(action))
103+
_actions.Remove(action);
104+
}
74105
}

Assets/SO Architecture/References/BaseReference.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ public TBase Value
2828
_constantValue = value;
2929
}
3030
}
31+
public void AddListener(IGameEventListener listener)
32+
{
33+
if (_variable != null)
34+
_variable.AddListener(listener);
35+
}
36+
public void RemoveListener(IGameEventListener listener)
37+
{
38+
if (_variable != null)
39+
_variable.RemoveListener(listener);
40+
}
41+
public void AddListener(System.Action action)
42+
{
43+
if (_variable != null)
44+
_variable.AddListener(action);
45+
}
46+
public void RemoveListener(System.Action action)
47+
{
48+
if (_variable != null)
49+
_variable.AddListener(action);
50+
}
3151
public override string ToString()
3252
{
3353
return Value.ToString();

Assets/SO Architecture/Variables/BaseVariable.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
using UnityEngine;
22

3-
public abstract class BaseVariable : SOArchitectureBaseObject
3+
public abstract class BaseVariable : GameEventBase
44
{
55
public abstract bool ReadOnly { get; }
66
public abstract System.Type Type { get; }
77
public abstract object BaseValue { get; set; }
88
}
99
public abstract class BaseVariable<T> : BaseVariable
1010
{
11-
public virtual T Value { get { return _value; } set { _value = SetValue(value); } }
11+
public virtual T Value
12+
{
13+
get
14+
{
15+
return _value;
16+
}
17+
set
18+
{
19+
_value = SetValue(value);
20+
Raise();
21+
}
22+
}
1223
public override bool ReadOnly { get { return _readOnly; } }
1324
public override System.Type Type { get { return typeof(T); } }
14-
public override object BaseValue { get { return _value; } set { _value = SetValue((T)value); } }
25+
public override object BaseValue
26+
{
27+
get
28+
{
29+
return _value;
30+
}
31+
set
32+
{
33+
_value = SetValue((T)value);
34+
Raise();
35+
}
36+
}
1537

1638
[SerializeField]
1739
protected T _value;

0 commit comments

Comments
 (0)