token_value_map/lib.rs
1//! A time-based data mapping library for animation and interpolation.
2//!
3//! This crate provides types for storing and manipulating data that changes
4//! over time. It supports both uniform (static) and animated (time-varying)
5//! values with automatic interpolation between keyframes.
6//!
7//! # Core Types
8//!
9//! - [`Value`]: A value that can be either uniform or animated over time.
10//! - [`Data`]: A variant enum containing all supported data types.
11//! - [`AnimatedData`]: Time-indexed data with interpolation support.
12//! - [`TimeDataMap`]: A mapping from time to data values.
13//! - [`TokenValueMap`]: A collection of named values indexed by tokens.
14//!
15//! # Data Types
16//!
17//! The library supports scalar types ([`Boolean`], [`Integer`], [`Real`],
18//! [`String`]), vector types ([`Vector2`], [`Vector3`], [`Color`],
19//! [`Matrix3`]), and collections of these types ([`BooleanVec`],
20//! [`IntegerVec`], etc.).
21//!
22//! # Motion Blur Sampling
23//!
24//! Use the [`Sample`] trait with a [`Shutter`] to generate motion blur samples
25//! for animated values during rendering.
26//!
27//! # Interpolation (Optional Feature)
28//!
29//! When the `interpolation` feature is enabled, [`TimeDataMap`] supports
30//! advanced interpolation modes including bezier curves with tangent control.
31//! This enables integration with professional animation systems like Dopamine.
32//!
33//! # Examples
34//!
35//! ```rust
36//! use frame_tick::Tick;
37//! use token_value_map::*;
38//!
39//! // Create a uniform value.
40//! let uniform = Value::uniform(42.0);
41//!
42//! // Create an animated value.
43//! let animated =
44//! Value::animated(vec![(Tick::new(0), 0.0), (Tick::new(10), 10.0)])
45//! .unwrap();
46//!
47//! // Sample at a specific time.
48//! let interpolated = animated.interpolate(Tick::new(5));
49//! ```
50
51#[cfg(feature = "facet")]
52use facet::Facet;
53#[cfg(feature = "builtin-types")]
54use function_name::named;
55#[cfg(feature = "rkyv")]
56use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
57#[cfg(feature = "serde")]
58use serde::{Deserialize, Serialize};
59use smallvec::SmallVec;
60use std::fmt::Debug;
61use std::hash::Hash;
62#[cfg(feature = "builtin-types")]
63use std::hash::Hasher;
64
65// Internal macro module.
66#[cfg(feature = "builtin-types")]
67mod macros;
68#[cfg(feature = "builtin-types")]
69use macros::impl_sample_for_value;
70
71// Math backend abstraction.
72#[cfg(feature = "builtin-types")]
73pub mod math;
74
75// Built-in types (feature-gated).
76#[cfg(feature = "builtin-types")]
77mod animated_data;
78#[cfg(feature = "builtin-types")]
79mod data;
80#[cfg(feature = "builtin-types")]
81mod data_types;
82#[cfg(feature = "builtin-types")]
83mod token_value_map;
84#[cfg(feature = "builtin-types")]
85mod value;
86
87// Generic types (always available).
88mod define_data_macro;
89mod generic_token_value_map;
90mod generic_value;
91mod traits;
92
93// Token type.
94mod token;
95
96// Other modules.
97#[cfg(feature = "egui-keyframe")]
98mod egui_keyframe_integration;
99mod error;
100#[cfg(feature = "interpolation")]
101mod interpolation;
102#[cfg(all(feature = "lua", feature = "builtin-types"))]
103mod lua;
104mod shutter;
105mod time_data_map;
106
107// Re-exports: built-in types (feature-gated).
108#[cfg(feature = "builtin-types")]
109pub use animated_data::*;
110#[cfg(feature = "builtin-types")]
111pub use data::*;
112#[cfg(feature = "builtin-types")]
113pub use data_types::*;
114#[cfg(feature = "builtin-types")]
115pub use token_value_map::*;
116#[cfg(feature = "builtin-types")]
117pub use value::*;
118
119// Re-exports: always available.
120pub use error::*;
121pub use generic_token_value_map::*;
122pub use generic_value::*;
123#[cfg(feature = "interpolation")]
124pub use interpolation::*;
125#[cfg(all(feature = "lua", feature = "builtin-types"))]
126pub use lua::*;
127pub use shutter::*;
128pub use time_data_map::*;
129pub use token::*;
130pub use traits::*;
131
132/// A time value represented as a fixed-point [`Tick`](frame_tick::Tick).
133pub type Time = frame_tick::Tick;
134
135/// Trait for getting data type information.
136///
137/// This trait is only available with the `builtin-types` feature.
138#[cfg(feature = "builtin-types")]
139pub trait DataTypeOps {
140 /// Returns the [`DataType`] variant for this value.
141 fn data_type(&self) -> DataType;
142 /// Returns a string name for this data type.
143 fn type_name(&self) -> &'static str;
144}
145
146#[cfg(feature = "builtin-types")]
147impl DataTypeOps for Value {
148 fn data_type(&self) -> DataType {
149 match self {
150 Value::Uniform(data) => data.data_type(),
151 Value::Animated(animated_data) => animated_data.data_type(),
152 }
153 }
154
155 fn type_name(&self) -> &'static str {
156 match self {
157 Value::Uniform(data) => data.type_name(),
158 Value::Animated(animated_data) => animated_data.type_name(),
159 }
160 }
161}