From 95033bf8fe578ac9ed09fdabe6623a17d5278c4c Mon Sep 17 00:00:00 2001 From: Abdelrahman Ashraf Date: Mon, 15 Jan 2024 13:28:20 +0700 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20split=20the=20modes?= =?UTF-8?q?=20handling=20logic=20into=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dotlottie-rs/src/dotlottie_player.rs | 138 +++++++++++++++------------ 1 file changed, 79 insertions(+), 59 deletions(-) diff --git a/dotlottie-rs/src/dotlottie_player.rs b/dotlottie-rs/src/dotlottie_player.rs index d8b4c621..6b359eba 100644 --- a/dotlottie-rs/src/dotlottie_player.rs +++ b/dotlottie-rs/src/dotlottie_player.rs @@ -156,83 +156,103 @@ impl DotLottieRuntime { }; let next_frame = match self.config.mode { - Mode::Forward => { + Mode::Forward => self.handle_forward_mode(next_frame), + Mode::Reverse => self.handle_reverse_mode(next_frame), + Mode::Bounce => self.handle_bounce_mode(next_frame), + Mode::ReverseBounce => self.handle_reverse_bounce_mode(next_frame), + }; + + next_frame + } + + fn handle_forward_mode(&mut self, next_frame: f32) -> f32 { + let total_frames = self.total_frames() - 1.0; + + if next_frame >= total_frames { + if self.config.loop_animation { + self.loop_count += 1; + self.start_time = SystemTime::now(); + + 0.0 + } else { + total_frames + } + } else { + next_frame + } + } + + fn handle_reverse_mode(&mut self, next_frame: f32) -> f32 { + let total_frames = self.total_frames() - 1.0; + if next_frame <= 0.0 { + if self.config.loop_animation { + self.loop_count += 1; + self.start_time = SystemTime::now(); + total_frames + } else { + 0.0 + } + } else { + next_frame + } + } + + fn handle_bounce_mode(&mut self, next_frame: f32) -> f32 { + let total_frames = self.total_frames() - 1.0; + + match self.direction { + Direction::Forward => { if next_frame >= total_frames { - if self.config.loop_animation { - self.loop_count += 1; - self.start_time = SystemTime::now(); - 0.0 - } else { - total_frames - } + self.direction = Direction::Reverse; + self.start_time = SystemTime::now(); + total_frames } else { next_frame } } - Mode::Reverse => { + Direction::Reverse => { if next_frame <= 0.0 { if self.config.loop_animation { self.loop_count += 1; + self.direction = Direction::Forward; self.start_time = SystemTime::now(); - total_frames - } else { - 0.0 } + + 0.0 } else { next_frame } } - Mode::Bounce => match self.direction { - Direction::Forward => { - if next_frame >= total_frames { - self.direction = Direction::Reverse; - self.start_time = SystemTime::now(); - total_frames - } else { - next_frame - } - } - Direction::Reverse => { - if next_frame <= 0.0 { - if self.config.loop_animation { - self.loop_count += 1; - self.direction = Direction::Forward; - self.start_time = SystemTime::now(); - } - - 0.0 - } else { - next_frame - } + } + } + + fn handle_reverse_bounce_mode(&mut self, next_frame: f32) -> f32 { + let total_frames = self.total_frames() - 1.0; + + match self.direction { + Direction::Reverse => { + if next_frame <= 0.0 { + self.direction = Direction::Forward; + self.start_time = SystemTime::now(); + 0.0 + } else { + next_frame } - }, - Mode::ReverseBounce => match self.direction { - Direction::Reverse => { - if next_frame <= 0.0 { - self.direction = Direction::Forward; + } + Direction::Forward => { + if next_frame >= total_frames { + if self.config.loop_animation { + self.loop_count += 1; + self.direction = Direction::Reverse; self.start_time = SystemTime::now(); - 0.0 - } else { - next_frame } - } - Direction::Forward => { - if next_frame >= total_frames { - if self.config.loop_animation { - self.loop_count += 1; - self.direction = Direction::Reverse; - self.start_time = SystemTime::now(); - } - - total_frames - } else { - next_frame - } - } - }, - }; - next_frame + total_frames + } else { + next_frame + } + } + } } pub fn set_frame(&mut self, no: f32) -> bool {