use std::sync::Arc;
use crate::{
error::CommandError,
manager::command::{producer::CommandProducer, ClockCommand, Command},
tween::Tween,
ClockSpeed,
};
use super::{ClockId, ClockShared, ClockTime};
pub struct ClockHandle {
pub(crate) id: ClockId,
pub(crate) shared: Arc<ClockShared>,
pub(crate) command_producer: CommandProducer,
}
impl ClockHandle {
pub fn id(&self) -> ClockId {
self.id
}
pub fn ticking(&self) -> bool {
self.shared.ticking()
}
pub fn time(&self) -> ClockTime {
ClockTime {
clock: self.id,
ticks: self.shared.ticks(),
}
}
pub fn fractional_position(&self) -> f64 {
self.shared.fractional_position()
}
pub fn set_speed(
&self,
speed: impl Into<ClockSpeed>,
tween: Tween,
) -> Result<(), CommandError> {
self.command_producer
.push(Command::Clock(ClockCommand::SetSpeed(
self.id,
speed.into(),
tween,
)))
}
pub fn start(&self) -> Result<(), CommandError> {
self.command_producer
.push(Command::Clock(ClockCommand::Start(self.id)))
}
pub fn pause(&self) -> Result<(), CommandError> {
self.command_producer
.push(Command::Clock(ClockCommand::Pause(self.id)))
}
pub fn stop(&self) -> Result<(), CommandError> {
self.command_producer
.push(Command::Clock(ClockCommand::Stop(self.id)))
}
}
impl Drop for ClockHandle {
fn drop(&mut self) {
self.shared.mark_for_removal();
}
}
impl From<&ClockHandle> for ClockId {
fn from(handle: &ClockHandle) -> Self {
handle.id()
}
}