Skip to content

Commit

Permalink
Refactor Animation Handling in Rust Animation
Browse files Browse the repository at this point in the history
- Extracted animation logic from Actor into a separate Animation struct.
- Replaced direct animation methods in Actor with set_animation() to use the new Animation struct.
- Updated examples (ani.rs, easing_functions.rs, flex_ui.rs, picture_viewer.rs) to use the new Animation struct instead of direct animation methods in Actor.
- Removed animation-related fields and methods from Actor, simplifying its implementation.
- Added a new animation module to encapsulate animation functionalities.
  • Loading branch information
joone committed Nov 27, 2023
1 parent 948e428 commit 9fea1e5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 277 deletions.
19 changes: 13 additions & 6 deletions examples/ani.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ extern crate glfw;
use glfw::{Action, Context, Key};

use rust_animation::actor::Actor;
use rust_animation::actor::EasingFunction;
use rust_animation::actor::LayoutMode;
use rust_animation::play::Play;
use rust_animation::animation::Animation;
use rust_animation::animation::EasingFunction;
use std::sync::mpsc::Receiver;

fn main() {
Expand Down Expand Up @@ -45,12 +46,15 @@ fn main() {
actor_1.y = 100;
actor_1.set_image("examples/splash.png".to_string());

let mut animation_1 = Animation::new();

// 1X -> 2X for 5 sec.
let time = 5.0;
actor_1.apply_scale_animation(1.0, 2.0, time, EasingFunction::Linear);
actor_1.apply_translation_x_animation(100, 1000, time, EasingFunction::EaseInOut);
actor_1.apply_translation_y_animation(100, 300, time, EasingFunction::EaseInOut);
actor_1.apply_rotation_animation(0, 360, time, EasingFunction::EaseInOut);
animation_1.apply_scale(1.0, 2.0, time, EasingFunction::Linear);
animation_1.apply_translation_x(100, 1000, time, EasingFunction::EaseInOut);
animation_1.apply_translation_y(100, 300, time, EasingFunction::EaseInOut);
animation_1.apply_rotation(0, 360, time, EasingFunction::EaseInOut);
actor_1.set_animation(Some(animation_1));

let mut actor_2 = Play::new_actor("actor_2".to_string(), 120, 120, None);
actor_2.x = 100;
Expand All @@ -59,7 +63,10 @@ fn main() {
actor_2.scale_y = 1.5;
actor_2.set_color(0.0, 0.0, 1.0);
// 0 degree -> 360 degree for 5 sec
actor_2.apply_rotation_animation(0, 360, 5.0, EasingFunction::EaseInOut);

let mut animation_2 = Animation::new();
animation_2.apply_rotation(0, 360, 5.0, EasingFunction::EaseInOut);
actor_2.set_animation(Some(animation_2));

let mut actor_3 = Play::new_actor("actor_3".to_string(), 50, 50, None);
actor_3.x = 10;
Expand Down
10 changes: 7 additions & 3 deletions examples/easing_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ extern crate glfw;
use glfw::{Action, Context, Key};

use rust_animation::actor::Actor;
use rust_animation::actor::EasingFunction;
use rust_animation::animation::Animation;
use rust_animation::animation::EasingFunction;
use rust_animation::actor::LayoutMode;
use rust_animation::play::Play;
use std::sync::mpsc::Receiver;
Expand Down Expand Up @@ -75,8 +76,11 @@ fn main() {
actor.y = y;
y += height as i32;
actor.set_color(i as f32 / 18.0, i as f32 / 18.0, i as f32 / 18.0);
actor.apply_translation_x_animation(0, (1920 - width) as i32, time, easing_functions[i]);
actor.apply_rotation_animation(0, 360, time, EasingFunction::Linear);

let mut animation = Animation::new();
animation.apply_translation_x(0, (1920 - width) as i32, time, easing_functions[i]);
animation.apply_rotation(0, 360, time, EasingFunction::Linear);
actor.set_animation(Some(animation));
stage.add_sub_actor(actor);
}
play.add_stage(stage);
Expand Down
1 change: 0 additions & 1 deletion examples/flex_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use glfw::{Action, Context, Key};
use stretch::{geometry::Rect, geometry::Size, node::Stretch, style::*};

use rust_animation::actor::Actor;

use rust_animation::actor::Layout;
use rust_animation::actor::LayoutMode;
use rust_animation::play::Play;
Expand Down
7 changes: 5 additions & 2 deletions examples/picture_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use std::sync::mpsc;
use std::thread;

use rust_animation::actor::Actor;
use rust_animation::actor::EasingFunction;
use rust_animation::animation::Animation;
use rust_animation::animation::EasingFunction;
use rust_animation::actor::EventHandler;
use rust_animation::actor::Layout;
use rust_animation::actor::LayoutMode;
Expand Down Expand Up @@ -93,7 +94,9 @@ impl ActorEvent {
impl EventHandler for ActorEvent {
fn key_focus_in(&mut self, actor: &mut Actor) {
println!("key_focus_in: {} {}", self.name, actor.name);
actor.apply_scale_animation(1.0, 1.1, 0.3, EasingFunction::EaseInOut);
let mut animation = Animation::new();
animation.apply_scale(1.0, 1.1, 0.3, EasingFunction::EaseInOut);
actor.set_animation(Some(animation));
}

fn key_focus_out(&mut self, actor: &mut Actor) {
Expand Down
Loading

0 comments on commit 9fea1e5

Please sign in to comment.