-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdmetrackgroup.cpp
More file actions
532 lines (446 loc) · 13.7 KB
/
dmetrackgroup.cpp
File metadata and controls
532 lines (446 loc) · 13.7 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
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
526
527
528
529
530
531
532
//====== Copyright © 1996-2005, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "movieobjects/dmetrackgroup.h"
#include <limits.h>
#include "tier0/dbg.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "movieobjects/dmetrack.h"
#include "movieobjects/dmeclip.h"
#include "movieobjects_interfaces.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// CDmeTrackGroup - contains a list of tracks
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeTrackGroup, CDmeTrackGroup );
void CDmeTrackGroup::OnConstruction()
{
m_Tracks.Init( this, "tracks", FATTRIB_MUSTCOPY | FATTRIB_HAS_CALLBACK );
m_bIsVisible.InitAndSet( this, "visible", true );
m_bMute.Init( this, "mute" );
m_flDisplayScale.InitAndSet( this, "displayScale", 1.0f );
m_bMinimized.InitAndSet( this, "minimized", true );
m_nMaxTrackCount = INT_MAX;
m_Volume.InitAndSet( this, "volume", 1.0 );
m_bForceMultiTrack.InitAndSet( this, "forcemultitrack", false );
}
void CDmeTrackGroup::OnDestruction()
{
// NOTE: The track owner handles may still be pointing to us when we get destructed,
// but their handles will be invalid, so GetTrackGroup on a track
// will correctly return NULL.
}
//-----------------------------------------------------------------------------
// Max track count
//-----------------------------------------------------------------------------
void CDmeTrackGroup::SetMaxTrackCount( int nCount )
{
m_nMaxTrackCount = nCount;
}
//-----------------------------------------------------------------------------
// Mute
//-----------------------------------------------------------------------------
void CDmeTrackGroup::SetMute( bool state )
{
m_bMute = state;
}
bool CDmeTrackGroup::IsMute( ) const
{
return m_bMute;
}
//-----------------------------------------------------------------------------
// Volume
//-----------------------------------------------------------------------------
void CDmeTrackGroup::SetVolume( float state )
{
m_Volume = state;
}
float CDmeTrackGroup::GetVolume() const
{
return m_Volume.Get();
}
//-----------------------------------------------------------------------------
// Owning clip
//-----------------------------------------------------------------------------
CDmeClip *CDmeTrackGroup::GetOwnerClip()
{
CDmeClip *pFindClip = FindReferringElement< CDmeClip >( this, "subClipTrackGroup" );
if ( !pFindClip )
{
pFindClip = FindReferringElement< CDmeClip >( this, "trackGroups" );
}
return pFindClip;
}
//-----------------------------------------------------------------------------
// Are we a film track group?
//-----------------------------------------------------------------------------
bool CDmeTrackGroup::IsFilmTrackGroup()
{
CDmeClip *pOwnerClip = GetOwnerClip();
if ( pOwnerClip )
return pOwnerClip->GetFilmTrackGroup() == this;
return m_nMaxTrackCount == 1;
}
//-----------------------------------------------------------------------------
// Is a particular clip typed able to be added?
//-----------------------------------------------------------------------------
bool CDmeTrackGroup::IsSubClipTypeAllowed( DmeClipType_t type )
{
if ( IsFilmTrackGroup() )
{
if ( type != DMECLIP_FILM )
return false;
}
else
{
if ( type == DMECLIP_FILM )
return false;
}
CDmeClip *pOwnerClip = GetOwnerClip();
Assert( pOwnerClip );
if ( !pOwnerClip )
return true;
return pOwnerClip->IsSubClipTypeAllowed( type );
}
//-----------------------------------------------------------------------------
// Track addition/removal
//-----------------------------------------------------------------------------
void CDmeTrackGroup::AddTrack( CDmeTrack *pTrack )
{
// FIXME: Should check if track with same name already exists???
if ( GetTrackIndex( pTrack ) < 0 )
{
// Tracks can only exist in one track group
Assert( GetTrackIndex( pTrack ) >= 0 );
m_Tracks.AddToTail( pTrack );
Assert( m_nMaxTrackCount >= m_Tracks.Count() );
}
}
CDmeTrack* CDmeTrackGroup::AddTrack( const char *pTrackName, DmeClipType_t trackType )
{
CDmeTrack *pTrack = CreateElement< CDmeTrack >( pTrackName, GetFileId() );
pTrack->SetClipType( trackType );
pTrack->SetCollapsed( false );
m_Tracks.AddToTail( pTrack );
Assert( m_nMaxTrackCount >= m_Tracks.Count() );
return pTrack;
}
CDmeTrack* CDmeTrackGroup::FindOrAddTrack( const char *pTrackName, DmeClipType_t trackType )
{
CDmeTrack *pTrack = FindTrack( pTrackName );
if ( pTrack )
{
// If we found it, but it's the wrong type, no dice
if ( pTrack->GetClipType() != trackType )
return NULL;
}
else
{
pTrack = AddTrack( pTrackName, trackType );
}
return pTrack;
}
void CDmeTrackGroup::RemoveTrack( int nIndex )
{
m_Tracks.Remove( nIndex );
}
void CDmeTrackGroup::RemoveTrack( CDmeTrack *pTrack )
{
int i = GetTrackIndex( pTrack );
if ( i >= 0 )
{
m_Tracks.Remove( i );
}
}
void CDmeTrackGroup::RemoveTrack( const char *pTrackName )
{
if ( !pTrackName )
{
pTrackName = DMETRACK_DEFAULT_NAME;
}
int c = m_Tracks.Count();
for ( int i = c; --i >= 0; )
{
if ( !Q_strcmp( m_Tracks[i]->GetName(), pTrackName ) )
{
m_Tracks.Remove( i );
return;
}
}
}
//-----------------------------------------------------------------------------
// Track finding
//-----------------------------------------------------------------------------
CDmeTrack *CDmeTrackGroup::FindTrack( const char *pTrackName ) const
{
if ( !pTrackName )
{
pTrackName = DMETRACK_DEFAULT_NAME;
}
int c = m_Tracks.Count();
for ( int i = 0 ; i < c; ++i )
{
CDmeTrack *pTrack = m_Tracks[i];
if ( !pTrack )
continue;
if ( !Q_strcmp( pTrack->GetName(), pTrackName ) )
return pTrack;
}
return NULL;
}
int CDmeTrackGroup::GetTrackIndex( CDmeTrack *pTrack ) const
{
int nTracks = m_Tracks.Count();
for ( int i = 0 ; i < nTracks; ++i )
{
if ( pTrack == m_Tracks[i] )
return i;
}
return -1;
}
//-----------------------------------------------------------------------------
// Creates the film track group [for internal use only]
//-----------------------------------------------------------------------------
CDmeTrack *CDmeTrackGroup::CreateFilmTrack()
{
Assert( GetTrackCount() == 0 );
return AddTrack( "Film", DMECLIP_FILM );
}
//-----------------------------------------------------------------------------
// Returns the film track, if any
//-----------------------------------------------------------------------------
CDmeTrack *CDmeTrackGroup::GetFilmTrack()
{
if ( !IsFilmTrackGroup() )
return NULL;
if ( GetTrackCount() > 0 )
{
Assert( GetTrackCount() == 1 );
return m_Tracks[0];
}
return NULL;
}
//-----------------------------------------------------------------------------
// Adding/removing clips from tracks
//-----------------------------------------------------------------------------
CDmeTrack *CDmeTrackGroup::AddClip( CDmeClip *pClip, const char *pTrackName )
{
DmeClipType_t type = pClip->GetClipType();
if ( !pTrackName )
{
pTrackName = DMETRACK_DEFAULT_NAME;
}
CDmeTrack *pTrack = FindOrAddTrack( pTrackName, type );
if ( pTrack )
{
pTrack->AddClip( pClip );
}
return pTrack;
}
bool CDmeTrackGroup::RemoveClip( CDmeClip *pClip )
{
CDmeTrack *pTrack = FindTrackForClip( pClip );
if ( pTrack )
return pTrack->RemoveClip( pClip );
return false;
}
//-----------------------------------------------------------------------------
// Changing clip track
//-----------------------------------------------------------------------------
CDmeTrack *CDmeTrackGroup::ChangeTrack( CDmeClip *pClip, const char *pNewTrack )
{
// Add, then remove, to avoid refcount problems
// Don't remove if it wasn't added for some reason.
CDmeTrack *pOldTrack = FindTrackForClip( pClip );
CDmeTrack *pTrack = AddClip( pClip, pNewTrack );
if ( pTrack && pOldTrack )
{
pOldTrack->RemoveClip( pClip );
}
return pTrack;
}
//-----------------------------------------------------------------------------
// Finding clips in tracks
//-----------------------------------------------------------------------------
CDmeTrack *CDmeTrackGroup::FindTrackForClip( CDmeClip *pClip ) const
{
int nTrackIndex = -1;
if ( !FindTrackForClip( pClip, &nTrackIndex, NULL ) )
return NULL;
return GetTrack( nTrackIndex );
}
bool CDmeTrackGroup::FindTrackForClip( CDmeClip *pClip, int *pTrackIndex, int *pClipIndex ) const
{
DmeClipType_t type = pClip->GetClipType();
int c = GetTrackCount();
for ( int i = 0; i < c; ++i )
{
CDmeTrack *pTrack = GetTrack( i );
if ( !pTrack )
continue;
if ( pTrack->GetClipType() != type )
continue;
int nClipCount = pTrack->GetClipCount();
for ( int j = 0; j < nClipCount; ++j )
{
if ( pTrack->GetClip( j ) == pClip )
{
if ( pTrackIndex )
{
*pTrackIndex = i;
}
if ( pClipIndex )
{
*pClipIndex = j;
}
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------------
// Finding clips in tracks by time
//-----------------------------------------------------------------------------
void CDmeTrackGroup::FindClipsAtTime( DmeClipType_t clipType, DmeTime_t time, DmeClipSkipFlag_t flags, CUtlVector< CDmeClip * >& clips ) const
{
if ( ( flags & DMESKIP_INVISIBLE ) && ( !IsVisible() || IsMinimized() ) )
return;
if ( ( flags & DMESKIP_MUTED ) && IsMute() )
return;
int c = GetTrackCount();
for ( int i = 0; i < c; ++i )
{
CDmeTrack *pTrack = GetTrack( i );
if ( !pTrack )
continue;
if ( ( clipType != DMECLIP_UNKNOWN ) && ( pTrack->GetClipType() != clipType ) )
continue;
pTrack->FindClipsAtTime( time, flags, clips );
}
}
void CDmeTrackGroup::FindClipsIntersectingTime( DmeClipType_t clipType, DmeTime_t startTime, DmeTime_t endTime, DmeClipSkipFlag_t flags, CUtlVector< CDmeClip * >& clips ) const
{
if ( ( flags & DMESKIP_INVISIBLE ) && ( !IsVisible() || IsMinimized() ) )
return;
if ( ( flags & DMESKIP_MUTED ) && IsMute() )
return;
int c = GetTrackCount();
for ( int i = 0; i < c; ++i )
{
CDmeTrack *pTrack = GetTrack( i );
if ( !pTrack )
continue;
if ( ( clipType != DMECLIP_UNKNOWN ) && ( pTrack->GetClipType() != clipType ) )
continue;
pTrack->FindClipsIntersectingTime( startTime, endTime, flags, clips );
}
}
void CDmeTrackGroup::FindClipsWithinTime( DmeClipType_t clipType, DmeTime_t startTime, DmeTime_t endTime, DmeClipSkipFlag_t flags, CUtlVector< CDmeClip * >& clips ) const
{
if ( ( flags & DMESKIP_INVISIBLE ) && ( !IsVisible() || IsMinimized() ) )
return;
if ( ( flags & DMESKIP_MUTED ) && IsMute() )
return;
int c = GetTrackCount();
for ( int i = 0; i < c; ++i )
{
CDmeTrack *pTrack = GetTrack( i );
if ( !pTrack )
continue;
if ( ( clipType != DMECLIP_UNKNOWN ) && ( pTrack->GetClipType() != clipType ) )
continue;
pTrack->FindClipsWithinTime( startTime, endTime, flags, clips );
}
}
//-----------------------------------------------------------------------------
// Removes empty tracks
//-----------------------------------------------------------------------------
void CDmeTrackGroup::RemoveEmptyTracks()
{
int tc = GetTrackCount();
for ( int i = tc; --i >= 0; )
{
CDmeTrack *pTrack = GetTrack( i );
if ( pTrack->GetClipCount() == 0 )
{
RemoveTrack( i );
}
}
}
//-----------------------------------------------------------------------------
// Sort tracks by track type, then alphabetically
//-----------------------------------------------------------------------------
static int TrackLessFunc( const void * lhs, const void * rhs )
{
CDmeTrack *pInfo1 = *(CDmeTrack**)lhs;
CDmeTrack *pInfo2 = *(CDmeTrack**)rhs;
if ( pInfo1->GetClipType() < pInfo2->GetClipType() )
return -1;
if ( pInfo1->GetClipType() > pInfo2->GetClipType() )
return 1;
return Q_strcmp( pInfo1->GetName(), pInfo2->GetName() );
}
void CDmeTrackGroup::SortTracksByType()
{
int tc = GetTrackCount();
if ( tc == 0 )
return;
CDmeTrack **ppTrack = (CDmeTrack**)_alloca( tc * sizeof(CDmeTrack*) );
for ( int i = 0; i < tc; ++i )
{
ppTrack[i] = GetTrack(i);
}
qsort( ppTrack, tc, sizeof(CDmeTrack*), TrackLessFunc );
m_Tracks.RemoveAll();
for ( int i = 0; i < tc; ++i )
{
m_Tracks.AddToTail( ppTrack[i] );
}
}
//-----------------------------------------------------------------------------
// Returns the flattened clip count
//-----------------------------------------------------------------------------
int CDmeTrackGroup::GetSubClipCount() const
{
int nCount = 0;
DMETRACKGROUP_FOREACH_CLIP_START( this, pTrack, pClip )
++nCount;
DMETRACKGROUP_FOREACH_CLIP_END()
return nCount;
}
void CDmeTrackGroup::GetSubClips( CDmeClip **ppClips )
{
int nCount = 0;
DMETRACKGROUP_FOREACH_CLIP_START( this, pTrack, pClip )
ppClips[nCount++] = pClip;
DMETRACKGROUP_FOREACH_CLIP_END()
}
bool CDmeTrackGroup::GetForceMultiTrack() const
{
return const_cast< CDmeTrackGroup * >( this )->IsFilmTrackGroup() && m_bForceMultiTrack;
}
void CDmeTrackGroup::SetForceMultiTrack( bool bForce )
{
m_bForceMultiTrack = bForce;
}
//-----------------------------------------------------------------------------
// helper methods
//-----------------------------------------------------------------------------
CDmeFilmClip *GetParentClip( CDmeTrackGroup *pTrackGroup )
{
DmAttributeReferenceIterator_t hAttr = g_pDataModel->FirstAttributeReferencingElement( pTrackGroup->GetHandle() );
for ( ; hAttr != DMATTRIBUTE_REFERENCE_ITERATOR_INVALID; hAttr = g_pDataModel->NextAttributeReferencingElement( hAttr ) )
{
CDmAttribute *pAttr = g_pDataModel->GetAttribute( hAttr );
if ( !pAttr )
continue;
CDmeFilmClip *pFilmClip = CastElement< CDmeFilmClip >( pAttr->GetOwner() );
if ( pFilmClip )
return pFilmClip;
}
return NULL;
}