-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponsiveUtilsCore.cs
116 lines (94 loc) · 4.61 KB
/
ResponsiveUtilsCore.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
using System.Collections.Generic;
using UnityEngine.Playables;
using UnityEngine.Timeline;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace AltSalt.Maestro
{
public static class ResponsiveUtilsCore
{
public class DynamicElementSort : Comparer<IDynamicLayoutElement>
{
public override int Compare(IDynamicLayoutElement x, IDynamicLayoutElement y)
{
return x.priority.CompareTo(y.priority);
}
}
#if UNITY_EDITOR
public static List<float> AddBreakpointToResponsiveElement(IResponsiveBreakpoints targetElement, float targetBreakpoint)
{
List<float> aspectRatioBreakpoints = targetElement.aspectRatioBreakpoints;
targetElement.hasBreakpoints = true;
if (aspectRatioBreakpoints.Contains(targetBreakpoint)) {
EditorUtility.DisplayDialog("Breakpoint already exists", "The breakpoint " + targetBreakpoint.ToString("F2") + " already exists on " + targetElement.elementName, "Okay");
return aspectRatioBreakpoints;
}
if (aspectRatioBreakpoints.Count == 0) {
targetElement.LogAddBreakpointMessage(targetBreakpoint);
aspectRatioBreakpoints.Add(targetBreakpoint);
return aspectRatioBreakpoints;
}
for (int i = 0; i < aspectRatioBreakpoints.Count; i++) {
if (targetBreakpoint < aspectRatioBreakpoints[i]) {
aspectRatioBreakpoints.Insert(i, targetBreakpoint);
break;
} else if (targetBreakpoint > aspectRatioBreakpoints[i] && aspectRatioBreakpoints.Count == i + 1) {
aspectRatioBreakpoints.Insert(i + 1, targetBreakpoint);
break;
}
}
targetElement.LogAddBreakpointMessage(targetBreakpoint);
return aspectRatioBreakpoints;
}
// =================================== //
// Utils for Responsive Clips //
// =================================== //
// Due to the way Unity instantiates ScriptPlayables, there's no way that I've found to get around strong casts
// to the behaviours in question to get the properties we need. The following functions are an admittedly bloated
// way to get those values. Time wasted trying to improve this: 5 hours.
public static IResponsiveBreakpoints GetResponsiveElementFromClipAsset(ResponsiveLerpToTargetClip responsiveLerpToTargetClip)
{
switch (responsiveLerpToTargetClip.GetType().Name) {
case nameof(ResponsiveVector3Clip): {
ResponsiveVector3Clip clipAsset = responsiveLerpToTargetClip as ResponsiveVector3Clip;
return clipAsset.template;
}
case nameof(ResponsiveFloatClip): {
ResponsiveFloatClip clipAsset = responsiveLerpToTargetClip as ResponsiveFloatClip;
return clipAsset.template;
}
}
return null;
}
public static PlayableAsset GetClipAssetFromResponsiveBehaviour(ResponsiveLerpToTargetBehaviour responsiveBehaviour)
{
switch (responsiveBehaviour.GetType().Name) {
case nameof(ResponsiveVector3Behaviour): {
ResponsiveVector3Behaviour behaviourInstance = responsiveBehaviour as ResponsiveVector3Behaviour;
return behaviourInstance.clipAsset;
}
case nameof(ResponsiveFloatBehaviour): {
ResponsiveFloatBehaviour behaviourInstance = responsiveBehaviour as ResponsiveFloatBehaviour;
return behaviourInstance.clipAsset;
}
}
return null;
}
public static TrackAsset GetParentTrackFromResponsiveBehaviour(ResponsiveLerpToTargetBehaviour responsiveBehaviour)
{
switch (responsiveBehaviour.GetType().Name) {
case nameof(ResponsiveVector3Behaviour): {
ResponsiveVector3Behaviour behaviourInstance = responsiveBehaviour as ResponsiveVector3Behaviour;
return behaviourInstance.parentTrack;
}
case nameof(ResponsiveFloatBehaviour): {
ResponsiveFloatBehaviour behaviourInstance = responsiveBehaviour as ResponsiveFloatBehaviour;
return behaviourInstance.parentTrack;
}
}
return null;
}
#endif
}
}