-
Notifications
You must be signed in to change notification settings - Fork 22
/
timeline.cpp
1500 lines (1271 loc) · 48.4 KB
/
timeline.cpp
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
// Timeline widget
#include "timeline.h"
#include "app.h"
#include "widgets.h"
#include "editing.h"
#include "colors.h"
#include <opentimelineio/clip.h>
#include <opentimelineio/composable.h>
#include <opentimelineio/effect.h>
#include <opentimelineio/gap.h>
#include <opentimelineio/linearTimeWarp.h>
#include <opentimelineio/marker.h>
#include <opentimelineio/transition.h>
#include <opentimelineio/track.h>
// counters to measure visibility-check performance optimization
static int __tracks_rendered;
static int __items_rendered;
double
TimeScalarForItem(otio::Item* item) {
double time_scalar = 1.0;
for (const auto& effect : item->effects()) {
if (const auto& timewarp = dynamic_cast<otio::LinearTimeWarp*>(effect.value)) {
time_scalar *= timewarp->time_scalar();
}
}
return time_scalar;
}
//otio::RationalTime TopLevelTime(otio::RationalTime time, otio::Item* context) {
// return context->transformed_time(time, appState.timeline->tracks());
//}
//
//otio::TimeRange TopLevelTimeRange(otio::TimeRange range, otio::Item* context) {
// return context->transformed_time_range(range, appState.timeline->tracks());
//}
// Transform this range map from the context item's coodinate space
// into the top-level timeline's coordinate space. This compensates for
// any source_range offsets in intermediate levels of nesting in the
// composition.
void TopLevelTimeRangeMap(
std::map<otio::Composable*, otio::TimeRange>& range_map,
otio::Item* context) {
auto zero = otio::RationalTime();
auto top = appState.timeline->tracks();
auto offset = context->transformed_time(zero, top);
for (auto& pair : range_map) {
auto& range = pair.second;
range = otio::TimeRange(range.start_time() + offset, range.duration());
}
}
void DrawItem(
otio::Item* item,
float scale,
ImVec2 origin,
float height,
std::map<otio::Composable*, otio::TimeRange>& range_map) {
auto duration = item->duration();
auto trimmed_range = item->trimmed_range();
float width = duration.to_seconds() * scale;
if (width < 1)
return;
const ImVec2 text_offset(5.0f, 5.0f);
float font_height = ImGui::GetTextLineHeight();
float font_width = font_height * 0.5; // estimate
// is there enough horizontal space for labels at all?
bool show_label = width > text_offset.x * 2;
// is there enough vertical *and* horizontal space for time ranges?
bool show_time_range = (height > font_height * 2 + text_offset.y * 2)
&& (width > font_width * 15);
auto range_it = range_map.find(item);
if (range_it == range_map.end()) {
Log("Couldn't find %s in range map?!", item->name().c_str());
assert(false);
}
auto item_range = range_it->second;
ImVec2 size(width, height);
ImVec2 render_pos(
item_range.start_time().to_seconds() * scale + origin.x,
ImGui::GetCursorPosY());
std::string label_str = item->name();
auto label_color = appTheme.colors[AppThemeCol_Label];
auto fill_color = appTheme.colors[AppThemeCol_Item];
auto selected_fill_color = appTheme.colors[AppThemeCol_ItemSelected];
auto hover_fill_color = appTheme.colors[AppThemeCol_ItemHovered];
bool fancy_corners = true;
auto item_color = GetItemColor(item);
if (item_color != "") {
fill_color = UIColorFromName(item_color);
selected_fill_color = TintedColorForUI(fill_color);
hover_fill_color = TintedColorForUI(fill_color);
}
if (auto gap = dynamic_cast<otio::Gap*>(item)) {
// different colors & style
fill_color = appTheme.colors[AppThemeCol_Background];
selected_fill_color = TintedColorForUI(fill_color);
hover_fill_color = TintedColorForUI(fill_color);
label_str = "";
fancy_corners = false;
show_time_range = false;
}
auto old_pos = ImGui::GetCursorPos();
ImGui::SetCursorPos(render_pos);
ImGui::PushID(item);
ImGui::BeginGroup();
ImGui::InvisibleButton("##Item", size);
if (!ImGui::IsItemVisible()) {
// exit early if this item is off-screen
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
return;
}
ImVec2 p0 = ImGui::GetItemRectMin();
ImVec2 p1 = ImGui::GetItemRectMax();
if (!ImGui::IsRectVisible(p0, p1)) {
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
return;
}
ImGui::SetItemAllowOverlap();
// Dragging...
// if (ImGui::IsItemActive() && ImGui::IsMouseDragging(ImGuiMouseButton_Left))
// {
// offset.x += ImGui::GetIO().MouseDelta.x;
// offset.y += ImGui::GetIO().MouseDelta.y;
// }
if (ImGui::IsItemHovered()) {
fill_color = hover_fill_color;
}
if (ImGui::IsItemClicked()) {
SelectObject(item);
}
if (appState.selected_object == item) {
fill_color = selected_fill_color;
}
if (ColorIsBright(fill_color)) {
label_color = ColorInvert(label_color);
}
ImGui::PushClipRect(p0, p1, true);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
auto light_edge_color = ImColor(255, 255, 255, 255 * 0.4);
auto dark_edge_color = ImColor(0, 0, 0, 255 * 0.5);
if (fancy_corners) {
const ImDrawFlags corners_tl_br = ImDrawFlags_RoundCornersTopLeft
| ImDrawFlags_RoundCornersBottomRight;
const float corner_radius = 5.0f;
draw_list
->AddRectFilled(p0, p1, fill_color, corner_radius, corners_tl_br);
// top edge
draw_list->AddLine(
ImVec2(p0.x + corner_radius, p0.y),
ImVec2(p1.x, p0.y),
light_edge_color);
// bottom edge
draw_list->AddLine(
ImVec2(p0.x, p1.y - 1),
ImVec2(p1.x - corner_radius, p1.y - 1),
dark_edge_color);
} else {
draw_list->AddRectFilled(p0, p1, fill_color);
}
if (show_label) {
const ImVec2 text_pos = ImVec2(p0.x + text_offset.x, p0.y + text_offset.y);
if (label_str != "") {
draw_list->AddText(text_pos, label_color, label_str.c_str());
}
}
if (show_time_range) {
// auto str1 = std::to_string(trimmed_range.start_time().to_frames());
// auto str2 =
// std::to_string(trimmed_range.end_time_inclusive().to_frames()); auto pos1
// = ImVec2(p0.x + text_offset.x, p1.y - text_offset.y - font_height); auto
// pos2 = ImVec2(p1.x - text_offset.x - ImGui::CalcTextSize(str2.c_str()).x,
// p1.y - text_offset.y - font_height); draw_list->AddText(pos1,
// label_color, str1.c_str()); draw_list->AddText(pos2, label_color,
// str2.c_str());
auto time_scalar = TimeScalarForItem(item);
auto trimmed_range = item->trimmed_range();
auto start = trimmed_range.start_time();
auto duration = trimmed_range.duration();
auto end = start
+ otio::RationalTime(
duration.value() * time_scalar,
duration.rate());
auto rate = start.rate();
float ruler_y_offset = font_height + text_offset.y;
ImGui::SetCursorPos(
ImVec2(render_pos.x, render_pos.y + ruler_y_offset));
DrawTimecodeRuler(
item + 1,
start,
end,
rate,
time_scalar,
scale,
width,
height - ruler_y_offset);
}
if (ImGui::IsItemHovered()) {
std::string extra;
if (const auto& comp = dynamic_cast<otio::Composition*>(item)) {
extra = "\nChildren: " + std::to_string(comp->children().size());
}
ImGui::SetTooltip(
"%s: %s\nRange: %s - %s\nDuration: %s%s",
item->schema_name().c_str(),
item->name().c_str(),
FormattedStringFromTime(trimmed_range.start_time()).c_str(),
FormattedStringFromTime(trimmed_range.end_time_inclusive()).c_str(),
FormattedStringFromTime(duration).c_str(),
extra.c_str());
}
ImGui::PopClipRect();
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
__items_rendered++;
}
void DrawTransition(
otio::Transition* transition,
float scale,
ImVec2 origin,
float height,
std::map<otio::Composable*, otio::TimeRange>& range_map) {
auto duration = transition->duration();
float width = duration.to_seconds() * scale;
auto range_it = range_map.find(transition);
if (range_it == range_map.end()) {
Log("Couldn't find %s in range map?!", transition->name().c_str());
assert(false);
}
auto item_range = range_it->second;
ImVec2 size(width, height);
ImVec2 render_pos(
item_range.start_time().to_seconds() * scale + origin.x,
ImGui::GetCursorPosY());
ImVec2 text_offset(5.0f, 5.0f);
auto fill_color = appTheme.colors[AppThemeCol_Transition];
auto line_color = appTheme.colors[AppThemeCol_TransitionLine];
auto selected_fill_color = appTheme.colors[AppThemeCol_TransitionSelected];
auto hover_fill_color = appTheme.colors[AppThemeCol_TransitionHovered];
if (ColorIsBright(fill_color)) {
auto inverted_fill_color = ColorInvert(fill_color);
selected_fill_color = TintedColorForUI(inverted_fill_color);
hover_fill_color = TintedColorForUI(inverted_fill_color);
}
auto old_pos = ImGui::GetCursorPos();
ImGui::SetCursorPos(render_pos);
ImGui::PushID(transition);
ImGui::BeginGroup();
ImGui::InvisibleButton("##Item", size);
ImVec2 p0 = ImGui::GetItemRectMin();
ImVec2 p1 = ImGui::GetItemRectMax();
if (!ImGui::IsRectVisible(p0, p1)) {
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
return;
}
// ImGui::SetItemAllowOverlap();
if (ImGui::IsItemHovered()) {
fill_color = hover_fill_color;
}
if (ImGui::IsItemClicked()) {
SelectObject(transition);
}
if (appState.selected_object == transition) {
fill_color = selected_fill_color;
}
ImGui::PushClipRect(p0, p1, true);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
const ImVec2 line_start = ImVec2(p0.x, p1.y);
const ImVec2 line_end = ImVec2(p1.x, p0.y);
const ImDrawFlags corners_tl_br = ImDrawFlags_RoundCornersTopLeft
| ImDrawFlags_RoundCornersBottomRight;
const float corner_radius = height / 2;
draw_list->AddRectFilled(p0, p1, fill_color, corner_radius, corners_tl_br);
draw_list->AddLine(line_start, line_end, line_color);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"%s: %s\nIn/Out Offset: %s / %s\nDuration: %s",
transition->schema_name().c_str(),
transition->name().c_str(),
FormattedStringFromTime(transition->in_offset()).c_str(),
FormattedStringFromTime(transition->out_offset()).c_str(),
FormattedStringFromTime(duration).c_str());
}
ImGui::PopClipRect();
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
}
void DrawEffects(
otio::Item* item,
float scale,
ImVec2 origin,
float row_height,
std::map<otio::Composable*, otio::TimeRange>& range_map) {
auto effects = item->effects();
if (effects.size() == 0)
return;
std::string label_str;
for (const auto& effect : effects) {
if (label_str != "")
label_str += ", ";
label_str += effect->name() != "" ? effect->name()
: effect->effect_name();
}
const auto text_size = ImGui::CalcTextSize(label_str.c_str());
ImVec2 text_offset(5.0f, 5.0f);
auto item_duration = item->duration();
float item_width = item_duration.to_seconds() * scale;
float width = fminf(item_width, text_size.x + text_offset.x * 2);
float height = fminf(row_height - 2, text_size.y + text_offset.y * 2);
auto range_it = range_map.find(item);
if (range_it == range_map.end()) {
Log("Couldn't find %s in range map?!", item->name().c_str());
assert(false);
}
auto item_range = range_it->second;
ImVec2 size(width, height*0.75);
// Does the label fit in the available space?
bool label_visible = (size.x > text_size.x && label_str != "");
if (!label_visible) {
// If not, then just put a dot.
size.x = fmin(size.y, width);
}
float item_x = item_range.start_time().to_seconds() * scale + origin.x;
ImVec2 render_pos(
item_x + item_width / 2 - size.x / 2, // centered
ImGui::GetCursorPosY() + row_height / 2 - size.y / 2 // centered
);
auto label_color = appTheme.colors[AppThemeCol_Label];
auto fill_color = appTheme.colors[AppThemeCol_Effect];
auto selected_fill_color = appTheme.colors[AppThemeCol_EffectSelected];
auto hover_fill_color = appTheme.colors[AppThemeCol_EffectHovered];
auto old_pos = ImGui::GetCursorPos();
ImGui::SetCursorPos(render_pos);
ImGui::PushID(item);
ImGui::BeginGroup();
ImGui::InvisibleButton("##Effect", size);
ImVec2 p0 = ImGui::GetItemRectMin();
ImVec2 p1 = ImGui::GetItemRectMax();
if (!ImGui::IsRectVisible(p0, p1)) {
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
return;
}
// ImGui::SetItemAllowOverlap();
if (ImGui::IsItemHovered()) {
fill_color = hover_fill_color;
}
if (effects.size() == 1) {
const auto& effect = effects[0];
if (ImGui::IsItemClicked()) {
SelectObject(effect, item);
}
if (appState.selected_object == effect) {
fill_color = selected_fill_color;
}
} else {
if (ImGui::IsItemClicked()) {
SelectObject(item);
}
if (appState.selected_object == item) {
fill_color = selected_fill_color;
}
}
if (ColorIsBright(fill_color)) {
label_color = ColorInvert(label_color);
}
ImGui::PushClipRect(p0, p1, true);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(p0, p1, fill_color, 10);
if (label_visible) {
const ImVec2 text_pos = ImVec2(
p0.x + size.x / 2 - text_size.x / 2,
p0.y + size.y / 2 - text_size.y / 2);
draw_list->AddText(text_pos, label_color, label_str.c_str());
}
if (ImGui::IsItemHovered()) {
std::string tooltip;
for (const auto& effect : effects) {
if (tooltip != "")
tooltip += "\n\n";
tooltip += effect->schema_name() + ": " + effect->name();
tooltip += "\nEffect Name: " + effect->effect_name();
if (const auto& timewarp = dynamic_cast<otio::LinearTimeWarp*>(effect.value)) {
tooltip += "\nTime Scale: " + std::to_string(timewarp->time_scalar());
}
}
ImGui::SetTooltip("%s", tooltip.c_str());
}
ImGui::PopClipRect();
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
}
void DrawMarkers(
otio::Item* item,
float scale,
ImVec2 origin,
float height,
std::map<otio::Composable*, otio::TimeRange>& range_map) {
auto markers = item->markers();
if (markers.size() == 0)
return;
auto item_trimmed_start = item->trimmed_range().start_time();
auto item_start_in_parent = otio::RationalTime();
if (item->parent() != NULL) {
auto range_it = range_map.find(item);
if (range_it == range_map.end()) {
Log("Couldn't find %s in range map?!", item->name().c_str());
assert(false);
}
auto item_range = range_it->second;
item_start_in_parent = item_range.start_time();
}
for (const auto& marker : markers) {
auto range = marker->marked_range();
auto duration = range.duration();
auto start = range.start_time();
const float arrow_width = height / 4;
float width = duration.to_seconds() * scale + arrow_width;
ImVec2 size(width, arrow_width);
ImVec2 render_pos(
(item_start_in_parent + (start - item_trimmed_start)).to_seconds()
* scale
+ origin.x - arrow_width / 2,
ImGui::GetCursorPosY());
auto fill_color = UIColorFromName(marker->color());
auto selected_fill_color = appTheme.colors[AppThemeCol_MarkerSelected];
auto hover_fill_color = appTheme.colors[AppThemeCol_MarkerHovered];
auto old_pos = ImGui::GetCursorPos();
ImGui::SetCursorPos(render_pos);
ImGui::PushID(item);
ImGui::BeginGroup();
ImGui::InvisibleButton("##Marker", size);
ImVec2 p0 = ImGui::GetItemRectMin();
ImVec2 p1 = ImGui::GetItemRectMax();
if (!ImGui::IsRectVisible(p0, p1)) {
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
continue;
;
}
// ImGui::SetItemAllowOverlap();
if (ImGui::IsItemHovered()) {
fill_color = hover_fill_color;
}
if (ImGui::IsItemClicked()) {
SelectObject(marker, item);
}
if (appState.selected_object == marker) {
fill_color = selected_fill_color;
}
ImGui::PushClipRect(p0, p1, true);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
auto dimmed_fill_color = ImColor(fill_color);
dimmed_fill_color.Value.w = 0.5;
draw_list->AddTriangleFilled(
ImVec2(p0.x, p0.y),
ImVec2(p0.x + arrow_width / 2, p1.y),
ImVec2(p0.x + arrow_width / 2, p0.y),
fill_color);
draw_list->AddRectFilled(
ImVec2(p0.x + arrow_width / 2, p0.y),
ImVec2(p1.x - arrow_width / 2, p1.y),
ImColor(dimmed_fill_color));
draw_list->AddTriangleFilled(
ImVec2(p1.x - arrow_width / 2, p0.y),
ImVec2(p1.x - arrow_width / 2, p1.y),
ImVec2(p1.x, p0.y),
fill_color);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(
"%s: %s\nColor: %s\nRange: %s - %s\nDuration: %s",
marker->schema_name().c_str(),
marker->name().c_str(),
marker->color().c_str(),
FormattedStringFromTime(range.start_time()).c_str(),
FormattedStringFromTime(range.end_time_exclusive()).c_str(),
FormattedStringFromTime(duration).c_str());
}
ImGui::PopClipRect();
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
}
}
void DrawObjectLabel(otio::SerializableObjectWithMetadata* object, float height) {
float width = ImGui::GetContentRegionAvail().x;
ImGui::BeginGroup();
ImGui::AlignTextToFramePadding();
ImVec2 size(width, height);
ImGui::InvisibleButton("##empty", size);
char label_str[200];
snprintf(
label_str,
sizeof(label_str),
"%s: %s",
object->schema_name().c_str(),
object->name().c_str());
auto label_color = appTheme.colors[AppThemeCol_Label];
auto fill_color = appTheme.colors[AppThemeCol_Track];
auto selected_fill_color = TintedColorForUI(fill_color);
auto hover_fill_color = TintedColorForUI(fill_color);
ImVec2 text_offset(5.0f, 5.0f);
if (ImGui::IsItemHovered()) {
fill_color = hover_fill_color;
}
if (ImGui::IsItemClicked()) {
SelectObject(object);
}
if (appState.selected_object == object) {
fill_color = selected_fill_color;
}
if (ColorIsBright(fill_color)) {
label_color = ColorInvert(label_color);
}
ImVec2 p0 = ImGui::GetItemRectMin();
ImVec2 p1 = ImGui::GetItemRectMax();
const ImVec2 text_pos = ImVec2(p0.x + text_offset.x, p0.y + text_offset.y);
ImGui::PushClipRect(p0, p1, true);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(p0, p1, fill_color);
draw_list->AddText(text_pos, label_color, label_str);
ImGui::PopClipRect();
ImGui::EndGroup();
}
void DrawTrackLabel(otio::Track* track, int index, float height) {
float width = ImGui::GetContentRegionAvail().x;
ImGui::BeginGroup();
ImGui::AlignTextToFramePadding();
ImVec2 size(width, height);
ImGui::InvisibleButton("##empty", size);
std::string kind(track->kind());
if(kind.empty()) {
// fallback when kind is not set
kind = "Other";
}
char label_str[200];
snprintf(
label_str,
sizeof(label_str),
"%c%d: %s",
kind[0],
index,
track->name().c_str());
auto label_color = appTheme.colors[AppThemeCol_Label];
auto fill_color = appTheme.colors[AppThemeCol_Track];
auto selected_fill_color = appTheme.colors[AppThemeCol_TrackSelected];
auto hover_fill_color = appTheme.colors[AppThemeCol_TrackHovered];
auto track_color = GetItemColor(track);
if (track_color != "") {
fill_color = UIColorFromName(track_color);
fill_color = TintedColorForUI(fill_color);
}
ImVec2 text_offset(5.0f, 5.0f);
if (ImGui::IsItemHovered()) {
fill_color = hover_fill_color;
}
if (ImGui::IsItemClicked()) {
SelectObject(track);
}
if (appState.selected_object == track) {
fill_color = selected_fill_color;
}
if (ColorIsBright(fill_color)) {
label_color = ColorInvert(label_color);
}
ImVec2 p0 = ImGui::GetItemRectMin();
ImVec2 p1 = ImGui::GetItemRectMax();
const ImVec2 text_pos = ImVec2(p0.x + text_offset.x, p0.y + text_offset.y);
ImGui::PushClipRect(p0, p1, true);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(p0, p1, fill_color);
draw_list->AddText(text_pos, label_color, label_str);
ImGui::PopClipRect();
if (ImGui::IsItemHovered()) {
auto trimmed_range = track->trimmed_range();
ImGui::SetTooltip(
"%s: %s\n%s #%d\nRange: %s - %s\nDuration: %s\nChildren: %ld",
track->schema_name().c_str(),
track->name().c_str(),
kind.c_str(),
index,
FormattedStringFromTime(trimmed_range.start_time()).c_str(),
FormattedStringFromTime(trimmed_range.end_time_inclusive()).c_str(),
FormattedStringFromTime(trimmed_range.duration()).c_str(),
track->children().size());
}
ImGui::EndGroup();
}
void DrawTrack(
otio::Track* track,
int index,
float scale,
ImVec2 origin,
float full_width,
float height) {
ImGui::BeginGroup();
otio::ErrorStatus error_status;
auto range_map = track->range_of_all_children(&error_status);
if (otio::is_error(error_status)) {
Message(
"Error calculating timing: %s",
otio_error_string(error_status).c_str());
assert(false);
}
TopLevelTimeRangeMap(range_map, track);
for (const auto& child : track->children()) {
if (const auto& item = dynamic_cast<otio::Item*>(child.value)) {
DrawItem(item, scale, origin, height, range_map);
}
}
for (const auto& child : track->children()) {
if (const auto& transition = dynamic_cast<otio::Transition*>(child.value)) {
DrawTransition(transition, scale, origin, height, range_map);
}
}
for (const auto& child : track->children()) {
if (const auto& item = dynamic_cast<otio::Item*>(child.value)) {
DrawEffects(item, scale, origin, height, range_map);
DrawMarkers(item, scale, origin, height, range_map);
}
}
ImGui::EndGroup();
__tracks_rendered++;
}
void DrawTimecodeRuler(
const void* ptr_id,
otio::RationalTime start,
otio::RationalTime end,
float frame_rate,
float time_scalar,
float zoom_scale,
float width,
float height) {
double scale = zoom_scale / time_scalar;
ImVec2 size(width, height);
ImVec2 text_offset(7.0f, 5.0f);
auto old_pos = ImGui::GetCursorPos();
ImGui::PushID(ptr_id);
ImGui::BeginGroup();
ImGui::Dummy(size);
const ImVec2 p0 = ImGui::GetItemRectMin();
const ImVec2 p1 = ImGui::GetItemRectMax();
ImGui::SetItemAllowOverlap();
if (!ImGui::IsRectVisible(p0, p1)) {
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
return;
}
ImDrawList* draw_list = ImGui::GetWindowDrawList();
auto fill_color = appTheme.colors[AppThemeCol_Background];
auto tick_color = appTheme.colors[AppThemeCol_TickMajor];
// auto tick2_color = appTheme.colors[AppThemeCol_TickMinor];
auto tick_label_color = appTheme.colors[AppThemeCol_Label];
auto zebra_color_dark = ImColor(0, 0, 0, 255.0 * appState.zebra_factor);
auto zebra_color_light = ImColor(255, 255, 255, 255.0 * appState.zebra_factor);
// background
// draw_list->AddRectFilled(p0, p1, fill_color);
// draw every frame?
// Note: "width" implies pixels, but "duration" implies time.
double single_frame_width = scale / frame_rate;
double tick_width = single_frame_width;
double min_tick_width = 15;
if (tick_width < min_tick_width) {
// every second?
tick_width = scale;
if (tick_width < min_tick_width) {
// every minute?
tick_width = scale * 60.0f;
if (tick_width < min_tick_width) {
// every hour
tick_width = scale * 60.0f * 60.0f;
}
}
}
// tick marks - roughly every N pixels
double pixels_per_second = scale;
double seconds_per_tick = tick_width / pixels_per_second;
// ticks must use frame_rate, and must have an integer value
// so that the tick labels align to the human expectation of "frames"
int tick_duration_in_frames = ceil(seconds_per_tick / frame_rate);
int tick_count = ceil(width / tick_width);
// start_floor_time and tick_offset_x adjust the display for cases where
// the item's start_time is not on a whole frame boundary.
auto start_floor_time = otio::RationalTime(floor(start.value()), start.rate());
auto tick_offset = (start - start_floor_time).rescaled_to(frame_rate);
double tick_offset_x = tick_offset.to_seconds() * scale;
// last_label_end_x tracks the tail of the last label, so we can prevent
// overlap
double last_label_end_x = p0.x - text_offset.x * 2;
for (int tick_index = 0; tick_index < tick_count; tick_index++) {
auto tick_time = start.rescaled_to(frame_rate)
+ otio::RationalTime(
tick_index * tick_duration_in_frames,
frame_rate)
- tick_offset;
double tick_x = tick_index * tick_width - tick_offset_x;
const ImVec2 tick_start = ImVec2(p0.x + tick_x, p0.y + height / 2);
const ImVec2 tick_end = ImVec2(tick_start.x + tick_width, p1.y);
if (!ImGui::IsRectVisible(tick_start, tick_end))
continue;
if (seconds_per_tick >= 0.5) {
// draw thin lines at each tick
draw_list->AddLine(
tick_start,
ImVec2(tick_start.x, tick_end.y),
tick_color);
} else {
// once individual frames are visible, draw dark/light stripes instead
int frame = tick_time.to_frames();
const ImVec2 zebra_start = ImVec2(p0.x + tick_x, p0.y);
const ImVec2 zebra_end = ImVec2(tick_start.x + tick_width, p1.y);
draw_list->AddRectFilled(
zebra_start,
zebra_end,
(frame & 1) ? zebra_color_dark : zebra_color_light);
}
const ImVec2 tick_label_pos = ImVec2(p0.x + tick_x + text_offset.x, p0.y + text_offset.y);
// only draw a label when there's room for it
if (tick_label_pos.x > last_label_end_x + text_offset.x) {
std::string tick_label = FormattedStringFromTime(tick_time);
auto label_size = ImGui::CalcTextSize(tick_label.c_str());
draw_list->AddText(
tick_label_pos,
tick_label_color,
tick_label.c_str());
// advance last_label_end_x so nothing will overlap with the one we just
// drew
last_label_end_x = tick_label_pos.x + label_size.x;
}
}
// For debugging, this is very helpful...
// ImGui::SetTooltip("tick_width = %f\nseconds_per_tick =
// %f\npixels_per_second = %f", tick_width, seconds_per_tick,
// pixels_per_second);
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
}
bool DrawTimecodeTrack(
otio::RationalTime start,
otio::RationalTime end,
float frame_rate,
float scale,
float full_width,
float track_height,
bool interactive = true) {
bool moved_playhead = false;
// Adjust track_height based on the number of visible elements
track_height = CalculateDynamicHeight();
float width = ImGui::GetContentRegionAvail().x;
ImVec2 size(fmaxf(full_width, width), track_height);
auto old_pos = ImGui::GetCursorPos();
ImGui::PushID("##DrawTimecodeTrack");
ImGui::BeginGroup();
if (interactive) {
ImGui::InvisibleButton("##empty", size);
} else {
ImGui::Dummy(size);
}
const ImVec2 p0 = ImGui::GetItemRectMin();
ImGui::SetItemAllowOverlap();
if (interactive && ImGui::IsItemActive()) // &&
// ImGui::IsMouseDragging(ImGuiMouseButton_Left))
{
// MousePos is in SCREEN space
// Subtract p0 which is also in SCREEN space, and includes scrolling, etc.
float mouse_x_widget = ImGui::GetIO().MousePos.x - p0.x;
SeekPlayhead(mouse_x_widget / scale + start.to_seconds());
moved_playhead = true;
}
ImGui::SetCursorPos(old_pos);
DrawTimecodeRuler(
"##TimecodeTrackRuler",
start,
end,
frame_rate,
1.0,
scale,
size.x,
size.y);
ImGui::EndGroup();
ImGui::PopID();
ImGui::SetCursorPos(old_pos);
return moved_playhead;
}
ImColor
ColorOverColor(ImColor a, ImColor b) {
const float alpha = a.Value.w; // for readability
return ImColor(
a.Value.x * alpha + b.Value.x * (1.0 - alpha),
a.Value.y * alpha + b.Value.y * (1.0 - alpha),
a.Value.z * alpha + b.Value.z * (1.0 - alpha),
b.Value.w);
}
float DrawPlayhead(
otio::RationalTime start,
otio::RationalTime end,
otio::RationalTime playhead,
float scale,
float full_width,
float track_height,
float full_height,
ImVec2 origin,
bool draw_arrow) {
float playhead_width = scale / playhead.rate();
float playhead_x = (playhead - start).to_seconds() * scale;
ImVec2 size(playhead_width, full_height);
ImVec2 text_offset(7.0f, 5.0f);
auto background_color = appTheme.colors[AppThemeCol_Background];
auto playhead_fill_color = appTheme.colors[AppThemeCol_Playhead];
auto playhead_line_color = appTheme.colors[AppThemeCol_PlayheadLine];
auto playhead_label_bg_color = ColorOverColor(playhead_fill_color, background_color);
// Ask for this position in the timeline
ImVec2 render_pos(playhead_x + origin.x, origin.y);
auto old_pos = ImGui::GetCursorPos();
ImGui::SetCursorPos(render_pos);
ImGui::PushID("##Playhead");
ImGui::BeginGroup();
ImGui::InvisibleButton("##Playhead2", size);
// Compute where we are rendering in screen space for draw list functions.
ImVec2 p0 = ImGui::GetItemRectMin();
ImVec2 p1 = ImGui::GetItemRectMax();
ImGui::SetItemAllowOverlap();
// compute the playhead x position in the local (aka window) coordinate system
// so that later we can use SetScrollFromPosX() to scroll the timeline.
// p0 is in screen space - where we're actually drawn (which includes current
// scroll) origin is the offset to the edge of the tracks in window space. So
// we compute SCREEN_RENDER_POS.x - LOCAL_EDGE.x - WINDOW_POS.x =>
// LOCAL_RENDER_POS.x float playhead_scroll_x = p0.x - origin.x -
// ImGui::GetWindowPos().x;
float playhead_scroll_x = playhead_x;
if (ImGui::IsItemActive()) // &&
// ImGui::IsMouseDragging(ImGuiMouseButton_Left))
{
// MousePos is in SCREEN space
float mouse_x_widget = ImGui::GetIO().MousePos.x
// Subtract WindowPos to get WINDOW space
- ImGui::GetWindowPos().x
// Subtract origin.x to get TIMELINE space
- origin.x
// Add ScrollX to compensate for scrolling