-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimeRulerView.swift
More file actions
244 lines (200 loc) · 9.04 KB
/
TimeRulerView.swift
File metadata and controls
244 lines (200 loc) · 9.04 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
//
// TimeRulerView.swift
// OpenTimelineIO-Sample
//
// Created by Anton Marini on 9/28/24.
//
import OpenTimelineIO_AVFoundation
import OpenTimelineIO
import TimecodeKit
import SwiftUI
struct TimeRulerView: View
{
static let VerticalPadding:CGFloat = 100
let timeline: OpenTimelineIO.Timeline
@Binding var secondsToPixels: Double
@Binding var currentTime: OpenTimelineIO.RationalTime
var body: some View
{
Canvas { context, size in
context.translateBy(x: TrackView.trackHeaderWidth, y: 0)
let safeRange = getSafeRange()
let startSeconds = safeRange.startTime.toSeconds()
let endSeconds = safeRange.endTimeInclusive().toSeconds()
// Draw ticks (including frame-level ticks)
drawTicks(context: context, startSeconds: startSeconds, endSeconds: endSeconds, secondsToPixels: secondsToPixels, size: size)
// Draw playhead
drawPlayhead(context: context, currentTime: currentTime, secondsToPixels: secondsToPixels, size: size)
drawMarkers(context: context, startSeconds: startSeconds, endSeconds: endSeconds, secondsToPixels: secondsToPixels, size: size)
}
.frame(width: self.getSafeWidth() + TrackView.trackHeaderWidth)
}
func drawMarkers(context: GraphicsContext, startSeconds: Double, endSeconds: Double, secondsToPixels: Double, size: CGSize)
{
let y = 20.0
if let tracks = self.timeline.tracks
{
let markerRange = tracks.markers.startIndex ..< tracks.markers.endIndex
for markerIndex in markerRange
{
let marker = tracks.markers[markerIndex]
let x = marker.markedRange.startTime.toSeconds() * self.secondsToPixels
let text = marker.name
if #available(macOS 14.0, *)
{
context.draw(
Text("\(Image(systemName: "arrowtriangle.down.fill"))")
.font(.system(size: 10))
.foregroundStyle(.red),
at: CGPoint(x: x + 0.5, y: y)
)
context.draw(
Text(text)
.font(.system(size: 10))
.foregroundStyle(.red),
at: CGPoint(x: x + 9 , y: y - 2.0),
anchor: UnitPoint(x: 0, y: 0.5)
)
}
else
{
context.draw(
Text("\(Image(systemName: "arrowtriangle.down.fill"))")
.font(.system(size: 10))
.foregroundColor(.red),
at: CGPoint(x: x + 0.5, y: y)
)
context.draw(
Text(text)
.font(.system(size: 10))
.foregroundColor(.red),
at: CGPoint(x: x + 9 , y: y - 2.0),
anchor: UnitPoint(x: 0, y: 0.5)
)
}
let tickHeight = 24.0
// Draw tick line
let tickRect = CGRect(x: x, y: size.height - tickHeight, width: 1, height: tickHeight)
context.fill(Path(tickRect), with: .color(.red))
}
}
}
func drawTicks(context: GraphicsContext, startSeconds: Double, endSeconds: Double, secondsToPixels: Double, size: CGSize)
{
if self.secondsToPixels > 75
{
self.drawFrameTicks(context: context, startSeconds: startSeconds, endSeconds: endSeconds, secondsToPixels: secondsToPixels, size: size)
}
self.drawSecondTicks(context: context, startSeconds: startSeconds, endSeconds: endSeconds, secondsToPixels: secondsToPixels, size: size)
}
func drawSecondTicks(context: GraphicsContext, startSeconds: Double, endSeconds: Double, secondsToPixels: Double, size: CGSize)
{
let maxPixelX = size.width
for timeInSeconds in stride(from: startSeconds, through: endSeconds, by: 1)
{
let positionX = (timeInSeconds - startSeconds) * secondsToPixels
// Skip if out of canvas bounds
guard positionX >= 0, positionX <= maxPixelX else { continue }
let tickHeight = 9.0
// Determine if it's an hour, minute, second, or frame tick
let seconds = timeInSeconds.truncatingRemainder(dividingBy: 60)
let minutes = (timeInSeconds / 60).truncatingRemainder(dividingBy: 60)
let hours = (timeInSeconds / 3600).truncatingRemainder(dividingBy: 24)
let label = String(format: "%02d:%02d:%02d:00", Int(hours), Int(minutes), Int(seconds))
// Draw tick line
let tickRect = CGRect(x: positionX, y: size.height - tickHeight, width: 1, height: tickHeight)
context.fill(Path(tickRect), with: .color(.white))
// Draw label if it's an hour or minute
if self.secondsToPixels > 75
{
context.draw(Text(label).font(.system(size: 10)), at: CGPoint(x: positionX + 2, y: size.height - tickHeight - 10))
}
}
}
func drawFrameTicks(context: GraphicsContext, startSeconds: Double, endSeconds: Double, secondsToPixels: Double, size: CGSize)
{
let maxPixelX = size.width
let frameRate = getFrameRate() // Get the frame rate of the timeline
let frameDuration = 1.0 / frameRate // Duration of one frame in seconds
for frameNum in stride(from: 1, through: Int((endSeconds - startSeconds) * frameRate), by: 1)
{
let positionX = Double(frameNum) * frameDuration * secondsToPixels
// Skip if out of canvas bounds
guard positionX >= 0, positionX <= maxPixelX else { continue }
let tickHeight = 4.0
// Draw tick line
let tickRect = CGRect(x: positionX, y: size.height - tickHeight, width: 1, height: tickHeight)
context.fill(Path(tickRect), with: .color(.white))
if self.secondsToPixels > 500
{
context.draw(Text(String(frameNum)).font(.system(size: 10)),
at: CGPoint(x: positionX, y: size.height - tickHeight - 5))
}
}
}
func drawPlayhead(context: GraphicsContext, currentTime: OpenTimelineIO.RationalTime, secondsToPixels: Double, size: CGSize)
{
let currentTimeLabel:String
do
{
currentTimeLabel = try currentTime.toTimecode()
}
catch
{
currentTimeLabel = currentTime.toTimestring()
}
let playheadPositionX = currentTime.toSeconds() * secondsToPixels
let playheadRect = CGRect(x: playheadPositionX, y: 22, width: 1, height: size.height - 22)
// context.fill(Path(playheadRect), with: .color(.orange))
if #available(macOS 14.0, *)
{
context.draw(
Text("\(Image(systemName: "arrowtriangle.down.fill"))")
.font(.system(size: 13))
.foregroundStyle(.orange),
at: CGPoint(x: playheadPositionX + 0.5, y: 15))
context.draw(
Text(currentTimeLabel)
.font(.system(size: 10, weight: .bold))
.foregroundStyle(.orange),
at: CGPoint(x: playheadPositionX, y: 5))
}
else
{
context.draw(
Text("\(Image(systemName: "arrowtriangle.down.fill"))")
.font(.system(size: 13))
.foregroundColor(.orange),
at: CGPoint(x: playheadPositionX + 0.5, y: 15))
context.draw(
Text(currentTimeLabel)
.font(.system(size: 10, weight: .bold))
.foregroundColor(.orange),
at: CGPoint(x: playheadPositionX, y: 5))
}
context.fill(Path(playheadRect), with: .color(.orange))
}
// New function to get frame rate of the timeline
func getFrameRate() -> Double
{
let rate = timeline.globalStartTime?.rate ?? 24.0 // Default to 24 fps if unavailable
return rate
}
func getSafeRange() -> OpenTimelineIO.TimeRange
{
var range: OpenTimelineIO.TimeRange
do
{
range = try TimeRange(startTime: timeline.globalStartTime ?? RationalTime(), duration: timeline.duration())
}
catch
{
range = TimeRange()
}
return range
}
func getSafeWidth() -> CGFloat
{
return self.getSafeRange().duration.toSeconds() * self.secondsToPixels
}
}