use crate::error::{Error, Result};
use crate::picture::Picture;
use thorvg_sys as sys;
pub struct Animation<'eng> {
raw: sys::Tvg_Animation,
picture: Picture<'eng>,
_engine: core::marker::PhantomData<&'eng ()>,
}
unsafe impl Send for Animation<'_> {}
impl<'eng> Animation<'eng> {
pub(crate) fn raw(&self) -> sys::Tvg_Animation {
self.raw
}
pub(crate) fn into_raw(self) -> sys::Tvg_Animation {
let me = core::mem::ManuallyDrop::new(self);
me.raw
}
pub(crate) unsafe fn from_raw(raw: sys::Tvg_Animation) -> Self {
let pic_raw = unsafe { sys::tvg_animation_get_picture(raw) };
assert!(!pic_raw.is_null(), "animation has no picture");
Self {
raw,
picture: unsafe { Picture::from_raw(pic_raw, false) },
_engine: core::marker::PhantomData,
}
}
pub(crate) fn new() -> Result<Self> {
let raw = unsafe { sys::tvg_animation_new() };
if raw.is_null() {
return Err(Error::FailedAllocation);
}
Ok(unsafe { Self::from_raw(raw) })
}
pub fn picture(&self) -> &Picture<'eng> {
&self.picture
}
pub fn picture_mut(&mut self) -> &mut Picture<'eng> {
&mut self.picture
}
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()
}
}