use std::cell::RefCell;
use std::rc::Rc;
use crate::easing::Easing;
use crate::timeline::{new_tween, Action};
mod computed;
mod tweenable;
pub use computed::Computed;
pub use tweenable::Tweenable;
pub struct Signal<T> {
cell: Rc<RefCell<T>>,
}
impl<T> Clone for Signal<T> {
fn clone(&self) -> Self {
Signal { cell: Rc::clone(&self.cell) }
}
}
impl<T: Tweenable> Signal<T> {
pub fn new(value: T) -> Self {
Signal { cell: Rc::new(RefCell::new(value)) }
}
pub fn get(&self) -> T {
self.cell.borrow().clone()
}
pub fn set(&self, value: T) {
*self.cell.borrow_mut() = value;
}
pub fn tween_to(&self, to: T, duration: f64, easing: Easing) -> Action {
new_tween(self.cell.clone(), self.get(), to, duration, easing)
}
pub(crate) fn tween_from(&self, from: T, to: T, duration: f64, easing: Easing) -> Action {
new_tween(self.cell.clone(), from, to, duration, easing)
}
pub fn step_to(&self, to: T) -> Action {
new_tween(self.cell.clone(), self.get(), to, 0.0, Easing::Linear)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn signal_shares_value() {
let a = Signal::new(1.0_f32);
let b = a.clone();
a.set(42.0);
assert_eq!(b.get(), 42.0);
}
}