pub struct Animation { /* private fields */ }
Expand description
Represents an animation: a collection of ordered Segment
to be run in sequence.
- An animation can be played, paused, resumed, and stopped.
- Each
Segment
in the animation is played in order (eventually looped, seeSegment
) until all are done.
§Example
Here is an example of an animation made of a single segment that sweeps a servo indefinitely,
from position 0° to 180° and back:
(sidenote: prefer use Servo::sweep()
helper for this purpose).
use hermes_five::pause;
use hermes_five::animations::{Animation, Easing, Keyframe, Segment, Track};
use hermes_five::hardware::{Board, BoardEvent};
use hermes_five::devices::Servo;
#[hermes_five::runtime]
async fn main() {
let board = Board::run();
board.on(BoardEvent::OnReady, |board: Board| async move {
let servo = Servo::new(&board, 9, 0).unwrap();
let mut animation = Animation::from(
Segment::from(
Track::new(servo)
.with_keyframe(Keyframe::new(180, 0, 500).set_transition(Easing::SineInOut))
.with_keyframe(Keyframe::new(90, 1000, 2000).set_transition(Easing::SineInOut)),
)
.set_fps(100)
.set_repeat(true)
);
animation.play();
pause!(3000);
animation.stop();
Ok(())
});
}
Implementations§
Source§impl Animation
impl Animation
Sourcepub fn play(&mut self) -> &mut Self
pub fn play(&mut self) -> &mut Self
Starts or resumes the animation.
The animation will start from the current segment or from the beginning if it was stopped.
Sourcepub fn pause(&mut self) -> &mut Self
pub fn pause(&mut self) -> &mut Self
Pauses the animation.
When resumed, the animation will continue from the point it was paused.
Sourcepub fn next(&mut self) -> &mut Self
pub fn next(&mut self) -> &mut Self
Skips the current sequence and jump to next.
Caveats:
- Skipping the current segment does not pause / resume the animation: if it was running, it continues to do so (from the beginning of next segment).
- If already on the last segment, the animation loop to the first one, hence, restart.
Sourcepub fn stop(&mut self) -> &mut Self
pub fn stop(&mut self) -> &mut Self
Stops the animation.
The animation will be reset to the beginning. The current segment is reset, and the index is set to 0.
Sourcepub fn is_playing(&self) -> bool
pub fn is_playing(&self) -> bool
Indicates if the animation is currently playing.
Sourcepub fn get_duration(&self) -> u64
pub fn get_duration(&self) -> u64
Gets the total duration of the animation.
The duration is determined by the sum of segment durations or u64::MAX if a segment is on repeat mode.
Sourcepub fn get_progress(&self) -> u64
pub fn get_progress(&self) -> u64
Gets the current play time. @todo fix: because we clone self on .play(), the progress is no longer available on segment.
Sourcepub fn get_segments(&self) -> &Vec<Segment>
pub fn get_segments(&self) -> &Vec<Segment>
Returns the list of segments in the animation.
Sourcepub fn set_segments(self, segments: Vec<Segment>) -> Self
pub fn set_segments(self, segments: Vec<Segment>) -> Self
Sets the list of segments for the animation.
Sourcepub fn with_segment(self, segment: Segment) -> Self
pub fn with_segment(self, segment: Segment) -> Self
Adds a new segment to the animation.
This segment will be enqueued at the end of current segment list.
Sourcepub fn get_current(&self) -> usize
pub fn get_current(&self) -> usize
Returns the index of the currently running segment.
Sourcepub fn set_current(&self, index: usize)
pub fn set_current(&self, index: usize)
Sets the index of the currently running segment.
Sourcepub fn on<S, F, T, Fut>(&self, event: S, callback: F) -> EventHandler
pub fn on<S, F, T, Fut>(&self, event: S, callback: F) -> EventHandler
Registers a callback to be executed on a given event.
Available events for an animation are defined by the enum: AnimationEvent
:
OnSegmentDone
|segment_done
: Triggered when a segment is done.
The callback must receive the following parameter:|_: Segment| { ... }
OnStart
|start
: Triggered when the animation starts.
The callback must receive the following parameter:|_: Animation| { ... }
OnComplete
|complete
: Triggered when the animation ends.
The callback must receive the following parameter:|_: Animation| { ... }
§Example
use hermes_five::hardware::Board;
use hermes_five::hardware::BoardEvent;
use hermes_five::devices::{Output, Led};
use hermes_five::animations::{Animation, AnimationEvent, Easing};
#[hermes_five::runtime]
async fn main() {
let board = Board::run();
board.on(BoardEvent::OnReady, |board: Board| async move {
let mut led = Led::new(&board, 11, false)?;
// This is a dummy animation (does nothing).
let animation = Animation::default();
animation.on(AnimationEvent::OnStart, |_: Animation| async move {
println!("Animation has started");
Ok(())
});
animation.on(AnimationEvent::OnComplete, |_: Animation| async move {
println!("Animation done");
Ok(())
});
Ok(())
});
}