use std::marker::PhantomData;
use crate::{
Animatable, AnimationCommand, AnimationState, IntoMotionAnimation, MotionError, MotionRuntime,
};
use super::{MotionId, PlaybackId};
#[must_use = "a motion handle is required to access the runtime-managed animation"]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Motion<T> {
id: MotionId,
marker: PhantomData<fn() -> T>,
}
impl<T> Copy for Motion<T> {}
impl<T> Clone for Motion<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Motion<T> {
pub(super) fn new(id: MotionId, marker: PhantomData<fn() -> T>) -> Self {
Self { id, marker }
}
pub(super) const fn id(self) -> MotionId {
self.id
}
#[must_use]
pub const fn motion_id(self) -> MotionId {
self.id
}
}
impl<T: Animatable> Motion<T> {
pub fn transition_to(self, target: T, runtime: &mut MotionRuntime) -> Result<(), MotionError> {
runtime.transition_to(self, target)
}
pub fn transition_to_tracked(
self,
target: T,
runtime: &mut MotionRuntime,
) -> Result<PlaybackId, MotionError> {
runtime.transition_to_tracked(self, target)
}
pub fn play<P, Kind>(self, playback: P, runtime: &mut MotionRuntime) -> Result<(), MotionError>
where
P: IntoMotionAnimation<T, Kind>,
{
runtime.play(self, playback)
}
pub fn play_tracked<P, Kind>(
self,
playback: P,
runtime: &mut MotionRuntime,
) -> Result<PlaybackId, MotionError>
where
P: IntoMotionAnimation<T, Kind>,
{
runtime.play_tracked(self, playback)
}
pub fn value(self, runtime: &MotionRuntime) -> Result<T, MotionError> {
runtime.value(self).cloned()
}
pub fn value_ref(self, runtime: &MotionRuntime) -> Result<&T, MotionError> {
runtime.value(self)
}
pub fn state(self, runtime: &MotionRuntime) -> Result<AnimationState, MotionError> {
runtime.state(self)
}
pub fn playback(self, runtime: &MotionRuntime) -> Result<PlaybackId, MotionError> {
runtime.playback(self)
}
pub fn is_active(self, runtime: &MotionRuntime) -> Result<bool, MotionError> {
runtime.is_active(self)
}
pub fn is_completed(self, runtime: &MotionRuntime) -> Result<bool, MotionError> {
self.state(runtime)
.map(|state| state == AnimationState::Completed)
}
pub fn pause(self, runtime: &mut MotionRuntime) -> Result<(), MotionError> {
runtime.command(self, AnimationCommand::Pause)
}
pub fn resume(self, runtime: &mut MotionRuntime) -> Result<(), MotionError> {
runtime.command(self, AnimationCommand::Resume)
}
pub fn cancel(self, runtime: &mut MotionRuntime) -> Result<(), MotionError> {
runtime.command(self, AnimationCommand::Cancel)
}
pub fn seek(self, progress: f32, runtime: &mut MotionRuntime) -> Result<(), MotionError> {
runtime.command(self, AnimationCommand::Seek(progress))
}
pub fn finish(self, runtime: &mut MotionRuntime) -> Result<(), MotionError> {
runtime.command(self, AnimationCommand::Finish)
}
pub fn remove(self, runtime: &mut MotionRuntime) -> Result<(), MotionError> {
runtime.remove(self)
}
}