Skip to content

Completion events on all Tweenables #69

@Xion

Description

@Xion

set_completed_event/with_completed_event are currently only available on the Tween type. It'd be nice to have them on any Tweenable, but especially on the compound ones: Sequence and Tracks.

My current workaround for this is to attach the completion to the last tweenable in sequence. This is pretty awkward, because you have to do it before you type-erase it, e.g.:

// `transitions` is Vec<(Duration, /* target alpha */ f32)>
let mut sequence: Vec<BoxedTweenable<Sprite>> = Vec::with_capacity(transitions.len());

let mut current_alpha = initial;
for (i, &(duration, end_alpha)) in transitions.iter().enumerate() {
    if (end_alpha - current_alpha).abs() < 0.01 {
        sequence.push(Box::new(Delay::new(duration)));
    } else {
        let lens = SpriteColorLens {
            start: fade_sprite_color(current_alpha),
            end: fade_sprite_color(end_alpha),
        };

        let mut tween = Tween::new(EaseMethod::Linear, TweeningType::Once, duration, lens);
        if i == transitions.len() - 1 {
            tween.set_completed_event(TWEEN_COMPLETION_COOKIE);
        }

        sequence.push(Box::new(tween));
    }
    current_alpha = end_alpha;
}

Sequence::new(sequence)

This also doesn't work if the last tweenable is Delay. For that case, you need to turn your Sequence into Tracks, with a parallel Tween that does nothing but provide the completion event. This might require a custom NoopLens<T: Component> type.

All in all, pretty messy. Having set_completed_event on more tweenable types would eliminate the need for tricks like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions