EditModeUtils.cs
3.97 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline
{
static class EditModeUtils
{
public static void Delete(IEnumerable<ITimelineItem> items)
{
if (items == null)
return;
foreach (var item in items)
item.Delete();
}
public static void SetStart(IEnumerable<ITimelineItem> items, double time)
{
var offset = time - items.Min(c => c.start);
foreach (var item in items)
item.start += offset;
}
public static void SetParentTrack(IEnumerable<ITimelineItem> items, TrackAsset parentTrack)
{
foreach (var item in items)
{
if (item.parentTrack == parentTrack)
continue;
item.parentTrack = parentTrack;
var clipGUI = item.gui as TimelineClipGUI;
if (clipGUI != null)
{
clipGUI.clipCurveEditor = null;
}
}
}
public static ITimelineItem GetFirstIntersectedItem(IEnumerable<ITimelineItem> items, double time)
{
return items.FirstOrDefault(c => Intersects(time, c.start, c.end));
}
static bool Intersects(double time, double start, double end)
{
var discreteTime = (DiscreteTime)time;
return discreteTime > (DiscreteTime)start && discreteTime < (DiscreteTime)end;
}
public static bool Overlaps(ITimelineItem item, double from, double to)
{
var discreteFrom = (DiscreteTime)from;
var discreteTo = (DiscreteTime)to;
var discreteStart = (DiscreteTime)item.start;
if (discreteStart >= discreteFrom && discreteStart < discreteTo)
return true;
var discreteEnd = (DiscreteTime)item.end;
if (discreteEnd > discreteFrom && discreteEnd <= discreteTo)
return true;
return false;
}
public static bool IsItemWithinRange(ITimelineItem item, double from, double to)
{
return (DiscreteTime)item.start >= (DiscreteTime)from && (DiscreteTime)item.end <= (DiscreteTime)to;
}
public static bool IsRangeWithinItem(double from, double to, ITimelineItem item)
{
return (DiscreteTime)from >= (DiscreteTime)item.start && (DiscreteTime)to <= (DiscreteTime)item.end;
}
public static bool Contains(double from, double to, ITimelineItem item)
{
return (DiscreteTime)from < (DiscreteTime)item.start && (DiscreteTime)to > (DiscreteTime)item.end;
}
public static bool HasBlends(ITimelineItem item, TrimEdge edge)
{
var blendable = item as IBlendable;
if (blendable == null) return false;
return edge == TrimEdge.Start && blendable.hasLeftBlend || edge == TrimEdge.End && blendable.hasRightBlend;
}
public static double BlendDuration(ITimelineItem item, TrimEdge edge)
{
var blendable = item as IBlendable;
if (blendable == null) return 0.0;
return edge == TrimEdge.Start ? blendable.leftBlendDuration : blendable.rightBlendDuration;
}
public static bool IsInfiniteTrack(TrackAsset track)
{
var aTrack = track as AnimationTrack;
return aTrack != null && aTrack.CanConvertToClipMode();
}
public static void GetInfiniteClipBoundaries(TrackAsset track, out double start, out double end)
{
var info = AnimationClipCurveCache.Instance.GetCurveInfo(((AnimationTrack)track).infiniteClip);
if (info.keyTimes.Length > 0)
{
start = info.keyTimes.Min();
end = info.keyTimes.Max();
}
else
{
start = end = 0.0f;
}
}
}
}