-
Notifications
You must be signed in to change notification settings - Fork 64
/
UIFrame.cs
311 lines (274 loc) · 11.5 KB
/
UIFrame.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using UnityEngine;
using UnityEngine.UI;
namespace deVoid.UIFramework
{
/// <summary>
/// This is the centralized access point for all things UI.
/// All your calls should be directed at this.
/// </summary>
public class UIFrame : MonoBehaviour
{
[Tooltip("Set this to false if you want to manually initialize this UI Frame.")]
[SerializeField] private bool initializeOnAwake = true;
private PanelUILayer panelLayer;
private WindowUILayer windowLayer;
private Canvas mainCanvas;
private GraphicRaycaster graphicRaycaster;
/// <summary>
/// The main canvas of this UI
/// </summary>
public Canvas MainCanvas {
get {
if (mainCanvas == null) {
mainCanvas = GetComponent<Canvas>();
}
return mainCanvas;
}
}
/// <summary>
/// The Camera being used by the Main UI Canvas
/// </summary>
public Camera UICamera {
get { return MainCanvas.worldCamera; }
}
private void Awake() {
if (initializeOnAwake) {
Initialize();
}
}
/// <summary>
/// Initializes this UI Frame. Initialization consists of initializing both the Panel and Window layers.
/// Although literally all the cases I've had to this day were covered by the "Window and Panel" approach,
/// I made it virtual in case you ever need additional layers or other special initialization.
/// </summary>
public virtual void Initialize() {
if (panelLayer == null) {
panelLayer = gameObject.GetComponentInChildren<PanelUILayer>(true);
if (panelLayer == null) {
Debug.LogError("[UI Frame] UI Frame lacks Panel Layer!");
}
else {
panelLayer.Initialize();
}
}
if (windowLayer == null) {
windowLayer = gameObject.GetComponentInChildren<WindowUILayer>(true);
if (panelLayer == null) {
Debug.LogError("[UI Frame] UI Frame lacks Window Layer!");
}
else {
windowLayer.Initialize();
windowLayer.RequestScreenBlock += OnRequestScreenBlock;
windowLayer.RequestScreenUnblock += OnRequestScreenUnblock;
}
}
graphicRaycaster = MainCanvas.GetComponent<GraphicRaycaster>();
}
/// <summary>
/// Shows a panel by its id, passing no Properties.
/// </summary>
/// <param name="screenId">Panel Id</param>
public void ShowPanel(string screenId) {
panelLayer.ShowScreenById(screenId);
}
/// <summary>
/// Shows a panel by its id, passing parameters.
/// </summary>
/// <param name="screenId">Identifier.</param>
/// <param name="properties">Properties.</param>
/// <typeparam name="T">The type of properties to be passed in.</typeparam>
/// <seealso cref="IPanelProperties"/>
public void ShowPanel<T>(string screenId, T properties) where T : IPanelProperties {
panelLayer.ShowScreenById<T>(screenId, properties);
}
/// <summary>
/// Hides the panel with the given id.
/// </summary>
/// <param name="screenId">Identifier.</param>
public void HidePanel(string screenId) {
panelLayer.HideScreenById(screenId);
}
/// <summary>
/// Opens the Window with the given Id, with no Properties.
/// </summary>
/// <param name="screenId">Identifier.</param>
public void OpenWindow(string screenId) {
windowLayer.ShowScreenById(screenId);
}
/// <summary>
/// Closes the Window with the given Id.
/// </summary>
/// <param name="screenId">Identifier.</param>
public void CloseWindow(string screenId) {
windowLayer.HideScreenById(screenId);
}
/// <summary>
/// Closes the currently open window, if any is open
/// </summary>
public void CloseCurrentWindow() {
if (windowLayer.CurrentWindow != null) {
CloseWindow(windowLayer.CurrentWindow.ScreenId);
}
}
/// <summary>
/// Opens the Window with the given id, passing in Properties.
/// </summary>
/// <param name="screenId">Identifier.</param>
/// <param name="properties">Properties.</param>
/// <typeparam name="T">The type of properties to be passed in.</typeparam>
/// <seealso cref="IWindowProperties"/>
public void OpenWindow<T>(string screenId, T properties) where T : IWindowProperties {
windowLayer.ShowScreenById<T>(screenId, properties);
}
/// <summary>
/// Searches for the given id among the Layers, opens the Screen if it finds it
/// </summary>
/// <param name="screenId">The Screen id.</param>
public void ShowScreen(string screenId) {
Type type;
if (IsScreenRegistered(screenId, out type)) {
if (type == typeof(IWindowController)) {
OpenWindow(screenId);
}
else if (type == typeof(IPanelController)) {
ShowPanel(screenId);
}
}
else {
Debug.LogError(string.Format("Tried to open Screen id {0} but it's not registered as Window or Panel!",
screenId));
}
}
/// <summary>
/// Registers a screen. If transform is passed, the layer will
/// reparent it to itself. Screens can only be shown after they're registered.
/// </summary>
/// <param name="screenId">Screen identifier.</param>
/// <param name="controller">Controller.</param>
/// <param name="screenTransform">Screen transform. If not null, will be reparented to proper layer</param>
public void RegisterScreen(string screenId, IUIScreenController controller, Transform screenTransform) {
IWindowController window = controller as IWindowController;
if (window != null) {
windowLayer.RegisterScreen(screenId, window);
if (screenTransform != null) {
windowLayer.ReparentScreen(controller, screenTransform);
}
return;
}
IPanelController panel = controller as IPanelController;
if (panel != null) {
panelLayer.RegisterScreen(screenId, panel);
if (screenTransform != null) {
panelLayer.ReparentScreen(controller, screenTransform);
}
}
}
/// <summary>
/// Registers the panel. Panels can only be shown after they're registered.
/// </summary>
/// <param name="screenId">Screen identifier.</param>
/// <param name="controller">Controller.</param>
/// <typeparam name="TPanel">The Controller type.</typeparam>
public void RegisterPanel<TPanel>(string screenId, TPanel controller) where TPanel : IPanelController {
panelLayer.RegisterScreen(screenId, controller);
}
/// <summary>
/// Unregisters the panel.
/// </summary>
/// <param name="screenId">Screen identifier.</param>
/// <param name="controller">Controller.</param>
/// <typeparam name="TPanel">The Controller type.</typeparam>
public void UnregisterPanel<TPanel>(string screenId, TPanel controller) where TPanel : IPanelController {
panelLayer.UnregisterScreen(screenId, controller);
}
/// <summary>
/// Registers the Window. Windows can only be opened after they're registered.
/// </summary>
/// <param name="screenId">Screen identifier.</param>
/// <param name="controller">Controller.</param>
/// <typeparam name="TWindow">The Controller type.</typeparam>
public void RegisterWindow<TWindow>(string screenId, TWindow controller) where TWindow : IWindowController {
windowLayer.RegisterScreen(screenId, controller);
}
/// <summary>
/// Unregisters the Window.
/// </summary>
/// <param name="screenId">Screen identifier.</param>
/// <param name="controller">Controller.</param>
/// <typeparam name="TWindow">The Controller type.</typeparam>
public void UnregisterWindow<TWindow>(string screenId, TWindow controller) where TWindow : IWindowController {
windowLayer.UnregisterScreen(screenId, controller);
}
/// <summary>
/// Checks if a given Panel is open.
/// </summary>
/// <param name="panelId">Panel identifier.</param>
public bool IsPanelOpen(string panelId) {
return panelLayer.IsPanelVisible(panelId);
}
/// <summary>
/// Hide all screens
/// </summary>
/// <param name="animate">Defines if screens should the screens animate out or not.</param>
public void HideAll(bool animate = true) {
CloseAllWindows(animate);
HideAllPanels(animate);
}
/// <summary>
/// Hide all screens on the Panel Layer
/// </summary>
/// <param name="animate">Defines if screens should the screens animate out or not.</param>
public void HideAllPanels(bool animate = true) {
panelLayer.HideAll(animate);
}
/// <summary>
/// Hide all screens in the Window Layer
/// </summary>
/// <param name="animate">Defines if screens should the screens animate out or not.</param>
public void CloseAllWindows(bool animate = true) {
windowLayer.HideAll(animate);
}
/// <summary>
/// Checks if a given screen id is registered to either the Window or Panel layers
/// </summary>
/// <param name="screenId">The Id to check.</param>
public bool IsScreenRegistered(string screenId) {
if (windowLayer.IsScreenRegistered(screenId)) {
return true;
}
if (panelLayer.IsScreenRegistered(screenId)) {
return true;
}
return false;
}
/// <summary>
/// Checks if a given screen id is registered to either the Window or Panel layers,
/// also returning the screen type
/// </summary>
/// <param name="screenId">The Id to check.</param>
/// <param name="type">The type of the screen.</param>
public bool IsScreenRegistered(string screenId, out Type type) {
if (windowLayer.IsScreenRegistered(screenId)) {
type = typeof(IWindowController);
return true;
}
if (panelLayer.IsScreenRegistered(screenId)) {
type = typeof(IPanelController);
return true;
}
type = null;
return false;
}
private void OnRequestScreenBlock() {
if (graphicRaycaster != null) {
graphicRaycaster.enabled = false;
}
}
private void OnRequestScreenUnblock() {
if (graphicRaycaster != null) {
graphicRaycaster.enabled = true;
}
}
}
}