Adding in animation::timestamps_difference function

This commit is contained in:
Pauan 2018-11-24 10:58:33 -10:00
parent 50b7b00f64
commit af462d41e7
1 changed files with 25 additions and 8 deletions

View File

@ -60,17 +60,20 @@ struct TimestampsGlobal {
value: Arc<RwLock<Option<f64>>>, value: Arc<RwLock<Option<f64>>>,
} }
#[derive(Debug)]
enum TimestampsEnum { enum TimestampsEnum {
First, First,
Changed, Changed,
NotChanged, NotChanged,
} }
#[derive(Debug)]
struct TimestampsState { struct TimestampsState {
state: TimestampsEnum, state: TimestampsEnum,
waker: Option<Waker>, waker: Option<Waker>,
} }
#[derive(Debug)]
pub struct Timestamps { pub struct Timestamps {
state: Arc<Mutex<TimestampsState>>, state: Arc<Mutex<TimestampsState>>,
// TODO verify that there aren't any Arc cycles // TODO verify that there aren't any Arc cycles
@ -189,6 +192,7 @@ impl<S: SignalVec> AnimatedSignalVec for S {
} }
#[derive(Debug)]
pub struct AnimatedMapBroadcaster(MutableAnimation); pub struct AnimatedMapBroadcaster(MutableAnimation);
impl AnimatedMapBroadcaster { impl AnimatedMapBroadcaster {
@ -200,12 +204,14 @@ impl AnimatedMapBroadcaster {
} }
#[derive(Debug)]
struct AnimatedMapState { struct AnimatedMapState {
animation: MutableAnimation, animation: MutableAnimation,
removing: Option<WaitFor<MutableAnimationSignal>>, removing: Option<WaitFor<MutableAnimationSignal>>,
} }
// TODO move this into signals crate and also generalize it to work with any future, not just animations // TODO move this into signals crate and also generalize it to work with any future, not just animations
#[derive(Debug)]
pub struct AnimatedMap<A, B> { pub struct AnimatedMap<A, B> {
duration: f64, duration: f64,
animations: Vec<AnimatedMapState>, animations: Vec<AnimatedMapState>,
@ -517,19 +523,27 @@ impl MutableTimestamps<F> where F: FnMut(f64) {
}*/ }*/
pub fn timestamps_difference() -> impl Signal<Item = Option<f64>> {
let mut starting_time = None;
timestamps().map(move |current_time| {
current_time.map(|current_time| {
let starting_time = *starting_time.get_or_insert(current_time);
current_time - starting_time
})
})
}
pub struct OnTimestampDiff(DiscardOnDrop<CancelableFutureHandle>); pub struct OnTimestampDiff(DiscardOnDrop<CancelableFutureHandle>);
impl OnTimestampDiff { impl OnTimestampDiff {
pub fn new<F>(mut callback: F) -> Self where F: FnMut(f64) + 'static { pub fn new<F>(mut callback: F) -> Self where F: FnMut(f64) + 'static {
let mut starting_time = None;
OnTimestampDiff(spawn_future( OnTimestampDiff(spawn_future(
timestamps() timestamps_difference()
.for_each(move |current_time| { .for_each(move |diff| {
if let Some(current_time) = current_time { if let Some(diff) = diff {
let starting_time = *starting_time.get_or_insert(current_time); callback(diff);
callback(current_time - starting_time);
} }
ready(()) ready(())
@ -546,6 +560,7 @@ impl fmt::Debug for OnTimestampDiff {
} }
#[derive(Debug)]
pub struct MutableAnimationSignal(MutableSignal<Percentage>); pub struct MutableAnimationSignal(MutableSignal<Percentage>);
impl Signal for MutableAnimationSignal { impl Signal for MutableAnimationSignal {
@ -570,6 +585,8 @@ struct MutableAnimationInner {
value: Mutable<Percentage>, value: Mutable<Percentage>,
} }
// TODO deref to ReadOnlyMutable ?
// TODO provide read_only() method ?
pub struct MutableAnimation { pub struct MutableAnimation {
inner: Arc<MutableAnimationInner>, inner: Arc<MutableAnimationInner>,
} }