use crate::error::{Error, Result};
use crate::picture::Picture;
use thorvg_sys as sys;
pub struct Animation<'eng> {
raw: sys::Tvg_Animation,
_engine: core::marker::PhantomData<&'eng ()>,
}
unsafe impl Send for Animation<'_> {}
impl Animation<'_> {
pub(crate) fn raw(&self) -> sys::Tvg_Animation {
self.raw
}
pub(crate) unsafe fn from_raw(raw: sys::Tvg_Animation) -> Self {
Self {
raw,
_engine: core::marker::PhantomData,
}
}
pub(crate) fn new() -> Self {
let raw = unsafe { sys::tvg_animation_new() };
assert!(!raw.is_null(), "failed to create Animation");
Self {
raw,
_engine: core::marker::PhantomData,
}
}
pub fn picture(&self) -> Picture<'_> {
let raw = unsafe { sys::tvg_animation_get_picture(self.raw) };
assert!(!raw.is_null(), "animation has no picture");
unsafe { Picture::from_raw(raw, false) }
}
pub fn set_frame(&mut self, frame: f32) -> Result<()> {
Error::from_raw(unsafe { sys::tvg_animation_set_frame(self.raw, frame) })
}
pub fn frame(&self) -> Result<f32> {
let mut frame: f32 = 0.0;
Error::from_raw(unsafe { sys::tvg_animation_get_frame(self.raw, &raw mut frame) })?;
Ok(frame)
}
pub fn total_frame(&self) -> Result<f32> {
let mut cnt: f32 = 0.0;
Error::from_raw(unsafe { sys::tvg_animation_get_total_frame(self.raw, &raw mut cnt) })?;
Ok(cnt)
}
pub fn duration(&self) -> Result<f32> {
let mut duration: f32 = 0.0;
Error::from_raw(unsafe { sys::tvg_animation_get_duration(self.raw, &raw mut duration) })?;
Ok(duration)
}
pub fn set_segment(&mut self, begin: f32, end: f32) -> Result<()> {
Error::from_raw(unsafe { sys::tvg_animation_set_segment(self.raw, begin, end) })
}
pub fn segment(&self) -> Result<(f32, f32)> {
let (mut begin, mut end) = (0.0f32, 0.0f32);
Error::from_raw(unsafe {
sys::tvg_animation_get_segment(self.raw, &raw mut begin, &raw mut end)
})?;
Ok((begin, end))
}
}
impl Drop for Animation<'_> {
fn drop(&mut self) {
unsafe {
sys::tvg_animation_del(self.raw);
}
}
}
impl core::fmt::Debug for Animation<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Animation").finish_non_exhaustive()
}
}