-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
313 lines (276 loc) · 7.84 KB
/
stack.go
File metadata and controls
313 lines (276 loc) · 7.84 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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
package gotio
import (
"encoding/json"
"github.com/Avalanche-io/gotio/opentime"
)
// StackSchema is the schema for Stack.
var StackSchema = Schema{Name: "Stack", Version: 1}
// Stack is a composition of items arranged in layers (overlapping in time).
type Stack struct {
CompositionBase
}
// NewStack creates a new Stack.
func NewStack(
name string,
sourceRange *opentime.TimeRange,
metadata AnyDictionary,
effects []Effect,
markers []*Marker,
color *Color,
) *Stack {
stack := &Stack{
CompositionBase: NewCompositionBase(name, sourceRange, metadata, effects, markers, color),
}
stack.SetSelf(stack)
return stack
}
// CompositionKind returns "Stack".
func (s *Stack) CompositionKind() string {
return "Stack"
}
// InsertChild inserts a child at the given index.
func (s *Stack) InsertChild(index int, child Composable) error {
if index < 0 || index > len(s.children) {
return &IndexError{Index: index, Size: len(s.children)}
}
child.SetParent(s)
s.children = append(s.children[:index], append([]Composable{child}, s.children[index:]...)...)
return nil
}
// AppendChild appends a child.
func (s *Stack) AppendChild(child Composable) error {
return s.InsertChild(len(s.children), child)
}
// SetChild sets the child at the given index.
func (s *Stack) SetChild(index int, child Composable) error {
if index < 0 || index >= len(s.children) {
return &IndexError{Index: index, Size: len(s.children)}
}
s.children[index].SetParent(nil)
child.SetParent(s)
s.children[index] = child
return nil
}
// RemoveChild removes the child at the given index.
func (s *Stack) RemoveChild(index int) error {
if index < 0 || index >= len(s.children) {
return &IndexError{Index: index, Size: len(s.children)}
}
s.children[index].SetParent(nil)
s.children = append(s.children[:index], s.children[index+1:]...)
return nil
}
// RangeOfChildAtIndex returns the range of the child at the given index.
// For a Stack, all children start at time 0 and have their own duration.
func (s *Stack) RangeOfChildAtIndex(index int) (opentime.TimeRange, error) {
if index < 0 || index >= len(s.children) {
return opentime.TimeRange{}, &IndexError{Index: index, Size: len(s.children)}
}
// In a stack, all children start at time 0
dur, err := s.children[index].Duration()
if err != nil {
return opentime.TimeRange{}, err
}
// Start time is zero at the same rate as duration
startTime := opentime.NewRationalTime(0, dur.Rate())
return opentime.NewTimeRange(startTime, dur), nil
}
// AvailableRange returns the available range of the stack.
// The duration is the maximum duration of all children.
func (s *Stack) AvailableRange() (opentime.TimeRange, error) {
if len(s.children) == 0 {
return opentime.TimeRange{}, nil
}
// Initialize max duration with first child
maxDuration, err := s.children[0].Duration()
if err != nil {
return opentime.TimeRange{}, err
}
for i := 1; i < len(s.children); i++ {
dur, err := s.children[i].Duration()
if err != nil {
return opentime.TimeRange{}, err
}
if dur.ToSeconds() > maxDuration.ToSeconds() {
maxDuration = dur
}
}
startTime := opentime.NewRationalTime(0, maxDuration.Rate())
return opentime.NewTimeRange(startTime, maxDuration), nil
}
// Duration returns the duration of the stack.
func (s *Stack) Duration() (opentime.RationalTime, error) {
if s.sourceRange != nil {
return s.sourceRange.Duration(), nil
}
ar, err := s.AvailableRange()
if err != nil {
return opentime.RationalTime{}, err
}
return ar.Duration(), nil
}
// ChildAtTime returns the child at the given time.
// For a Stack, this returns the topmost (last) child that contains the time.
func (s *Stack) ChildAtTime(searchTime opentime.RationalTime, shallowSearch bool) (Composable, error) {
// Search from top to bottom (reverse order)
for i := len(s.children) - 1; i >= 0; i-- {
child := s.children[i]
childRange, err := s.RangeOfChildAtIndex(i)
if err != nil {
return nil, err
}
if childRange.Contains(searchTime) {
if !shallowSearch {
if comp, ok := child.(Composition); ok {
return comp.ChildAtTime(searchTime, false)
}
}
return child, nil
}
}
return nil, nil
}
// ChildrenInRange returns all children within the given range.
func (s *Stack) ChildrenInRange(searchRange opentime.TimeRange) ([]Composable, error) {
var result []Composable
for i, child := range s.children {
childRange, err := s.RangeOfChildAtIndex(i)
if err != nil {
return nil, err
}
if searchRange.Intersects(childRange, opentime.DefaultEpsilon) {
result = append(result, child)
}
}
return result, nil
}
// RangeOfAllChildren returns a map of child to range.
func (s *Stack) RangeOfAllChildren() (map[Composable]opentime.TimeRange, error) {
result := make(map[Composable]opentime.TimeRange)
for i, child := range s.children {
dur, err := s.children[i].Duration()
if err != nil {
return nil, err
}
result[child] = opentime.NewTimeRange(opentime.RationalTime{}, dur)
}
return result, nil
}
// AvailableImageBounds returns the union of all clips' image bounds.
func (s *Stack) AvailableImageBounds() (*Box2d, error) {
var result *Box2d
for _, child := range s.children {
var bounds *Box2d
var err error
if clip, ok := child.(*Clip); ok {
bounds, err = clip.AvailableImageBounds()
} else if track, ok := child.(*Track); ok {
bounds, err = track.AvailableImageBounds()
} else if stack, ok := child.(*Stack); ok {
bounds, err = stack.AvailableImageBounds()
}
if err != nil || bounds == nil {
continue
}
if result == nil {
result = &Box2d{
Min: bounds.Min,
Max: bounds.Max,
}
} else {
if bounds.Min.X < result.Min.X {
result.Min.X = bounds.Min.X
}
if bounds.Min.Y < result.Min.Y {
result.Min.Y = bounds.Min.Y
}
if bounds.Max.X > result.Max.X {
result.Max.X = bounds.Max.X
}
if bounds.Max.Y > result.Max.Y {
result.Max.Y = bounds.Max.Y
}
}
}
return result, nil
}
// SchemaName returns the schema name.
func (s *Stack) SchemaName() string {
return StackSchema.Name
}
// SchemaVersion returns the schema version.
func (s *Stack) SchemaVersion() int {
return StackSchema.Version
}
// Clone creates a deep copy.
func (s *Stack) Clone() SerializableObject {
clone := &Stack{
CompositionBase: CompositionBase{
ItemBase: ItemBase{
ComposableBase: ComposableBase{
SerializableObjectWithMetadataBase: SerializableObjectWithMetadataBase{
name: s.name,
metadata: CloneAnyDictionary(s.metadata),
},
},
sourceRange: cloneSourceRange(s.sourceRange),
effects: cloneEffects(s.effects),
markers: cloneMarkers(s.markers),
enabled: s.enabled,
color: cloneColor(s.color),
},
children: cloneChildren(s.children),
},
}
clone.SetSelf(clone)
for _, child := range clone.children {
child.SetParent(clone)
}
return clone
}
// IsEquivalentTo returns true if equivalent.
func (s *Stack) IsEquivalentTo(other SerializableObject) bool {
otherS, ok := other.(*Stack)
if !ok {
return false
}
if s.name != otherS.name {
return false
}
if len(s.children) != len(otherS.children) {
return false
}
for i := range s.children {
if !s.children[i].IsEquivalentTo(otherS.children[i]) {
return false
}
}
return true
}
// MarshalJSON implements json.Marshaler.
func (s *Stack) MarshalJSON() ([]byte, error) {
j, err := s.marshalCompositionJSON(StackSchema)
if err != nil {
return nil, err
}
return json.Marshal(j)
}
// UnmarshalJSON implements json.Unmarshaler.
func (s *Stack) UnmarshalJSON(data []byte) error {
var j compositionJSON
if err := json.Unmarshal(data, &j); err != nil {
return err
}
if err := s.unmarshalCompositionJSON(&j); err != nil {
return err
}
s.SetSelf(s)
return nil
}
func init() {
RegisterSchema(StackSchema, func() SerializableObject {
return NewStack("", nil, nil, nil, nil, nil)
})
}