From 6ac573dab9f16f1479d8e330349dab0faa6fa10c Mon Sep 17 00:00:00 2001 From: Belligerently Date: Thu, 5 Nov 2026 12:44:34 -0500 Subject: [PATCH] Clear parameter drivers when restoring defaults --- crates/engine/src/node_cmds.rs | 8 ++- crates/engine/tests/reset_drivers.rs | 87 ++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 crates/engine/tests/reset_drivers.rs diff --git a/crates/engine/src/node_cmds.rs b/crates/engine/src/node_cmds.rs index 72e87e6..73ced9b 100644 --- a/crates/engine/src/node_cmds.rs +++ b/crates/engine/src/node_cmds.rs @@ -177,8 +177,14 @@ fn reset(s: &mut Session, p: &J) -> Result { match ¶m { Some(k) => { n.params.remove(k); + n.bindings.remove(k); + n.functions.remove(k); + } + None => { + n.params.retain(|k, _| k == "graph" || k == "identifier"); + n.bindings.clear(); + n.functions.clear(); } - None => n.params.retain(|k, _| k == "graph" || k == "identifier"), } Ok(J::Null) }) diff --git a/crates/engine/tests/reset_drivers.rs b/crates/engine/tests/reset_drivers.rs new file mode 100644 index 0000000..e843d40 --- /dev/null +++ b/crates/engine/tests/reset_drivers.rs @@ -0,0 +1,87 @@ +use materialcraft_engine::{ + Session, + core::{NodeId, Value, function::FnGraph}, + execute, formats, new_session, +}; +use serde_json::{Value as Json, json}; + +fn run(s: &mut Session, id: &str, params: Json) -> Json { + execute(s, id, ¶ms).unwrap() +} + +fn setup() -> (Session, NodeId) { + let mut s = new_session(); + s.preview_size = Some(32); + let source = run(&mut s, "graph.node.add", json!({"def": "uniform"}))["id"].as_u64().unwrap(); + let levels = NodeId(run(&mut s, "graph.node.add", json!({"def": "levels", "connectFrom": {"node": source}}))["id"].as_u64().unwrap()); + (s, levels) +} + +fn drive(s: &mut Session, id: NodeId, param: &str, value: f32, function: bool) { + if function { + run(s, "graph.node.function", json!({"id": id.0, "param": param, "function": FnGraph::constant(Value::Float(value))})); + } else { + run(s, "graph.exposed.add", json!({"id": param, "default": value, "bind": {"node": id.0, "param": param}})); + } +} + +fn pixel(s: &mut Session, id: NodeId) -> f32 { + let cooked = s.cook(); + assert!(cooked.nodes[&id].error.is_none()); + cooked.image(id, 0).unwrap().get_gray(0, 0, false) +} + +fn check_single_reset(function: bool) { + let (mut s, id) = setup(); + drive(&mut s, id, "in_low", 0.4, function); + drive(&mut s, id, "out_high", 0.8, !function); + let before = s.pkg.graphs.clone(); + let driven = pixel(&mut s, id); + assert!(driven < 0.2); + run(&mut s, "graph.node.reset", json!({"id": id.0, "param": "in_low"})); + assert!((pixel(&mut s, id) - 0.4).abs() < 1e-5, "reset left the in_low driver active (function={function})"); + let n = s.graph().unwrap().node(id).unwrap(); + assert!(!n.bindings.contains_key("in_low") && !n.functions.contains_key("in_low")); + assert!(n.bindings.contains_key("out_high") || n.functions.contains_key("out_high")); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.pkg.graphs, before); + assert!((pixel(&mut s, id) - driven).abs() < 1e-5); + run(&mut s, "edit.redo", json!({})); + assert!((pixel(&mut s, id) - 0.4).abs() < 1e-5); +} + +#[test] +fn resetting_one_bound_parameter_preserves_other_drivers() { + check_single_reset(false); +} + +#[test] +fn resetting_one_function_parameter_preserves_other_drivers() { + check_single_reset(true); +} + +#[test] +fn resetting_all_parameters_removes_drivers_and_restores_default_output() { + let (mut s, id) = setup(); + drive(&mut s, id, "in_low", 0.4, false); + drive(&mut s, id, "out_high", 0.8, true); + run(&mut s, "graph.node.param", json!({"id": id.0, "param": "out_low", "value": 0.1})); + let before = s.pkg.graphs.clone(); + let driven = pixel(&mut s, id); + assert!((driven - 0.5).abs() > 0.1); + let history = s.history().len(); + run(&mut s, "graph.node.reset", json!({"id": id.0})); + assert!((pixel(&mut s, id) - 0.5).abs() < 1e-5, "reset left dynamic parameters active"); + let n = s.graph().unwrap().node(id).unwrap(); + assert!(n.params.is_empty() && n.bindings.is_empty() && n.functions.is_empty()); + assert_eq!(s.history().len(), history + 1); + assert_eq!(s.graph().unwrap().exposed, before[0].exposed); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.pkg.graphs, before); + assert!((pixel(&mut s, id) - driven).abs() < 1e-5); + run(&mut s, "edit.redo", json!({})); + assert!((pixel(&mut s, id) - 0.5).abs() < 1e-5); + let mut reopened = Session::new(formats::load_package(&formats::save_package(&s.pkg).unwrap()).unwrap()); + reopened.preview_size = Some(32); + assert!((pixel(&mut reopened, id) - 0.5).abs() < 1e-5); +}