use crate::design::Rgb;
mod engine;
pub use engine::InkEngine;
pub const MAX_SHAPES: usize = 512;
pub const MAX_BEADS: usize = 64;
pub const LITE_ENTER_MS: f64 = 26.0;
pub const LITE_LEAVE_MS: f64 = 17.5;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Effect {
Mark,
Text,
Wipe,
Drops,
Wobble,
Loader,
Pour,
Cursor,
Ripple,
Count,
Reveal,
}
impl Effect {
#[must_use]
pub const fn all() -> [Self; 11] {
[
Self::Mark,
Self::Text,
Self::Wipe,
Self::Drops,
Self::Wobble,
Self::Loader,
Self::Pour,
Self::Cursor,
Self::Ripple,
Self::Count,
Self::Reveal,
]
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Mark => "mark",
Self::Text => "text",
Self::Wipe => "wipe",
Self::Drops => "drops",
Self::Wobble => "wobble",
Self::Loader => "loader",
Self::Pour => "pour",
Self::Cursor => "cursor",
Self::Ripple => "ripple",
Self::Count => "count",
Self::Reveal => "reveal",
}
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Shape {
Circle {
centre_x: f64,
centre_y: f64,
radius: f64,
},
Ellipse {
centre_x: f64,
centre_y: f64,
radius_x: f64,
radius_y: f64,
},
Quad {
points: [[f64; 2]; 4],
},
Rect {
left: f64,
top: f64,
right: f64,
bottom: f64,
},
}
impl Shape {
fn mix(self, digest: &mut u64) {
let mut mix = |value: f64| {
*digest = (*digest ^ value.to_bits()).wrapping_mul(0x0000_0100_0000_01b3);
};
match self {
Self::Circle {
centre_x,
centre_y,
radius,
} => {
mix(1.0);
mix(centre_x);
mix(centre_y);
mix(radius);
}
Self::Ellipse {
centre_x,
centre_y,
radius_x,
radius_y,
} => {
mix(2.0);
mix(centre_x);
mix(centre_y);
mix(radius_x);
mix(radius_y);
}
Self::Quad { points } => {
mix(3.0);
for point in points {
mix(point[0]);
mix(point[1]);
}
}
Self::Rect {
left,
top,
right,
bottom,
} => {
mix(4.0);
mix(left);
mix(top);
mix(right);
mix(bottom);
}
}
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Frame {
shapes: Vec<Shape>,
pub alpha: f64,
pub blur: f64,
pub displacement: f64,
label: Option<String>,
}
impl Frame {
#[must_use]
fn resolved() -> Self {
Self {
shapes: Vec::new(),
alpha: 1.0,
blur: 0.0,
displacement: 0.0,
label: None,
}
}
#[must_use]
pub fn shapes(&self) -> &[Shape] {
&self.shapes
}
#[must_use]
pub fn label(&self) -> Option<&str> {
self.label.as_deref()
}
#[must_use]
pub fn digest(&self) -> u64 {
let mut digest: u64 = 0xcbf2_9ce4_8422_2325;
for shape in &self.shapes {
shape.mix(&mut digest);
}
for value in [self.alpha, self.blur, self.displacement] {
digest = (digest ^ value.to_bits()).wrapping_mul(0x0000_0100_0000_01b3);
}
if let Some(label) = self.label.as_deref() {
for byte in label.bytes() {
digest = (digest ^ u64::from(byte)).wrapping_mul(0x0000_0100_0000_01b3);
}
}
digest
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum InkError {
InvalidDelta,
InvalidInput,
InvalidOption(&'static str),
}
impl std::fmt::Display for InkError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Self::InvalidDelta => {
formatter.write_str("frame delta must be finite and non-negative")
}
Self::InvalidInput => {
formatter.write_str("pointer and click coordinates must be finite")
}
Self::InvalidOption(field) => {
write!(formatter, "option `{field}` is outside its domain")
}
}
}
}
impl std::error::Error for InkError {}
pub const MAX_OPTION_MS: f64 = 3_600_000.0;
pub const MAX_OPTION_PIXELS: f64 = 10_000.0;
pub const MAX_OPTION_AMOUNT: f64 = 1_000.0;
pub const MAX_COUNT_MAGNITUDE: f64 = 1.0e15;
pub const MAX_COUNT_DECIMALS: usize = 12;
pub const MAX_CANVAS: f64 = 16_384.0;
fn within(value: Option<f64>, low: f64, high: f64, field: &'static str) -> Result<(), InkError> {
match value {
Some(number) if !(number.is_finite() && number >= low && number <= high) => {
Err(InkError::InvalidOption(field))
}
Some(_) | None => Ok(()),
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct Input {
pub pointer: Option<(f64, f64)>,
pub click: Option<(f64, f64)>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Quality {
Auto,
Lite,
Full,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Canvas {
pub width: f64,
pub height: f64,
}
impl Canvas {
#[must_use]
pub fn new(width: f64, height: f64) -> Self {
Self {
width: clamp_axis(width),
height: clamp_axis(height),
}
}
}
fn clamp_axis(value: f64) -> f64 {
if value.is_nan() {
4.0
} else {
value.clamp(4.0, MAX_CANVAS)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Options {
pub ink: Option<Rgb>,
pub accent: Option<Rgb>,
pub duration_ms: Option<f64>,
pub hold_ms: Option<f64>,
pub loop_forever: Option<bool>,
pub stagger_ms: Option<f64>,
pub blur: Option<f64>,
pub displacement: Option<f64>,
pub level: Option<f64>,
pub beads: Option<usize>,
pub amount: Option<f64>,
pub hover: Option<f64>,
pub size: Option<f64>,
pub spout: Option<f64>,
pub auto_ms: Option<f64>,
pub count_to: Option<f64>,
pub count_from: Option<f64>,
pub count_decimals: Option<usize>,
pub count_prefix: Option<&'static str>,
pub count_suffix: Option<&'static str>,
}
impl Options {
#[must_use]
pub const fn new() -> Self {
Self {
ink: None,
accent: None,
duration_ms: None,
hold_ms: None,
loop_forever: None,
stagger_ms: None,
blur: None,
displacement: None,
level: None,
beads: None,
amount: None,
hover: None,
size: None,
spout: None,
auto_ms: None,
count_to: None,
count_from: None,
count_decimals: None,
count_prefix: None,
count_suffix: None,
}
}
}
impl Options {
pub fn validate(&self) -> Result<(), InkError> {
within(self.duration_ms, 0.0, MAX_OPTION_MS, "duration_ms")?;
within(self.hold_ms, 0.0, MAX_OPTION_MS, "hold_ms")?;
within(self.stagger_ms, 0.0, MAX_OPTION_MS, "stagger_ms")?;
within(self.auto_ms, 0.0, MAX_OPTION_MS, "auto_ms")?;
within(self.blur, 0.0, MAX_OPTION_PIXELS, "blur")?;
within(self.displacement, 0.0, MAX_OPTION_PIXELS, "displacement")?;
within(self.size, 0.0, MAX_OPTION_PIXELS, "size")?;
within(self.level, 0.0, 1.0, "level")?;
within(self.spout, 0.0, 1.0, "spout")?;
within(self.amount, 0.0, MAX_OPTION_AMOUNT, "amount")?;
within(self.hover, 0.0, MAX_OPTION_AMOUNT, "hover")?;
within(
self.count_to,
-MAX_COUNT_MAGNITUDE,
MAX_COUNT_MAGNITUDE,
"count_to",
)?;
within(
self.count_from,
-MAX_COUNT_MAGNITUDE,
MAX_COUNT_MAGNITUDE,
"count_from",
)?;
if self
.count_decimals
.is_some_and(|decimals| decimals > MAX_COUNT_DECIMALS)
{
lgwks_std::trace::debug!(
decimals = ?self.count_decimals,
max = MAX_COUNT_DECIMALS,
"ink option refused: count_decimals is past its bound"
);
return Err(InkError::InvalidOption("count_decimals"));
}
Ok(())
}
}
impl Default for Options {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HostId(pub u32);
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct HostSpec {
pub effect: Effect,
pub canvas: Canvas,
pub visible: bool,
}
impl HostSpec {
#[must_use]
pub const fn new(effect: Effect, canvas: Canvas, visible: bool) -> Self {
Self {
effect,
canvas,
visible,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Stats {
pub frames: u64,
pub live: u32,
pub lite: bool,
pub ema_ms: f64,
pub stride_skips: u64,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Render {
pub host: HostId,
pub frame: Frame,
}