Skip to content

Commit

Permalink
fix: 🐛 set_frame to return an error for invalid frame number (#70)
Browse files Browse the repository at this point in the history
* fix: 🐛 set_frame to return an error for invalid frame number

* fix: 🐛 validate frame no before updating the renderer

* fix: 🐛 only update the start_time when frame is updated

* chore: 🤖 update demo player

* chore: 🤖 always return a valid start_time
  • Loading branch information
theashraf authored Jan 31, 2024
1 parent b676da4 commit 5bf0bc1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions demo-player/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ fn main() {
lottie_player.play();
}

if window.is_key_down(Key::J) {
let updated = lottie_player.set_frame(20.0);
if updated {
lottie_player.render();
}
}

if window.is_key_down(Key::Left) {
let mut config = lottie_player.config();

Expand Down
12 changes: 10 additions & 2 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,21 @@ impl DotLottieRuntime {
};

// update start_time to account for the already elapsed time
self.start_time = Instant::now() - Duration::from_secs_f32(elapsed_time_for_frame);
self.start_time =
match Instant::now().checked_sub(Duration::from_secs_f32(elapsed_time_for_frame)) {
Some(start_time) => start_time,
None => Instant::now(),
};
}

pub fn set_frame(&mut self, no: f32) -> bool {
if no < self.start_frame() || no > self.end_frame() {
return false;
}

let is_ok = self.renderer.set_frame(no).is_ok();

if self.is_playing() {
if self.is_playing() && is_ok {
self.update_start_time_for_frame(no);
}

Expand Down
11 changes: 11 additions & 0 deletions dotlottie-rs/src/lottie_renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ impl LottieRenderer {
.as_mut()
.ok_or(LottieRendererError::AnimationNotLoaded)?;

let total_frames = thorvg_animation
.get_total_frame()
.map_err(|e| LottieRendererError::ThorvgError(e))?;

if no < 0.0 || no >= total_frames {
return Err(LottieRendererError::InvalidArgument(format!(
"Frame number must be between 0 and {}",
total_frames - 1.0
)));
}

thorvg_animation
.set_frame(no)
.map_err(|e| LottieRendererError::ThorvgError(e))
Expand Down

0 comments on commit 5bf0bc1

Please sign in to comment.