-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicLayoutController.cs
525 lines (434 loc) · 19.7 KB
/
DynamicLayoutController.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using UnityEngine.Serialization;
#if UNITY_EDITOR
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.Timeline;
using UnityEditor.SceneManagement;
#endif
namespace AltSalt.Maestro.Layout {
[ExecuteInEditMode]
public class DynamicLayoutController : MonoBehaviour {
[Required]
[SerializeField]
[ReadOnly]
private AppSettingsReference _appSettings = new AppSettingsReference();
private AppSettings appSettings => _appSettings.GetVariable() as AppSettings;
[SerializeField]
private bool _refreshLayoutOnStart;
private bool refreshLayoutOnStart => _refreshLayoutOnStart;
private float deviceAspectRatio
{
get => appSettings.GetDeviceAspectRatio(this);
set => appSettings.SetDeviceAspectRatio(this.gameObject, value);
}
private float deviceWidth
{
get => appSettings.GetDeviceWidth(this);
set => appSettings.SetDeviceWidth(this.gameObject, value);
}
private float deviceHeight
{
get => appSettings.GetDeviceHeight(this);
set => appSettings.SetDeviceHeight(this.gameObject, value);
}
[FormerlySerializedAs("_sceneAspectRatio"),SerializeField]
private FloatReference _targetAspectRatio = new FloatReference();
private float targetAspectRatio
{
get => _targetAspectRatio.GetValue();
set => _targetAspectRatio.SetValue(this.gameObject, value);
}
[FormerlySerializedAs("_sceneWidth"),SerializeField]
private FloatReference _targetWidth = new FloatReference();
private float sceneWidth
{
get => _targetWidth.GetValue();
set => _targetWidth.SetValue(this.gameObject, value);
}
[FormerlySerializedAs("_sceneHeight"),SerializeField]
private FloatReference _targetHeight = new FloatReference();
private float sceneHeight
{
get => _targetHeight.GetValue();
set => _targetHeight.SetValue(this.gameObject, value);
}
[SerializeField]
private SimpleEventTrigger _callbackEvent;
private SimpleEventTrigger callbackEvent => _callbackEvent;
[SerializeField]
private bool _setScreenOrientation;
private bool setScreenOrientation => _setScreenOrientation;
[SerializeField]
[ShowIf(nameof(setScreenOrientation))]
private DimensionType _primaryOrientation;
private DimensionType primaryOrientation => _primaryOrientation;
[SerializeField]
private bool _hasBreakpoints;
private bool hasBreakpoints => _hasBreakpoints;
[SerializeField]
[ShowIf(nameof(hasBreakpoints))]
[InfoBox("You should always have x+1 breakpoint values; e.g., for 1 breakpoint at 1.34, you must specify 2 breakpoint values - one for aspect ratios wider than 1.34, and another for aspect ratios narrower than or equal to 1.34.")]
[InfoBox("Breakpoint examples: To target devices narrow than iPad (aspect ratio 1.33), specify a breakpoint of 1.34; to target narrower than iPhone (1.77), specify a breakpoint of 1.78.")]
private List<float> _deviceBreakpoints = new List<float>();
private List<float> deviceBreakpoints => _deviceBreakpoints;
[SerializeField]
private List<SceneDimension> _sceneDimensions = new List<SceneDimension>();
private List<SceneDimension> sceneDimensions => _sceneDimensions;
[ShowInInspector]
private List<IDynamicLayoutElement> _priorityDynamicElements = new List<IDynamicLayoutElement>();
private List<IDynamicLayoutElement> priorityDynamicElements => _priorityDynamicElements;
[ShowInInspector]
private List<IDynamicLayoutElement> _dynamicElements = new List<IDynamicLayoutElement>();
private List<IDynamicLayoutElement> dynamicElements => _dynamicElements;
[SerializeField]
private bool _delayStart;
private bool delayStart => _delayStart;
[SerializeField]
[ShowIf(nameof(delayStart))]
private float _delayAmount;
private float delayAmount => _delayAmount;
private enum ResizeType { PillarBox, LetterBox }
private bool _mobilePlayerActive;
public bool mobilePlayerActive
{
get => _mobilePlayerActive;
set => _mobilePlayerActive = value;
}
private bool _isFullscreen;
private bool isFullscreen
{
get => _isFullscreen;
set => _isFullscreen = value;
}
private void Start()
{
#if !UNITY_EDITOR && UNITY_IOS || UNITY_ANDROID
mobilePlayerActive = true;
#endif
if (Application.isPlaying == false || refreshLayoutOnStart == true) {
RefreshLayout();
}
isFullscreen = Screen.fullScreen;
}
private void OnDisable()
{
priorityDynamicElements.Clear();
dynamicElements.Clear();
}
#if UNITY_EDITOR
private void OnEnable()
{
_appSettings.PopulateVariable(this, nameof(_appSettings));
_targetAspectRatio.PopulateVariable(this, nameof(_targetAspectRatio));
_targetWidth.PopulateVariable(this, nameof(_targetWidth));
_targetHeight.PopulateVariable(this, nameof(_targetHeight));
_callbackEvent.PopulateVariable(this, nameof(_callbackEvent));
EditorSceneManager.sceneSaved += scene =>
{
if (TimelineEditor.inspectedDirector != null) {
TimelineEditor.Refresh(RefreshReason.ContentsModified);
TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);
TimelineEditor.Refresh(RefreshReason.WindowNeedsRedraw);
}
};
}
#endif
private void Update()
{
#if UNITY_EDITOR
// Do not execute if we are in prefab editing mode
if (PrefabStageUtility.GetCurrentPrefabStage() != null) {
return;
}
#endif
if (ScreenResized() == true || isFullscreen != Screen.fullScreen) {
RefreshLayout();
}
}
private bool ScreenResized()
{
if (Mathf.Approximately(deviceWidth, Screen.width) == false ||
Mathf.Approximately(deviceHeight, Screen.height) == false) {
return true;
}
return false;
}
public void RegisterDynamicElement(ComplexPayload complexPayload)
{
IDynamicLayoutElement element = complexPayload.GetObjectValue() as IDynamicLayoutElement;
if(element.parentScene == this.gameObject.scene &&
priorityDynamicElements.Contains(element) == false && dynamicElements.Contains(element) == false) {
if(element.priority != 0) {
priorityDynamicElements.Add(element);
} else {
dynamicElements.Add(element);
}
}
}
public void DeregisterDynamicElement(ComplexPayload complexPayload)
{
IDynamicLayoutElement element = complexPayload.GetObjectValue() as IDynamicLayoutElement;
if(priorityDynamicElements.Contains(element) == true) {
priorityDynamicElements.Remove(element);
}
if (dynamicElements.Contains(element) == true) {
dynamicElements.Remove(element);
}
}
[Button(ButtonSizes.Large), GUIColor(0.4f, 0.8f, 1)]
[PropertyOrder(8)]
public void CallRefreshLayout()
{
RefreshLayout();
}
private void RefreshLayout()
{
StartCoroutine(SaveScreenValues(() =>
{
PopulateSceneDimensions();
if (appSettings.dynamicLayoutActive == false) {
return;
}
// We track all priority responsive elements separately
// because sorting is an expensive operation
priorityDynamicElements.Sort(new ResponsiveUtilsCore.DynamicElementSort());
List<IDynamicLayoutElement> elementsToRemove = new List<IDynamicLayoutElement>();
UpdateDynamicElements(priorityDynamicElements, this, 1);
UpdateDynamicElements(dynamicElements, this, int.MinValue);
UpdateDynamicElements(priorityDynamicElements, this, int.MinValue, 0);
if (Application.isPlaying == true) {
if(delayStart == false) {
if (callbackEvent.GetVariable() != null) {
callbackEvent.RaiseEvent(this.gameObject);
}
} else {
StartCoroutine(LayoutRenderCompleteTimeDelay());
}
}
}));
}
private IEnumerator SaveScreenValues(Action callback)
{
// Sometimes these values are erroneous, so make sure
// we don't use them when they are
if (Screen.width <= 0 || Screen.height <= 0) yield break;
// It takes a few moments for the screen orientation to
// update on device, so yield until the values have been updated
if (mobilePlayerActive == true && setScreenOrientation == true) {
if (primaryOrientation == DimensionType.Vertical) {
Screen.autorotateToPortrait = true;
Screen.autorotateToPortraitUpsideDown = false;
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
Screen.orientation = ScreenOrientation.Portrait;
while (Screen.height < Screen.width) {
yield return null;
}
}
else {
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.orientation = ScreenOrientation.AutoRotation;
while (Screen.width < Screen.height) {
yield return null;
}
}
}
deviceWidth = Screen.width;
deviceHeight = Screen.height;
// Aspect ratio is calculated by dividing device height
// by device width. In the case of horizontal orientation
// in the editor, however, the screen values will be reversed,
// so we need to account for that in order to get the
// correct aspect ratio value, i.e. (4:3 = 1.333, 16:9 = 1.777)
#if UNITY_IOS || UNITY_ANDROID
if (Screen.height > Screen.width) {
deviceAspectRatio = deviceHeight / deviceWidth;
}
else {
deviceAspectRatio = deviceWidth / deviceHeight;
}
#endif
#if PLATFORM_STANDALONE_OSX
deviceWidth = Screen.safeArea.width;
deviceHeight = Screen.safeArea.height;
deviceAspectRatio = deviceHeight / deviceWidth;
#endif
callback();
}
private static List<IDynamicLayoutElement> UpdateDynamicElements(List<IDynamicLayoutElement> dynamicElementsList,
DynamicLayoutController dynamicLayoutController, int minPriority, int maxPriority = int.MaxValue)
{
// Sanitize our list in case any elements have been removed
for (int i = 0; i < dynamicElementsList.Count; i++) {
if (dynamicElementsList[i] == null || dynamicElementsList[i].Equals(null)) {
dynamicElementsList.Remove(dynamicElementsList[i]);
}
}
for (int i = 0; i < dynamicElementsList.Count; i++) {
if (dynamicElementsList[i].priority >= minPriority && dynamicElementsList[i].priority <= maxPriority) {
if (dynamicElementsList[i] is ISceneDimensionListener sceneDimensionListener) {
sceneDimensionListener.sceneAspectRatio = dynamicLayoutController.targetAspectRatio;
sceneDimensionListener.sceneWidth = dynamicLayoutController.sceneWidth;
sceneDimensionListener.sceneHeight = dynamicLayoutController.sceneHeight;
}
dynamicElementsList[i].CallExecuteLayoutUpdate(dynamicLayoutController.gameObject);
}
}
return dynamicElementsList;
}
private void PopulateSceneDimensions()
{
if(sceneDimensions.Count > 0) {
int breakpointIndex = 0;
if (hasBreakpoints == true) {
breakpointIndex = Utils.GetValueIndexInList(deviceAspectRatio, deviceBreakpoints);
}
SceneDimension sceneDimension = sceneDimensions[breakpointIndex];
switch (sceneDimension.targetAspectRatio) {
case AspectRatioType.x9x16:
if (deviceHeight > deviceWidth) {
if (deviceAspectRatio > 1.77) {
sceneWidth = deviceWidth;
sceneHeight = (16f * sceneWidth) / 9f;
}
else {
sceneHeight = deviceHeight;
sceneWidth = (9f * sceneHeight) / 16f;
}
}
else {
sceneHeight = deviceHeight;
sceneWidth = (9f * sceneHeight) / 16f;
}
break;
case AspectRatioType.x16x9:
if (deviceWidth > deviceHeight) {
if (deviceAspectRatio > 1.77) {
sceneHeight = deviceHeight;
sceneWidth = (16f * sceneHeight) / 9f;
}
else {
sceneWidth = deviceWidth;
sceneHeight = (9f * sceneWidth) / 16f;
}
}
else {
sceneWidth = deviceWidth;
sceneHeight = (9f * sceneWidth) / 16f;
}
break;
case AspectRatioType.x3x4:
if (deviceHeight > deviceWidth) {
if (deviceAspectRatio > 1.33) {
sceneWidth = deviceWidth;
sceneHeight = (4f * sceneWidth) / 3f;
} else {
sceneHeight = deviceHeight;
sceneWidth = (3f * sceneHeight) / 4f;
}
}
else {
sceneHeight = deviceHeight;
sceneWidth = (3f * sceneHeight) / 4f;
}
break;
case AspectRatioType.x4x3:
if (deviceWidth > deviceHeight) {
if (deviceAspectRatio > 1.33) {
sceneHeight = deviceHeight;
sceneWidth = (4f * sceneHeight) / 3f;
} else {
sceneWidth = deviceWidth;
sceneHeight = (3f * sceneWidth) / 4f;
}
}
else {
sceneWidth = deviceWidth;
sceneHeight = (3f * sceneWidth) / 4f;
}
break;
case AspectRatioType.x1x1:
if (deviceHeight > deviceWidth) {
sceneWidth = deviceWidth;
sceneHeight = deviceWidth;
}
else {
sceneHeight = deviceHeight;
sceneWidth = deviceHeight;
}
break;
case AspectRatioType.Dynamic:
sceneWidth = deviceWidth;
sceneHeight = deviceHeight;
break;
default:
Debug.Log("Specified aspect ratio " + nameof(sceneDimension.targetAspectRatio) + " is not supported.");
break;
}
} else {
sceneWidth = deviceWidth;
sceneHeight = deviceHeight;
}
// Aspect ratio is calculated by dividing device height
// by device width. In the case of a horizontal orientation, however,
// then screen values will be reversed, so we need to account for
// that in order to get the correct aspect ratio value, i.e.
// (4:3 = 1.333, 16:9 = 1.777)
if (sceneHeight > sceneWidth) {
targetAspectRatio = sceneHeight / sceneWidth;
}
else {
targetAspectRatio = sceneWidth / sceneHeight;
}
}
private IEnumerator LayoutRenderCompleteTimeDelay()
{
yield return new WaitForSeconds(delayAmount);
if (callbackEvent.GetVariable() != null) {
callbackEvent.RaiseEvent(this.gameObject);
}
}
[Serializable]
private class SceneDimension
{
[ValueDropdown(nameof(aspectRatioValues))]
[SerializeField]
public AspectRatioType targetAspectRatio = AspectRatioType.x9x16;
private ValueDropdownList<AspectRatioType> aspectRatioValues = new ValueDropdownList<AspectRatioType>(){
{"9 x 16", AspectRatioType.x9x16 },
{"16 x 9", AspectRatioType.x16x9 },
{"3 x 4", AspectRatioType.x3x4 },
{"4 x 3", AspectRatioType.x4x3 },
{"1 x 1 | Box", AspectRatioType.x1x1 },
{"None | Resize", AspectRatioType.Dynamic }
};
[ValueDropdown(nameof(resizeTypeValues))]
[SerializeField]
[ShowIf(nameof(CanChooseCrop))]
[HideInInspector]
public ResizeType resizeType = ResizeType.LetterBox;
private ValueDropdownList<ResizeType> resizeTypeValues = new ValueDropdownList<ResizeType>(){
{"Pillarbox : Crop or box top and bottom", ResizeType.PillarBox },
{"Letterbox : Crop or box sides", ResizeType.LetterBox }
};
private bool CanChooseCrop()
{
if(targetAspectRatio == AspectRatioType.Dynamic || targetAspectRatio == AspectRatioType.x1x1) {
return false;
}
return true;
}
}
private static bool IsPopulated(FloatReference attribute)
{
return Utils.IsPopulated(attribute);
}
}
}