diff --git a/examples/ani.rs b/examples/ani.rs index 6bcba60..631a949 100644 --- a/examples/ani.rs +++ b/examples/ani.rs @@ -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() { @@ -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; @@ -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; diff --git a/examples/easing_functions.rs b/examples/easing_functions.rs index 113bf82..06bfe5c 100644 --- a/examples/easing_functions.rs +++ b/examples/easing_functions.rs @@ -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; @@ -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); diff --git a/examples/flex_ui.rs b/examples/flex_ui.rs index b70ac0d..964432d 100644 --- a/examples/flex_ui.rs +++ b/examples/flex_ui.rs @@ -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; diff --git a/examples/picture_viewer.rs b/examples/picture_viewer.rs index fc42620..4a95136 100644 --- a/examples/picture_viewer.rs +++ b/examples/picture_viewer.rs @@ -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; @@ -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) { diff --git a/src/actor.rs b/src/actor.rs index d80f342..c7c6921 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -13,15 +13,13 @@ use std::mem; use std::os::raw::c_void; use std::path::Path; use std::ptr; -use std::time::Instant; -use keyframe::{ease, functions::*}; use stretch::{ node::{Node, Stretch}, style::*, }; - +use crate::animation::Animation; #[repr(i32)] #[derive(Copy, Clone, Debug, PartialEq)] @@ -37,27 +35,6 @@ pub enum Key { Up = 265, } -#[derive(Copy, Clone, Debug)] -pub enum EasingFunction { - EaseIn, - EaseInCubic, - EaseInOut, - EaseInOutCubic, - EaseInOutQuad, - EaseInOutQuart, - EaseInOutQuint, - EaseInQuad, - EaseInQuart, - EaseInQuint, - EaseOut, - EaseOutCubic, - EaseOutQuad, - EaseOutQuart, - EaseOutQuint, - Linear, - Step, -} - #[derive(Copy, Clone, Debug)] pub enum LayoutMode { UserDefine, @@ -89,36 +66,7 @@ pub struct Actor { vertex_array_obj: gl::types::GLuint, texture: gl::types::GLuint, pub animated: bool, - animation_time_instance: Instant, - - translation_x_animation_running: bool, - translation_x_animation_starting_time: u128, - translation_x_animation_time_duration: f32, - translation_x_animation_from_value: i32, - translation_x_animation_to_value: i32, - translation_x_animation_ease: EasingFunction, - - translation_y_animation_running: bool, - translation_y_animation_starting_time: u128, - translation_y_animation_time_duration: f32, - translation_y_animation_from_value: i32, - translation_y_animation_to_value: i32, - translation_y_animation_ease: EasingFunction, - - scale_animation_running: bool, - scale_animation_starting_time: u128, - scale_animation_time_duration: f32, - scale_animation_from_value: f32, - scale_animation_to_value: f32, - scale_animation_ease: EasingFunction, - - rotation_animation_running: bool, - rotation_animation_starting_time: u128, - rotation_animation_time_duration: f32, - rotation_animation_from_value: i32, - rotation_animation_to_value: i32, - rotation_animation_ease: EasingFunction, - + pub animation: Option, event_handler: Option>, layout: Option>, focused_sub_actor: usize, @@ -166,31 +114,7 @@ impl Actor { vertex_array_obj: gl::types::GLuint::default(), texture: gl::types::GLuint::default(), animated: false, - animation_time_instance: Instant::now(), - translation_x_animation_running: false, - translation_x_animation_starting_time: 0, - translation_x_animation_time_duration: 0.0, - translation_x_animation_from_value: 0, - translation_x_animation_ease: EasingFunction::Linear, - translation_x_animation_to_value: 0, - translation_y_animation_running: false, - translation_y_animation_starting_time: 0, - translation_y_animation_time_duration: 0.0, - translation_y_animation_from_value: 0, - translation_y_animation_to_value: 0, - translation_y_animation_ease: EasingFunction::Linear, - scale_animation_running: false, - scale_animation_starting_time: 0, - scale_animation_time_duration: 0.0, - scale_animation_from_value: 0.0, - scale_animation_to_value: 0.0, - scale_animation_ease: EasingFunction::Linear, - rotation_animation_running: false, - rotation_animation_starting_time: 0, - rotation_animation_time_duration: 0.0, - rotation_animation_from_value: 0, - rotation_animation_to_value: 0, - rotation_animation_ease: EasingFunction::Linear, + animation: None, event_handler: event_handler, layout: None, focused_sub_actor: 0, @@ -324,6 +248,10 @@ impl Actor { self.layout = layout; } + pub fn set_animation(&mut self, animation: Option) { + self.animation = animation; + } + pub fn set_style(&mut self, style: Style) { self.style = Some(style); } @@ -337,199 +265,18 @@ impl Actor { self.sub_actor_list.sort_by(|a, b| a.z.partial_cmp(&b.z).unwrap()); }*/ - fn easing_function(easing: EasingFunction, from: f32, to: f32, duration: f32) -> f32 { - match easing { - EasingFunction::EaseIn => ease(EaseIn, from, to, duration), - EasingFunction::EaseInCubic => ease(EaseInCubic, from, to, duration), - EasingFunction::EaseInOut => ease(EaseInOut, from, to, duration), - EasingFunction::EaseInOutCubic => ease(EaseInOutCubic, from, to, duration), - EasingFunction::EaseInOutQuad => ease(EaseInOutQuad, from, to, duration), - EasingFunction::EaseInOutQuart => ease(EaseInOutQuart, from, to, duration), - EasingFunction::EaseInOutQuint => ease(EaseInOutQuint, from, to, duration), - EasingFunction::EaseInQuad => ease(EaseInQuad, from, to, duration), - EasingFunction::EaseInQuart => ease(EaseInQuart, from, to, duration), - EasingFunction::EaseInQuint => ease(EaseInQuint, from, to, duration), - EasingFunction::EaseOut => ease(EaseOut, from, to, duration), - EasingFunction::EaseOutCubic => ease(EaseOutCubic, from, to, duration), - EasingFunction::EaseOutQuad => ease(EaseOutQuad, from, to, duration), - EasingFunction::EaseOutQuart => ease(EaseOutQuart, from, to, duration), - EasingFunction::EaseOutQuint => ease(EaseOutQuint, from, to, duration), - EasingFunction::Linear => ease(Linear, from, to, duration), - EasingFunction::Step => ease(Step, from, to, duration), - } - } pub fn animate(&mut self) { - if self.translation_x_animation_running == true { - if self.translation_x_animation_starting_time == 0 { - self.translation_x_animation_starting_time = - self.animation_time_instance.elapsed().as_millis(); - } - let cur_time = (self.animation_time_instance.elapsed().as_millis() - - self.translation_x_animation_starting_time) as f32 - / self.translation_x_animation_time_duration; - if cur_time <= 1.0 { - self.x = Actor::easing_function( - self.translation_x_animation_ease, - self.translation_x_animation_from_value as f32, - self.translation_x_animation_to_value as f32, - cur_time, - ) as i32; - } else { - self.translation_x_animation_running = false; - self.translation_x_animation_starting_time = 0; - self.x = self.translation_x_animation_to_value; - } - } - - if self.translation_y_animation_running == true { - if self.translation_y_animation_starting_time == 0 { - self.translation_y_animation_starting_time = - self.animation_time_instance.elapsed().as_millis(); - } - let cur_time = (self.animation_time_instance.elapsed().as_millis() - - self.translation_y_animation_starting_time) as f32 - / self.translation_y_animation_time_duration; - if cur_time <= 1.0 { - self.y = Actor::easing_function( - self.translation_y_animation_ease, - self.translation_y_animation_from_value as f32, - self.translation_y_animation_to_value as f32, - cur_time, - ) as i32; - } else { - self.translation_y_animation_running = false; - self.translation_y_animation_starting_time = 0; - self.y = self.translation_y_animation_to_value; - } - } - - if self.rotation_animation_running == true { - if self.rotation_animation_starting_time == 0 { - self.rotation_animation_starting_time = self.animation_time_instance.elapsed().as_millis(); - } - - let cur_time = (self.animation_time_instance.elapsed().as_millis() - - self.rotation_animation_starting_time) as f32 - / self.rotation_animation_time_duration as f32; - if cur_time <= 1.0 { - self.rotation = Actor::easing_function( - self.rotation_animation_ease, - self.rotation_animation_from_value as f32, - self.rotation_animation_to_value as f32, - cur_time, - ) as i32; - } else { - self.rotation_animation_running = false; - self.rotation_animation_starting_time = 0; - self.rotation = self.rotation_animation_to_value; - } - } - - if self.scale_animation_running == true { - if self.scale_animation_starting_time == 0 { - self.scale_animation_starting_time = self.animation_time_instance.elapsed().as_millis(); - } - - let cur_time = (self.animation_time_instance.elapsed().as_millis() - - self.scale_animation_starting_time) as f32 - / self.scale_animation_time_duration as f32; - if cur_time <= 1.0 { - self.scale_x = Actor::easing_function( - self.scale_animation_ease, - self.scale_animation_from_value, - self.scale_animation_to_value, - cur_time, - ) as f32; - self.scale_y = Actor::easing_function( - self.scale_animation_ease, - self.scale_animation_from_value, - self.scale_animation_to_value, - cur_time, - ) as f32; - } else { - self.scale_animation_running = false; - self.scale_animation_starting_time = 0; - self.scale_x = self.scale_animation_to_value; - self.scale_y = self.scale_animation_to_value; - } - } - - if self.translation_x_animation_running == true - || self.translation_y_animation_running == true - || self.rotation_animation_running == true - || self.scale_animation_running == true - { - self.animated = true; - } else { - self.animated = false; - } + println!("animate: {}", self.name); + if let Some(mut animation) = self.animation.take() { + animation.run(self); + self.animation = Some(animation); + } for sub_actor in self.sub_actor_list.iter_mut() { sub_actor.animate(); } } - pub fn apply_translation_x_animation( - &mut self, - from_value: i32, - to_value: i32, - time: f32, - easing: EasingFunction, - ) { - self.translation_x_animation_running = true; - self.translation_x_animation_ease = easing; - self.translation_x_animation_from_value = from_value; - self.translation_x_animation_to_value = to_value; - self.translation_x_animation_time_duration = time * 1000.0; // msec. - self.x = self.translation_x_animation_from_value; - } - - pub fn apply_translation_y_animation( - &mut self, - from_value: i32, - to_value: i32, - time: f32, - easing: EasingFunction, - ) { - self.translation_y_animation_running = true; - self.translation_y_animation_ease = easing; - self.translation_y_animation_from_value = from_value; - self.translation_y_animation_to_value = to_value; - self.translation_y_animation_time_duration = time * 1000.0; // msec. - self.x = self.translation_y_animation_from_value; - } - - pub fn apply_rotation_animation( - &mut self, - from_value: i32, - to_value: i32, - time: f32, - easing: EasingFunction, - ) { - self.rotation_animation_running = true; - self.rotation_animation_ease = easing; - self.rotation_animation_from_value = from_value; - self.rotation_animation_to_value = to_value; - self.rotation_animation_time_duration = time * 1000.0; // msec. - self.rotation = self.rotation_animation_from_value; - } - - pub fn apply_scale_animation( - &mut self, - from_value: f32, - to_value: f32, - time: f32, - easing: EasingFunction, - ) { - self.scale_animation_running = true; - self.scale_animation_ease = easing; - self.scale_animation_from_value = from_value; - self.scale_animation_to_value = to_value; - self.scale_animation_time_duration = time * 1000.0; // msec. - self.scale_x = self.scale_animation_from_value; - self.scale_y = self.scale_animation_from_value; - } - pub fn select_next_sub_actor(&mut self) { if self.sub_actor_list.len() <= 0 { return; diff --git a/src/lib.rs b/src/lib.rs index 70aff43..9c68592 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,3 +4,4 @@ pub mod actor; pub mod play; +pub mod animation;