use lgwks_std::id::Uuid;
use super::easing::Easing;
use super::error::{MotionError, MotionErrorCode};
use super::graph::MotionGraph;
use super::node::accumulator::AccumulatorStore;
use super::node::tween::TweenComponent;
use super::node::{MotionNode, NodeKind};
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct PatchOp {
path: String,
value: PatchValue,
}
impl PatchOp {
#[must_use]
pub fn new(path: String, value: PatchValue) -> Self {
Self { path, value }
}
#[must_use]
pub fn path(&self) -> &str {
&self.path
}
#[must_use]
pub const fn value(&self) -> &PatchValue {
&self.value
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum PatchValue {
Float(f32),
Easing(Easing),
Bool(bool),
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct MotionPatch {
patch_id: Uuid,
node_id: Uuid,
ops: Vec<PatchOp>,
preserve_velocity: bool,
}
impl MotionPatch {
#[must_use]
pub fn new(patch_id: Uuid, node_id: Uuid, ops: Vec<PatchOp>, preserve_velocity: bool) -> Self {
Self {
patch_id,
node_id,
ops,
preserve_velocity,
}
}
#[must_use]
pub const fn patch_id(&self) -> Uuid {
self.patch_id
}
#[must_use]
pub const fn node_id(&self) -> Uuid {
self.node_id
}
#[must_use]
pub fn ops(&self) -> &[PatchOp] {
&self.ops
}
#[must_use]
pub const fn preserve_velocity(&self) -> bool {
self.preserve_velocity
}
}
static IMMUTABLE_PATHS: &[&str] = &["kind", "node_id", "accumulator_kind"];
pub fn apply_patch(
graph: MotionGraph,
patch: &MotionPatch,
accumulators: &mut AccumulatorStore,
) -> Result<MotionGraph, MotionError> {
validate_patch_paths(patch)?;
let node = graph.get_node(&patch.node_id).ok_or_else(|| {
MotionError::new(MotionErrorCode::NodeNotFound)
.with_detail("patch references unknown node_id")
})?;
for op in &patch.ops {
validate_op_against_node(op, node)?;
}
let updated_node = apply_ops_to_node(node.clone(), patch)?;
if patch.preserve_velocity && matches!(updated_node.kind(), NodeKind::Accumulator(_)) {
preserve_accumulator_state(&patch.node_id, accumulators);
}
graph.replace_node(updated_node)
}
fn validate_patch_paths(patch: &MotionPatch) -> Result<(), MotionError> {
for op in &patch.ops {
MotionError::require(
!IMMUTABLE_PATHS.contains(&op.path.as_str()),
MotionErrorCode::PatchPathImmutable,
"attempted to patch an immutable field",
)?;
}
Ok(())
}
fn validate_op_against_node(op: &PatchOp, node: &MotionNode) -> Result<(), MotionError> {
match *node.kind() {
NodeKind::Tween(_) => validate_tween_op(op),
NodeKind::Accumulator(_) => validate_accumulator_op(op),
}
}
fn validate_tween_op(op: &PatchOp) -> Result<(), MotionError> {
const VALID_PATHS: &[&str] = &["from", "to", "duration_ms", "start_ms", "easing"];
MotionError::require(
VALID_PATHS.contains(&op.path.as_str()),
MotionErrorCode::PatchPathUnknown,
"unknown patch path for tween node",
)
}
fn validate_accumulator_op(op: &PatchOp) -> Result<(), MotionError> {
const VALID_PATHS: &[&str] = &[
"tension",
"friction",
"mass",
"preserve_velocity",
"scrub_source_id",
];
MotionError::require(
VALID_PATHS.contains(&op.path.as_str()),
MotionErrorCode::PatchPathUnknown,
"unknown patch path for accumulator node",
)
}
fn apply_ops_to_node(mut node: MotionNode, patch: &MotionPatch) -> Result<MotionNode, MotionError> {
match node.kind().clone() {
NodeKind::Tween(tween) => {
let updated = apply_tween_ops(tween, &patch.ops)?;
node.set_kind(NodeKind::Tween(updated));
Ok(node)
}
NodeKind::Accumulator(_) => Err(MotionError::new(MotionErrorCode::PatchInvalid)
.with_detail("accumulator field patching is refused")),
}
}
struct TweenFields {
from: f32,
to: f32,
start_ms: f32,
duration_ms: f32,
easing: Easing,
}
impl TweenFields {
fn from_component(tween: &TweenComponent) -> Self {
Self {
from: tween.from_value(),
to: tween.to_value(),
start_ms: tween.start_ms(),
duration_ms: tween.duration_ms(),
easing: tween.easing().clone(),
}
}
fn apply(&mut self, op: &PatchOp) -> Result<(), MotionError> {
let path = op.path.as_str();
if path == "easing" {
self.easing = extract_easing(&op.value, path)?;
return Ok(());
}
let slot: &mut f32 = match path {
"from" => Some(&mut self.from),
"to" => Some(&mut self.to),
"start_ms" => Some(&mut self.start_ms),
"duration_ms" => Some(&mut self.duration_ms),
_ => None,
}
.ok_or_else(|| {
MotionError::new(MotionErrorCode::PatchPathUnknown)
.with_detail("unexpected path in apply_tween_ops")
})?;
*slot = extract_float(&op.value, path)?;
Ok(())
}
fn into_component(self) -> Result<TweenComponent, MotionError> {
TweenComponent::new(
self.from,
self.to,
self.start_ms,
self.duration_ms,
self.easing,
)
}
}
fn apply_tween_ops(tween: TweenComponent, ops: &[PatchOp]) -> Result<TweenComponent, MotionError> {
let mut fields = TweenFields::from_component(&tween);
for op in ops {
fields.apply(op)?;
}
fields.into_component()
}
fn extract_float(value: &PatchValue, field: &str) -> Result<f32, MotionError> {
match *value {
PatchValue::Float(number) => Ok(number),
_ => Err(MotionError::new(MotionErrorCode::PatchInvalid).with_detail(field)),
}
}
fn extract_easing(value: &PatchValue, _field: &str) -> Result<Easing, MotionError> {
match *value {
PatchValue::Easing(ref easing) => Ok(easing.clone()),
_ => Err(MotionError::new(MotionErrorCode::PatchInvalid)
.with_detail("easing patch requires PatchValue::Easing")),
}
}
fn preserve_accumulator_state(node_id: &Uuid, accumulators: &mut AccumulatorStore) {
let _state = accumulators.get_or_default(*node_id);
}