forked from OpenShot/openshot-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.js
1490 lines (1326 loc) · 47.1 KB
/
controllers.js
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* @brief The AngularJS controller used by the OpenShot Timeline
* @author Jonathan Thomas <[email protected]>
* @author Cody Parker <[email protected]>
*
* @section LICENSE
*
* Copyright (c) 2008-2018 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Video Editor, an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Video Editor is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Video Editor is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
// Initialize the main controller module
App.controller('TimelineCtrl',function($scope) {
// DEMO DATA (used when debugging outside of Qt using Chrome)
$scope.project = {
fps: {
num : 24,
den : 1
},
duration : 300, //length of project in seconds
scale : 16.0, //seconds per tick
tick_pixels : 100, //pixels between tick mark
playhead_position : 10, //position of play head
clips : [],
// [
// {
// id : '1',
// layer : 1,
// image : './media/images/thumbnail.png',
// locked : false,
// duration : 32, //max length in seconds
// start : 0,
// end : 32,
// position : 0.0,
// title : 'Clip U2V5ENELDY',
// effects : [
// { type : 'Saturation', icon : 'bw.png'},
// { type : 'ChromaKey',icon : 'om.png'},
// { type : 'Negate',icon : 'neg.png'},
// { type : 'Blur', icon: 'blur.png'},
// { type : 'Brightness', icon: 'cartoon.png'}
// ],
// images : {start: 1, end: 4},
// show_audio : false,
//
// alpha: {
// Points: [
// {
// "interpolation": 2,
// "co": {
// "Y": 0,
// "X": 0
// }
// },
// {
// "interpolation": 1,
// "co": {
// "Y": 0,
// "X": 250
// }
// },
// {
// "interpolation": 1,
// "co": {
// "Y": 1,
// "X": 500
// }
// }
// ]
// },
// location_x: { Points: [] },
// location_y: { Points: [] },
// scale_x: { Points: [] },
// scale_y: { Points: [] },
// rotation: { Points: [] },
// time: { Points: [] },
// volume: { Points: [] }
//
// },
// {
// id : '2',
// layer : 2,
// image : './media/images/thumbnail.png',
// locked : false,
// duration : 45,
// start : 0,
// end : 45,
// position : 0.0,
// title : 'Clip B',
// effects : [],
// images : {start: 3, end: 7},
// show_audio : false,
// alpha: { Points: [] },
// location_x: { Points: [] },
// location_y: { Points: [] },
// scale_x: { Points: [] },
// scale_y: { Points: [] },
// rotation: { Points: [] },
// time: { Points: [] },
// volume: { Points: [] }
// },
// {
// id : '3',
// layer : 3,
// image : './media/images/thumbnail.png',
// locked : false,
// duration : 120,
// start : 0,
// end : 120,
// position : 32.0,
// title : 'Clip C',
// effects : [
// { type : 'Deinterlace',icon : 'om.png'},
// { type : 'Blur', icon: 'blur.png'},
// { type : 'Mask', icon: 'cartoon.png'}
// ],
// images : { start: 5, end: 10 },
// show_audio : false,
// audio_data : [.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3,.5, .6, .7, .7, .6, .5, .4, .1, 0, -0.1, -0.3, -0.6, -0.6, -0.3, -0.1, 0, .2, .3, ],
// alpha: { Points: [] },
// location_x: { Points: [] },
// location_y: { Points: [] },
// scale_x: { Points: [] },
// scale_y: { Points: [] },
// rotation: { Points: [] },
// time: { Points: [] },
// volume: { Points: [] }
// },
// ],
effects : [],
// [
// {
// id : '5',
// layer : 4,
// title : 'Transition',
// position : 20.0,
// start : 0,
// end : 30
// },
// {
// id : '6',
// layer : 3,
// title : 'Transition',
// position : 137.5,
// start : 0,
// end : 30
// },
// {
// id : '7',
// layer : 2,
// title : 'Transition',
// position : 30.5,
// start : 0,
// end : 30
// }
//
// ],
layers : [
{id: 'L0', number:0, y:0, label: '', lock: false},
{id: 'L1', number:1, y:0, label: '', lock: false},
{id: 'L2', number:2, y:0, label: '', lock: false},
{id: 'L3', number:3, y:0, label: '', lock: false},
{id: 'L4', number:4, y:0, label: '', lock: false}
],
markers : [],
// [
// {
// id : 'M1',
// position : 16,
// icon : 'yellow.png'
// },
// {
// id : 'M2',
// position: 120,
// icon : 'green.png'
// },
// {
// id : 'M3',
// position: 300,
// icon : 'red.png'
// },
// {
// id : 'M4',
// position: 10,
// icon : 'purple.png'
// },
// ],
progress : {}
// {
// max_bytes : "0",
// ranges : [
// {
// end : "30",
// start : "0"
// },
// {
// end : "40",
// start : "50"
// },
// {
// end : "100",
// start : "150"
// }
// ],
// version : "2"
// }
};
// Additional variables used to control the rendering of HTML
$scope.pixelsPerSecond = parseFloat($scope.project.tick_pixels) / parseFloat($scope.project.scale);
$scope.playheadOffset = 0;
$scope.keyframePointOffset = 3;
$scope.playhead_animating = false;
$scope.playhead_height = 300;
$scope.playheadTime = secondsToTime($scope.project.playhead_position, $scope.project.fps.num, $scope.project.fps.den);
$scope.shift_pressed = false;
$scope.snapline_position = 0.0;
$scope.snapline = false;
$scope.enable_snapping = true;
$scope.enable_razor = false;
$scope.debug = false;
$scope.min_width = 1024;
$scope.track_label = "Track %s";
$scope.enable_sorting = true;
// Method to set if Qt is detected (which clears demo data)
$scope.Qt = false;
$scope.EnableQt = function() {
$scope.Qt = true;
timeline.qt_log("$scope.Qt = true;");
};
// Move the playhead to a specific time
$scope.MovePlayhead = function(position_seconds) {
// Update internal scope (in seconds)
$scope.project.playhead_position = position_seconds;
$scope.playheadTime = secondsToTime(position_seconds, $scope.project.fps.num, $scope.project.fps.den);
// Use JQuery to move playhead (for performance reasons) - scope.apply is too expensive here
$(".playhead-top").css("left", (($scope.project.playhead_position * $scope.pixelsPerSecond) + $scope.playheadOffset) + "px");
$(".playhead-line").css("left", (($scope.project.playhead_position * $scope.pixelsPerSecond) + $scope.playheadOffset) + "px");
$("#ruler_time").text($scope.playheadTime.hour + ":" + $scope.playheadTime.min + ":" + $scope.playheadTime.sec + ":" + $scope.playheadTime.frame);
};
// Move the playhead to a specific frame
$scope.MovePlayheadToFrame = function(position_frames) {
// Don't move the playhead if it's currently animating
if ($scope.playhead_animating) {
return;
}
// Determine seconds
var frames_per_second = $scope.project.fps.num / $scope.project.fps.den;
var position_seconds = ((position_frames - 1) / frames_per_second);
// Update internal scope (in seconds)
$scope.MovePlayhead(position_seconds);
};
// Move the playhead to a specific time
$scope.PreviewFrame = function(position_seconds) {
// Determine frame
var frames_per_second = $scope.project.fps.num / $scope.project.fps.den;
var frame = Math.round(position_seconds * frames_per_second) + 1;
// Update GUI with position (so the preview can be updated)
if ($scope.Qt) {
timeline.PlayheadMoved(frame);
}
};
// Move the playhead to a specific time
$scope.PreviewClipFrame = function(clip_id, position_seconds) {
// Get the nearest starting frame position to the playhead (this helps to prevent cutting
// in-between frames, and thus less likely to repeat or skip a frame).
// Determine frame
position_seconds_rounded = (Math.round((position_seconds * $scope.project.fps.num) / $scope.project.fps.den ) * $scope.project.fps.den ) / $scope.project.fps.num;
var frames_per_second = $scope.project.fps.num / $scope.project.fps.den;
var frame = Math.round(position_seconds_rounded * frames_per_second) + 1;
// Update GUI with position (so the preview can be updated)
if ($scope.Qt) {
timeline.PreviewClipFrame(clip_id, frame);
}
};
// Get an array of keyframe points for the selected clips
$scope.getKeyframes = function(object) {
// List of keyframes
keyframes = {};
var frames_per_second = $scope.project.fps.num / $scope.project.fps.den;
var clip_start_x = Math.round(object.start * frames_per_second) + 1;
var clip_end_x = Math.round(object.end * frames_per_second) + 1;
// Loop through properties of an object (clip/transition), looking for keyframe points
for (child in object) {
if (!object.hasOwnProperty(child)) {
//The current property is not a direct property of p
continue;
}
// Determine if this property is a Keyframe
if (typeof object[child] == "object" && "Points" in object[child]) {
for (const point of object[child].Points) {
if (point.co.X >= clip_start_x && point.co.X <= clip_end_x)
// Only add keyframe coordinates that are within the bounds of the clip
keyframes[point.co.X] = point.co.Y;
}
}
// Determine if this property is a Color Keyframe
if (typeof object[child] == "object" && "red" in object[child]) {
for (const point of object[child]["red"].Points) {
if (point.co.X >= clip_start_x && point.co.X <= clip_end_x)
// Only add keyframe coordinates that are within the bounds of the clip
keyframes[point.co.X] = point.co.Y;
}
}
}
// Determine if this property contains effects (i.e. clips have their own effects)
if ("effects" in object) {
for (effect in object["effects"]) {
// Loop through properties of an effect, looking for keyframe points
for (child in object["effects"][effect]) {
if (!object["effects"][effect].hasOwnProperty(child)) {
//The current property is not a direct property of p
continue;
}
// Determine if this property is a Keyframe
if (typeof object["effects"][effect][child] == "object" && "Points" in object["effects"][effect][child]) {
for (const point of object["effects"][effect][child].Points) {
if (point.co.X >= clip_start_x && point.co.X <= clip_end_x)
// Only add keyframe coordinates that are within the bounds of the clip
keyframes[point.co.X] = point.co.Y;
}
}
// Determine if this property is a Color Keyframe
if (typeof object["effects"][effect][child] == "object" && "red" in object["effects"][effect][child]) {
for (const point of object["effects"][effect][child]["red"].Points) {
if (point.co.X >= clip_start_x && point.co.X <= clip_end_x)
// Only add keyframe coordinates that are within the bounds of the clip
keyframes[point.co.X] = point.co.Y;
}
}
}
}
}
// Return keyframe array
return keyframes;
};
// Determine track top (in vertical pixels)
$scope.getTrackTop = function(layer) {
// Get scrollbar position
var vert_scroll_offset = $("#scrolling_tracks").scrollTop();
var horz_scroll_offset = $("#scrolling_tracks").scrollLeft();
// Find this tracks Y location
var track_id = "div#track_" + layer;
if ($(track_id).length) {
return $(track_id).position().top + vert_scroll_offset;
}
else {
return 0;
}
};
// ############# QT FUNCTIONS #################### //
// Change the scale and apply to scope
$scope.setScale = function(scaleVal, cursor_x) {
// Get scrollbar positions
var horz_scroll_offset = $("#scrolling_tracks").scrollLeft();
var track_labels_width = $("#track_controls").width();
// Determine actual x coordinate (over timeline)
var center_x = Math.max(cursor_x - track_labels_width, 0);
if (cursor_x === 0) {
center_x = 0;
}
// Determine time of cursor position
var cursor_time = parseFloat(center_x + horz_scroll_offset) / $scope.pixelsPerSecond;
$scope.$apply(function() {
$scope.project.scale = parseFloat(scaleVal);
$scope.pixelsPerSecond = parseFloat($scope.project.tick_pixels) / parseFloat($scope.project.scale);
});
// Scroll back to correct cursor time (minus the difference of the cursor location)
var new_cursor_x = Math.round((cursor_time * $scope.pixelsPerSecond) - center_x);
$("#scrolling_tracks").scrollLeft(new_cursor_x);
};
// Update thumbnail for clip
$scope.updateThumbnail = function(clip_id) {
// Find matching clip, update thumbnail to same path (to force reload)
var existing_thumb_path = $("#clip_" + clip_id + " .thumb").attr("src");
timeline.qt_log(existing_thumb_path);
$("#clip_" + clip_id + " .thumb").attr("src", existing_thumb_path);
};
// Set the audio data for a clip
$scope.setAudioData = function(clip_id, audio_data) {
// Find matching clip
for (const clip of $scope.project.clips) {
if (clip.id == clip_id) {
// Set audio data
$scope.$apply(function(){
clip.audio_data = audio_data;
clip.show_audio = true;
});
timeline.qt_log("Audio data successful set on clip JSON");
break;
}
// Draw audio data
drawAudio($scope, clip_id);
}
};
// Hide the audio waveform for a clip
$scope.hideAudioData = function(clip_id, audio_data) {
// Find matching clip
for (const clip of $scope.project.clips) {
if (clip.id == clip_id) {
// Set audio data
$scope.$apply(function(){
clip.show_audio = false;
clip.audio_data = [];
});
break;
}
}
};
// Redraw all audio waveforms on the timeline (if any)
$scope.reDrawAllAudioData = function() {
// Find matching clip
for (const clip of $scope.project.clips) {
if ("audio_data" in clip && clip.audio_data.length > 0) {
// Redraw audio data (since it has audio data)
drawAudio($scope, clip.id);
}
}
};
// Does clip have audio_data?
$scope.hasAudioData = function(clip_id) {
// Find matching clip
for (const clip of $scope.project.clips) {
if (clip.id == clip_id && "audio_data" in clip && clip.audio_data.length > 0) {
return true;
break;
}
}
return false;
};
// Change the snapping mode
$scope.SetSnappingMode = function(enable_snapping) {
$scope.$apply(function() {
$scope.enable_snapping = enable_snapping;
if (enable_snapping) {
$(".droppable").draggable("option", "snapTolerance", 20);
}
else {
$(".droppable").draggable("option", "snapTolerance", 0);
}
});
};
// Change the razor mode
$scope.SetRazorMode = function(enable_razor) {
$scope.$apply(function() {
$scope.enable_razor = enable_razor;
});
};
// Get the color of an effect
$scope.GetEffectColor = function(effect_type) {
switch (effect_type) {
case "Blur":
return "#0095bf";
case "Brightness":
return "#5500ff";
case "ChromaKey":
return "#00ad2d";
case "Deinterlace":
return "#006001";
case "Mask":
return "#cb0091";
case "Negate":
return "#ff9700";
case "Saturation":
return "#ff3d00";
default:
return "#000000";
}
};
// Add a new clip to the timeline
$scope.AddClip = function(x, y, clip_json) {
$scope.$apply(function() {
// Convert x and y into timeline vars
var scrolling_tracks_offset = $("#scrolling_tracks").offset().left;
var clip_position = parseFloat(x - scrolling_tracks_offset) / parseFloat($scope.pixelsPerSecond);
// Get the nearest starting frame position to the clip position (this helps to prevent cutting
// in-between frames, and thus less likely to repeat or skip a frame).
clip_position = (Math.round((clip_position * $scope.project.fps.num) / $scope.project.fps.den ) * $scope.project.fps.den ) / $scope.project.fps.num;
clip_json.position = clip_position;
clip_json.layer = $scope.GetTrackAtY(y).number;
// Push new clip onto stack
$scope.project.clips.push(clip_json);
});
};
// Update cache json
$scope.RenderCache = function(cache_json) {
// Push new clip onto stack
$scope.project.progress = cache_json;
//clear the canvas first
var ruler = $("#progress");
var ctx = ruler[0].getContext('2d');
ctx.clearRect(0, 0, ruler.width(), ruler.height());
// Determine fps & and get cached ranges
var fps = $scope.project.fps.num / $scope.project.fps.den;
var progress = $scope.project.progress.ranges;
// Loop through each cached range of frames, and draw rect
for(p=0;p<progress.length;p++) {
//get the progress item details
var start_second = parseFloat(progress[p]["start"]) / fps;
var stop_second = parseFloat(progress[p]["end"]) / fps;
//figure out the actual pixel position
var start_pixel = start_second * $scope.pixelsPerSecond;
var stop_pixel = stop_second * $scope.pixelsPerSecond;
var rect_length = stop_pixel - start_pixel;
//get the element and draw the rects
ctx.beginPath();
ctx.rect(start_pixel, 0, rect_length, 5);
ctx.fillStyle = '#4B92AD';
ctx.fill();
}
};
// Clear all selections
$scope.ClearAllSelections = function() {
// Clear the selections on the main window
$scope.SelectTransition("", true);
$scope.SelectEffect("", true);
// Update scope
$scope.$apply(function() {
for (const clip of $scope.project.clips) {
clip.selected = false;
}
for (const effect of $scope.project.effects) {
effect.selected = false;
}
});
};
// Select all clips and transitions
$scope.SelectAll = function() {
$scope.$apply(function() {
// Select all clips
for (const clip of $scope.project.clips) {
clip.selected = true;
timeline.addSelection(clip.id, "clip", false);
}
// Select all transitions
for (const effect of $scope.project.effects) {
effect.selected = true;
timeline.addSelection(effect.id, "transition", false);
}
});
};
// Select clip in scope
$scope.SelectClip = function(clip_id, clear_selections, event) {
// Trim clip_id
var id = clip_id.replace("clip_", "");
// Clear transitions also (if needed)
if (id != "" && clear_selections) {
$scope.SelectTransition("", clear_selections);
$scope.SelectEffect("", clear_selections);
}
// Call slice method and exit (don't actually select the clip)
if (id != "" && $scope.enable_razor) {
if ($scope.Qt) {
cursor_seconds = $scope.GetJavaScriptPosition(event.clientX);
timeline.RazorSliceAtCursor(id, "", cursor_seconds);
}
// Don't actually select clip
return;
}
// Is CTRL pressed?
is_ctrl = false;
if (event && event.ctrlKey) {
is_ctrl = true;
}
// Unselect all clips
for (const clip of $scope.project.clips) {
if (clip.id == id) {
clip.selected = true;
if ($scope.Qt) {
timeline.addSelection(id, "clip", clear_selections);
}
}
else if (clear_selections && !is_ctrl) {
clip.selected = false;
if ($scope.Qt) {
timeline.removeSelection(clip.id, "clip");
}
}
}
};
// Select transition in scope
$scope.SelectTransition = function(tran_id, clear_selections, event) {
// Trim tran_id
var id = tran_id.replace("transition_", "");
// Clear clips also (if needed)
if (id != "" && clear_selections) {
$scope.SelectClip("", true);
$scope.SelectEffect("", true);
}
// Call slice method and exit (don't actually select the transition)
if (id != "" && $scope.enable_razor) {
if ($scope.Qt) {
cursor_seconds = $scope.GetJavaScriptPosition(event.clientX)
timeline.RazorSliceAtCursor("", id, cursor_seconds)
}
// Don't actually select transition
return;
}
// Is CTRL pressed?
is_ctrl = false;
if (event && event.ctrlKey) {
is_ctrl = true;
}
// Unselect all transitions
for (const tran of $scope.project.effects) {
if (tran.id == id) {
tran.selected = true;
if ($scope.Qt)
timeline.addSelection(id, "transition", clear_selections);
}
else if (clear_selections && !is_ctrl) {
tran.selected = false;
if ($scope.Qt)
timeline.removeSelection(tran.id, "transition");
}
}
};
// Format the thumbnail path
$scope.FormatThumbPath = function(image_url) {
if (image_url.charAt(0) == ".") {
return image_url;
}
else {
return "file:///" + image_url;
}
};
// Select transition in scope
$scope.SelectEffect = function(effect_id) {
if ($scope.Qt) {
timeline.addSelection(effect_id, "effect", true);
}
};
// Find the furthest right edge on the timeline (and resize it if too small)
$scope.ResizeTimeline = function() {
// Unselect all clips
var furthest_right_edge = 0;
for (const clip of $scope.project.clips) {
var right_edge = clip.position + (clip.end - clip.start);
if (right_edge > furthest_right_edge)
furthest_right_edge = right_edge;
}
// Resize timeline
if (furthest_right_edge > $scope.project.duration) {
if ($scope.Qt) {
timeline.resizeTimeline(furthest_right_edge + 10)
$scope.project.duration = furthest_right_edge + 10;
}
}
};
// Show clip context menu
$scope.ShowClipMenu = function(clip_id, event) {
if ($scope.Qt) {
timeline.qt_log("$scope.ShowClipMenu");
$scope.SelectClip(clip_id, false, event);
timeline.ShowClipMenu(clip_id);
}
};
// Show clip context menu
$scope.ShowEffectMenu = function(effect_id) {
if ($scope.Qt) {
timeline.qt_log("$scope.ShowEffectMenu");
timeline.ShowEffectMenu(effect_id);
}
};
// Show transition context menu
$scope.ShowTransitionMenu = function(tran_id, event) {
if ($scope.Qt) {
timeline.qt_log("$scope.ShowTransitionMenu");
$scope.SelectTransition(tran_id, false, event);
timeline.ShowTransitionMenu(tran_id);
}
};
// Show track context menu
$scope.ShowTrackMenu = function(layer_id) {
if ($scope.Qt) {
timeline.qt_log("$scope.ShowTrackMenu");
timeline.ShowTrackMenu(layer_id);
}
};
// Show marker context menu
$scope.ShowMarkerMenu = function(marker_id) {
if ($scope.Qt) {
timeline.qt_log("$scope.ShowMarkerMenu");
timeline.ShowMarkerMenu(marker_id);
}
};
// Show playhead context menu
$scope.ShowPlayheadMenu = function(position) {
if ($scope.Qt) {
timeline.qt_log("$scope.ShowPlayheadMenu");
timeline.ShowPlayheadMenu(position);
}
};
// Show timeline context menu
$scope.ShowTimelineMenu = function(e, layer_number) {
if ($scope.Qt) {
timeline.ShowTimelineMenu($scope.GetJavaScriptPosition(e.pageX), layer_number);
}
};
// Get the name of the track
$scope.GetTrackName = function(layer_label, layer_number) {
// Determine custom label or default track name
if (layer_label && layer_label.length > 0) {
return layer_label;
}
else {
return $scope.track_label.replace('%s', layer_number.toString());
}
};
$scope.SetTrackLabel = function (label) {
$scope.track_label = label;
};
// Get the width of the timeline in pixels
$scope.GetTimelineWidth = function(min_value) {
// Adjust for minimim length
return Math.max(min_value, $scope.project.duration * $scope.pixelsPerSecond);
};
// Get Position of item (used by Qt)
$scope.GetJavaScriptPosition = function(x) {
// Adjust for scrollbar position
var horz_scroll_offset = $("#scrolling_tracks").scrollLeft();
var scrolling_tracks_offset_left = $("#scrolling_tracks").offset().left;
x += horz_scroll_offset;
// Convert x into position in seconds
var clip_position = parseFloat(x - scrolling_tracks_offset_left) / parseFloat($scope.pixelsPerSecond);
if (clip_position < 0) {
clip_position = 0;
}
// Return position in seconds
return clip_position;
};
// Get Track number of item (used by Qt)
$scope.GetJavaScriptTrack = function(y) {
// Adjust for scrollbar position
var scrolling_tracks_offset_top = $("#scrolling_tracks").offset().top;
var vert_scroll_offset = $("#scrolling_tracks").scrollTop();
y += vert_scroll_offset;
// Return number of track
var track_number = parseInt($scope.GetTrackAtY(y - scrolling_tracks_offset_top).number);
return track_number;
};
// Get JSON of most recent item (used by Qt)
$scope.UpdateRecentItemJSON = function(item_type, item_id) {
// Find item in JSON
var item_object = null;
if (item_type == 'clip') {
item_object = findElement($scope.project.clips, "id", item_id);
}
else if (item_type == 'transition') {
item_object = findElement($scope.project.effects, "id", item_id);
}
else {
// Bail out if no id found
return;
}
// Get position of item
var scrolling_tracks_offset_top = $("#scrolling_tracks").offset().top;
var clip_position = parseFloat(bounding_box.left) / parseFloat($scope.pixelsPerSecond);
var layer_num = $scope.GetTrackAtY(bounding_box.track_position - scrolling_tracks_offset_top).number;
// update scope with final position of items
$scope.$apply(function() {
// update item
item_object.position = clip_position;
item_object.layer = layer_num;
});
// update clip in Qt (very important =)
if (item_type == 'clip') {
timeline.update_clip_data(JSON.stringify(item_object), true, true, false);
}
else if (item_type == 'transition') {
timeline.update_transition_data(JSON.stringify(item_object), true, false);
}
// Resize timeline if it's too small to contain all clips
$scope.ResizeTimeline();
// Hide snapline (if any)
$scope.HideSnapline();
// Check again for missing transitions
missing_transition_details = $scope.GetMissingTransitions(item_object);
if ($scope.Qt && missing_transition_details != null) {
timeline.add_missing_transition(JSON.stringify(missing_transition_details));
}
// Remove manual move stylesheet
bounding_box.element.removeClass("manual-move");
// Remove CSS class (after the drag)
bounding_box = {};
};
// Init bounding boxes for manual move
$scope.StartManualMove = function(item_type, item_id) {
// Select the item
$scope.$apply(function() {
if (item_type == 'clip') {
$scope.SelectClip(item_id, true);
}
else if (item_type == 'transition') {
$scope.SelectTransition(item_id, true);
}
});
// Select new clip object (and unselect others)
// This needs to be done inline due to async issues with the
// above calls to SelectClip/SelectTransition
for (const clip of $scope.project.clips) {
if (clip.id == item_id) {
clip.selected = true;
} else {
clip.selected = false;
}
}
// Select new transition object (and unselect others)
for (const tran of $scope.project.effects) {
if (tran.id == item_id) {
tran.selected = true;
} else {
tran.selected = false;
}
}
// JQuery selector for element (clip or transition)
var element_id = "#" + item_type + "_" + item_id;
// Init bounding box
bounding_box = {};
setBoundingBox($scope, $(element_id));
// Init some variables to track the changing position
bounding_box.previous_x = bounding_box.left;
bounding_box.previous_y = bounding_box.top;
bounding_box.offset_x = 0;
bounding_box.offset_y = 0;
bounding_box.element = $(element_id);
bounding_box.track_position = 0;
// Set z-order to be above other clips/transitions
if (item_type != "os_drop") {
bounding_box.element.addClass("manual-move");
}
};
// Move a new clip to the timeline
$scope.MoveItem = function(x, y, item_type) {
var vert_scroll_offset = $("#scrolling_tracks").scrollTop();
var horz_scroll_offset = $("#scrolling_tracks").scrollLeft();
x += horz_scroll_offset;
y += vert_scroll_offset;
// Convert x and y into timeline vars
var scrolling_tracks_offset_left = $("#scrolling_tracks").offset().left;
var scrolling_tracks_offset_top = $("#scrolling_tracks").offset().top;
// Calculate the x,y of cursor
var left = parseFloat(x - scrolling_tracks_offset_left);
var top = parseFloat(y - scrolling_tracks_offset_top);
// Calculate amount to move transitions
var x_offset = left - bounding_box.previous_x;
var y_offset = top - bounding_box.previous_y;
// Move the bounding box and apply snapping rules
results = moveBoundingBox($scope, bounding_box.previous_x, bounding_box.previous_y, x_offset, y_offset, left, top);
// Track previous values
bounding_box.previous_x = results.position.left;
bounding_box.previous_y = results.position.top;
var clip_position = parseFloat(results.position.left) / parseFloat($scope.pixelsPerSecond);
if (clip_position < 0) {
clip_position = 0;
}
// Loop through each layer (looking for the closest track based on Y coordinate)
bounding_box.track_position = 0;
for (const layer of $scope.project.layers) {
// Compare position of track to Y param (for unlocked tracks)
if (!layer.lock) {
if ((top < layer.y && top > bounding_box.track_position) || bounding_box.track_position === 0) {
// return first matching layer
bounding_box.track_position = layer.y;
}
}
}
//change the element location
bounding_box.element.css('left', results.position.left);
bounding_box.element.css('top', bounding_box.track_position - scrolling_tracks_offset_top);
};
// Update X,Y indexes of tracks / layers (anytime the project.layers scope changes)
$scope.UpdateLayerIndex = function(){
if ($scope.Qt) {
timeline.qt_log('UpdateLayerIndex');
}
var vert_scroll_offset = $("#scrolling_tracks").scrollTop();
var horz_scroll_offset = $("#scrolling_tracks").scrollLeft();
// Get scrollbar offsets
var scrolling_tracks_offset_left = $("#scrolling_tracks").offset().left;
var scrolling_tracks_offset_top = $("#scrolling_tracks").offset().top;
$scope.$apply(function() {
// Loop through each layer
for (const layer of $scope.project.layers) {