1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use std::hash::Hash;
use std::marker;
use std::time::Duration;

use amethyst_assets::{AssetStorage, Handle};
use amethyst_core::specs::prelude::{
    Component, Entities, Entity, Join, Read, ReadStorage, Resources, System, SystemData,
    WriteStorage,
};
use amethyst_core::timing::secs_to_duration;
use fnv::FnvHashMap;
use minterpolate::InterpolationPrimitive;

use resources::{
    Animation, AnimationCommand, AnimationControl, AnimationControlSet, AnimationHierarchy,
    AnimationSampling, AnimationSet, ApplyData, ControlState, DeferStartRelation, RestState,
    Sampler, SamplerControl, SamplerControlSet, StepDirection,
};

/// System for setting up animations, should run before `SamplerInterpolationSystem`.
///
/// Will process all active `AnimationControl` + `AnimationHierarchy`, and do processing of the
/// animations they describe. If an animation only targets a single node/entity, there is no need
/// for `AnimationHierarchy`.
///
/// ### Type parameters:
///
/// - `I`: identifier type for running animations, only one animation can be run at the same time
///        with the same id
/// - `T`: the component type that the animation should be applied to
#[derive(Default)]
pub struct AnimationControlSystem<I, T>
where
    I: Eq + Hash,
{
    m: marker::PhantomData<(I, T)>,
    next_id: u64,
    remove_ids: Vec<I>,
    state_set: FnvHashMap<I, f32>,
    deferred_start: Vec<(I, f32)>,
}

impl<I, T> AnimationControlSystem<I, T>
where
    I: Eq + Hash,
{
    pub fn new() -> Self {
        AnimationControlSystem {
            m: marker::PhantomData,
            next_id: 1,
            remove_ids: Vec::default(),
            state_set: FnvHashMap::default(),
            deferred_start: Vec::default(),
        }
    }
}

impl<'a, I, T> System<'a> for AnimationControlSystem<I, T>
where
    I: PartialEq + Eq + Hash + Copy + Send + Sync + 'static,
    T: AnimationSampling + Component + Clone,
{
    type SystemData = (
        Entities<'a>,
        Read<'a, AssetStorage<Animation<T>>>,
        Read<'a, AssetStorage<Sampler<T::Primitive>>>,
        WriteStorage<'a, AnimationControlSet<I, T>>,
        WriteStorage<'a, SamplerControlSet<T>>,
        ReadStorage<'a, AnimationHierarchy<T>>,
        ReadStorage<'a, T>,
        WriteStorage<'a, RestState<T>>,
        <T as ApplyData<'a>>::ApplyData,
    );

    fn run(&mut self, data: Self::SystemData) {
        let (
            entities,
            animation_storage,
            sampler_storage,
            mut controls,
            mut samplers,
            hierarchies,
            transforms,
            mut rest_states,
            apply_data,
        ) = data;
        let mut remove_sets = Vec::default();
        for (entity, control_set) in (&*entities, &mut controls).join() {
            self.remove_ids.clear();
            self.state_set.clear();
            let hierarchy = hierarchies.get(entity);
            for &mut (ref id, ref mut control) in control_set.animations.iter_mut() {
                let mut remove = false;
                if let Some(state) = animation_storage.get(&control.animation).and_then(
                    |animation| {
                        process_animation_control(
                            &entity,
                            animation,
                            control,
                            hierarchy,
                            &*sampler_storage,
                            &mut samplers,
                            &mut rest_states,
                            &transforms,
                            &mut remove,
                            &mut self.next_id,
                            &apply_data,
                        )
                    },
                ) {
                    control.state = state;
                }
                if let AnimationCommand::Step(_) = control.command {
                    control.command = AnimationCommand::Start;
                }
                if let AnimationCommand::SetInputValue(_) = control.command {
                    control.command = AnimationCommand::Start;
                }
                if remove {
                    self.remove_ids.push(*id);
                } else {
                    self.state_set.insert(
                        *id,
                        get_running_duration(&entity, control, hierarchies.get(entity), &samplers),
                    );
                }
            }
            for deferred_animation in &control_set.deferred_animations {
                self.state_set.insert(deferred_animation.animation_id, -1.0);
            }
            self.deferred_start.clear();
            for deferred_animation in &control_set.deferred_animations {
                let (start, start_dur) =
                    if let Some(dur) = self.state_set.get(&deferred_animation.relation.0) {
                        if *dur < 0. {
                            (false, 0.)
                        } else if let DeferStartRelation::Start(start_dur) =
                            deferred_animation.relation.1
                        {
                            let remain_dur = dur - start_dur;
                            (remain_dur >= 0., remain_dur)
                        } else {
                            (false, 0.)
                        }
                    } else {
                        (true, 0.)
                    };
                if start {
                    self.deferred_start
                        .push((deferred_animation.animation_id, start_dur));
                    self.state_set
                        .insert(deferred_animation.animation_id, start_dur);
                }
            }
            let mut next_id = self.next_id;
            for &(id, start_dur) in &self.deferred_start {
                let index = control_set
                    .deferred_animations
                    .iter()
                    .position(|a| a.animation_id == id)
                    .unwrap();
                let mut def = control_set.deferred_animations.remove(index);
                def.control.state = ControlState::Deferred(secs_to_duration(start_dur));
                def.control.command = AnimationCommand::Start;
                let mut remove = false;
                if let Some(state) = animation_storage.get(&def.control.animation).and_then(
                    |animation| {
                        process_animation_control(
                            &entity,
                            animation,
                            &mut def.control,
                            hierarchy,
                            &*sampler_storage,
                            &mut samplers,
                            &mut rest_states,
                            &transforms,
                            &mut remove,
                            &mut next_id,
                            &apply_data,
                        )
                    },
                ) {
                    def.control.state = state;
                }
                control_set.insert(id, def.control);
            }
            self.next_id = next_id;
            for id in &self.remove_ids {
                control_set.remove(*id);
                if control_set.is_empty() {
                    remove_sets.push(entity);
                }
            }
        }

        for entity in remove_sets {
            controls.remove(entity);
        }
    }

    fn setup(&mut self, res: &mut Resources) {
        Self::SystemData::setup(res);
        ReadStorage::<AnimationSet<I, T>>::setup(res);
    }
}

fn get_running_duration<T>(
    entity: &Entity,
    control: &AnimationControl<T>,
    hierarchy: Option<&AnimationHierarchy<T>>,
    samplers: &WriteStorage<SamplerControlSet<T>>,
) -> f32
where
    T: AnimationSampling,
{
    match &control.state {
        &ControlState::Running(_) => find_max_duration(
            control.id,
            samplers.get(*hierarchy
                .and_then(|h| h.nodes.values().next())
                .unwrap_or(entity)),
        ),
        _ => -1.0,
    }
}

fn find_max_duration<T>(control_id: u64, samplers: Option<&SamplerControlSet<T>>) -> f32
where
    T: AnimationSampling,
{
    samplers
        .and_then(|set| set.get_running_duration(control_id))
        .unwrap_or(0.)
}

/// Check if the given animation list is for a single node. If so, we don't need an
/// `AnimationHierarchy`.
fn only_one_index<C, P>(nodes: &[(usize, C, Handle<Sampler<P>>)]) -> bool
where
    P: InterpolationPrimitive,
{
    if nodes.is_empty() {
        true
    } else {
        let first = nodes[0].0;
        nodes.iter().all(|&(ref i, _, _)| *i == first)
    }
}

/// Process a single animation control object.
///
/// ## Parameters:
///
/// - `entity`: the entity the control object is active for
/// - `animation`: the animation the control is for
/// - `control`: animation control object
/// - `hierarchy`: the animation node hierarchy for the entity hierarchy the animation instance is
///                active for, if this is None the animation must be for a single node, which is the
///                local entity. If the animation contains more than a single node index, the
///                animation will be silently dropped.
/// - `sampler_storage`: `AssetStorage` for all `Sampler`s
/// - `samplers`: the active sampler sets
/// - `targets`: Target components, used to retrieve the rest pose before animation starts.
/// - `remove`: all entities pushed here will have the control object removed at the end of the system execution
/// - `next_id`: next id to use for the animation control id
///
/// ##
///
/// Optionally returns a new `ControlState` for the animation. This will be the new state of the
/// control object.
fn process_animation_control<T>(
    entity: &Entity,
    animation: &Animation<T>,
    control: &mut AnimationControl<T>,
    hierarchy: Option<&AnimationHierarchy<T>>,
    sampler_storage: &AssetStorage<Sampler<T::Primitive>>,
    samplers: &mut WriteStorage<SamplerControlSet<T>>,
    rest_states: &mut WriteStorage<RestState<T>>,
    targets: &ReadStorage<T>,
    remove: &mut bool,
    next_id: &mut u64,
    apply_data: &<T as ApplyData>::ApplyData,
) -> Option<ControlState>
where
    T: AnimationSampling + Component + Clone,
{
    // Checking hierarchy
    let h_fallback = AnimationHierarchy::new_single(animation.nodes[0].0, *entity);
    let hierarchy = match hierarchy {
        Some(h) => h,
        None => if only_one_index(&animation.nodes) {
            &h_fallback
        } else {
            error!(
                "Animation control which target multiple nodes without a hierarchy detected, dropping"
            );
            *remove = true;
            return None;
        },
    };
    match (&control.state, &control.command) {
        // Check for aborted or done animation
        (_, &AnimationCommand::Abort) | (&ControlState::Abort, _) | (&ControlState::Done, _) => {
            // signal samplers to abort, and remove control object if all samplers are done and removed
            if check_and_terminate_animation(control.id, hierarchy, samplers) {
                *remove = true;
            }
            Some(ControlState::Abort)
        }

        // Animation was just requested, start it
        // We ignore the command here because we need the animation to be
        // started before we can pause it, and to avoid a lot of checks for
        // abort. The command will be processed next frame.
        (&ControlState::Requested, &AnimationCommand::Start) => {
            control.id = *next_id;
            *next_id += 1;
            if start_animation(
                animation,
                sampler_storage,
                control,
                hierarchy,
                samplers,
                rest_states,
                targets,
                apply_data,
            ) {
                Some(ControlState::Running(Duration::from_secs(0)))
            } else {
                None // Try again next frame, might just be that samplers haven't finished loading
            }
        }

        (&ControlState::Deferred(..), &AnimationCommand::Start) => {
            control.id = *next_id;
            *next_id += 1;
            if start_animation(
                animation,
                sampler_storage,
                control,
                hierarchy,
                samplers,
                rest_states,
                targets,
                apply_data,
            ) {
                Some(ControlState::Running(Duration::from_secs(0)))
            } else {
                None // Try again next frame, might just be that samplers haven't finished loading
            }
        }

        // If pause was requested on a running animation, pause it
        (&ControlState::Running(..), &AnimationCommand::Pause) => {
            pause_animation(control.id, hierarchy, samplers);
            Some(ControlState::Paused(Duration::from_secs(0)))
        }

        // If start was requested on a paused animation, unpause it
        (&ControlState::Paused(_), &AnimationCommand::Start) => {
            unpause_animation(control.id, hierarchy, samplers);
            Some(ControlState::Running(Duration::from_secs(0)))
        }

        (&ControlState::Running(..), &AnimationCommand::Step(ref dir)) => {
            step_animation(control.id, hierarchy, samplers, sampler_storage, dir);
            None
        }

        (&ControlState::Running(..), &AnimationCommand::SetInputValue(value)) => {
            set_animation_input(control.id, hierarchy, samplers, value);
            None
        }

        (&ControlState::Running(..), &AnimationCommand::SetBlendWeights(ref weights)) => {
            set_blend_weights(control.id, hierarchy, samplers, weights);
            None
        }

        // check for finished/aborted animations, wait for samplers to signal done,
        // then remove control objects
        (&ControlState::Running(..), _) => {
            if check_termination(control.id, hierarchy, &samplers) {
                // Do termination
                for (_, node_entity) in &hierarchy.nodes {
                    let empty = samplers
                        .get_mut(*node_entity)
                        .map(|sampler| {
                            sampler.clear(control.id);
                            sampler.is_empty()
                        })
                        .unwrap_or(false);
                    if empty {
                        samplers.remove(*node_entity);
                    }
                }
                *remove = true;
            } else {
                update_animation_rate(control.id, hierarchy, samplers, control.rate_multiplier);
            }
            None
        }

        _ => None,
    }
}

/// Process animation creation request.
/// Will build `SamplerControlSet`s for the `AnimationHierarchy` given, based on the `Sampler`s in
/// the given `Animation`.
///
/// ## Parameters
///
/// - `animation`: the animation to start
/// - `sampler_storage`: all samplers
/// - `control`: the control object for the animation instance
/// - `hierarchy`: the animation node hierarchy for the entity hierarchy the animation instance is active for
/// - `samplers`: the active sampler sets
/// - `targets`: Target components, used to retrieve the rest pose before animation starts.
///
/// ## Returns
///
/// True if the animation was started, false if it wasn't.
fn start_animation<T>(
    animation: &Animation<T>,
    sampler_storage: &AssetStorage<Sampler<T::Primitive>>,
    control: &AnimationControl<T>,
    hierarchy: &AnimationHierarchy<T>,
    samplers: &mut WriteStorage<SamplerControlSet<T>>,
    rest_states: &mut WriteStorage<RestState<T>>,
    targets: &ReadStorage<T>, // for rest state
    apply_data: &<T as ApplyData>::ApplyData,
) -> bool
where
    T: AnimationSampling + Component + Clone,
{
    // check that hierarchy is valid, and all samplers exist
    if animation
        .nodes
        .iter()
        .any(|&(ref node_index, _, ref sampler_handle)| {
            !hierarchy.nodes.contains_key(node_index)
                || sampler_storage.get(sampler_handle).is_none()
        }) {
        return false;
    }

    hierarchy.rest_state(|entity| targets.get(entity).cloned(), rest_states);

    let start_state = if let ControlState::Deferred(dur) = control.state {
        ControlState::Deferred(dur)
    } else {
        ControlState::Requested
    };

    // setup sampler tree
    for &(ref node_index, ref channel, ref sampler_handle) in &animation.nodes {
        let node_entity = hierarchy.nodes.get(node_index).unwrap();
        let component = rest_states
            .get(*node_entity)
            .map(|r| r.state())
            .or_else(|| targets.get(*node_entity))
            .unwrap();
        let sampler_control = SamplerControl::<T> {
            control_id: control.id,
            channel: channel.clone(),
            state: start_state.clone(),
            sampler: sampler_handle.clone(),
            end: control.end.clone(),
            after: component.current_sample(channel, apply_data),
            rate_multiplier: control.rate_multiplier,
            blend_weight: 1.0,
        };
        let add = if let Some(ref mut set) = samplers.get_mut(*node_entity) {
            set.add_control(sampler_control);
            None
        } else {
            Some(sampler_control)
        };
        if let Some(sampler_control) = add {
            let mut set = SamplerControlSet::default();
            set.add_control(sampler_control);
            if let Err(err) = samplers.insert(*node_entity, set) {
                error!(
                    "Failed creating SamplerControl for AnimationHierarchy because: {}",
                    err
                );
            }
        }
    }
    true
}

fn pause_animation<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    samplers: &mut WriteStorage<SamplerControlSet<T>>,
) where
    T: AnimationSampling,
{
    for (_, node_entity) in &hierarchy.nodes {
        if let Some(ref mut s) = samplers.get_mut(*node_entity) {
            s.pause(control_id);
        }
    }
}

fn unpause_animation<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    samplers: &mut WriteStorage<SamplerControlSet<T>>,
) where
    T: AnimationSampling,
{
    for (_, node_entity) in &hierarchy.nodes {
        if let Some(ref mut s) = samplers.get_mut(*node_entity) {
            s.unpause(control_id);
        }
    }
}

fn step_animation<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    controls: &mut WriteStorage<SamplerControlSet<T>>,
    sampler_storage: &AssetStorage<Sampler<T::Primitive>>,
    direction: &StepDirection,
) where
    T: AnimationSampling,
{
    for (_, node_entity) in &hierarchy.nodes {
        if let Some(ref mut s) = controls.get_mut(*node_entity) {
            s.step(control_id, sampler_storage, direction);
        }
    }
}

fn set_animation_input<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    controls: &mut WriteStorage<SamplerControlSet<T>>,
    input: f32,
) where
    T: AnimationSampling,
{
    for (_, node_entity) in &hierarchy.nodes {
        if let Some(ref mut s) = controls.get_mut(*node_entity) {
            s.set_input(control_id, input);
        }
    }
}

fn set_blend_weights<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    controls: &mut WriteStorage<SamplerControlSet<T>>,
    weights: &Vec<(usize, T::Channel, f32)>,
) where
    T: AnimationSampling,
{
    for &(node_index, ref channel, weight) in weights {
        if let Some(node_entity) = hierarchy.nodes.get(&node_index) {
            if let Some(ref mut s) = controls.get_mut(*node_entity) {
                s.set_blend_weight(control_id, channel, weight);
            }
        }
    }
}

fn update_animation_rate<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    samplers: &mut WriteStorage<SamplerControlSet<T>>,
    rate_multiplier: f32,
) where
    T: AnimationSampling,
{
    for (_, node_entity) in &hierarchy.nodes {
        if let Some(ref mut s) = samplers.get_mut(*node_entity) {
            s.set_rate_multiplier(control_id, rate_multiplier);
        }
    }
}

/// Check if all nodes in an `AnimationHierarchy` are ready for termination, if so remove all
/// `SamplerControlSet`s for the hierarchy, if not request termination on all sampler controls
fn check_and_terminate_animation<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    samplers: &mut WriteStorage<SamplerControlSet<T>>,
) -> bool
where
    T: AnimationSampling,
{
    // Check for termination
    if check_termination(control_id, hierarchy, &samplers) {
        // Do termination
        for (_, node_entity) in &hierarchy.nodes {
            let empty = samplers
                .get_mut(*node_entity)
                .map(|sampler| {
                    sampler.clear(control_id);
                    sampler.is_empty()
                })
                .unwrap_or(false);
            if empty {
                samplers.remove(*node_entity);
            }
        }
        true
    } else {
        // Request termination of samplers
        for (_, node_entity) in &hierarchy.nodes {
            if let Some(ref mut s) = samplers.get_mut(*node_entity) {
                s.abort(control_id);
            }
        }
        false
    }
}

/// Check if all nodes in an `AnimationHierarcy` are ready for termination.
fn check_termination<T>(
    control_id: u64,
    hierarchy: &AnimationHierarchy<T>,
    samplers: &WriteStorage<SamplerControlSet<T>>,
) -> bool
where
    T: AnimationSampling,
{
    hierarchy
        .nodes
        .iter()
        .flat_map(|(_, node_entity)| samplers.get(*node_entity))
        .all(|s| s.check_termination(control_id))
}