File size: 7,103 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

namespace UnityEditor.Timeline
{
    class SequenceState : ISequenceState
    {
        readonly WindowState m_WindowState;
        readonly SequenceState m_ParentSequence;

        double m_Time;
        Range? m_CachedEvaluableRange;

        public TimelineAsset asset { get; }
        public PlayableDirector director { get; }
        public TimelineClip hostClip { get; }
        public double start { get; }
        public double timeScale { get; }

        public bool isAssetOnly { get; set; }

        public double duration
        {
            get
            {
                if (asset == null)
                    return 0.0;

                var assetDuration = asset.durationMode == TimelineAsset.DurationMode.FixedLength ? asset.fixedDuration : asset.duration;
                return hostClip == null ? assetDuration : Math.Min(hostClip.duration, assetDuration);
            }
        }

        [NonSerialized] List<UnityEngine.Object> m_CachedChildAssets;
        public List<UnityEngine.Object> cachedChildAssets
        {
            get
            {
                if (m_CachedChildAssets == null && asset != null)
                {
                    m_CachedChildAssets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(asset)).ToList();
                }

                return m_CachedChildAssets;
            }
        }

        public void InvalidateChildAssetCache()
        {
            m_CachedChildAssets = null;
        }

        bool? m_IsReadOnly;
        public bool isReadOnly
        {
            get
            {
                if (!m_IsReadOnly.HasValue)
                    m_IsReadOnly = FileUtility.IsReadOnly(asset);
                return m_IsReadOnly.Value;
            }
        }

        public void ResetIsReadOnly()
        {
            m_IsReadOnly = null;
        }

        public TimelineAssetViewModel viewModel
        {
            get
            {
                return TimelineWindowViewPrefs.GetOrCreateViewModel(asset);
            }
        }

        public double time
        {
            get
            {
                if (m_ParentSequence != null)
                    return hostClip.ToLocalTimeUnbound(m_ParentSequence.time);

                return GetLocalTime();
            }
            set
            {
                var correctedValue = Math.Min(value, TimeUtility.k_MaxTimelineDurationInSeconds);
                viewModel.windowTime = correctedValue;

                if (m_ParentSequence != null)
                    m_ParentSequence.time = hostClip.FromLocalTimeUnbound(correctedValue);
                else
                    SetLocalTime(correctedValue);
            }
        }

        public int frame
        {
            get { return TimeUtility.ToFrames(time, frameRate); }
            set { time = TimeUtility.FromFrames(Mathf.Max(0, value), frameRate); }
        }

        public double frameRate
        {
            get
            {
                if (asset != null)
                    return asset.editorSettings.frameRate;

                return TimelineAsset.EditorSettings.kDefaultFrameRate;
            }
            set
            {
                var settings = asset.editorSettings;
                if (Math.Abs(settings.frameRate - value) > TimeUtility.kFrameRateEpsilon)
                {
                    settings.frameRate = Math.Max(value, TimeUtility.kFrameRateEpsilon);
                    EditorUtility.SetDirty(asset);
                }
            }
        }

        public SequenceState(WindowState windowState, TimelineAsset asset, PlayableDirector director, TimelineClip hostClip, SequenceState parentSequence)
        {
            m_WindowState = windowState;
            m_ParentSequence = parentSequence;

            this.asset = asset;
            this.director = director;
            this.hostClip = hostClip;
            isAssetOnly = asset != null && director == null;

            start = hostClip == null ? 0.0 : hostClip.start;
            timeScale = hostClip == null ? 1.0 : hostClip.timeScale * parentSequence.timeScale;

            if (asset != null)
            {
                asset.AssetModifiedOnDisk += AssetOnAssetModifiedOnDisk;
            }
        }

        void AssetOnAssetModifiedOnDisk()
        {
            m_WindowState?.Refresh();
        }

        public Range GetEvaluableRange()
        {
            if (hostClip == null)
                return new Range
                {
                    start = 0.0,
                    end = duration
                };

            if (!m_CachedEvaluableRange.HasValue)
            {
                var globalRange = GetGlobalEvaluableRange();
                m_CachedEvaluableRange = new Range
                {
                    start = ToLocalTime(globalRange.start),
                    end = ToLocalTime(globalRange.end)
                };
            }

            return m_CachedEvaluableRange.Value;
        }

        public double ToGlobalTime(double t)
        {
            if (hostClip == null)
                return t;

            return m_ParentSequence.ToGlobalTime(hostClip.FromLocalTimeUnbound(t));
        }

        public double ToLocalTime(double t)
        {
            if (hostClip == null)
                return t;

            return hostClip.ToLocalTimeUnbound(m_ParentSequence.ToLocalTime(t));
        }

        double GetLocalTime()
        {
            if (!m_WindowState.previewMode && !Application.isPlaying)
                return viewModel.windowTime;

            // the time needs to always be synchronized with the director
            if (director != null)
                m_Time = director.time;

            return m_Time;
        }

        void SetLocalTime(double newTime)
        {
            // do this prior to the calback, because the callback pulls from the get
            if (director != null)
                director.time = newTime;

            if (Math.Abs(m_Time - newTime) > TimeUtility.kTimeEpsilon)
            {
                m_Time = newTime;
                m_WindowState.InvokeTimeChangeCallback();
            }
        }

        Range GetGlobalEvaluableRange()
        {
            if (hostClip == null)
                return new Range
                {
                    start = 0.0,
                    end = duration
                };

            var currentRange = new Range
            {
                start = hostClip.ToLocalTimeUnbound(ToGlobalTime(hostClip.start)),
                end = hostClip.ToLocalTimeUnbound(ToGlobalTime(hostClip.end))
            };

            return Range.Intersection(currentRange, m_ParentSequence.GetGlobalEvaluableRange());
        }

        public void Dispose()
        {
            if (asset != null)
            {
                asset.AssetModifiedOnDisk -= AssetOnAssetModifiedOnDisk;
            }

            TimelineWindowViewPrefs.SaveViewModel(asset);
        }
    }
}