import 'dart:math' as math; import 'dart:ui'; import 'package:cutdex_core/editor.dart'; import 'package:cutdex_timeline_ui/cutdex_timeline_ui.dart'; /// Speed segments in the clip's local coordinates. Paint and hit targets use /// the same cells, including the minimum pixel width and clipping at low zoom. final class TimelineSpeedLayout { TimelineSpeedLayout({ required TimelineClipLayout clip, required Iterable segments, required double pixelsPerTick, }) { bounds = Rect.fromLTWH( clip.header.left, clip.header.top, clip.header.width, math.min( clip.header.height, EditorLayoutConfiguration.speedSectionHeight.toDouble(), ), ); var left = bounds.left; final cells = []; for (final segment in segments) { final width = math.max(1.0, segment.projectDuration * pixelsPerTick); final right = math.min(bounds.right, left + width); final cell = Rect.fromLTWH( math.min(left, bounds.right), bounds.top, math.max(0, right - left), bounds.height, ); cells.add(TimelineSpeedSegmentLayout(segment, cell)); left += width; } this.segments = List.unmodifiable(cells); } late final Rect bounds; late final List segments; } final class TimelineSpeedSegmentLayout { TimelineSpeedSegmentLayout(this.segment, this.cell) { bar = Rect.fromLTWH( cell.left, cell.top, math.max(0, cell.width - segmentGap), cell.height, ); label = Rect.fromLTWH( bar.left + math.min(labelPadding, bar.width / 2), bar.top + 1, math.max(0, bar.width - labelPadding * 2), math.max(0, bar.height - 2), ); final horizontalInset = math.min(handleInset, bar.width / 2); final verticalInset = math.min(handleInset, bar.height / 2); final handleRight = bar.right - horizontalInset; final availableWidth = math.max(0.0, bar.width - horizontalInset * 2); final handleTop = bar.top + verticalInset; final handleHeight = math.max(0.0, bar.height - verticalInset * 2); handle = Rect.fromLTWH( handleRight - math.min(handleWidth, availableWidth), handleTop, math.min(handleWidth, availableWidth), handleHeight, ); final hitWidth = math.min(handleHitWidth, bar.width); handleHit = Rect.fromLTWH( (handle.center.dx - hitWidth / 2).clamp(bar.left, bar.right - hitWidth), bar.top, hitWidth, bar.height, ); } static const segmentGap = 2.0; static const labelPadding = 8.0; static const handleInset = 3.0; static const handleWidth = 2.0; static const handleHitWidth = 10.0; static const handleRadius = 1.0; final ClipSpeedSegment segment; final Rect cell; late final Rect bar; late final Rect label; late final Rect handle; late final Rect handleHit; double get speed => segment.speed; }