use crate::style::Easing;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Tween {
from: i32,
to: i32,
start_ms: u32,
duration_ms: u16,
delay_ms: u16,
easing: Easing,
}
impl Tween {
#[inline(always)]
pub const fn new(value: i32) -> Self {
Self {
from: value,
to: value,
start_ms: 0,
duration_ms: 200,
delay_ms: 0,
easing: Easing::EaseOut,
}
}
#[inline(always)]
pub const fn with_easing(mut self, easing: Easing) -> Self {
self.easing = easing;
self
}
#[inline(always)]
pub const fn with_duration(mut self, duration_ms: u16) -> Self {
self.duration_ms = duration_ms;
self
}
#[inline(always)]
pub const fn with_delay(mut self, delay_ms: u16) -> Self {
self.delay_ms = delay_ms;
self
}
#[inline(always)]
pub const fn target(&self) -> i32 {
self.to
}
#[inline]
pub fn animate_to(&mut self, now_ms: u32, to: i32) {
if to == self.to {
return;
}
self.from = self.value(now_ms);
self.to = to;
self.start_ms = now_ms;
}
#[inline]
pub fn start(&mut self, now_ms: u32, from: i32, to: i32) {
self.from = from;
self.to = to;
self.start_ms = now_ms;
}
#[inline]
pub fn jump_to(&mut self, value: i32) {
self.from = value;
self.to = value;
}
#[inline]
pub fn progress_256(&self, now_ms: u32) -> i32 {
let elapsed = now_ms.wrapping_sub(self.start_ms);
if elapsed < self.delay_ms as u32 {
return 0;
}
self.easing
.sample_256(elapsed - self.delay_ms as u32, self.duration_ms as u32)
}
#[inline]
pub fn value(&self, now_ms: u32) -> i32 {
if self.from == self.to {
return self.to;
}
let t = self.progress_256(now_ms) as i64;
(self.from as i64 + (((self.to as i64 - self.from as i64) * t) >> 8)) as i32
}
#[inline]
pub fn is_running(&self, now_ms: u32) -> bool {
self.from != self.to
&& now_ms.wrapping_sub(self.start_ms) < self.delay_ms as u32 + self.duration_ms as u32
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Presence {
tween: Tween,
}
impl Presence {
#[inline(always)]
pub const fn new(visible: bool, duration_ms: u16) -> Self {
Self {
tween: Tween::new(if visible { 256 } else { 0 }).with_duration(duration_ms),
}
}
#[inline(always)]
pub const fn with_easing(mut self, easing: Easing) -> Self {
self.tween = self.tween.with_easing(easing);
self
}
#[inline(always)]
pub const fn is_visible(&self) -> bool {
self.tween.target() == 256
}
#[inline]
pub fn show(&mut self, now_ms: u32) {
self.tween.animate_to(now_ms, 256);
}
#[inline]
pub fn hide(&mut self, now_ms: u32) {
self.tween.animate_to(now_ms, 0);
}
#[inline]
pub fn toggle(&mut self, now_ms: u32) {
if self.is_visible() {
self.hide(now_ms);
} else {
self.show(now_ms);
}
}
#[inline]
pub fn set_visible(&mut self, now_ms: u32, visible: bool) {
if visible {
self.show(now_ms);
} else {
self.hide(now_ms);
}
}
#[inline]
pub fn is_mounted(&self, now_ms: u32) -> bool {
self.is_visible() || self.tween.value(now_ms) > 0
}
#[inline]
pub fn is_animating(&self, now_ms: u32) -> bool {
self.tween.is_running(now_ms)
}
#[inline]
pub fn progress_256(&self, now_ms: u32) -> i32 {
self.tween.value(now_ms)
}
#[inline]
pub fn opacity(&self, now_ms: u32) -> u8 {
((self.progress_256(now_ms).clamp(0, 256) * 255) >> 8) as u8
}
#[inline]
pub fn offset(&self, now_ms: u32, distance: i16) -> i16 {
let rem = 256 - self.progress_256(now_ms);
((distance as i32 * rem) >> 8) as i16
}
}