Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion events on all Tweenables #69

Open
Xion opened this issue Nov 5, 2022 · 1 comment
Open

Completion events on all Tweenables #69

Xion opened this issue Nov 5, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@Xion
Copy link

Xion commented Nov 5, 2022

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.

@djeedai djeedai added the enhancement New feature or request label Nov 6, 2022
@djeedai
Copy link
Owner

djeedai commented Nov 6, 2022

That sounds reasonable I think.

djeedai added a commit that referenced this issue Nov 9, 2022
Change `Delay` into `Delay<T>`, and add support for raising a completion
event or callback.

Bug: #69
djeedai added a commit that referenced this issue Nov 9, 2022
Change `Delay` into `Delay<T>`, and add support for raising a completion
event or callback.

Bug: #69
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants