app: play past a clip's end and zoom the scrubber to the clip
Playing in clip mode froze whenever the play head already sat at or past the end of the selection: the preview rule stopped playback as soon as the head reached the end, so a play started there was over before it began. Remember where playback began and stop at the end only when it began before it. The scrubber now shows a window around the selection while a clip is being chosen, the selection taking half the track, so its two ends can be placed to the pixel. The window holds still while an end is dragged, and a drag that leaves the bar keeps counting, so a clip can still be grown past the window.
This commit is contained in:
1 parent
0c7e80d896
commit
74b35b0120
1 file changed
+124
-23
+124
-23
@@ -28,6 +28,12 @@ pub struct Review {
|
||||
/// Where the scrubber starts in the window, in points, so an arrow drag can read the
|
||||
/// pointer against the track.
|
||||
pub scrubber_x: f32,
|
||||
/// Where playback began, while it runs. A selection preview stops at its end only when the
|
||||
/// play head started before it; played from the end, there is nothing left to preview.
|
||||
pub play_from: Option<f64>,
|
||||
/// The stretch the scrubber shows, held still while an end is dragged so the track does not
|
||||
/// move under the pointer.
|
||||
pub frozen_window: Option<(f64, f64)>,
|
||||
pub show_details: bool,
|
||||
pub highlights: Vec<Highlight>,
|
||||
/// The clip being chosen on the scrubber, once the Clip button is pressed.
|
||||
@@ -183,6 +189,31 @@ impl Review {
|
||||
};
|
||||
}
|
||||
|
||||
/// The stretch of the recording the scrubber lays out: the whole file, unless a clip is
|
||||
/// being chosen, when it zooms to a window around the selection so its ends can be placed
|
||||
/// to the pixel. A drag freezes it; the window follows once the drag is over.
|
||||
pub fn shown(&self) -> (f64, f64) {
|
||||
let duration = self.duration.max(0.0);
|
||||
if let Some(window) = self.frozen_window {
|
||||
return window;
|
||||
}
|
||||
let Some(clip) = self.clip else {
|
||||
return (0.0, duration);
|
||||
};
|
||||
let half = (clip.end - clip.start).max(1.0) * 0.5;
|
||||
let mut from = clip.start - half;
|
||||
let mut to = clip.end + half;
|
||||
if from < 0.0 {
|
||||
to -= from;
|
||||
from = 0.0;
|
||||
}
|
||||
if to > duration {
|
||||
from = (from - (to - duration)).max(0.0);
|
||||
to = duration;
|
||||
}
|
||||
(from, to)
|
||||
}
|
||||
|
||||
/// Move one end of the selection to `at`, a second away from the other end at least, and
|
||||
/// show the picture there. Only the arrows on the scrubber call this.
|
||||
pub fn drag_clip_end(&mut self, start: bool, at: f64) {
|
||||
@@ -260,6 +291,8 @@ impl Default for Review {
|
||||
duration: 0.0,
|
||||
scrubber_width: 0.0,
|
||||
scrubber_x: 0.0,
|
||||
play_from: None,
|
||||
frozen_window: None,
|
||||
highlights: Vec::new(),
|
||||
clip: None,
|
||||
show_details: false,
|
||||
@@ -348,6 +381,8 @@ fn follow_sound(app: &mut App) {
|
||||
pub fn leave(app: &mut App) {
|
||||
app.review_rx = None;
|
||||
app.review.loading = false;
|
||||
app.review.play_from = None;
|
||||
app.review.frozen_window = None;
|
||||
app.review.player = None;
|
||||
app.review.sounds.clear();
|
||||
app.review.game = None;
|
||||
@@ -417,13 +452,19 @@ pub fn tick(app: &mut App, tx: &mut Tx) -> bool {
|
||||
}
|
||||
follow_sound(app);
|
||||
keep_in_step(app);
|
||||
// A clip preview stops where the clip does: the selection is what is being watched.
|
||||
if let Some(clip) = app.review.clip
|
||||
&& playing(app)
|
||||
&& app.review.time >= clip.end
|
||||
{
|
||||
app.review.playback();
|
||||
changed = true;
|
||||
// A clip preview stops where the clip does: the selection is what is being watched. A play
|
||||
// that started at or past the end previews nothing, so it runs on rather than freezing.
|
||||
if playing(app) {
|
||||
let from = *app.review.play_from.get_or_insert(app.review.time);
|
||||
if let Some(clip) = app.review.clip
|
||||
&& from < clip.end
|
||||
&& app.review.time >= clip.end
|
||||
{
|
||||
app.review.playback();
|
||||
changed = true;
|
||||
}
|
||||
} else {
|
||||
app.review.play_from = None;
|
||||
}
|
||||
if let Some(rx) = &app.review.export {
|
||||
match rx.try_recv() {
|
||||
@@ -831,14 +872,23 @@ fn playing(app: &App) -> bool {
|
||||
app.review.player.as_ref().is_some_and(Player::is_playing)
|
||||
}
|
||||
|
||||
/// Where `x`, in points across the track, falls in time within the stretch the scrubber shows.
|
||||
/// `x` may lie past either end: a drag that leaves the bar keeps counting, so an end can be
|
||||
/// pulled beyond the window rather than stopping at its edge.
|
||||
fn time_at(x: f32, width: f32, (from, to): (f64, f64)) -> f64 {
|
||||
from + (x / width.max(1.0)) as f64 * (to - from)
|
||||
}
|
||||
|
||||
fn scrubber(app: &App, p: Paint, game: &Timeline) -> El {
|
||||
let width = if app.review.scrubber_width > 0.0 {
|
||||
app.review.scrubber_width
|
||||
} else {
|
||||
800.0
|
||||
};
|
||||
let duration = app.review.duration.max(0.001);
|
||||
let progress = (app.review.time / duration).clamp(0.0, 1.0) as f32;
|
||||
let (shown_from, shown_to) = app.review.shown();
|
||||
let shown = (shown_to - shown_from).max(0.001);
|
||||
let shown_x = |at: f64| ((at - shown_from) / shown).clamp(0.0, 1.0) as f32;
|
||||
let progress = shown_x(app.review.time);
|
||||
let knob = 14.0;
|
||||
let player = game.summoner();
|
||||
|
||||
@@ -846,7 +896,7 @@ fn scrubber(app: &App, p: Paint, game: &Timeline) -> El {
|
||||
.key("scrubber")
|
||||
.role("slider")
|
||||
.label("Playback position")
|
||||
.range_value(app.review.time, 0.0, duration)
|
||||
.range_value(app.review.time, shown_from, shown_to)
|
||||
.w_full()
|
||||
.h(34.0)
|
||||
.on_layout(|app: &mut App, [x, _, w, _]| {
|
||||
@@ -859,7 +909,7 @@ fn scrubber(app: &App, p: Paint, game: &Timeline) -> El {
|
||||
})
|
||||
.on_drag(move |app: &mut App, d: &mut rune::Drag, tx| {
|
||||
let w = app.review.scrubber_width.max(1.0);
|
||||
let at = (d.x / w).clamp(0.0, 1.0) as f64 * app.review.duration;
|
||||
let at = time_at(d.x, w, app.review.shown());
|
||||
app.review.seek(at);
|
||||
tx.instant();
|
||||
})
|
||||
@@ -875,8 +925,11 @@ fn scrubber(app: &App, p: Paint, game: &Timeline) -> El {
|
||||
|
||||
// Each highlight as a band on the track, so the clip it exports is visible at a glance.
|
||||
for (index, clip) in app.review.highlights.iter().enumerate() {
|
||||
let x = (clip.start / duration).clamp(0.0, 1.0) as f32 * width;
|
||||
let end = (clip.end / duration).clamp(0.0, 1.0) as f32 * width;
|
||||
if clip.end < shown_from || clip.start > shown_to {
|
||||
continue;
|
||||
}
|
||||
let x = shown_x(clip.start) * width;
|
||||
let end = shown_x(clip.end) * width;
|
||||
let ink = match clip.kind {
|
||||
highlight::Kind::Death => p.danger,
|
||||
highlight::Kind::Assist => p.muted(),
|
||||
@@ -912,11 +965,10 @@ fn scrubber(app: &App, p: Paint, game: &Timeline) -> El {
|
||||
|
||||
// The clip being chosen: only its span is gold. The two ends are drawn after the knob, so
|
||||
// their arrows stay visible over it.
|
||||
let bounds = app.review.clip.map(|clip| {
|
||||
let from = (clip.start / duration).clamp(0.0, 1.0) as f32 * width;
|
||||
let to = (clip.end / duration).clamp(0.0, 1.0) as f32 * width;
|
||||
(from, to)
|
||||
});
|
||||
let bounds = app
|
||||
.review
|
||||
.clip
|
||||
.map(|clip| (shown_x(clip.start) * width, shown_x(clip.end) * width));
|
||||
if let Some((from, to)) = bounds {
|
||||
bar = bar.child(
|
||||
el().key("clip-span")
|
||||
@@ -932,10 +984,10 @@ fn scrubber(app: &App, p: Paint, game: &Timeline) -> El {
|
||||
// recording has, not only the player's own.
|
||||
for (index, event) in game.events.iter().enumerate() {
|
||||
let at = event.time();
|
||||
if !at.is_finite() || at < 0.0 || at > duration {
|
||||
if !at.is_finite() || at < shown_from || at > shown_to {
|
||||
continue;
|
||||
}
|
||||
let x = (at / duration) as f32 * (width - 2.0);
|
||||
let x = ((at - shown_from) / shown) as f32 * (width - 2.0);
|
||||
let involvement = player
|
||||
.as_deref()
|
||||
.and_then(|name| highlight::involvement(event, name));
|
||||
@@ -1016,10 +1068,18 @@ fn clip_end(p: Paint, key: &str, icon: &'static rune::Icon, start: bool, x: f32)
|
||||
.border(1.0, p.t.bg)
|
||||
.shadow(0.0, 1.0, p.t.edge)
|
||||
.on_drag(move |app: &mut App, d: &mut rune::Drag, tx| {
|
||||
// The window holds still for the drag: zooming as the end moves would slide the
|
||||
// track under the pointer.
|
||||
if d.phase == rune::Phase::Down {
|
||||
app.review.frozen_window = Some(app.review.shown());
|
||||
}
|
||||
let w = app.review.scrubber_width.max(1.0);
|
||||
let track = (d.window_point()[0] - app.review.scrubber_x) / w;
|
||||
let at = track.clamp(0.0, 1.0) as f64 * app.review.duration;
|
||||
app.review.drag_clip_end(start, at);
|
||||
let x = d.window_point()[0] - app.review.scrubber_x;
|
||||
app.review
|
||||
.drag_clip_end(start, time_at(x, w, app.review.shown()));
|
||||
if d.phase == rune::Phase::Up {
|
||||
app.review.frozen_window = None;
|
||||
}
|
||||
tx.instant();
|
||||
})
|
||||
.child(rune::icon(icon, 12.0).color(p.accent_ink).a11y_hidden())
|
||||
@@ -1259,6 +1319,47 @@ fn events(_app: &App, p: Paint, game: &Timeline) -> El {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_scrubber_shows_the_whole_file_without_a_clip() {
|
||||
let mut review = review(10.0, 20.0);
|
||||
review.clip = None;
|
||||
assert_eq!(review.shown(), (0.0, 100.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_clip_zooms_the_scrubber_around_it() {
|
||||
let review = review(40.0, 60.0);
|
||||
assert_eq!(review.shown(), (30.0, 70.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_window_stays_inside_the_recording() {
|
||||
assert_eq!(review(0.0, 20.0).shown(), (0.0, 40.0));
|
||||
assert_eq!(review(80.0, 100.0).shown(), (60.0, 100.0));
|
||||
for (start, end) in [(0.0, 20.0), (80.0, 100.0), (40.0, 60.0)] {
|
||||
let window = review(start, end).shown();
|
||||
assert!(
|
||||
window.0 <= start && window.1 >= end,
|
||||
"{window:?} misses the clip"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_past_the_bar_keeps_counting() {
|
||||
assert_eq!(time_at(0.0, 100.0, (10.0, 20.0)), 10.0);
|
||||
assert_eq!(time_at(50.0, 100.0, (10.0, 20.0)), 15.0);
|
||||
assert_eq!(time_at(150.0, 100.0, (10.0, 20.0)), 25.0);
|
||||
assert_eq!(time_at(-50.0, 100.0, (10.0, 20.0)), 5.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_holds_the_window_still() {
|
||||
let mut review = review(40.0, 60.0);
|
||||
review.frozen_window = Some((0.0, 100.0));
|
||||
assert_eq!(review.shown(), (0.0, 100.0));
|
||||
}
|
||||
|
||||
fn review(start: f64, end: f64) -> Review {
|
||||
Review {
|
||||
duration: 100.0,
|
||||
|
||||
Reference in new issue
Block a user