feat(player): add drag-to-seek on progress bar
This commit is contained in:
33
src/main.rs
33
src/main.rs
@@ -65,6 +65,7 @@ fn run(config: &Config) -> Result<()> {
|
||||
let mut yolo: Option<YoloLoader> = None;
|
||||
let mut chunks: Option<ChunkLoader> = None;
|
||||
let mut is_fullscreen = false;
|
||||
let mut is_dragging = false;
|
||||
|
||||
if let Some(ref video_path) = config.video {
|
||||
info!("Loading video: {:?}", video_path);
|
||||
@@ -281,6 +282,8 @@ fn run(config: &Config) -> Result<()> {
|
||||
let bar_x_start = 10i32;
|
||||
let bar_width = config.width as i32 - 20;
|
||||
if x >= bar_x_start && x <= bar_x_start + bar_width {
|
||||
is_dragging = true;
|
||||
player_state.playback = PlaybackState::Paused;
|
||||
let ratio = (x - bar_x_start) as f64 / bar_width as f64;
|
||||
let target_frame = (player_state.total_frames as f64 * ratio) as u64;
|
||||
if let Some(ref mut dec) = decoder {
|
||||
@@ -299,6 +302,36 @@ fn run(config: &Config) -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
sdl2::event::Event::MouseButtonUp { .. } => {
|
||||
if is_dragging {
|
||||
is_dragging = false;
|
||||
info!("Drag seek ended");
|
||||
}
|
||||
}
|
||||
sdl2::event::Event::MouseMotion { x, y, .. } => {
|
||||
if is_dragging {
|
||||
let progress_y = config.height as i32 - progress_height + 5;
|
||||
let bar_height = 20i32;
|
||||
if y >= progress_y - 30
|
||||
&& y <= progress_y + bar_height + 30
|
||||
&& player_state.total_frames > 0
|
||||
{
|
||||
let bar_x_start = 10i32;
|
||||
let bar_width = config.width as i32 - 20;
|
||||
let clamped_x = x.clamp(bar_x_start, bar_x_start + bar_width);
|
||||
let ratio = (clamped_x - bar_x_start) as f64 / bar_width as f64;
|
||||
let target_frame = (player_state.total_frames as f64 * ratio) as u64;
|
||||
if let Some(ref mut dec) = decoder {
|
||||
let time_ms =
|
||||
((target_frame as f64 / player_state.fps) * 1000.0) as u64;
|
||||
if dec.seek(time_ms).is_ok() {
|
||||
player_state.current_frame = target_frame;
|
||||
player_state.current_time_ms = time_ms;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user