-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUNetworkedCanvasController.cs
201 lines (161 loc) · 6.4 KB
/
UNetworkedCanvasController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
namespace UnscriptedEngine
{
public class UNetworkedCanvasController : UNetworkedLevelObject, ICanvasController
{
[SerializeField] private UTheme theme;
[SerializeField] private Dictionary<string, UUIComponent> components;
public UTheme CanvasTheme => theme;
/// <summary>
/// Called when this UI controller has been created.
/// </summary>
/// <param name="context"></param>
public virtual void OnWidgetAttached(UNetworkedLevelObject context)
{
components = new Dictionary<string, UUIComponent>();
BroadcastMessage("InitializeUIComponent", this, SendMessageOptions.DontRequireReceiver);
}
/// <summary>
/// Called when this object is to be destroyed
/// </summary>
/// <param name="context"></param>
public virtual void OnWidgetDetached(UNetworkedLevelObject context)
{
DeInitializeAllComponents();
}
public void AddUIComponent(UUIComponent uIComponent)
{
if (components.ContainsKey(uIComponent.ID)) return;
components.Add(uIComponent.ID, uIComponent);
}
public T GetUIComponent<T>(string id) where T : UUIComponent
{
if (!components.ContainsKey(id))
{
BroadcastMessage("InitializeUIComponent", this);
}
T uIComponent = components[id] as T;
if (uIComponent == null)
{
Debug.LogError($"UIComponent with ID of {id} could not be found.");
}
return uIComponent;
}
/// <summary>
/// Binds a UIComponent to a Bindable variable, subscribing to it's events and updating when something changes.
/// </summary>
public void BindUI<T>(ref Bindable<T> variable, string id)
{
UUIComponent component = GetUIComponent<UUIComponent>(id);
variable.OnValueChanged += component.OnBindedValueChanged;
component.OnBindedValueChanged(variable.Value);
}
/// <summary>
/// Binds a UIComponent to a Bindable variable, subscribing to it's events and updating when something changes.
/// </summary>
public void BindUI<T>(ref Bindable<T> variable, string id, Func<T, object> defineBindingMethod)
{
UUIComponent component = GetUIComponent<UUIComponent>(id);
variable.OnValueChanged += (value) =>
{
component.OnBindedValueChanged(defineBindingMethod(value));
};
component.OnBindedValueChanged(defineBindingMethod(variable.Value));
}
/// <summary>
/// Binds a UIComponent to a Bindable variable, subscribing to it's events and updating when something changes.
/// </summary>
public void UnBindUI<T>(ref Bindable<T> variable, string id)
{
UUIComponent component = GetUIComponent<UUIComponent>(id);
variable.OnValueChanged -= component.OnBindedValueChanged;
}
/// <summary>
/// A shorthand to binding a method to any UIComponent that can be subscribed to when interacted with.
/// </summary>
public void Bind<T>(string id, UnityAction method) where T : UUIComponent, IBindable
{
T component = GetUIComponent<T>(id);
component.Bind(method);
}
/// <summary>
/// A shorthand to binding a method to any UIComponent that can be subscribed to when interacted with.
/// Bounded method expects to recieve the ID of the UI component.
/// </summary>
public void Bind<T>(string id, UnityAction<string> method) where T : UUIComponent, IBindable
{
T component = GetUIComponent<T>(id);
component.Bind(method);
}
/// <summary>
/// A shorthand to binding a method to any UIComponent that can be subscribed to when interacted with.
/// returns out the component that was bound to.
/// </summary>
public void Bind<T>(string id, UnityAction method, out T component) where T : UUIComponent, IBindable
{
component = GetUIComponent<T>(id);
component.Bind(method);
}
/// <summary>
/// A shorthand to binding a method to any UIComponent that can be subscribed to when interacted with.
/// Takes in a bindable component
/// </summary>
public void Bind(IBindable bindableComponent, UnityAction method)
{
bindableComponent.Bind(method);
}
/// <summary>
/// A shorthand to unbinding a method from any UIComponent that can be subscribed to when interacted with.
/// </summary>
public void UnBind<T>(string id, UnityAction method) where T : UUIComponent, IBindable
{
T component = GetUIComponent<T>(id);
component.UnBind(method);
}
/// <summary>
/// A shorthand to unbinding all methods from any UIComponent that can be subscribed to when interacted with.
/// </summary>
public void UnBind<T>(string id) where T : UUIComponent, IBindable
{
T component = GetUIComponent<T>(id);
component.UnBind();
}
protected override void OnLevelStopped()
{
base.OnLevelStopped();
UnBindAllComponents();
DeInitializeAllComponents();
}
public override void OnDestroy()
{
base.OnDestroy();
UnBindAllComponents();
DeInitializeAllComponents();
}
private void UnBindAllComponents()
{
if (components == null) return;
for (int i = 0; i < components.Count; i++)
{
UUIComponent component = components.ElementAt(i).Value;
IBindable bindable = component as IBindable;
if (bindable != null)
{
bindable.UnBind();
}
}
}
private void DeInitializeAllComponents()
{
if (components == null) return;
for (int i = 0; i < components.Count; i++)
{
components.ElementAt(i).Value.DeInitializeUIComponent();
}
}
}
}