mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-19 22:10:53 +01:00
Add linked graph instances
This commit is contained in:
1 parent
8101e8da68
commit
570bc528a6
95 files changed
+2872
-373
No files matched your search
@@ -28,8 +28,16 @@ impl FunctionGraph {
|
||||
pub fn add(&mut self, operation: &str, inputs: &[u32], value: Option<Value>) -> u32 {
|
||||
let id = self.next_id();
|
||||
let horizontal_position = self.nodes.len() as f32 * 140.0;
|
||||
let node =
|
||||
FunctionNode { id, type_code: 0, op: operation.into(), inputs: inputs.iter().copied().map(Some).collect(), value, pos: [horizontal_position, 0.0] };
|
||||
let node = FunctionNode {
|
||||
id,
|
||||
instance: None,
|
||||
source: None,
|
||||
type_code: 0,
|
||||
op: operation.into(),
|
||||
inputs: inputs.iter().copied().map(Some).collect(),
|
||||
value,
|
||||
pos: [horizontal_position, 0.0],
|
||||
};
|
||||
|
||||
self.nodes.push(node);
|
||||
id
|
||||
|
||||
@@ -9,6 +9,11 @@ pub struct FunctionNode {
|
||||
#[serde(default)]
|
||||
pub type_code: u32,
|
||||
pub op: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub instance: Option<crate::GraphInstance>,
|
||||
/// Resolved call body shared by cooks; SBS stores only the source reference
|
||||
#[serde(skip)]
|
||||
pub source: Option<Result<std::sync::Arc<crate::FunctionGraph>, String>>,
|
||||
// Slots follow the native operation's connector order
|
||||
#[serde(default)]
|
||||
pub inputs: Vec<Option<u32>>,
|
||||
|
||||
@@ -71,6 +71,37 @@ impl Graph {
|
||||
}
|
||||
}
|
||||
|
||||
/// All editable Function canvases, including processor and parameter functions
|
||||
pub fn functions(&self) -> impl Iterator<Item = &crate::FunctionGraph> {
|
||||
self.function.iter().chain(self.nodes.iter().flat_map(|node| node.functions.values().chain(node.params.values().flat_map(value_functions))))
|
||||
}
|
||||
|
||||
pub fn functions_mut(&mut self) -> impl Iterator<Item = &mut crate::FunctionGraph> {
|
||||
self.function
|
||||
.iter_mut()
|
||||
.chain(self.nodes.iter_mut().flat_map(|node| node.functions.values_mut().chain(node.params.values_mut().flat_map(value_functions_mut))))
|
||||
}
|
||||
|
||||
pub fn instances(&self) -> impl Iterator<Item = &crate::GraphInstance> {
|
||||
self.nodes
|
||||
.iter()
|
||||
.filter_map(|node| node.instance.as_ref())
|
||||
.chain(self.functions().flat_map(|function| function.nodes.iter().filter_map(|node| node.instance.as_ref())))
|
||||
}
|
||||
|
||||
pub fn instances_mut(&mut self) -> impl Iterator<Item = &mut crate::GraphInstance> {
|
||||
self.function.iter_mut().flat_map(|function| function.nodes.iter_mut().filter_map(|node| node.instance.as_mut())).chain(self.nodes.iter_mut().flat_map(
|
||||
|node| {
|
||||
node.instance.iter_mut().chain(
|
||||
node.functions
|
||||
.values_mut()
|
||||
.chain(node.params.values_mut().flat_map(value_functions_mut))
|
||||
.flat_map(|function| function.nodes.iter_mut().filter_map(|node| node.instance.as_mut())),
|
||||
)
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> GraphKind {
|
||||
if self.function.is_some() { GraphKind::Function } else { GraphKind::Material }
|
||||
}
|
||||
@@ -165,6 +196,36 @@ impl Graph {
|
||||
}
|
||||
}
|
||||
|
||||
fn value_functions(value: &Value) -> impl Iterator<Item = &crate::FunctionGraph> {
|
||||
let (function, pattern) = match value {
|
||||
Value::Function(function) => (Some(function.as_ref()), None),
|
||||
Value::PatternGraph(pattern) => (None, Some(pattern.as_ref())),
|
||||
_ => (None, None),
|
||||
};
|
||||
function.into_iter().chain(
|
||||
pattern
|
||||
.into_iter()
|
||||
.flat_map(|pattern| &pattern.nodes)
|
||||
.flat_map(|node| node.parameters.values())
|
||||
.filter_map(|value| if let Value::Function(function) = value { Some(function.as_ref()) } else { None }),
|
||||
)
|
||||
}
|
||||
|
||||
fn value_functions_mut(value: &mut Value) -> impl Iterator<Item = &mut crate::FunctionGraph> {
|
||||
let (function, pattern) = match value {
|
||||
Value::Function(function) => (Some(function.as_mut()), None),
|
||||
Value::PatternGraph(pattern) => (None, Some(pattern.as_mut())),
|
||||
_ => (None, None),
|
||||
};
|
||||
function.into_iter().chain(
|
||||
pattern
|
||||
.into_iter()
|
||||
.flat_map(|pattern| &mut pattern.nodes)
|
||||
.flat_map(|node| node.parameters.values_mut())
|
||||
.filter_map(|value| if let Value::Function(function) = value { Some(function.as_mut()) } else { None }),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::GraphPort;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// An SBS graph reference and its last known interface, without a copy of its nodes
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct GraphInstance {
|
||||
/// Empty for a graph in the same package
|
||||
pub package: String,
|
||||
pub graph: String,
|
||||
pub inputs: Vec<GraphPort>,
|
||||
pub outputs: Vec<GraphPort>,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A named connector retained when an instance's source is unavailable
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct GraphPort {
|
||||
pub identifier: String,
|
||||
pub type_code: u32,
|
||||
}
|
||||
@@ -18,7 +18,9 @@ mod gradient;
|
||||
mod gradient_key;
|
||||
mod graph;
|
||||
mod graph_index;
|
||||
mod graph_instance;
|
||||
mod graph_kind;
|
||||
mod graph_port;
|
||||
mod material_channel;
|
||||
mod missing_node;
|
||||
mod node;
|
||||
@@ -53,7 +55,9 @@ pub use gradient::Gradient;
|
||||
pub use gradient_key::GradientKey;
|
||||
pub use graph::{Graph, MAX_SIZE_LOG2, MIN_SIZE_LOG2};
|
||||
pub use graph_index::GraphIndex;
|
||||
pub use graph_instance::GraphInstance;
|
||||
pub use graph_kind::GraphKind;
|
||||
pub use graph_port::GraphPort;
|
||||
pub use material_channel::MaterialChannel;
|
||||
pub use missing_node::{MISSING_NODE, missing_ports};
|
||||
pub use node::Node;
|
||||
|
||||
@@ -11,6 +11,8 @@ pub struct Node {
|
||||
pub id: NodeId,
|
||||
/// Catalog id, e.g. `blend`, `pixel_processor`, `output`, `input_color`, `graph_instance`
|
||||
pub def: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub instance: Option<crate::GraphInstance>,
|
||||
/// User label; empty = the definition's label
|
||||
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||
pub label: String,
|
||||
@@ -38,6 +40,7 @@ impl Node {
|
||||
Node {
|
||||
id,
|
||||
def: def.into(),
|
||||
instance: None,
|
||||
label: String::new(),
|
||||
pos,
|
||||
params: BTreeMap::new(),
|
||||
|
||||
@@ -10,6 +10,9 @@ pub struct Package {
|
||||
pub graphs: Vec<Graph>,
|
||||
#[serde(default)]
|
||||
pub resources: Vec<Resource>,
|
||||
/// Resolved source packages shared by cooks; SBS stores their file references instead
|
||||
#[serde(skip)]
|
||||
pub dependencies: std::collections::BTreeMap<String, std::sync::Arc<Package>>,
|
||||
/// Format version of `package.json`
|
||||
#[serde(default = "version")]
|
||||
pub version: u32,
|
||||
@@ -21,7 +24,7 @@ fn version() -> u32 {
|
||||
|
||||
impl Package {
|
||||
pub fn new(name: &str) -> Package {
|
||||
Package { name: name.into(), graphs: Vec::new(), resources: Vec::new(), version: 1 }
|
||||
Package { name: name.into(), graphs: Vec::new(), resources: Vec::new(), version: 1, dependencies: Default::default() }
|
||||
}
|
||||
|
||||
pub fn graph(&self, i: usize) -> Result<&Graph> {
|
||||
@@ -48,4 +51,19 @@ impl Package {
|
||||
}
|
||||
(1..).map(|i| format!("{base}_{i}")).find(|n| self.graph_by_name(n).is_none()).unwrap_or_else(|| base.to_string())
|
||||
}
|
||||
|
||||
pub fn instance_package(&self, node: &crate::Node) -> Option<&Package> {
|
||||
match node.instance.as_ref().map(|instance| instance.package.as_str()) {
|
||||
Some(file) if !file.is_empty() => self.dependencies.get(file).map(AsRef::as_ref),
|
||||
_ => Some(self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn instance_graph(&self, node: &crate::Node) -> Option<(usize, &Graph)> {
|
||||
let name = node.instance.as_ref().map(|instance| instance.graph.as_str()).or_else(|| match node.params.get("graph") {
|
||||
Some(crate::Value::Text(name)) => Some(name.as_str()),
|
||||
_ => None,
|
||||
})?;
|
||||
self.instance_package(node)?.graph_by_name(name)
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,9 @@ pub fn command_specs() -> &'static [CommandSpec] {
|
||||
SPECS.get_or_init(|| {
|
||||
let mut v = Vec::new();
|
||||
v.extend(crate::file_cmds::specs());
|
||||
v.extend(crate::package_commands::specs());
|
||||
v.extend(crate::graph_instance_commands::specs());
|
||||
v.extend(crate::node_info_commands::specs());
|
||||
v.extend(crate::atomic_sample_commands::specs());
|
||||
v.extend(crate::material_cmds::specs());
|
||||
v.extend(crate::edit_cmds::specs());
|
||||
|
||||
@@ -78,9 +78,18 @@ pub(crate) fn validate_connection(session: &Session, from: NodeId, from_port: &s
|
||||
|
||||
let graph = session.graph()?;
|
||||
let target = graph.node(to)?;
|
||||
let source_type = materialcraft_nodes::output_value_type(graph, from).map_err(|error| bad("graph.connect", error))?;
|
||||
if let Some(expected) = target.input_values.get(to_port) {
|
||||
if source_type != Some(*expected) {
|
||||
let source_type = materialcraft_nodes::output_port_value_type(graph, from, from_port).map_err(|error| bad("graph.connect", error))?;
|
||||
let expected = target.input_values.get(to_port).copied().or_else(|| {
|
||||
target
|
||||
.instance
|
||||
.as_ref()?
|
||||
.inputs
|
||||
.iter()
|
||||
.find(|port| port.identifier == to_port)
|
||||
.and_then(|port| materialcraft_core::ValueType::from_code(port.type_code))
|
||||
});
|
||||
if let Some(expected) = expected {
|
||||
if source_type != Some(expected) {
|
||||
return Err(bad("graph.connect", format!("input `{to_port}` requires {}", expected.label())));
|
||||
}
|
||||
} else if source_type.is_some() && !matches!(target.def.as_str(), "output" | "dot") {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
pub(crate) fn normalize(path: &str) -> String {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use std::path::{Component, PathBuf};
|
||||
let path = PathBuf::from(path);
|
||||
if let Ok(path) = path.canonicalize() {
|
||||
return path.to_string_lossy().into_owned();
|
||||
}
|
||||
let absolute = if path.is_absolute() { path } else { std::env::current_dir().unwrap_or_default().join(path) };
|
||||
let mut normalized = PathBuf::new();
|
||||
for part in absolute.components() {
|
||||
match part {
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
normalized.pop();
|
||||
}
|
||||
part => normalized.push(part.as_os_str()),
|
||||
}
|
||||
}
|
||||
normalized.to_string_lossy().into_owned()
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
path.replace('\\', "/")
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn resolve(owner: &str, reference: &str) -> String {
|
||||
let path = std::path::Path::new(reference);
|
||||
if path.is_absolute() {
|
||||
return normalize(reference);
|
||||
}
|
||||
normalize(&std::path::Path::new(owner).parent().unwrap_or(std::path::Path::new("")).join(path).to_string_lossy())
|
||||
}
|
||||
|
||||
pub(crate) fn relative(owner: &str, source: &str) -> String {
|
||||
let folder = std::path::Path::new(owner).parent().unwrap_or(std::path::Path::new(""));
|
||||
let from: Vec<_> = folder.components().collect();
|
||||
let to: Vec<_> = std::path::Path::new(source).components().collect();
|
||||
let common = from.iter().zip(&to).take_while(|(left, right)| left == right).count();
|
||||
if common == 0 {
|
||||
return source.replace('\\', "/");
|
||||
}
|
||||
let mut path = std::path::PathBuf::new();
|
||||
for _ in common..from.len() {
|
||||
path.push("..");
|
||||
}
|
||||
for part in to.iter().skip(common) {
|
||||
path.push(part.as_os_str());
|
||||
}
|
||||
path.to_string_lossy().replace('\\', "/")
|
||||
}
|
||||
@@ -20,7 +20,7 @@ pub(crate) fn specs() -> Vec<CommandSpec> {
|
||||
always,
|
||||
new_graph
|
||||
),
|
||||
cmd!("file.newPackage", "Empty package", ["File", "New"], None, "{name?} replaces the session with an empty package", always, new_package),
|
||||
cmd!("file.newPackage", "Empty package", ["File", "New"], None, "{name?} opens a new package alongside the existing packages", always, new_package),
|
||||
cmd!(
|
||||
"file.open",
|
||||
"Open...",
|
||||
@@ -94,11 +94,11 @@ fn new_graph(session: &mut Session, parameters: &J) -> Result<J> {
|
||||
fn new_package(s: &mut Session, p: &J) -> Result<J> {
|
||||
let mut pkg = Package::new(strp(p, "name").unwrap_or("Untitled package"));
|
||||
pkg.graphs.push(template("pbr", "New_graph", 10)?);
|
||||
replace_package(s, pkg, None);
|
||||
s.open_package(pkg, None)?;
|
||||
Ok(json!({"name": s.pkg.name}))
|
||||
}
|
||||
|
||||
/// Make `pkg` the session's document (after open/new): history and views reset
|
||||
/// Replace the active document explicitly, resetting its history and views
|
||||
pub fn replace_package(s: &mut Session, pkg: Package, path: Option<String>) {
|
||||
s.pkg = pkg;
|
||||
s.clipboard = None;
|
||||
@@ -130,17 +130,31 @@ pub fn open_bytes(session: &mut Session, bytes: &[u8], name: Option<String>) ->
|
||||
}
|
||||
|
||||
pub fn open_bytes_linked(session: &mut Session, bytes: &[u8], name: Option<String>, linked: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<()> {
|
||||
let name = name.map(|path| crate::document_path::normalize(&path));
|
||||
if let Some(name) = &name
|
||||
&& let Some((id, ..)) = session.open_packages().into_iter().find(|entry| entry.2 == Some(name.as_str()))
|
||||
{
|
||||
return session.activate_document(id);
|
||||
}
|
||||
if materialcraft_formats::material::is_material(bytes) {
|
||||
let (manifest, source) = materialcraft_formats::material::read_material(bytes)?;
|
||||
replace_package(session, materialcraft_formats::sbs::read_sbs(&source, "source.sbs", &|_| None)?.pkg, None);
|
||||
session.open_package(materialcraft_formats::sbs::read_sbs(&source, "source.sbs", &|_| None)?.pkg, None)?;
|
||||
session.graph = session.pkg.graph_by_name(&manifest.graph).map_or(0, |(index, _)| index);
|
||||
session.open_material_preview();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let read = materialcraft_formats::sbs::read_sbs(bytes, name.as_deref().unwrap_or(""), linked)?;
|
||||
let mut read = materialcraft_formats::sbs::read_sbs(bytes, name.as_deref().unwrap_or(""), linked)?;
|
||||
if let Some(path) = &name {
|
||||
crate::linked_files::normalize_references(&mut read.pkg, path);
|
||||
}
|
||||
read.pkg.dependencies = session.pkg.dependencies.clone();
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let link_warnings = crate::linked_files::load(&mut read.pkg);
|
||||
let path = if read.warnings.is_empty() { name } else { None };
|
||||
replace_package(session, read.pkg, path);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
read.warnings.extend(link_warnings);
|
||||
session.open_package(read.pkg, path)?;
|
||||
for warning in &read.warnings {
|
||||
session.warn(warning.clone());
|
||||
}
|
||||
@@ -149,7 +163,19 @@ pub fn open_bytes_linked(session: &mut Session, bytes: &[u8], name: Option<Strin
|
||||
}
|
||||
|
||||
pub fn save_bytes(session: &Session) -> Result<Vec<u8>> {
|
||||
Ok(materialcraft_formats::sbs::write_sbs(&session.pkg)?)
|
||||
save_bytes_at(session, session.path.as_deref())
|
||||
}
|
||||
|
||||
fn save_bytes_at(session: &Session, path: Option<&str>) -> Result<Vec<u8>> {
|
||||
let mut package = session.pkg.clone();
|
||||
if let Some(path) = path {
|
||||
for instance in package.graphs.iter_mut().flat_map(|graph| graph.instances_mut()) {
|
||||
if !instance.package.is_empty() {
|
||||
instance.package = crate::document_path::relative(path, &instance.package);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(materialcraft_formats::sbs::write_sbs(&package)?)
|
||||
}
|
||||
|
||||
/// Largest file an SBS package may link to
|
||||
@@ -206,9 +232,14 @@ fn save(s: &mut Session, p: &J) -> Result<J> {
|
||||
None => s.path.clone().ok_or_else(|| bad("file.save", "the package has no file yet (new, or opened from another program): give a `path`"))?,
|
||||
};
|
||||
|
||||
let bytes = save_bytes(s)?;
|
||||
let path = crate::document_path::normalize(&path);
|
||||
if s.open_packages().into_iter().any(|entry| entry.0 != s.document_id && entry.2 == Some(path.as_str())) {
|
||||
return Err(bad("file.save", "another open package already uses this file"));
|
||||
}
|
||||
let bytes = save_bytes_at(s, Some(&path))?;
|
||||
materialcraft_formats::write_atomic(std::path::Path::new(&path), &bytes)?;
|
||||
s.path = Some(path.clone());
|
||||
s.refresh_links();
|
||||
s.modified = false;
|
||||
s.log(format!("Saved {path}"));
|
||||
Ok(json!({"path": path, "bytes": bytes.len()}))
|
||||
|
||||
@@ -42,6 +42,8 @@ impl FunctionEdit {
|
||||
let definition = materialcraft_nodes::function::definition(operation).ok_or("unknown function operation")?;
|
||||
let id = graph.next_id();
|
||||
graph.nodes.push(FunctionNode {
|
||||
instance: None,
|
||||
source: None,
|
||||
id,
|
||||
type_code: 0,
|
||||
op: operation.clone(),
|
||||
@@ -70,7 +72,7 @@ impl FunctionEdit {
|
||||
}
|
||||
|
||||
let node = graph.nodes.iter_mut().find(|node| node.id == *to).ok_or("missing function destination")?;
|
||||
let count = materialcraft_nodes::function::definition(&node.op).map_or(0, |definition| definition.inputs.len());
|
||||
let count = materialcraft_nodes::function::input_identifiers(node).count();
|
||||
if *input >= count {
|
||||
return Err("invalid function input".into());
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ fn info(session: &mut Session, parameters: &Json) -> Result<Json> {
|
||||
let id = parameters.get("id").and_then(Json::as_u64).and_then(|id| u32::try_from(id).ok()).ok_or_else(|| bad(command, "id must be a function node id"))?;
|
||||
let graph = target.current(session)?;
|
||||
let node = graph.node(id).ok_or_else(|| bad(command, "missing function node"))?;
|
||||
let label = materialcraft_nodes::function::definition(&node.op).map_or(node.op.as_str(), |definition| definition.label);
|
||||
let label = materialcraft_nodes::function::node_label(node);
|
||||
let output = materialcraft_nodes::function::output_node(graph).is_some_and(|node| node.id == id);
|
||||
let text = serde_json::to_string_pretty(&json!({"target": target, "label": label, "node": node, "output": output}))
|
||||
.map_err(|error| bad(command, error.to_string()))?;
|
||||
|
||||
@@ -96,6 +96,11 @@ fn rename(s: &mut Session, p: &J) -> Result<J> {
|
||||
let old = std::mem::replace(&mut s.pkg.graph_mut(i)?.name, name.clone());
|
||||
// Instances follow the rename
|
||||
for g in &mut s.pkg.graphs {
|
||||
for instance in g.instances_mut() {
|
||||
if instance.package.is_empty() && instance.graph == old {
|
||||
instance.graph = name.clone();
|
||||
}
|
||||
}
|
||||
for n in g.nodes.iter_mut().filter(|n| n.def == "graph_instance") {
|
||||
if n.params.get("graph") == Some(&Value::Text(old.clone())) {
|
||||
n.params.insert("graph".into(), Value::Text(name.clone()));
|
||||
@@ -411,6 +416,9 @@ fn from_selection(s: &mut Session, p: &J) -> Result<J> {
|
||||
sub.connect(c.from, &c.from_port, id, "input")?;
|
||||
outputs.push((c.from, c.from_port.clone(), ident));
|
||||
}
|
||||
if !sub.nodes.iter().any(|node| node.def == "output") {
|
||||
return Err(bad("graph.createFromSelection", "the selection needs an output node or an outgoing connection before it can become an instance"));
|
||||
}
|
||||
s.pkg.graphs.push(sub);
|
||||
let g = s.graph_mut()?;
|
||||
g.remove_nodes(&ids);
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
use crate::{
|
||||
Result, Session, cmd,
|
||||
commands::{CommandSpec, bad, has_graph, point, strp, u64p},
|
||||
};
|
||||
use materialcraft_core::{GraphInstance, Package};
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
const COMMAND: &str = "graph.instance.add";
|
||||
|
||||
pub(crate) fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"graph.instance.reference",
|
||||
"Open graph reference",
|
||||
[],
|
||||
None,
|
||||
"{id, function?: FunctionGraphTarget} resolves the source package and graph without changing the document",
|
||||
has_graph,
|
||||
reference
|
||||
),
|
||||
cmd!(
|
||||
"graph.instance.add",
|
||||
"Add graph instance",
|
||||
[],
|
||||
None,
|
||||
"{document, graph, function?: FunctionGraphTarget, at? [x,y]} instances a compatible graph from an open SBS package; requires an output and rejects cycles",
|
||||
has_graph,
|
||||
add
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
fn reaches(
|
||||
package: &Package,
|
||||
source_file: &str,
|
||||
graph: &str,
|
||||
target_file: &str,
|
||||
target_graph: &str,
|
||||
sources: &std::collections::BTreeMap<String, std::sync::Arc<Package>>,
|
||||
) -> bool {
|
||||
let mut pending = vec![(source_file.to_string(), graph.to_string())];
|
||||
let mut visited = BTreeSet::new();
|
||||
while let Some((file, name)) = pending.pop() {
|
||||
if !visited.insert((file.clone(), name.clone())) {
|
||||
continue;
|
||||
}
|
||||
if file == target_file && name == target_graph {
|
||||
return true;
|
||||
}
|
||||
let source = if file == source_file { Some(package) } else { sources.get(&file).map(AsRef::as_ref) };
|
||||
let Some((_, graph)) = source.and_then(|source| source.graph_by_name(&name)) else { continue };
|
||||
for instance in graph.instances() {
|
||||
pending.push((if instance.package.is_empty() { file.clone() } else { instance.package.clone() }, instance.graph.clone()));
|
||||
}
|
||||
for node in &graph.nodes {
|
||||
if node.instance.is_none()
|
||||
&& node.def == "graph_instance"
|
||||
&& let Some(materialcraft_core::Value::Text(name)) = node.params.get("graph")
|
||||
{
|
||||
pending.push((file.clone(), name.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn add(session: &mut Session, parameters: &Value) -> Result<Value> {
|
||||
if parameters.get("pattern").is_some() {
|
||||
return Err(bad(COMMAND, "FX-Map graphs do not accept graph instances"));
|
||||
}
|
||||
let target: Option<crate::FunctionGraphTarget> = parameters
|
||||
.get("function")
|
||||
.cloned()
|
||||
.map(serde_json::from_value)
|
||||
.transpose()
|
||||
.map_err(|error| bad(COMMAND, error.to_string()))?
|
||||
.or_else(|| session.graph().ok()?.function.as_ref().map(|_| crate::FunctionGraphTarget::Graph));
|
||||
if let Some(target) = &target {
|
||||
target.current(session)?;
|
||||
}
|
||||
let document = u64p(parameters, "document").ok_or_else(|| bad(COMMAND, "document is required"))?;
|
||||
let name = strp(parameters, "graph").ok_or_else(|| bad(COMMAND, "graph is required"))?;
|
||||
let packages = session.open_packages();
|
||||
let (_, source, path, _) = packages.iter().find(|entry| entry.0 == document).copied().ok_or_else(|| bad(COMMAND, "the source package is not open"))?;
|
||||
let (_, graph) = source.graph_by_name(name).ok_or_else(|| bad(COMMAND, "the source graph no longer exists"))?;
|
||||
if graph.function.is_some() != target.is_some() {
|
||||
return Err(bad(COMMAND, "Function instances require a Function canvas; main instances require a main graph"));
|
||||
}
|
||||
let (inputs, outputs) = if target.is_some() {
|
||||
materialcraft_nodes::function::graph_ports(graph).map_err(|error| bad(COMMAND, error))?
|
||||
} else {
|
||||
materialcraft_nodes::graph_interface::graph_ports(graph)
|
||||
};
|
||||
if outputs.is_empty() {
|
||||
return Err(bad(COMMAND, "the source graph needs an output before it can be instanced"));
|
||||
}
|
||||
for ports in [&inputs, &outputs] {
|
||||
if ports.iter().map(|port| &port.identifier).collect::<BTreeSet<_>>().len() != ports.len() {
|
||||
return Err(bad(COMMAND, "source connector identifiers must be unique"));
|
||||
}
|
||||
}
|
||||
let file = if document == session.document_id {
|
||||
""
|
||||
} else {
|
||||
path.ok_or_else(|| bad(COMMAND, "save the source SBS file before instancing its graph in another package"))?
|
||||
};
|
||||
let source_file = path.map(str::to_string).unwrap_or_else(|| format!("unsaved:{document}"));
|
||||
let target_file = session.path.clone().unwrap_or_else(|| format!("unsaved:{}", session.document_id));
|
||||
if reaches(source, &source_file, name, &target_file, &session.graph()?.name, &session.pkg.dependencies) {
|
||||
return Err(bad(COMMAND, "the instance would create a graph dependency cycle"));
|
||||
}
|
||||
let instance = GraphInstance { package: file.into(), graph: name.into(), inputs, outputs };
|
||||
let at =
|
||||
crate::graph_grid::snap_position(point(parameters, "at").unwrap_or([0.0; 2]), parameters.get("snapToGrid").and_then(Value::as_bool).unwrap_or(false));
|
||||
session.edit("Add graph instance", |session| {
|
||||
if let Some(target) = target {
|
||||
let mut function = target.current(session)?.clone();
|
||||
if function.nodes.len() >= 4096 {
|
||||
return Err(bad(COMMAND, "function exceeds the 4096-node limit"));
|
||||
}
|
||||
let id = function.next_id();
|
||||
function.nodes.push(materialcraft_core::FunctionNode {
|
||||
id,
|
||||
op: "instance".into(),
|
||||
type_code: instance.outputs.first().map_or(0, |port| port.type_code),
|
||||
inputs: vec![None; instance.inputs.len()],
|
||||
value: None,
|
||||
pos: at,
|
||||
instance: Some(instance),
|
||||
source: None,
|
||||
});
|
||||
target.replace(session, function)?;
|
||||
if matches!(target, crate::FunctionGraphTarget::Graph) {
|
||||
session.selection = vec![materialcraft_core::NodeId(u64::from(id))];
|
||||
}
|
||||
return Ok(json!({"id": id}));
|
||||
}
|
||||
let graph = session.graph_mut()?;
|
||||
let id = graph.add_node("graph_instance", at);
|
||||
graph.node_mut(id)?.instance = Some(instance);
|
||||
session.selection = vec![id];
|
||||
Ok(json!({"id": id}))
|
||||
})
|
||||
}
|
||||
|
||||
fn reference(session: &mut Session, parameters: &Value) -> Result<Value> {
|
||||
let command = "graph.instance.reference";
|
||||
let id = u64p(parameters, "id").ok_or_else(|| bad(command, "id is required"))?;
|
||||
let instance = if let Some(target) = parameters.get("function") {
|
||||
let target: crate::FunctionGraphTarget = serde_json::from_value(target.clone()).map_err(|error| bad(command, error.to_string()))?;
|
||||
let id = u32::try_from(id).map_err(|_| bad(command, "invalid function node id"))?;
|
||||
target.current(session)?.node(id).and_then(|node| node.instance.as_ref())
|
||||
} else {
|
||||
session.graph()?.node(materialcraft_core::NodeId(id))?.instance.as_ref()
|
||||
}
|
||||
.ok_or_else(|| bad(command, "the node has no graph reference"))?;
|
||||
let source = if instance.package.is_empty() { Some(&session.pkg) } else { session.pkg.dependencies.get(&instance.package).map(AsRef::as_ref) }
|
||||
.ok_or_else(|| bad(command, "the source file is unavailable"))?;
|
||||
let (graph, _) = source.graph_by_name(&instance.graph).ok_or_else(|| bad(command, "the source graph is missing"))?;
|
||||
let document = if instance.package.is_empty() {
|
||||
Some(session.document_id)
|
||||
} else {
|
||||
session.open_packages().into_iter().find(|entry| entry.2 == Some(instance.package.as_str())).map(|entry| entry.0)
|
||||
};
|
||||
Ok(json!({"graph": graph, "document": document, "path": instance.package}))
|
||||
}
|
||||
@@ -30,6 +30,7 @@ pub fn session(s: &Session, graph: Option<usize>) -> J {
|
||||
json!({
|
||||
"id": n.id.0,
|
||||
"def": n.def,
|
||||
"instance": n.instance,
|
||||
"label": if n.label.is_empty() { materialcraft_nodes::find(&n.def).map_or(n.def.as_str(), |d| d.label).to_string() } else { n.label.clone() },
|
||||
"pos": n.pos,
|
||||
"params": n.params,
|
||||
@@ -56,6 +57,7 @@ pub fn session(s: &Session, graph: Option<usize>) -> J {
|
||||
})
|
||||
});
|
||||
json!({
|
||||
"documents": s.inspect_documents(),
|
||||
"package": {"name": s.pkg.name, "path": s.path, "modified": s.modified, "warnings": s.load_warnings, "resources": s.pkg.resources.iter().map(|r| json!({"name": r.name, "bytes": r.bytes.len()})).collect::<Vec<_>>()},
|
||||
"graphs": s.pkg.graphs.iter().enumerate().map(|(i, g)| json!({"index": i, "name": g.name, "nodes": g.nodes.len(), "kind": g.kind()})).collect::<Vec<_>>(),
|
||||
"openGraph": s.graph,
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
mod document_path;
|
||||
mod graph_instance_commands;
|
||||
mod linked_files;
|
||||
mod linked_packages;
|
||||
mod material_preview;
|
||||
mod material_preview_commands;
|
||||
mod material_source;
|
||||
mod node_info_commands;
|
||||
mod open_document;
|
||||
mod package_commands;
|
||||
mod preview_cook;
|
||||
pub use material_preview::MaterialPreview;
|
||||
pub use material_source::MaterialSource;
|
||||
pub use open_document::OpenDocument;
|
||||
pub use preview_cook::PreviewCook;
|
||||
|
||||
mod alignment_commands;
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
use crate::document_path;
|
||||
use materialcraft_core::Package;
|
||||
|
||||
pub(crate) fn normalize_references(package: &mut Package, path: &str) {
|
||||
for instance in package.graphs.iter_mut().flat_map(|graph| graph.instances_mut()) {
|
||||
if !instance.package.is_empty() {
|
||||
instance.package = document_path::resolve(path, &instance.package);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Session {
|
||||
pub(crate) fn discard_source_changes(&mut self, path: &str) {
|
||||
self.linked_sources.remove(path);
|
||||
self.pkg.dependencies.remove(path);
|
||||
for document in self.documents.values_mut() {
|
||||
document.pkg.dependencies.remove(path);
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let mut package = self.pkg.clone();
|
||||
// Include inactive dependents so closing a source also refreshes their references
|
||||
package.graphs.extend(self.documents.values().flat_map(|document| document.pkg.graphs.iter().cloned()));
|
||||
package.dependencies = self.linked_sources.clone();
|
||||
for warning in load(&mut package) {
|
||||
self.warn(warning);
|
||||
}
|
||||
self.linked_sources.extend(package.dependencies);
|
||||
}
|
||||
self.refresh_links_from(std::collections::BTreeSet::from([path.to_string()]));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub(crate) fn load(package: &mut Package) -> Vec<String> {
|
||||
use std::{collections::BTreeSet, sync::Arc};
|
||||
let references = |package: &Package| {
|
||||
package
|
||||
.graphs
|
||||
.iter()
|
||||
.flat_map(|graph| graph.instances())
|
||||
.filter(|instance| !instance.package.is_empty())
|
||||
.map(|instance| instance.package.clone())
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let mut pending = references(package);
|
||||
let mut visited = BTreeSet::new();
|
||||
let mut warnings = Vec::new();
|
||||
let mut remaining = 256_u64 << 20;
|
||||
while let Some(path) = pending.pop() {
|
||||
if !visited.insert(path.clone()) {
|
||||
continue;
|
||||
}
|
||||
if visited.len() > 64 {
|
||||
warnings.push("Linked packages exceed the 64-file limit".into());
|
||||
break;
|
||||
}
|
||||
if let Some(source) = package.dependencies.get(&path) {
|
||||
pending.extend(references(source));
|
||||
continue;
|
||||
}
|
||||
let mut read = || -> std::result::Result<Package, String> {
|
||||
let size = std::fs::metadata(&path).map_err(|error| error.to_string())?.len();
|
||||
if size > remaining {
|
||||
return Err("linked package exceeds the remaining file budget".into());
|
||||
}
|
||||
// Bound the read even if the file grows after the metadata check
|
||||
use std::io::Read;
|
||||
let mut bytes = Vec::new();
|
||||
std::fs::File::open(&path).map_err(|error| error.to_string())?.take(remaining + 1).read_to_end(&mut bytes).map_err(|error| error.to_string())?;
|
||||
if bytes.len() as u64 > remaining {
|
||||
return Err("linked package exceeds the remaining file budget".into());
|
||||
}
|
||||
remaining -= bytes.len() as u64;
|
||||
let read = materialcraft_formats::sbs::read_sbs(&bytes, &path, &crate::file_cmds::linked_files(&path)).map_err(|error| error.to_string())?;
|
||||
if !read.warnings.is_empty() {
|
||||
return Err(read.warnings.join("; "));
|
||||
}
|
||||
Ok(read.pkg)
|
||||
};
|
||||
match read() {
|
||||
Ok(mut source) => {
|
||||
normalize_references(&mut source, &path);
|
||||
pending.extend(references(&source));
|
||||
package.dependencies.insert(path, Arc::new(source));
|
||||
}
|
||||
Err(error) => warnings.push(format!("Could not resolve {path}: {error}")),
|
||||
}
|
||||
}
|
||||
warnings
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
use crate::Session;
|
||||
use materialcraft_core::{GraphInstance, Package};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
fn references(package: &Package) -> impl Iterator<Item = &GraphInstance> {
|
||||
package.graphs.iter().flat_map(|graph| graph.instances())
|
||||
}
|
||||
|
||||
fn reaches(package: &Package, changed: &BTreeSet<String>, sources: &BTreeMap<String, Arc<Package>>) -> bool {
|
||||
let mut pending: Vec<_> = references(package).filter(|instance| !instance.package.is_empty()).map(|instance| instance.package.as_str()).collect();
|
||||
let mut visited = BTreeSet::new();
|
||||
while let Some(file) = pending.pop() {
|
||||
if !visited.insert(file) {
|
||||
continue;
|
||||
}
|
||||
if changed.contains(file) {
|
||||
return true;
|
||||
}
|
||||
if let Some(source) = sources.get(file) {
|
||||
pending.extend(references(source).filter(|instance| !instance.package.is_empty()).map(|instance| instance.package.as_str()));
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn interfaces(package: &mut Package, sources: &BTreeMap<String, Arc<Package>>) -> bool {
|
||||
let updates: Vec<_> = package
|
||||
.graphs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(graph_index, graph)| graph.nodes.iter().enumerate().map(move |(node_index, node)| (graph_index, node_index, graph, node)))
|
||||
.filter_map(|(graph_index, node_index, owner, node)| {
|
||||
let mut instance = node.instance.clone().or_else(|| {
|
||||
if node.def != "graph_instance" {
|
||||
return None;
|
||||
}
|
||||
let materialcraft_core::Value::Text(name) = node.params.get("graph")? else { return None };
|
||||
Some(GraphInstance { package: String::new(), graph: name.clone(), inputs: Vec::new(), outputs: Vec::new() })
|
||||
})?;
|
||||
let source = if instance.package.is_empty() { &*package } else { sources.get(&instance.package)?.as_ref() };
|
||||
let (_, graph) = source.graph_by_name(&instance.graph)?;
|
||||
if graph.function.is_some() {
|
||||
return None;
|
||||
}
|
||||
let (mut inputs, mut outputs) = materialcraft_nodes::graph_interface::graph_ports(graph);
|
||||
// Keep connected missing ports so saving cannot silently discard the user's wires
|
||||
for port in &instance.inputs {
|
||||
if owner.connections.iter().any(|wire| wire.to == node.id && wire.to_port == port.identifier)
|
||||
&& !inputs.iter().any(|input| input.identifier == port.identifier)
|
||||
{
|
||||
inputs.push(port.clone());
|
||||
}
|
||||
}
|
||||
for port in &instance.outputs {
|
||||
if owner.connections.iter().any(|wire| wire.from == node.id && wire.from_port == port.identifier)
|
||||
&& !outputs.iter().any(|output| output.identifier == port.identifier)
|
||||
{
|
||||
outputs.push(port.clone());
|
||||
}
|
||||
}
|
||||
instance.inputs = inputs;
|
||||
instance.outputs = outputs;
|
||||
(node.instance.as_ref() != Some(&instance)).then_some((graph_index, node_index, instance))
|
||||
})
|
||||
.collect();
|
||||
let mut changed = !updates.is_empty();
|
||||
for (graph, node, instance) in updates {
|
||||
if let Some(node) = package.graphs.get_mut(graph).and_then(|graph| graph.nodes.get_mut(node)) {
|
||||
node.instance = Some(instance);
|
||||
}
|
||||
}
|
||||
let local = package.clone();
|
||||
for function in package.graphs.iter_mut().flat_map(|graph| graph.functions_mut()) {
|
||||
if function.nodes.iter().any(|node| node.instance.is_some()) {
|
||||
let previous = function.clone();
|
||||
materialcraft_nodes::function::resolve_instances(function, &local, sources);
|
||||
changed |= *function != previous;
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Refresh source snapshots and invalidate open dependents without merging their undo histories
|
||||
pub(crate) fn refresh_links(&mut self) {
|
||||
self.refresh_links_from(BTreeSet::new());
|
||||
}
|
||||
|
||||
pub(crate) fn refresh_links_from(&mut self, mut changed: BTreeSet<String>) {
|
||||
let mut sources = self.linked_sources.clone();
|
||||
// Interface types can propagate through several files; the workspace has at most 64 open packages
|
||||
for _ in 0..64 {
|
||||
for (_, package, path, _) in self.open_packages() {
|
||||
let Some(path) = path else { continue };
|
||||
let same = sources
|
||||
.get(path)
|
||||
.is_some_and(|source| source.graphs == package.graphs && source.resources == package.resources && source.name == package.name);
|
||||
if !same {
|
||||
let mut source = package.clone();
|
||||
source.dependencies.clear();
|
||||
sources.insert(path.to_string(), Arc::new(source));
|
||||
changed.insert(path.to_string());
|
||||
}
|
||||
}
|
||||
let previous_sources = sources.clone();
|
||||
let mut updated = false;
|
||||
for (path, source) in &previous_sources {
|
||||
let mut package = source.as_ref().clone();
|
||||
if interfaces(&mut package, &previous_sources) {
|
||||
sources.insert(path.clone(), Arc::new(package));
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
updated |= interfaces(&mut self.pkg, &sources);
|
||||
for document in self.documents.values_mut() {
|
||||
updated |= interfaces(&mut document.pkg, &sources);
|
||||
}
|
||||
if !updated {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if reaches(&self.pkg, &changed, &sources) {
|
||||
self.modified = true;
|
||||
self.dirty = true;
|
||||
self.cook_epoch = self.cook_epoch.wrapping_add(1);
|
||||
}
|
||||
self.pkg.dependencies = sources.clone();
|
||||
for document in self.documents.values_mut() {
|
||||
if reaches(&document.pkg, &changed, &sources) {
|
||||
document.modified = true;
|
||||
}
|
||||
document.pkg.dependencies = sources.clone();
|
||||
}
|
||||
self.linked_sources = sources;
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,9 @@ impl MaterialSource {
|
||||
port = connection.from_port.clone();
|
||||
}
|
||||
"graph_instance" => {
|
||||
if let Some(instance) = &node.instance {
|
||||
return instance.outputs.iter().any(|output| output.identifier == port && matches!(output.type_code, 1 | 2));
|
||||
}
|
||||
let Some((index, graph)) = materialcraft_eval::instance_graph(package, node) else { return false };
|
||||
let Some(output) = graph.nodes.iter().find(|node| node.def == "output" && materialcraft_eval::identifier(node) == port) else {
|
||||
return false;
|
||||
|
||||
@@ -9,7 +9,7 @@ pub(crate) fn specs() -> Vec<CommandSpec> {
|
||||
"Edit node graph",
|
||||
["Edit"],
|
||||
Some("Ctrl+E"),
|
||||
"{} resolves the selected FX-Map, processor or local graph instance to {kind, node, parameter?, graph?}; UI invocation opens it in the graph panel",
|
||||
"{} resolves the selected FX-Map, processor or graph instance to {kind, node, parameter?, graph?, document?, path?}; UI invocation opens its reference",
|
||||
|session| resolve(session).map(|_| ()),
|
||||
|session, _| resolve(session).map_err(|error| bad("graph.node.edit", error))
|
||||
)]
|
||||
@@ -30,7 +30,12 @@ fn resolve(session: &Session) -> std::result::Result<Value, String> {
|
||||
}
|
||||
"graph_instance" => {
|
||||
let (index, _) = materialcraft_eval::instance_graph(&session.pkg, node).ok_or("the referenced graph is missing")?;
|
||||
Ok(json!({"kind": "graph", "node": id, "graph": index}))
|
||||
let path = node.instance.as_ref().filter(|instance| !instance.package.is_empty()).map(|instance| instance.package.as_str());
|
||||
let document = match path {
|
||||
Some(path) => session.open_packages().into_iter().find(|entry| entry.2 == Some(path)).map(|entry| entry.0),
|
||||
None => Some(session.document_id),
|
||||
};
|
||||
Ok(json!({"kind": "graph", "node": id, "graph": index, "document": document, "path": path}))
|
||||
}
|
||||
_ => Err("the selected node has no editable graph".into()),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
use crate::{
|
||||
Result, Session, cmd,
|
||||
commands::{CommandSpec, bad, has_graph, u64p},
|
||||
};
|
||||
use materialcraft_core::NodeId;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub(crate) fn specs() -> Vec<CommandSpec> {
|
||||
vec![cmd!(
|
||||
"graph.node.info",
|
||||
"Copy node info",
|
||||
[],
|
||||
None,
|
||||
"{id} returns text describing a node without changing the document or node clipboard",
|
||||
has_graph,
|
||||
info
|
||||
)]
|
||||
}
|
||||
|
||||
fn info(session: &mut Session, parameters: &Value) -> Result<Value> {
|
||||
let command = "graph.node.info";
|
||||
let id = NodeId(u64p(parameters, "id").ok_or_else(|| bad(command, "id is required"))?);
|
||||
let node = session.graph()?.node(id)?;
|
||||
let text = serde_json::to_string_pretty(node).map_err(|error| bad(command, error.to_string()))?;
|
||||
Ok(json!({"text": text}))
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
use crate::{AnnotationSelection, Clip, EmbeddedClipboard, MaterialPreview, Session, View2d, View3d, snapshot::Snapshot};
|
||||
use materialcraft_core::{NodeId, Package};
|
||||
|
||||
/// Editing state retained while another SBS package is active
|
||||
pub struct OpenDocument {
|
||||
pub id: u64,
|
||||
pub pkg: Package,
|
||||
pub path: Option<String>,
|
||||
pub graph: usize,
|
||||
pub modified: bool,
|
||||
selection: Vec<NodeId>,
|
||||
annotation_selection: Option<AnnotationSelection>,
|
||||
view2d: View2d,
|
||||
view3d: View3d,
|
||||
material_preview: MaterialPreview,
|
||||
load_warnings: Vec<String>,
|
||||
clipboard: Option<Clip>,
|
||||
embedded_clipboard: Option<EmbeddedClipboard>,
|
||||
undo: Vec<Snapshot>,
|
||||
redo: Vec<Snapshot>,
|
||||
}
|
||||
|
||||
impl OpenDocument {
|
||||
pub(crate) fn new(id: u64, pkg: Package, path: Option<String>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
material_preview: MaterialPreview::for_graph(&pkg, 0),
|
||||
pkg,
|
||||
path,
|
||||
graph: 0,
|
||||
modified: false,
|
||||
selection: Vec::new(),
|
||||
annotation_selection: None,
|
||||
view2d: View2d::default(),
|
||||
view3d: View3d::default(),
|
||||
load_warnings: Vec::new(),
|
||||
clipboard: None,
|
||||
embedded_clipboard: None,
|
||||
undo: Vec::new(),
|
||||
redo: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn exchange(&mut self, session: &mut Session) {
|
||||
session.end_group();
|
||||
session.finish_edit();
|
||||
std::mem::swap(&mut self.id, &mut session.document_id);
|
||||
macro_rules! exchange {
|
||||
($($field:ident),+ $(,)?) => { $(std::mem::swap(&mut self.$field, &mut session.$field);)+ };
|
||||
}
|
||||
exchange!(
|
||||
pkg,
|
||||
path,
|
||||
graph,
|
||||
modified,
|
||||
selection,
|
||||
annotation_selection,
|
||||
view2d,
|
||||
view3d,
|
||||
material_preview,
|
||||
load_warnings,
|
||||
clipboard,
|
||||
embedded_clipboard,
|
||||
undo,
|
||||
redo
|
||||
);
|
||||
// A worker from the previous document must never publish into this one
|
||||
session.cook_epoch = session.cook_epoch.wrapping_add(1);
|
||||
session.clear_preview_results();
|
||||
session.cooked = Default::default();
|
||||
session.clear_decoded();
|
||||
session.dirty = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
use crate::{
|
||||
OpenDocument, Result, Session, cmd,
|
||||
commands::{CommandSpec, always, bad, u64p},
|
||||
};
|
||||
use materialcraft_core::Package;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub(crate) fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"package.activate",
|
||||
"Open package",
|
||||
[],
|
||||
None,
|
||||
"{document} activates an open package without losing edits or history",
|
||||
always,
|
||||
|session, parameters| {
|
||||
let id = u64p(parameters, "document").ok_or_else(|| bad("package.activate", "document is required"))?;
|
||||
session.activate_document(id)?;
|
||||
Ok(json!({"document": session.document_id}))
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"package.close",
|
||||
"Close package",
|
||||
[],
|
||||
None,
|
||||
"{document?, discard?: bool} closes a package; modified packages require explicit discard",
|
||||
always,
|
||||
|session, parameters| {
|
||||
let id = u64p(parameters, "document").unwrap_or(session.document_id);
|
||||
let modified = session
|
||||
.open_packages()
|
||||
.into_iter()
|
||||
.find(|entry| entry.0 == id)
|
||||
.map(|entry| entry.3)
|
||||
.ok_or_else(|| bad("package.close", "the package is no longer open"))?;
|
||||
if modified && parameters.get("discard").and_then(Value::as_bool) != Some(true) {
|
||||
return Err(bad("package.close", "save the package or explicitly discard its edits"));
|
||||
}
|
||||
let path = session.open_packages().into_iter().find(|entry| entry.0 == id).and_then(|entry| entry.2).map(str::to_string);
|
||||
if id != session.document_id {
|
||||
session.documents.remove(&id);
|
||||
} else if let Some(id) = session.documents.keys().next().copied() {
|
||||
if let Some(mut document) = session.documents.remove(&id) {
|
||||
document.exchange(session);
|
||||
}
|
||||
} else {
|
||||
crate::file_cmds::replace_package(session, Package::new("Untitled package"), None);
|
||||
}
|
||||
if modified && let Some(path) = path {
|
||||
session.discard_source_changes(&path);
|
||||
}
|
||||
Ok(Value::Null)
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"package.closeAll",
|
||||
"Close all packages",
|
||||
[],
|
||||
None,
|
||||
"{discard?: bool} closes all packages; modified packages require explicit discard",
|
||||
always,
|
||||
|session, parameters| {
|
||||
if session.open_packages().iter().any(|entry| entry.3) && parameters.get("discard").and_then(Value::as_bool) != Some(true) {
|
||||
return Err(bad("package.closeAll", "save the packages or explicitly discard their edits"));
|
||||
}
|
||||
session.documents.clear();
|
||||
session.linked_sources.clear();
|
||||
crate::file_cmds::replace_package(session, Package::new("Untitled package"), None);
|
||||
Ok(Value::Null)
|
||||
}
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn activate_document(&mut self, id: u64) -> Result<()> {
|
||||
if id == self.document_id {
|
||||
return Ok(());
|
||||
}
|
||||
if !self.documents.contains_key(&id) {
|
||||
return Err(bad("package.activate", "the package is no longer open"));
|
||||
}
|
||||
// Publish pending source edits while the destination is still among the open dependents
|
||||
self.end_group();
|
||||
let mut document = self.documents.remove(&id).ok_or_else(|| bad("package.activate", "the package is no longer open"))?;
|
||||
document.exchange(self);
|
||||
self.documents.insert(document.id, document);
|
||||
self.refresh_links();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn open_package(&mut self, package: Package, path: Option<String>) -> Result<()> {
|
||||
if self.documents.len() >= 63 {
|
||||
return Err(bad("file.open", "at most 64 packages can be open at once"));
|
||||
}
|
||||
let id = self.next_document_id;
|
||||
self.next_document_id = id.checked_add(1).ok_or_else(|| bad("file.open", "document identifiers exhausted"))?;
|
||||
self.linked_sources.extend(package.dependencies.clone());
|
||||
let mut document = OpenDocument::new(id, package, path);
|
||||
document.exchange(self);
|
||||
self.documents.insert(document.id, document);
|
||||
self.refresh_links();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn open_packages(&self) -> Vec<(u64, &Package, Option<&str>, bool)> {
|
||||
let mut packages = vec![(self.document_id, &self.pkg, self.path.as_deref(), self.modified)];
|
||||
packages.extend(self.documents.values().map(|document| (document.id, &document.pkg, document.path.as_deref(), document.modified)));
|
||||
packages.sort_by_key(|entry| entry.0);
|
||||
packages
|
||||
}
|
||||
|
||||
pub fn inspect_documents(&self) -> Value {
|
||||
json!(self.open_packages().into_iter().map(|(id, package, path, modified)| json!({
|
||||
"id": id, "name": package.name, "path": path, "modified": modified,
|
||||
"active": id == self.document_id,
|
||||
"graphs": package.graphs.iter().enumerate().map(|(index, graph)| json!({"index": index, "name": graph.name, "kind": graph.kind()})).collect::<Vec<_>>()
|
||||
})).collect::<Vec<_>>())
|
||||
}
|
||||
}
|
||||
@@ -119,6 +119,10 @@ pub struct View2d {
|
||||
pub const WARNING_PREFIX: &str = "Warning: ";
|
||||
|
||||
pub struct Session {
|
||||
pub document_id: u64,
|
||||
pub(crate) next_document_id: u64,
|
||||
pub(crate) documents: std::collections::BTreeMap<u64, crate::OpenDocument>,
|
||||
pub(crate) linked_sources: std::collections::BTreeMap<String, Arc<Package>>,
|
||||
pub pkg: Package,
|
||||
/// Where the package was opened from or last saved to.
|
||||
pub path: Option<String>,
|
||||
@@ -151,8 +155,8 @@ pub struct Session {
|
||||
pub cache: Arc<Mutex<Cache>>,
|
||||
/// A mesh loaded for the 3D view (`scene: "custom"`): its file name and geometry.
|
||||
pub custom_mesh: Option<(String, Arc<materialcraft_geom::Mesh>)>,
|
||||
undo: Vec<Snapshot>,
|
||||
redo: Vec<Snapshot>,
|
||||
pub(crate) undo: Vec<Snapshot>,
|
||||
pub(crate) redo: Vec<Snapshot>,
|
||||
/// Edits with the same key coalesce into one undo step (slider drags).
|
||||
merge_key: Option<String>,
|
||||
group: Option<(Snapshot, bool)>,
|
||||
@@ -161,7 +165,11 @@ pub struct Session {
|
||||
|
||||
impl Session {
|
||||
pub fn new(pkg: Package) -> Session {
|
||||
Session {
|
||||
let mut session = Session {
|
||||
document_id: 0,
|
||||
next_document_id: 1,
|
||||
documents: std::collections::BTreeMap::new(),
|
||||
linked_sources: pkg.dependencies.clone(),
|
||||
material_preview: crate::MaterialPreview::for_graph(&pkg, 0),
|
||||
cooked_graph: None,
|
||||
material_cooked: std::collections::BTreeMap::new(),
|
||||
@@ -189,7 +197,9 @@ impl Session {
|
||||
merge_key: None,
|
||||
group: None,
|
||||
decoded: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
};
|
||||
session.refresh_links();
|
||||
session
|
||||
}
|
||||
|
||||
/// The graph being edited.
|
||||
@@ -261,6 +271,7 @@ impl Session {
|
||||
&& edited
|
||||
{
|
||||
self.record_undo(snap);
|
||||
self.refresh_links();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +345,10 @@ impl Session {
|
||||
pub fn changed(&mut self) {
|
||||
self.modified = true;
|
||||
self.dirty = true;
|
||||
// Publish a grouped edit only after it succeeds, so rollback cannot dirty other packages
|
||||
if self.group.is_none() {
|
||||
self.refresh_links();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_undo(&self) -> bool {
|
||||
@@ -358,6 +373,7 @@ impl Session {
|
||||
self.restore(s);
|
||||
self.merge_key = None;
|
||||
self.dirty = true;
|
||||
self.refresh_links();
|
||||
Some(label)
|
||||
}
|
||||
|
||||
@@ -370,6 +386,7 @@ impl Session {
|
||||
self.restore(s);
|
||||
self.merge_key = None;
|
||||
self.dirty = true;
|
||||
self.refresh_links();
|
||||
Some(label)
|
||||
}
|
||||
|
||||
@@ -383,7 +400,13 @@ impl Session {
|
||||
|
||||
/// A decoder for the package's bitmap resources (memoized, shareable with worker threads).
|
||||
pub fn resource_decoder(&self) -> impl Fn(&str) -> Option<Image> + Sync + Send + 'static {
|
||||
let res: Vec<(String, ResourceBytes)> = self.pkg.resources.iter().map(|r| (r.name.clone(), r.bytes.clone())).collect();
|
||||
let mut res: Vec<(String, ResourceBytes)> = self.pkg.resources.iter().map(|r| (r.name.clone(), r.bytes.clone())).collect();
|
||||
res.extend(
|
||||
self.pkg
|
||||
.dependencies
|
||||
.iter()
|
||||
.flat_map(|(file, package)| package.resources.iter().map(move |resource| (format!("{file}\0{}", resource.name), resource.bytes.clone()))),
|
||||
);
|
||||
let memo = self.decoded.clone();
|
||||
// Release decoded images of deleted or replaced resources
|
||||
memo.lock().unwrap_or_else(PoisonError::into_inner).retain(|(k, _), _| res.iter().any(|(_, b)| b.content_hash() == *k));
|
||||
|
||||
@@ -194,7 +194,7 @@ fn selected_node_graph_is_resolved_without_changing_the_document() {
|
||||
session.pkg.graphs.push(materialcraft_engine::core::Graph::new("Child"));
|
||||
let original = session.pkg.clone();
|
||||
let history = session.history();
|
||||
assert_eq!(execute(&mut session, "graph.node.edit", &json!({})).unwrap(), json!({"kind": "graph", "node": node, "graph": 1}));
|
||||
assert_eq!(execute(&mut session, "graph.node.edit", &json!({})).unwrap(), json!({"kind": "graph", "node": node, "graph": 1, "document": 0, "path": null}));
|
||||
assert_eq!(session.pkg, original);
|
||||
assert_eq!(session.history(), history);
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
use materialcraft_engine::{
|
||||
Session,
|
||||
core::{ExposedParameter, FunctionGraph, Graph, GraphInstance, GraphPort, NodeId, Package, Value},
|
||||
execute, file_cmds, formats,
|
||||
nodes::function::{self, EvaluationContext, EvaluationState},
|
||||
};
|
||||
use serde_json::{Value as Json, json};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
fn source() -> Package {
|
||||
let mut package = Package::new("Functions");
|
||||
let mut graph = Graph::new("Twice");
|
||||
graph.exposed.push(ExposedParameter {
|
||||
id: "amount".into(),
|
||||
label: "Amount".into(),
|
||||
group: String::new(),
|
||||
default: Value::Float(0.5),
|
||||
min: 0.0,
|
||||
max: 1.0,
|
||||
options: Vec::new(),
|
||||
});
|
||||
let mut function = FunctionGraph::default();
|
||||
let input = function.add("get_float1", &[], Some(Value::Text("amount".into())));
|
||||
let factor = function.add_constant(Value::Float(2.0));
|
||||
function.output = Some(function.add("mul", &[input, factor], None));
|
||||
graph.function = Some(function);
|
||||
package.graphs.push(graph);
|
||||
let mut graph = Graph::new("Tint");
|
||||
graph.function = Some(FunctionGraph::constant(Value::Float4([0.2, 0.4, 0.6, 1.0])));
|
||||
package.graphs.push(graph);
|
||||
package
|
||||
}
|
||||
|
||||
fn edit(session: &mut Session, target: &Json, edit: Json) -> Json {
|
||||
execute(session, "graph.function.edit", &json!({"target": target, "edit": edit})).unwrap()
|
||||
}
|
||||
|
||||
fn call(session: &mut Session, document: u64, graph: &str, target: &Json) -> u32 {
|
||||
execute(session, "graph.instance.add", &json!({"document": document, "graph": graph, "function": target})).unwrap()["id"].as_u64().unwrap() as u32
|
||||
}
|
||||
|
||||
fn evaluate(graph: &FunctionGraph) -> Value {
|
||||
function::compile(graph, 0)
|
||||
.unwrap()
|
||||
.evaluate(
|
||||
&EvaluationContext { position: [0.5; 2], size: [16.0; 2], images: &[], tile: true, variables: &BTreeMap::new() },
|
||||
&mut EvaluationState::default(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn functions_link_across_documents_and_keep_independent_undo() {
|
||||
let mut session = Session::new(source());
|
||||
session.path = Some(std::env::temp_dir().join("materialcraft-function-source.sbs").to_string_lossy().into_owned());
|
||||
let mut target_package = Package::new("Caller");
|
||||
let mut graph = Graph::new("Caller");
|
||||
graph.function = Some(FunctionGraph::default());
|
||||
target_package.graphs.push(graph);
|
||||
file_cmds::open_bytes(&mut session, &formats::sbs::write_sbs(&target_package).unwrap(), Some("function-target.sbs".into())).unwrap();
|
||||
let document = session.document_id;
|
||||
let target = json!({"kind": "graph"});
|
||||
let instance = call(&mut session, 0, "Twice", &target);
|
||||
let constant = edit(&mut session, &target, json!({"action": "add", "operation": "const_float1", "at": [0,0]}))["id"].as_u64().unwrap();
|
||||
edit(&mut session, &target, json!({"action": "value", "id": constant, "value": Value::Float(0.25)}));
|
||||
edit(&mut session, &target, json!({"action": "output", "id": instance}));
|
||||
assert!(function::warnings(session.graph().unwrap().function.as_ref().unwrap())[0].contains("missing input"));
|
||||
edit(&mut session, &target, json!({"action": "connect", "from": constant, "to": instance, "input": 0}));
|
||||
assert_eq!(evaluate(session.graph().unwrap().function.as_ref().unwrap()), Value::Float(0.5));
|
||||
let history = session.history();
|
||||
session.modified = false;
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
edit(&mut session, &target, json!({"action": "value", "id": 2, "value": Value::Float(3.0)}));
|
||||
execute(&mut session, "package.activate", &json!({"document": document})).unwrap();
|
||||
assert!(session.modified);
|
||||
assert_eq!(session.history(), history);
|
||||
assert_eq!(evaluate(session.graph().unwrap().function.as_ref().unwrap()), Value::Float(0.75));
|
||||
execute(&mut session, "edit.undo", &json!({})).unwrap();
|
||||
assert!(function::compile(session.graph().unwrap().function.as_ref().unwrap(), 0).is_err());
|
||||
execute(&mut session, "edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(evaluate(session.graph().unwrap().function.as_ref().unwrap()), Value::Float(0.75));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn processor_calls_recook_when_source_changes_and_round_trip_without_copying_source() {
|
||||
let mut session = Session::new(source());
|
||||
let directory = std::env::temp_dir().join(format!("materialcraft-function-links-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&directory).unwrap();
|
||||
let source_path = directory.join("source.sbs");
|
||||
let target_path = directory.join("target.sbs");
|
||||
execute(&mut session, "file.saveAs", &json!({"path": source_path})).unwrap();
|
||||
let mut target_package = Package::new("Processors");
|
||||
target_package.graphs.push(Graph::new("Processors"));
|
||||
file_cmds::open_bytes(&mut session, &formats::sbs::write_sbs(&target_package).unwrap(), Some(target_path.to_string_lossy().into())).unwrap();
|
||||
let document = session.document_id;
|
||||
let mut processors = Vec::new();
|
||||
for def in ["pixel_processor", "value_processor"] {
|
||||
let id = execute(&mut session, "graph.node.add", &json!({"def": def})).unwrap()["id"].as_u64().unwrap();
|
||||
let target = json!({"kind": "parameter", "node": id, "parameter": "function"});
|
||||
let instance = call(&mut session, 0, "Tint", &target);
|
||||
edit(&mut session, &target, json!({"action": "output", "id": instance}));
|
||||
processors.push(NodeId(id));
|
||||
}
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(processors[0], 0).unwrap().get(0, 0, false), [0.2, 0.4, 0.6, 1.0]);
|
||||
assert_eq!(session.cooked.value(processors[1], 0), Some(&Value::Float4([0.2, 0.4, 0.6, 1.0])));
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
execute(&mut session, "graph.open", &json!({"graph": 1})).unwrap();
|
||||
edit(&mut session, &json!({"kind": "graph"}), json!({"action": "value", "id": 1, "value": Value::Float4([0.7,0.3,0.1,1.0])}));
|
||||
execute(&mut session, "file.save", &json!({})).unwrap();
|
||||
execute(&mut session, "package.activate", &json!({"document": document})).unwrap();
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(processors[0], 0).unwrap().get(0, 0, false), [0.7, 0.3, 0.1, 1.0]);
|
||||
assert_eq!(session.cooked.value(processors[1], 0), Some(&Value::Float4([0.7, 0.3, 0.1, 1.0])));
|
||||
execute(&mut session, "file.saveAs", &json!({"path": target_path})).unwrap();
|
||||
let saved = std::fs::read(&target_path).unwrap();
|
||||
let xml = String::from_utf8(saved.clone()).unwrap();
|
||||
assert!(xml.contains("source.sbs") && !xml.contains(source_path.to_str().unwrap()));
|
||||
assert!(xml.contains("pkg:///Tint?dependency="));
|
||||
assert!(!xml.contains("0.7 0.3 0.1"));
|
||||
let mut reopened = Session::new(Package::new("Empty"));
|
||||
execute(&mut reopened, "file.open", &json!({"path": target_path})).unwrap();
|
||||
reopened.preview_size = Some(16);
|
||||
reopened.cook();
|
||||
for result in reopened.cooked.nodes.values() {
|
||||
assert!(result.error.is_none(), "{:?}", result.error);
|
||||
}
|
||||
std::fs::remove_file(&source_path).unwrap();
|
||||
let mut missing = Session::new(Package::new("Empty"));
|
||||
file_cmds::open_bytes(&mut missing, &saved, Some(target_path.to_string_lossy().into())).unwrap();
|
||||
assert!(missing.graph().unwrap().functions().all(|function| function.nodes.iter().any(|node| node.instance.is_some())));
|
||||
assert!(missing.graph().unwrap().functions().all(|function| !function::warnings(function).is_empty()));
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_calls_reject_wrong_canvas_cycles_and_outputless_sources() {
|
||||
let mut package = source();
|
||||
let mut incomplete = Graph::new("Incomplete");
|
||||
incomplete.function = Some(FunctionGraph::default());
|
||||
package.graphs.push(incomplete);
|
||||
package.graphs.push(Graph::new("Material"));
|
||||
let mut session = Session::new(package);
|
||||
let before = session.pkg.clone();
|
||||
for name in ["Twice", "Incomplete", "Material"] {
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": name})).is_err());
|
||||
}
|
||||
assert_eq!(before, session.pkg);
|
||||
execute(&mut session, "graph.open", &json!({"graph": 3})).unwrap();
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Tint"})).is_err());
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Tint", "pattern": 1})).is_err());
|
||||
execute(&mut session, "graph.open", &json!({"graph": 2})).unwrap();
|
||||
call(&mut session, 0, "Tint", &json!({"kind": "graph"}));
|
||||
assert!(session.graph().unwrap().function.as_ref().unwrap().output.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calls_isolate_variables_and_evaluate_arguments_once() {
|
||||
let mut package = source();
|
||||
let mut source = FunctionGraph::default();
|
||||
let input = source.add("get_float1", &[], Some(Value::Text("amount".into())));
|
||||
let private = source.add_constant(Value::Float(99.0));
|
||||
let write = source.add("set", &[private], Some(Value::Text("private".into())));
|
||||
let twice = source.add("add", &[input, input], None);
|
||||
source.output = Some(source.add("sequence", &[write, twice], None));
|
||||
package.graphs[0].function = Some(source);
|
||||
let mut caller = FunctionGraph::default();
|
||||
let one = caller.add_constant(Value::Float(1.0));
|
||||
let init = caller.add("set", &[one], Some(Value::Text("private".into())));
|
||||
let get = caller.add("get_float1", &[], Some(Value::Text("private".into())));
|
||||
let next = caller.add("add", &[get, one], None);
|
||||
let argument = caller.add("set", &[next], Some(Value::Text("private".into())));
|
||||
let call = caller.add("instance", &[argument], None);
|
||||
caller.nodes.last_mut().unwrap().instance = Some(GraphInstance {
|
||||
package: String::new(),
|
||||
graph: "Twice".into(),
|
||||
inputs: vec![GraphPort { identifier: "amount".into(), type_code: 256 }],
|
||||
outputs: vec![GraphPort { identifier: "output".into(), type_code: 256 }],
|
||||
});
|
||||
let total = caller.add("add", &[call, get], None);
|
||||
caller.output = Some(caller.add("sequence", &[init, total], None));
|
||||
function::resolve_instances(&mut caller, &package, &BTreeMap::new());
|
||||
assert_eq!(evaluate(&caller), Value::Float(6.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connected_inputs_survive_source_interface_changes_and_missing_sources() {
|
||||
let mut session = Session::new(source());
|
||||
execute(&mut session, "file.new", &json!({"name": "Caller", "kind": "function"})).unwrap();
|
||||
let target = json!({"kind": "graph"});
|
||||
let instance = call(&mut session, 0, "Twice", &target);
|
||||
let constant = edit(&mut session, &target, json!({"action": "add", "operation": "const_float1", "at": [0,0]}))["id"].as_u64().unwrap();
|
||||
edit(&mut session, &target, json!({"action": "connect", "from": constant, "to": instance, "input": 0}));
|
||||
edit(&mut session, &target, json!({"action": "output", "id": instance}));
|
||||
execute(&mut session, "graph.open", &json!({"graph": 0})).unwrap();
|
||||
execute(&mut session, "graph.exposed.delete", &json!({"id": "amount"})).unwrap();
|
||||
let caller = session.pkg.graphs[2].function.as_ref().unwrap();
|
||||
assert_eq!(caller.node(instance).unwrap().inputs, vec![Some(constant as u32)]);
|
||||
assert!(function::warnings(caller)[0].contains("no longer exists"));
|
||||
let saved = formats::sbs::write_sbs(&session.pkg).unwrap();
|
||||
let read = formats::sbs::read_sbs(&saved, "interface.sbs", &|_| None).unwrap();
|
||||
assert!(read.warnings.is_empty(), "{:?}", read.warnings);
|
||||
let reopened = Session::new(read.pkg);
|
||||
let caller = reopened.pkg.graphs[2].function.as_ref().unwrap();
|
||||
let instance = caller.nodes.iter().find(|node| node.instance.is_some()).unwrap();
|
||||
assert_eq!(instance.inputs.len(), 1);
|
||||
assert!(instance.inputs[0].is_some());
|
||||
assert!(function::warnings(caller)[0].contains("no longer exists"));
|
||||
execute(&mut session, "edit.undo", &json!({})).unwrap();
|
||||
assert!(function::warnings(session.pkg.graphs[2].function.as_ref().unwrap()).is_empty());
|
||||
execute(&mut session, "graph.rename", &json!({"name": "Renamed function"})).unwrap();
|
||||
let caller = session.pkg.graphs[2].function.as_ref().unwrap();
|
||||
assert_eq!(caller.node(instance_id(caller)).unwrap().instance.as_ref().unwrap().graph, "Renamed function");
|
||||
assert!(function::warnings(caller).is_empty());
|
||||
}
|
||||
|
||||
fn instance_id(function: &FunctionGraph) -> u32 {
|
||||
function.nodes.iter().find(|node| node.instance.is_some()).unwrap().id
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_calls_propagate_edits_and_reject_indirect_cycles() {
|
||||
let mut session = Session::new(source());
|
||||
let target = json!({"kind": "graph"});
|
||||
execute(&mut session, "file.new", &json!({"name": "Middle", "kind": "function"})).unwrap();
|
||||
let inner = call(&mut session, 0, "Tint", &target);
|
||||
edit(&mut session, &target, json!({"action": "output", "id": inner}));
|
||||
execute(&mut session, "file.new", &json!({"name": "Outer", "kind": "function"})).unwrap();
|
||||
let outer = call(&mut session, 0, "Middle", &target);
|
||||
edit(&mut session, &target, json!({"action": "output", "id": outer}));
|
||||
assert_eq!(evaluate(session.graph().unwrap().function.as_ref().unwrap()), Value::Float4([0.2, 0.4, 0.6, 1.0]));
|
||||
execute(&mut session, "graph.open", &json!({"graph": 1})).unwrap();
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Outer"})).is_err());
|
||||
edit(&mut session, &target, json!({"action": "value", "id": 1, "value": Value::Float4([0.1,0.2,0.3,1.0])}));
|
||||
assert_eq!(evaluate(session.pkg.graphs[3].function.as_ref().unwrap()), Value::Float4([0.1, 0.2, 0.3, 1.0]));
|
||||
let reopened = Session::new(formats::sbs::read_sbs(&formats::sbs::write_sbs(&session.pkg).unwrap(), "nested.sbs", &|_| None).unwrap().pkg);
|
||||
assert_eq!(evaluate(reopened.pkg.graphs[3].function.as_ref().unwrap()), Value::Float4([0.1, 0.2, 0.3, 1.0]));
|
||||
execute(&mut session, "graph.open", &json!({"graph": 3})).unwrap();
|
||||
let before = session.pkg.clone();
|
||||
let history = session.history();
|
||||
let reference = execute(&mut session, "graph.instance.reference", &json!({"id": outer, "function": target})).unwrap();
|
||||
assert_eq!(reference["graph"], 2);
|
||||
assert_eq!(reference["document"], 0);
|
||||
assert_eq!(session.pkg, before);
|
||||
assert_eq!(session.history(), history);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_edits_refresh_main_instances_through_a_closed_middle_package() {
|
||||
let directory = std::env::temp_dir().join(format!("materialcraft-nested-function-files-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&directory).unwrap();
|
||||
let mut session = Session::new(source());
|
||||
execute(&mut session, "file.saveAs", &json!({"path": directory.join("source.sbs")})).unwrap();
|
||||
let mut middle = Package::new("Middle");
|
||||
middle.graphs.push(Graph::new("Material"));
|
||||
file_cmds::open_bytes(&mut session, &formats::sbs::write_sbs(&middle).unwrap(), Some(directory.join("middle.sbs").to_string_lossy().into())).unwrap();
|
||||
let middle_document = session.document_id;
|
||||
let pixel = execute(&mut session, "graph.node.add", &json!({"def": "pixel_processor"})).unwrap()["id"].as_u64().unwrap();
|
||||
let target = json!({"kind": "parameter", "node": pixel, "parameter": "function"});
|
||||
let instance = call(&mut session, 0, "Tint", &target);
|
||||
edit(&mut session, &target, json!({"action": "output", "id": instance}));
|
||||
let output = execute(&mut session, "graph.node.add", &json!({"def": "output"})).unwrap()["id"].as_u64().unwrap();
|
||||
execute(&mut session, "graph.connect", &json!({"from": pixel, "to": output})).unwrap();
|
||||
execute(&mut session, "file.save", &json!({})).unwrap();
|
||||
let mut outer = Package::new("Outer");
|
||||
outer.graphs.push(Graph::new("Outer"));
|
||||
let outer_path = directory.join("outer.sbs");
|
||||
file_cmds::open_bytes(&mut session, &formats::sbs::write_sbs(&outer).unwrap(), Some(outer_path.to_string_lossy().into())).unwrap();
|
||||
let outer_document = session.document_id;
|
||||
let instance =
|
||||
NodeId(execute(&mut session, "graph.instance.add", &json!({"document": middle_document, "graph": "Material"})).unwrap()["id"].as_u64().unwrap());
|
||||
execute(&mut session, "file.save", &json!({})).unwrap();
|
||||
execute(&mut session, "package.close", &json!({"document": middle_document})).unwrap();
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.2, 0.4, 0.6, 1.0]);
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
execute(&mut session, "graph.open", &json!({"graph": 1})).unwrap();
|
||||
edit(&mut session, &json!({"kind": "graph"}), json!({"action": "value", "id": 1, "value": Value::Float4([0.6,0.4,0.2,1.0])}));
|
||||
execute(&mut session, "file.save", &json!({})).unwrap();
|
||||
execute(&mut session, "package.activate", &json!({"document": outer_document})).unwrap();
|
||||
assert!(session.modified);
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.6, 0.4, 0.2, 1.0]);
|
||||
let mut reopened = Session::new(Package::new("Empty"));
|
||||
execute(&mut reopened, "file.open", &json!({"path": outer_path})).unwrap();
|
||||
reopened.preview_size = Some(16);
|
||||
reopened.cook();
|
||||
let result = reopened.cooked.nodes.values().next().unwrap();
|
||||
assert!(result.error.is_none(), "{:?}", result.error);
|
||||
assert_eq!(result.outputs[0].as_image().unwrap().get(0, 0, false), [0.6, 0.4, 0.2, 1.0]);
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_instance_metadata_is_rejected_without_changing_the_document() {
|
||||
let mut session = Session::new(source());
|
||||
execute(&mut session, "file.new", &json!({"name": "Caller", "kind": "function"})).unwrap();
|
||||
let instance = call(&mut session, 0, "Tint", &json!({"kind": "graph"}));
|
||||
let before = session.pkg.clone();
|
||||
let mut function = session.graph().unwrap().function.clone().unwrap();
|
||||
function.nodes.iter_mut().find(|node| node.id == instance).unwrap().op = "const_float4".into();
|
||||
assert!(execute(&mut session, "graph.function.set", &json!({"function": function})).is_err());
|
||||
assert_eq!(session.pkg, before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cyclic_references_loaded_from_sbs_report_errors_without_recursing_forever() {
|
||||
let mut package = Package::new("Malformed reference");
|
||||
let mut graph = Graph::new("Recursive function");
|
||||
let mut function = FunctionGraph::default();
|
||||
let id = function.add("instance", &[], None);
|
||||
function.nodes[0].instance = Some(GraphInstance {
|
||||
package: String::new(),
|
||||
graph: graph.name.clone(),
|
||||
inputs: Vec::new(),
|
||||
outputs: vec![GraphPort { identifier: "output".into(), type_code: 256 }],
|
||||
});
|
||||
function.output = Some(id);
|
||||
graph.function = Some(function);
|
||||
package.graphs.push(graph);
|
||||
let bytes = formats::sbs::write_sbs(&package).unwrap();
|
||||
let report = formats::sbs::read_sbs(&bytes, "cycle.sbs", &|_| None).unwrap();
|
||||
assert!(report.warnings.is_empty());
|
||||
let session = Session::new(report.pkg);
|
||||
let function = session.graph().unwrap().function.as_ref().unwrap();
|
||||
assert!(function::warnings(function)[0].contains("cycle"));
|
||||
let error = function::compile(function, 0).err().unwrap();
|
||||
assert!(error.contains("cycle"), "{error}");
|
||||
}
|
||||
@@ -97,6 +97,8 @@ fn connected_selection_performance() {
|
||||
let mut graph = FunctionGraph::default();
|
||||
for id in 1..=4096 {
|
||||
graph.nodes.push(materialcraft_engine::core::FunctionNode {
|
||||
instance: None,
|
||||
source: None,
|
||||
id,
|
||||
op: "passthrough".into(),
|
||||
type_code: 0,
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
use materialcraft_engine::{
|
||||
Session,
|
||||
core::{Graph, NodeId, Package, Value},
|
||||
execute, file_cmds, formats,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
fn source() -> (Package, NodeId) {
|
||||
let mut package = Package::new("Source file");
|
||||
let mut graph = Graph::new("Source");
|
||||
graph.size = [4, 4];
|
||||
let value = graph.add_node("uniform", [0.0; 2]);
|
||||
graph.node_mut(value).unwrap().params.insert("color".into(), Value::Color([0.25, 0.5, 0.75, 1.0]));
|
||||
let output = graph.add_node("output", [200.0, 0.0]);
|
||||
graph.node_mut(output).unwrap().params.insert("identifier".into(), Value::Text("color".into()));
|
||||
graph.connect(value, "output", output, "input").unwrap();
|
||||
package.graphs.push(graph);
|
||||
(package, value)
|
||||
}
|
||||
|
||||
fn open(session: &mut Session, package: &Package, file: &str) -> u64 {
|
||||
file_cmds::open_bytes(session, &formats::sbs::write_sbs(package).unwrap(), Some(file.into())).unwrap();
|
||||
session.document_id
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_edits_update_instances_and_dirty_dependents_before_saving() {
|
||||
let (source, value) = source();
|
||||
let mut session = Session::new(source);
|
||||
session.selection = vec![value];
|
||||
session.path = Some(std::env::temp_dir().join("materialcraft-instance-source.sbs").to_string_lossy().into_owned());
|
||||
let mut target = Package::new("Target file");
|
||||
target.graphs.push(Graph::new("Target"));
|
||||
let target_id = open(&mut session, &target, "target.sbs");
|
||||
let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap());
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
assert_eq!(session.output_ports(instance), ["color"]);
|
||||
assert!(session.cooked.nodes[&instance].error.is_none(), "{:?}", session.cooked.nodes[&instance].error);
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.25, 0.5, 0.75, 1.0]);
|
||||
let history = session.history();
|
||||
session.modified = false;
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
execute(&mut session, "graph.select", &json!({"ids": [value.0]})).unwrap();
|
||||
execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [0.8, 0.2, 0.1, 1.0]})).unwrap();
|
||||
assert!(session.open_packages().iter().find(|entry| entry.0 == target_id).unwrap().3);
|
||||
execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap();
|
||||
assert_eq!(session.history(), history);
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.8, 0.2, 0.1, 1.0]);
|
||||
execute(&mut session, "edit.undo", &json!({})).unwrap();
|
||||
assert!(session.graph().unwrap().nodes.is_empty());
|
||||
execute(&mut session, "edit.redo", &json!({})).unwrap();
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.8, 0.2, 0.1, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creation_rejects_cycles_missing_outputs_and_incompatible_graph_kinds_atomically() {
|
||||
let (package, _) = source();
|
||||
let mut session = Session::new(package);
|
||||
let before = session.pkg.clone();
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).is_err());
|
||||
assert_eq!(session.pkg, before);
|
||||
assert!(!session.can_undo());
|
||||
execute(&mut session, "file.new", &json!({"name": "Incomplete", "template": "empty"})).unwrap();
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Incomplete"})).is_err());
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source", "pattern": 1})).is_err());
|
||||
execute(&mut session, "file.new", &json!({"name": "Function", "kind": "function"})).unwrap();
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_edit_groups_do_not_publish_source_changes() {
|
||||
let (source, value) = source();
|
||||
let mut session = Session::new(source);
|
||||
session.selection = vec![value];
|
||||
session.path = Some(std::env::temp_dir().join("materialcraft-group-source.sbs").to_string_lossy().into_owned());
|
||||
let mut target = Package::new("Target");
|
||||
target.graphs.push(Graph::new("Target"));
|
||||
let target_id = open(&mut session, &target, "group-target.sbs");
|
||||
let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap());
|
||||
session.modified = false;
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
let result = session.group("Failed source edit", |session| {
|
||||
execute(session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [1.0, 0.0, 0.0, 1.0]})).unwrap();
|
||||
execute(session, "graph.node.param", &json!({"id": 99999, "param": "color", "value": [0.0, 0.0, 0.0, 0.0]}))
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert!(!session.open_packages().iter().find(|entry| entry.0 == target_id).unwrap().3);
|
||||
execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap();
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.25, 0.5, 0.75, 1.0]);
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
session.begin_group("Complete source edit on package switch");
|
||||
execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [1.0, 0.0, 0.0, 1.0]})).unwrap();
|
||||
execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap();
|
||||
assert!(session.modified);
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [1.0, 0.0, 0.0, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instance_info_is_read_only_and_describes_the_source_reference() {
|
||||
let (source, _) = source();
|
||||
let mut session = Session::new(source);
|
||||
execute(&mut session, "file.new", &json!({"name": "Target", "template": "empty"})).unwrap();
|
||||
let instance = execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap();
|
||||
let before = session.pkg.clone();
|
||||
let history = session.history();
|
||||
let info = execute(&mut session, "graph.node.info", &json!({"id": instance})).unwrap();
|
||||
let info: serde_json::Value = serde_json::from_str(info["text"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(info["instance"]["graph"], "Source");
|
||||
assert_eq!(session.pkg, before);
|
||||
assert_eq!(session.history(), history);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updates_propagate_through_three_packages_and_cycles_are_rejected() {
|
||||
let (source, value) = source();
|
||||
let mut session = Session::new(source);
|
||||
session.selection = vec![value];
|
||||
session.path = Some(std::env::temp_dir().join("materialcraft-transitive-source.sbs").to_string_lossy().into_owned());
|
||||
let mut target = Package::new("Target");
|
||||
target.graphs.push(Graph::new("Target"));
|
||||
let middle = open(&mut session, &target, "transitive-middle.sbs");
|
||||
let instance = execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap();
|
||||
let output = execute(&mut session, "graph.node.add", &json!({"def": "output"})).unwrap()["id"].as_u64().unwrap();
|
||||
execute(&mut session, "graph.connect", &json!({"from": instance, "fromPort": "color", "to": output})).unwrap();
|
||||
session.modified = false;
|
||||
let outer = open(&mut session, &target, "transitive-outer.sbs");
|
||||
let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": middle, "graph": "Target"})).unwrap()["id"].as_u64().unwrap());
|
||||
let output = execute(&mut session, "graph.node.add", &json!({"def": "output"})).unwrap()["id"].as_u64().unwrap();
|
||||
execute(&mut session, "graph.connect", &json!({"from": instance, "to": output})).unwrap();
|
||||
session.modified = false;
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.25, 0.5, 0.75, 1.0]);
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
assert!(execute(&mut session, "graph.instance.add", &json!({"document": outer, "graph": "Target"})).is_err());
|
||||
execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [0.1, 0.2, 0.3, 1.0]})).unwrap();
|
||||
assert!(session.open_packages().iter().filter(|entry| entry.0 != 0).all(|entry| entry.3));
|
||||
execute(&mut session, "package.activate", &json!({"document": outer})).unwrap();
|
||||
session.cook();
|
||||
assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.1, 0.2, 0.3, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_a_modified_source_discards_its_unsaved_changes_in_dependents() {
|
||||
let directory = std::env::temp_dir().join(format!("materialcraft-linked-discard-{}", std::process::id()));
|
||||
std::fs::create_dir(&directory).unwrap();
|
||||
let source_path = directory.join("source.sbs");
|
||||
let (source, value) = source();
|
||||
let mut session = Session::new(source);
|
||||
session.selection = vec![value];
|
||||
execute(&mut session, "file.save", &json!({"path": source_path})).unwrap();
|
||||
let mut target = Package::new("Target");
|
||||
target.graphs.push(Graph::new("Target"));
|
||||
let target_id = open(&mut session, &target, "discard-target.sbs");
|
||||
let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap());
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [1.0, 0.0, 0.0, 1.0]})).unwrap();
|
||||
assert!(execute(&mut session, "package.close", &json!({})).is_err());
|
||||
execute(&mut session, "package.close", &json!({"discard": true})).unwrap();
|
||||
assert_eq!(session.document_id, target_id);
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
let color = session.cooked.image(instance, 0).unwrap().get(0, 0, false);
|
||||
assert!(color.into_iter().zip([0.25, 0.5, 0.75, 1.0]).all(|(actual, expected)| (actual - expected).abs() < 1e-6));
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn named_value_inputs_and_outputs_keep_their_type_across_files() {
|
||||
let mut source = Package::new("Value source");
|
||||
let mut graph = Graph::new("Source");
|
||||
let input = graph.add_node("input_value", [0.0; 2]);
|
||||
graph.node_mut(input).unwrap().params.extend([("identifier".into(), Value::Text("number".into())), ("default".into(), Value::Float(0.25))]);
|
||||
let output = graph.add_node("output", [200.0, 0.0]);
|
||||
graph.node_mut(output).unwrap().params.insert("identifier".into(), Value::Text("result".into()));
|
||||
graph.connect(input, "output", output, "input").unwrap();
|
||||
source.graphs.push(graph);
|
||||
let mut session = Session::new(source);
|
||||
session.path = Some(std::env::temp_dir().join("materialcraft-value-source.sbs").to_string_lossy().into_owned());
|
||||
let mut target = Package::new("Target");
|
||||
target.graphs.push(Graph::new("Target"));
|
||||
open(&mut session, &target, "value-target.sbs");
|
||||
let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap());
|
||||
let value = execute(&mut session, "graph.node.add", &json!({"def": "input_value", "params": {"default": 0.75}})).unwrap()["id"].as_u64().unwrap();
|
||||
execute(&mut session, "graph.connect", &json!({"from": value, "to": instance, "toPort": "number"})).unwrap();
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
let cooked = &session.cooked.nodes[&instance];
|
||||
assert!(cooked.error.is_none(), "{:?}", cooked.error);
|
||||
assert_eq!(cooked.outputs[0].as_value(), Some(&Value::Float(0.75)));
|
||||
assert_eq!(session.output_ports(instance), ["result"]);
|
||||
assert!(!materialcraft_engine::MaterialSource { graph: session.graph, node: instance, port: "result".into() }.is_image(&session.pkg));
|
||||
let mut local = session.open_packages().into_iter().find(|entry| entry.0 == 0).unwrap().1.clone();
|
||||
let mut caller = session.graph().unwrap().clone();
|
||||
caller.node_mut(instance).unwrap().instance.as_mut().unwrap().package.clear();
|
||||
local.graphs.push(caller);
|
||||
local.dependencies.clear();
|
||||
let bytes = formats::sbs::write_sbs(&local).unwrap();
|
||||
let mut reopened = Session::new(formats::sbs::read_sbs(&bytes, "local.sbs", &|_| None).unwrap().pkg);
|
||||
execute(&mut reopened, "graph.open", &json!({"graph": "Target"})).unwrap();
|
||||
reopened.preview_size = Some(16);
|
||||
reopened.cook();
|
||||
let instance = reopened.graph().unwrap().nodes.iter().find(|node| node.def == "graph_instance").unwrap().id;
|
||||
let cooked = &reopened.cooked.nodes[&instance];
|
||||
assert!(cooked.error.is_none(), "{:?}", cooked.error);
|
||||
assert_eq!(cooked.outputs[0].as_value(), Some(&Value::Float(0.75)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_source_output_does_not_erase_connected_instance_wires_on_save() {
|
||||
let (source, _) = source();
|
||||
let mut session = Session::new(source);
|
||||
session.path = Some(std::env::temp_dir().join("materialcraft-port-source.sbs").to_string_lossy().into_owned());
|
||||
let source_output = session.graph().unwrap().nodes.iter().find(|node| node.def == "output").unwrap().id;
|
||||
let mut target = Package::new("Target");
|
||||
target.graphs.push(Graph::new("Target"));
|
||||
let target_id = open(&mut session, &target, "port-target.sbs");
|
||||
let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap());
|
||||
let output = execute(&mut session, "graph.node.add", &json!({"def": "output"})).unwrap()["id"].as_u64().unwrap();
|
||||
execute(&mut session, "graph.connect", &json!({"from": instance, "fromPort": "color", "to": output})).unwrap();
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
execute(&mut session, "graph.select", &json!({"ids": [source_output]})).unwrap();
|
||||
execute(&mut session, "edit.delete", &json!({})).unwrap();
|
||||
execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap();
|
||||
let bytes = file_cmds::save_bytes(&session).unwrap();
|
||||
let read = formats::sbs::read_sbs(&bytes, "target.sbs", &|_| None).unwrap();
|
||||
assert_eq!(read.pkg.graphs[0].connections.len(), 1);
|
||||
session.preview_size = Some(16);
|
||||
session.cook();
|
||||
assert!(session.cooked.nodes[&instance].error.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_instances_follow_graph_renames_and_undo() {
|
||||
let (source, _) = source();
|
||||
let mut session = Session::new(source);
|
||||
execute(&mut session, "file.new", &json!({"name": "Target", "template": "empty"})).unwrap();
|
||||
let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap());
|
||||
execute(&mut session, "graph.rename", &json!({"graph": "Source", "name": "Renamed"})).unwrap();
|
||||
assert_eq!(session.graph().unwrap().node(instance).unwrap().instance.as_ref().unwrap().graph, "Renamed");
|
||||
execute(&mut session, "edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(session.graph().unwrap().node(instance).unwrap().instance.as_ref().unwrap().graph, "Source");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extracting_a_selection_cannot_create_an_instance_without_outputs() {
|
||||
let mut package = Package::new("Source");
|
||||
package.graphs.push(Graph::new("Graph"));
|
||||
let mut session = Session::new(package);
|
||||
execute(&mut session, "graph.node.add", &json!({"def": "uniform"})).unwrap();
|
||||
let before = session.pkg.clone();
|
||||
let history = session.history();
|
||||
assert!(execute(&mut session, "graph.createFromSelection", &json!({})).is_err());
|
||||
assert_eq!(session.pkg, before);
|
||||
assert_eq!(session.history(), history);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_dependencies_reopen_without_source_files_in_explorer_and_missing_sources_keep_wires() {
|
||||
let directory = std::env::temp_dir().join(format!("materialcraft-linked-roundtrip-{}", std::process::id()));
|
||||
std::fs::create_dir(&directory).unwrap();
|
||||
let source_path = directory.join("source.sbs");
|
||||
let target_path = directory.join("target.sbs");
|
||||
let (source, _) = source();
|
||||
let mut session = Session::new(source);
|
||||
execute(&mut session, "file.save", &json!({"path": source_path})).unwrap();
|
||||
execute(&mut session, "file.newPackage", &json!({"name": "Target"})).unwrap();
|
||||
execute(&mut session, "file.new", &json!({"name": "Target", "template": "empty"})).unwrap();
|
||||
let instance = execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap();
|
||||
let output = execute(&mut session, "graph.node.add", &json!({"def": "output"})).unwrap()["id"].as_u64().unwrap();
|
||||
execute(&mut session, "graph.connect", &json!({"from": instance, "fromPort": "color", "to": output})).unwrap();
|
||||
execute(&mut session, "file.save", &json!({"path": target_path})).unwrap();
|
||||
let saved = std::fs::read_to_string(&target_path).unwrap();
|
||||
assert!(saved.contains("filename v=\"source.sbs\""));
|
||||
assert!(!saved.contains(&directory.to_string_lossy().to_string()));
|
||||
let mut reopened = Session::new(Package::new("Initial"));
|
||||
file_cmds::open_file(&mut reopened, target_path.to_str().unwrap()).unwrap();
|
||||
assert!(reopened.load_warnings.is_empty(), "{:?}", reopened.load_warnings);
|
||||
assert_eq!(reopened.open_packages().len(), 2);
|
||||
execute(&mut reopened, "graph.open", &json!({"graph": "Target"})).unwrap();
|
||||
reopened.preview_size = Some(16);
|
||||
reopened.cook();
|
||||
let graph = reopened.graph().unwrap();
|
||||
let output = graph.nodes.iter().find(|node| node.def == "output").unwrap().id;
|
||||
let color = reopened.cooked.image(output, 0).unwrap().get(0, 0, false);
|
||||
assert!(color.into_iter().zip([0.25, 0.5, 0.75, 1.0]).all(|(actual, expected)| (actual - expected).abs() < 1e-6));
|
||||
std::fs::remove_file(&source_path).unwrap();
|
||||
let mut missing = Session::new(Package::new("Initial"));
|
||||
file_cmds::open_file(&mut missing, target_path.to_str().unwrap()).unwrap();
|
||||
execute(&mut missing, "graph.open", &json!({"graph": "Target"})).unwrap();
|
||||
assert_eq!(missing.graph().unwrap().connections.len(), 1);
|
||||
let resaved = file_cmds::save_bytes(&missing).unwrap();
|
||||
let resaved = formats::sbs::read_sbs(&resaved, "target.sbs", &|_| None).unwrap();
|
||||
assert_eq!(resaved.pkg.graph_by_name("Target").unwrap().1.connections.len(), 1);
|
||||
assert!(missing.path.is_some());
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use materialcraft_engine::{Session, core::Package, execute, file_cmds, formats, inspect};
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn opening_another_package_preserves_edits_selection_and_undo() {
|
||||
let mut session = Session::new(Package::new("First"));
|
||||
execute(&mut session, "file.new", &json!({"template": "empty", "name": "Source"})).unwrap();
|
||||
let added = execute(&mut session, "graph.node.add", &json!({"def": "uniform"})).unwrap();
|
||||
let history = session.history();
|
||||
let selection = session.selection.clone();
|
||||
let original = session.pkg.clone();
|
||||
let bytes = formats::sbs::write_sbs(&Package::new("Second")).unwrap();
|
||||
file_cmds::open_bytes(&mut session, &bytes, Some("second.sbs".into())).unwrap();
|
||||
assert_eq!(inspect::session(&session, None)["documents"].as_array().unwrap().len(), 2);
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
assert_eq!(session.pkg, original);
|
||||
assert_eq!(session.selection, selection);
|
||||
assert_eq!(session.history(), history);
|
||||
assert!(session.modified);
|
||||
execute(&mut session, "edit.undo", &json!({})).unwrap();
|
||||
assert!(session.graph().unwrap().nodes.iter().all(|node| node.id.0 != added["id"].as_u64().unwrap()));
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Cache, identifier, instance_graph};
|
||||
use crate::{Cache, identifier};
|
||||
use materialcraft_core::{Node, Package, Value};
|
||||
use materialcraft_image::Image;
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
@@ -40,12 +40,17 @@ pub(crate) fn hash_of(v: &impl Hash) -> u64 {
|
||||
h.finish()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn graph_tree_hash(pkg: &Package, gi: usize) -> u64 {
|
||||
graph_tree_hash_with_registry(pkg, pkg, gi)
|
||||
}
|
||||
|
||||
pub(crate) fn graph_tree_hash_with_registry(registry: &Package, pkg: &Package, gi: usize) -> u64 {
|
||||
let mut h = DefaultHasher::new();
|
||||
let mut pending = vec![gi];
|
||||
let mut pending = vec![(pkg, gi)];
|
||||
let mut seen = HashSet::new();
|
||||
while let Some(index) = pending.pop() {
|
||||
if !seen.insert(index) {
|
||||
while let Some((pkg, index)) = pending.pop() {
|
||||
if !seen.insert((std::ptr::from_ref(pkg) as usize, index)) {
|
||||
continue;
|
||||
}
|
||||
let Some(graph) = pkg.graphs.get(index) else { continue };
|
||||
@@ -57,14 +62,22 @@ pub(crate) fn graph_tree_hash(pkg: &Package, gi: usize) -> u64 {
|
||||
id.hash(&mut h);
|
||||
graph.input_images_linear.contains(id).hash(&mut h);
|
||||
}
|
||||
for instance in graph.functions().flat_map(|function| &function.nodes).filter_map(|node| node.instance.as_ref()) {
|
||||
let source = if instance.package.is_empty() { Some(pkg) } else { registry.dependencies.get(&instance.package).map(AsRef::as_ref) };
|
||||
if let Some(source) = source
|
||||
&& let Some((index, _)) = source.graph_by_name(&instance.graph)
|
||||
{
|
||||
pending.push((source, index));
|
||||
}
|
||||
}
|
||||
for n in &graph.nodes {
|
||||
if let Some(Value::Text(name)) = n.params.get("resource") {
|
||||
pkg.resource(name).map(|r| r.bytes.content_hash()).hash(&mut h);
|
||||
}
|
||||
if n.def == "graph_instance"
|
||||
&& let Some((si, _)) = instance_graph(pkg, n)
|
||||
&& let Some((source, index, _)) = crate::graph_instance::resolve(registry, pkg, n)
|
||||
{
|
||||
pending.push(si);
|
||||
pending.push((source, index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,16 +85,26 @@ pub(crate) fn graph_tree_hash(pkg: &Package, gi: usize) -> u64 {
|
||||
}
|
||||
|
||||
pub(crate) fn variable_key(node: &Node, parameters: &BTreeMap<String, Value>, variables: &BTreeMap<String, Value>) -> u64 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
let mut names = std::collections::BTreeSet::new();
|
||||
let mut pending: Vec<&Value> = parameters.values().collect();
|
||||
while let Some(value) = pending.pop() {
|
||||
match value {
|
||||
Value::Function(graph) => {
|
||||
for function in &graph.nodes {
|
||||
if (function.op.starts_with("get_") || function.op == "isdefined")
|
||||
&& let Some(Value::Text(name)) = &function.value
|
||||
{
|
||||
names.insert(name.as_str());
|
||||
let mut pending = vec![graph.as_ref()];
|
||||
while let Some(graph) = pending.pop() {
|
||||
serde_json::to_string(graph).unwrap_or_default().hash(&mut hasher);
|
||||
for function in &graph.nodes {
|
||||
match &function.source {
|
||||
Some(Ok(source)) => pending.push(source.as_ref()),
|
||||
Some(Err(error)) => error.hash(&mut hasher),
|
||||
None => {}
|
||||
}
|
||||
if (function.op.starts_with("get_") || function.op == "isdefined")
|
||||
&& let Some(Value::Text(name)) = &function.value
|
||||
{
|
||||
names.insert(name.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,7 +120,6 @@ pub(crate) fn variable_key(node: &Node, parameters: &BTreeMap<String, Value>, va
|
||||
names.insert(&input_identifier);
|
||||
}
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
for name in names {
|
||||
name.hash(&mut hasher);
|
||||
variables.get(name).map(|value| serde_json::to_string(value).unwrap_or_default()).hash(&mut hasher);
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
use crate::{Cache, MAX_DEPTH, Options, Port, cook, identifier, instance_graph};
|
||||
use materialcraft_core::{Node, Package, Value};
|
||||
use materialcraft_image::{Image, Kind};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashSet},
|
||||
sync::Mutex,
|
||||
};
|
||||
use crate::{Cache, MAX_DEPTH, NodeOutput, Options, Port, cook, identifier};
|
||||
use materialcraft_core::{Graph, Node, Package, Value};
|
||||
use std::{collections::BTreeMap, sync::Mutex};
|
||||
|
||||
pub(crate) fn resolve<'a>(registry: &'a Package, current: &'a Package, node: &Node) -> Option<(&'a Package, usize, &'a Graph)> {
|
||||
let package = match &node.instance {
|
||||
Some(instance) if !instance.package.is_empty() => registry.dependencies.get(&instance.package)?.as_ref(),
|
||||
_ => current,
|
||||
};
|
||||
let name = node.instance.as_ref().map(|instance| instance.graph.as_str()).or_else(|| match node.params.get("graph") {
|
||||
Some(Value::Text(name)) => Some(name.as_str()),
|
||||
_ => None,
|
||||
})?;
|
||||
let (index, graph) = package.graph_by_name(name)?;
|
||||
Some((package, index, graph))
|
||||
}
|
||||
|
||||
/// Whether graph `from` reaches `target`, without recursive stack growth
|
||||
#[cfg(test)]
|
||||
pub(crate) fn instances_reach(pkg: &Package, from: usize, target: usize) -> bool {
|
||||
let mut pending = vec![from];
|
||||
let mut seen = HashSet::new();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
while let Some(index) = pending.pop() {
|
||||
if !seen.insert(index) {
|
||||
continue;
|
||||
}
|
||||
let Some(graph) = pkg.graphs.get(index) else { continue };
|
||||
|
||||
for (si, _) in graph.nodes.iter().filter(|n| n.def == "graph_instance").filter_map(|n| instance_graph(pkg, n)) {
|
||||
for (si, _) in graph.nodes.iter().filter(|n| n.def == "graph_instance").filter_map(|n| crate::instance_graph(pkg, n)) {
|
||||
if si == target {
|
||||
return true;
|
||||
}
|
||||
@@ -32,40 +42,65 @@ pub(crate) fn cook_instance(
|
||||
node: &Node,
|
||||
in_ports: &[Port],
|
||||
out_ports: &[Port],
|
||||
inputs: &[Option<Image>],
|
||||
inputs: &[Option<&NodeOutput>],
|
||||
params: &BTreeMap<String, Value>,
|
||||
size: (u32, u32),
|
||||
opts: &Options,
|
||||
cache: &Mutex<Cache>,
|
||||
) -> Result<Vec<Image>, String> {
|
||||
) -> Result<Vec<NodeOutput>, String> {
|
||||
if opts.depth >= MAX_DEPTH {
|
||||
return Err("graph instances nest too deep (does a graph use itself?)".into());
|
||||
}
|
||||
let (sub_gi, sub) = instance_graph(pkg, node).ok_or("the instanced graph does not exist")?;
|
||||
if sub_gi == gi || opts.graph_stack.contains(&sub_gi) || instances_reach(pkg, sub_gi, gi) {
|
||||
let registry = opts.registry.unwrap_or(pkg);
|
||||
let (source, sub_gi, sub) = resolve(registry, pkg, node).ok_or("the instanced graph does not exist")?;
|
||||
let identity = std::ptr::from_ref(sub) as usize;
|
||||
if pkg.graphs.get(gi).is_some_and(|graph| std::ptr::eq(graph, sub)) || opts.graph_stack.contains(&identity) {
|
||||
return Err(format!("graph `{}` uses itself through a graph instance", sub.name));
|
||||
}
|
||||
let mut o = Options::new(opts.resource);
|
||||
if sub.function.is_some() || !sub.nodes.iter().any(|node| node.def == "output") {
|
||||
return Err("the referenced graph must be a main graph with an output".into());
|
||||
}
|
||||
let root_resource = opts.resource_root.unwrap_or(opts.resource);
|
||||
let resource = |name: &str| match &node.instance {
|
||||
Some(instance) if !instance.package.is_empty() => {
|
||||
let (name, linear) = materialcraft_image::split_resource_key(name);
|
||||
root_resource(&materialcraft_image::resource_key(&format!("{}\0{name}", instance.package), linear))
|
||||
}
|
||||
_ => (opts.resource)(name),
|
||||
};
|
||||
let mut o = Options::new(&resource);
|
||||
o.registry = Some(registry);
|
||||
o.resource_root = Some(root_resource);
|
||||
o.max_size = opts.max_size;
|
||||
o.cancel = opts.cancel;
|
||||
o.depth = opts.depth + 1;
|
||||
o.graph_stack = opts.graph_stack.clone();
|
||||
o.graph_stack.push(gi);
|
||||
o.inputs = in_ports.iter().zip(inputs).filter_map(|(p, i)| Some((p.id.clone(), i.clone()?))).collect();
|
||||
o.graph_stack.push(identity);
|
||||
o.only = Some(sub.nodes.iter().filter(|node| node.def == "output").map(|node| node.id).collect());
|
||||
o.inputs = in_ports.iter().zip(inputs).filter_map(|(port, input)| Some((port.id.clone(), input.as_ref()?.as_image()?.clone()))).collect();
|
||||
o.overrides = params.iter().filter(|(k, _)| sub.exposed.iter().any(|e| &e.id == *k)).map(|(k, v)| (k.clone(), v.clone())).collect();
|
||||
let cooked = cook(pkg, sub_gi, &o, cache);
|
||||
o.overrides.extend(in_ports.iter().zip(inputs).filter_map(|(port, input)| Some((port.id.clone(), input.as_ref()?.as_value()?.clone()))));
|
||||
let cooked = cook(source, sub_gi, &o, cache);
|
||||
if cooked.cancelled {
|
||||
return Err("cancelled".into());
|
||||
}
|
||||
Ok(out_ports
|
||||
if let Some(error) = cooked.nodes.values().find_map(|result| result.error.as_deref()) {
|
||||
return Err(format!("{}: {error}", sub.name));
|
||||
}
|
||||
out_ports
|
||||
.iter()
|
||||
.map(|p| {
|
||||
sub.nodes
|
||||
let output = sub
|
||||
.nodes
|
||||
.iter()
|
||||
.find(|n| n.def == "output" && identifier(n) == p.id)
|
||||
.and_then(|n| cooked.image(n.id, 0))
|
||||
.map(|i| i.resize(size.0, size.1, true))
|
||||
.unwrap_or_else(|| Image::filled(size.0, size.1, Kind::Gray, [0.0; 4]))
|
||||
.and_then(|node| cooked.nodes.get(&node.id))
|
||||
.and_then(|result| result.outputs.first())
|
||||
.ok_or_else(|| format!("the source output `{}` is missing", p.id))?;
|
||||
Ok(match output {
|
||||
NodeOutput::Image(image) => NodeOutput::Image(image.resize(size.0, size.1, true)),
|
||||
NodeOutput::Value(value) => NodeOutput::Value(value.clone()),
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
.collect()
|
||||
}
|
||||
@@ -17,6 +17,7 @@ mod parameter;
|
||||
mod port;
|
||||
mod ports;
|
||||
mod prepared_graph;
|
||||
mod resource_decoder;
|
||||
mod time;
|
||||
mod value_execution;
|
||||
mod value_input_label;
|
||||
@@ -32,6 +33,7 @@ pub use node_size::node_size;
|
||||
pub use options::Options;
|
||||
pub use port::Port;
|
||||
pub use ports::{identifier, instance_graph, ports, ports_with_connections};
|
||||
pub use resource_decoder::ResourceDecoder;
|
||||
pub use time::now_ms;
|
||||
pub use value_input_label::value_input_label;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{Cache, Cooked, NodeOutput, NodeResult, Options, identifier, instance_graph, node_size, now_ms};
|
||||
use crate::{Cache, Cooked, NodeOutput, NodeResult, Options, identifier, node_size, now_ms};
|
||||
use crate::{
|
||||
cache_key::{graph_tree_hash, hash_of, image_key, variable_key},
|
||||
cache_key::{graph_tree_hash_with_registry, hash_of, image_key, variable_key},
|
||||
graph_instance::cook_instance,
|
||||
parameter::resolve_parameters,
|
||||
prepared_graph::PreparedGraph,
|
||||
@@ -52,9 +52,17 @@ pub(crate) fn cook_node(
|
||||
input_keys.push(source.as_ref().map_or(0, |source| source.1));
|
||||
let output = source.map(|source| source.0);
|
||||
|
||||
if let Some(expected) = node.input_values.get(&port.id) {
|
||||
let expected = node.input_values.get(&port.id).copied().or_else(|| {
|
||||
node.instance
|
||||
.as_ref()?
|
||||
.inputs
|
||||
.iter()
|
||||
.find(|input| input.identifier == port.id)
|
||||
.and_then(|input| materialcraft_core::ValueType::from_code(input.type_code))
|
||||
});
|
||||
if let Some(expected) = expected {
|
||||
let value = match output.as_ref() {
|
||||
Some(NodeOutput::Value(value)) if materialcraft_core::ValueType::of(value) == Some(*expected) => value.clone(),
|
||||
Some(NodeOutput::Value(value)) if materialcraft_core::ValueType::of(value) == Some(expected) => value.clone(),
|
||||
Some(_) => {
|
||||
input_error = Some(format!("input `{}` requires {}", port.id, expected.label()));
|
||||
expected.default_value()
|
||||
@@ -101,7 +109,12 @@ pub(crate) fn cook_node(
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
let sub_hash = if node.def == "graph_instance" { instance_graph(pkg, node).map_or(0, |(si, _)| graph_tree_hash(pkg, si)) } else { 0 };
|
||||
let registry = opts.registry.unwrap_or(pkg);
|
||||
let sub_hash = if node.def == "graph_instance" {
|
||||
crate::graph_instance::resolve(registry, pkg, node).map_or(0, |(source, index, _)| graph_tree_hash_with_registry(registry, source, index))
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let key = hash_of(&(
|
||||
&node.def,
|
||||
@@ -137,8 +150,7 @@ pub(crate) fn cook_node(
|
||||
|
||||
Err(format!("unsupported node `{name}`"))
|
||||
} else if node.def == "graph_instance" {
|
||||
cook_instance(pkg, gi, node, in_ports, out_ports, &inputs, ¶ms, (w, h), opts, cache)
|
||||
.map(|images| images.into_iter().map(NodeOutput::Image).collect())
|
||||
cook_instance(pkg, gi, node, in_ports, out_ports, &incoming, ¶ms, (w, h), opts, cache)
|
||||
} else if let Some(definition) = def {
|
||||
let context = CookContext { w, h, seed, tile, inputs: &inputs, params: ¶ms, resource: opts.resource, vars };
|
||||
(definition.cook)(&context).map(|images| images.into_iter().map(NodeOutput::Image).collect())
|
||||
|
||||
@@ -14,16 +14,30 @@ pub struct Options<'a> {
|
||||
/// Values for the graph's exposed parameters (graph instances), over the graph's own
|
||||
pub overrides: BTreeMap<String, Value>,
|
||||
/// Decodes a package resource (bitmap) by name
|
||||
pub resource: &'a (dyn Fn(&str) -> Option<Image> + Sync),
|
||||
pub resource: &'a crate::ResourceDecoder<'a>,
|
||||
/// Set from another thread to stop between nodes
|
||||
pub cancel: Option<&'a AtomicBool>,
|
||||
pub depth: u32,
|
||||
/// Graphs being cooked around this one, outermost first (cycle detection)
|
||||
/// Graph addresses being cooked around this one, outermost first (cycle detection)
|
||||
pub graph_stack: Vec<usize>,
|
||||
/// Root package supplies the shared registry when evaluating an external graph
|
||||
pub registry: Option<&'a materialcraft_core::Package>,
|
||||
pub resource_root: Option<&'a crate::ResourceDecoder<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Options<'a> {
|
||||
pub fn new(resource: &'a (dyn Fn(&str) -> Option<Image> + Sync)) -> Options<'a> {
|
||||
Options { max_size: None, only: None, inputs: BTreeMap::new(), overrides: BTreeMap::new(), resource, cancel: None, depth: 0, graph_stack: Vec::new() }
|
||||
pub fn new(resource: &'a crate::ResourceDecoder<'a>) -> Options<'a> {
|
||||
Options {
|
||||
max_size: None,
|
||||
only: None,
|
||||
inputs: BTreeMap::new(),
|
||||
overrides: BTreeMap::new(),
|
||||
resource,
|
||||
cancel: None,
|
||||
depth: 0,
|
||||
graph_stack: Vec::new(),
|
||||
registry: None,
|
||||
resource_root: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,23 @@ pub fn ports_with_connections<'a>(
|
||||
incoming: impl IntoIterator<Item = &'a materialcraft_core::Connection>,
|
||||
) -> (Vec<Port>, Vec<Port>) {
|
||||
if node.def == "graph_instance" {
|
||||
if let Some(instance) = &node.instance {
|
||||
let ports = |ports: &[materialcraft_core::GraphPort]| {
|
||||
ports
|
||||
.iter()
|
||||
.map(|port| Port {
|
||||
id: port.identifier.clone(),
|
||||
label: port.identifier.clone(),
|
||||
kind: match port.type_code {
|
||||
1 => PortKind::Color,
|
||||
2 => PortKind::Gray,
|
||||
_ => PortKind::Value,
|
||||
},
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
return (ports(&instance.inputs), ports(&instance.outputs));
|
||||
}
|
||||
let Some((_, graph)) = instance_graph(package, node) else {
|
||||
return (Vec::new(), Vec::new());
|
||||
};
|
||||
@@ -80,10 +97,7 @@ pub fn ports_with_connections<'a>(
|
||||
|
||||
/// The graph a graph-instance node refers to
|
||||
pub fn instance_graph<'a>(pkg: &'a Package, node: &Node) -> Option<(usize, &'a Graph)> {
|
||||
match node.params.get("graph") {
|
||||
Some(Value::Text(name)) => pkg.graph_by_name(name),
|
||||
_ => None,
|
||||
}
|
||||
pkg.instance_graph(node)
|
||||
}
|
||||
|
||||
/// An input or output node's identifier (its `identifier` parameter, default "input"/"output")
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
use materialcraft_image::Image;
|
||||
|
||||
/// Decodes an image resource by name within the current package
|
||||
pub type ResourceDecoder<'a> = dyn Fn(&str) -> Option<Image> + Sync + 'a;
|
||||
@@ -228,7 +228,16 @@ fn input_change_recooks_only_readers() {
|
||||
use materialcraft_core::{FunctionGraph, FunctionNode};
|
||||
let (mut p, noise, blur, out) = sample();
|
||||
let reader = p.graphs[0].add_node("pixel_processor", [0.0, 100.0]);
|
||||
let get_a = FunctionNode { id: 1, type_code: 0, op: "get_float1".into(), inputs: vec![], value: Some(Value::Text("a".into())), pos: [0.0; 2] };
|
||||
let get_a = FunctionNode {
|
||||
instance: None,
|
||||
source: None,
|
||||
id: 1,
|
||||
type_code: 0,
|
||||
op: "get_float1".into(),
|
||||
inputs: vec![],
|
||||
value: Some(Value::Text("a".into())),
|
||||
pos: [0.0; 2],
|
||||
};
|
||||
p.graphs[0].node_mut(reader).unwrap().params.insert("function".into(), Value::Function(Box::new(FunctionGraph { nodes: vec![get_a], output: Some(1) })));
|
||||
let cache = &Mutex::new(Cache::default());
|
||||
let mut opts = Options::new(&no_res);
|
||||
|
||||
@@ -19,31 +19,50 @@ pub(super) fn read(element: XmlNode<'_, '_>) -> Result<FunctionGraph, String> {
|
||||
return Err(format!("duplicate function uid {id}"));
|
||||
}
|
||||
let op = child_value(element, "function").ok_or("function node has no operation")?;
|
||||
let definition = function::definition(op).ok_or_else(|| format!("unsupported function `{op}`"))?;
|
||||
let mut inputs = vec![None; definition.inputs.len()];
|
||||
for connection in child_element(element, "connections").into_iter().flat_map(|connections| children_named(connections, "connection")) {
|
||||
let port = child_value(connection, "identifier").unwrap_or("");
|
||||
let index = definition.inputs.iter().position(|input| *input == port).ok_or_else(|| format!("{op}: unknown input `{port}`"))?;
|
||||
let source = child_value(connection, "connRef").and_then(|value| value.parse().ok()).ok_or("invalid function connection")?;
|
||||
let input = inputs.get_mut(index).ok_or("invalid input index")?;
|
||||
if input.replace(source).is_some() {
|
||||
return Err(format!("{op}: duplicate input `{port}`"));
|
||||
}
|
||||
}
|
||||
|
||||
let value = child_element(element, "funcDatas")
|
||||
.into_iter()
|
||||
.flat_map(|data| children_named(data, "funcData"))
|
||||
.find(|data| child_value(*data, "name") == Some(op))
|
||||
.and_then(|data| data.children().filter(|child| child.is_element()).find_map(|child| sbs_constant_element(child).or_else(|| sbs_constant(child))))
|
||||
.map(|value| to_natural_value(&value));
|
||||
let type_code = child_value(element, "type").and_then(|value| value.parse().ok()).unwrap_or(0);
|
||||
let mut instance = if op == "instance" { Some(super::function_instance::read(element, value.as_ref(), type_code)?) } else { None };
|
||||
let mut names: Vec<String> = if let Some(instance) = &instance {
|
||||
instance.inputs.iter().map(|port| port.identifier.clone()).collect()
|
||||
} else {
|
||||
function::definition(op).ok_or_else(|| format!("unsupported function `{op}`"))?.inputs.iter().map(|name| (*name).into()).collect()
|
||||
};
|
||||
let mut inputs = vec![None; names.len()];
|
||||
for connection in child_element(element, "connections").into_iter().flat_map(|connections| children_named(connections, "connection")) {
|
||||
let port = child_value(connection, "identifier").unwrap_or("");
|
||||
let index = if let Some(index) = names.iter().position(|name| name == port) {
|
||||
index
|
||||
} else if let Some(instance) = &mut instance {
|
||||
if names.len() >= 4096 {
|
||||
return Err("Function instance exceeds the 4096-input limit".into());
|
||||
}
|
||||
instance.inputs.push(materialcraft_core::GraphPort { identifier: port.into(), type_code: 0 });
|
||||
names.push(port.into());
|
||||
inputs.push(None);
|
||||
names.len() - 1
|
||||
} else {
|
||||
return Err(format!("{op}: unknown input `{port}`"));
|
||||
};
|
||||
let source = child_value(connection, "connRef").and_then(|value| value.parse().ok()).ok_or("invalid function connection")?;
|
||||
let input = inputs.get_mut(index).ok_or("invalid input index")?;
|
||||
if input.replace(source).is_some() {
|
||||
return Err(format!("{op}: duplicate input `{port}`"));
|
||||
}
|
||||
}
|
||||
let position = child_element(element, "GUILayout").and_then(|layout| child_value(layout, "gpos")).map(parse_floats).unwrap_or_default();
|
||||
graph.nodes.push(FunctionNode {
|
||||
instance,
|
||||
source: None,
|
||||
id,
|
||||
op: op.into(),
|
||||
inputs,
|
||||
type_code: child_value(element, "type").and_then(|value| value.parse().ok()).unwrap_or(0),
|
||||
value: value.or_else(|| native_default(op)),
|
||||
type_code,
|
||||
value: if op == "instance" { None } else { value.or_else(|| native_default(op)) },
|
||||
pos: [position.first().copied().unwrap_or(0.0), position.get(1).copied().unwrap_or(0.0)],
|
||||
});
|
||||
}
|
||||
@@ -84,7 +103,7 @@ pub(super) fn write(graph: &FunctionGraph, writer: &mut SbsWriter) -> Result<Str
|
||||
}
|
||||
output.push_str("<paramNodes>");
|
||||
for node in &graph.nodes {
|
||||
let definition = function::definition(&node.op).ok_or_else(|| format!("cannot save unknown function `{}`", node.op))?;
|
||||
let inputs = function::input_identifiers(node);
|
||||
let inferred = types.get(&node.id).copied().unwrap_or(256);
|
||||
let code = if node.type_code == 0 { inferred } else { node.type_code };
|
||||
let _ = write!(
|
||||
@@ -95,13 +114,15 @@ pub(super) fn write(graph: &FunctionGraph, writer: &mut SbsWriter) -> Result<Str
|
||||
node.pos[0],
|
||||
node.pos[1]
|
||||
);
|
||||
for (input, source) in definition.inputs.iter().zip(&node.inputs) {
|
||||
for (input, source) in inputs.zip(&node.inputs) {
|
||||
if let Some(source) = source.and_then(|source| identifiers.get(&source)) {
|
||||
let _ = write!(output, "<connection><identifier v=\"{}\"/><connRef v=\"{source}\"/></connection>", xml_escape(input));
|
||||
}
|
||||
}
|
||||
output.push_str("</connections>");
|
||||
if let Some(value) = &node.value {
|
||||
if let Some(instance) = &node.instance {
|
||||
output.push_str(&super::function_instance::write(instance, writer));
|
||||
} else if let Some(value) = &node.value {
|
||||
let constant = constant_xml(value).ok_or("function data must be a scalar or vector constant")?;
|
||||
let _ =
|
||||
write!(output, "<funcDatas><funcData><name v=\"{}\"/><constantValue>{constant}</constantValue></funcData></funcDatas>", xml_escape(&node.op));
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
use super::{
|
||||
constant::SELF_DEPENDENCY,
|
||||
sbs_writer::SbsWriter,
|
||||
xml::{child_element, child_value, children_named, xml_escape},
|
||||
};
|
||||
use materialcraft_core::{GraphInstance, GraphPort, Value};
|
||||
use roxmltree::Node as XmlNode;
|
||||
use std::fmt::Write;
|
||||
|
||||
pub(super) fn read(element: XmlNode<'_, '_>, value: Option<&Value>, type_code: u32) -> Result<GraphInstance, String> {
|
||||
let Some(Value::Text(reference)) = value else { return Err("function instance has no source reference".into()) };
|
||||
let reference = reference.strip_prefix("pkg:///").ok_or("invalid function source reference")?;
|
||||
let (graph, query) = reference.split_once('?').unwrap_or((reference, ""));
|
||||
let dependency = query.split('&').find_map(|part| part.strip_prefix("dependency="));
|
||||
let package = if let Some(uid) = dependency {
|
||||
child_element(element.document().root_element(), "dependencies")
|
||||
.into_iter()
|
||||
.flat_map(|dependencies| children_named(dependencies, "dependency"))
|
||||
.find(|dependency| child_value(*dependency, "uid") == Some(uid))
|
||||
.and_then(|dependency| child_value(dependency, "filename"))
|
||||
.ok_or("function source dependency is missing")?
|
||||
} else {
|
||||
SELF_DEPENDENCY
|
||||
};
|
||||
if package != SELF_DEPENDENCY && (package.contains("://") || !package.to_ascii_lowercase().ends_with(".sbs")) {
|
||||
return Err("function sources must be SBS files".into());
|
||||
}
|
||||
let inputs: Vec<_> = child_element(element, "materialcraftInterface")
|
||||
.into_iter()
|
||||
.flat_map(|interface| children_named(interface, "input"))
|
||||
.take(4097)
|
||||
.map(|port| GraphPort {
|
||||
identifier: child_value(port, "identifier").unwrap_or("").into(),
|
||||
type_code: child_value(port, "type").and_then(|value| value.parse().ok()).unwrap_or(0),
|
||||
})
|
||||
.collect();
|
||||
if inputs.len() > 4096 {
|
||||
return Err("Function instance exceeds the 4096-input limit".into());
|
||||
}
|
||||
let names: std::collections::BTreeSet<_> = inputs.iter().map(|port| &port.identifier).collect();
|
||||
if names.len() != inputs.len() {
|
||||
return Err("Duplicate Function input identifiers".into());
|
||||
}
|
||||
Ok(GraphInstance {
|
||||
package: if package == SELF_DEPENDENCY { String::new() } else { package.into() },
|
||||
graph: graph.into(),
|
||||
inputs,
|
||||
outputs: vec![GraphPort { identifier: "output".into(), type_code }],
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn write(instance: &GraphInstance, writer: &mut SbsWriter) -> String {
|
||||
let reference = if instance.package.is_empty() {
|
||||
format!("pkg:///{}", instance.graph)
|
||||
} else {
|
||||
format!("pkg:///{}?dependency={}", instance.graph, writer.external_package_uid(&instance.package))
|
||||
};
|
||||
let mut xml = format!(
|
||||
"<funcDatas><funcData><name v=\"instance\"/><constantValue><constantValueString v=\"{}\"/></constantValue></funcData></funcDatas>",
|
||||
xml_escape(&reference)
|
||||
);
|
||||
// Cache the editable interface so a missing source cannot erase unconnected pins
|
||||
xml.push_str("<materialcraftInterface>");
|
||||
for port in &instance.inputs {
|
||||
let _ = write!(xml, "<input><identifier v=\"{}\"/><type v=\"{}\"/></input>", xml_escape(&port.identifier), port.type_code);
|
||||
}
|
||||
xml.push_str("</materialcraftInterface>");
|
||||
xml
|
||||
}
|
||||
@@ -6,6 +6,7 @@ mod constant;
|
||||
mod embedded;
|
||||
mod filter_map;
|
||||
mod function_graph;
|
||||
mod function_instance;
|
||||
mod image_input;
|
||||
mod input_bridge;
|
||||
mod node_identifiers;
|
||||
|
||||
@@ -1,39 +1 @@
|
||||
use materialcraft_nodes::PortKind;
|
||||
|
||||
pub(super) fn output_type(graph: &materialcraft_core::Graph, id: materialcraft_core::NodeId, port: &str) -> u32 {
|
||||
if let Ok(Some(kind)) = materialcraft_nodes::output_value_type(graph, id) {
|
||||
return kind as u32;
|
||||
}
|
||||
|
||||
let mut pending = vec![(id, port.to_string())];
|
||||
let mut visited = std::collections::BTreeSet::new();
|
||||
while let Some((id, port)) = pending.pop() {
|
||||
if !visited.insert(id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let Ok(node) = graph.node(id) else { continue };
|
||||
let Some(definition) = materialcraft_nodes::find(&node.def) else { return 1 };
|
||||
if let Some(parameter) = definition.param("color_mode") {
|
||||
if node.params.get("color_mode").unwrap_or(¶meter.default).as_bool() == Some(true) {
|
||||
return 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
match definition.outputs.iter().find(|output| output.id == port).map(|output| output.kind) {
|
||||
Some(PortKind::Color) => return 1,
|
||||
Some(PortKind::Gray) => continue,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let sources = definition.inputs.iter().filter(|input| input.kind != PortKind::Gray).filter_map(|input| graph.input_of(id, input.id));
|
||||
for source in sources {
|
||||
pending.push((source.from, source.from_port.clone()));
|
||||
if node.def != "blend" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
|
||||
}
|
||||
pub(super) use materialcraft_nodes::graph_interface::output_type;
|
||||
@@ -270,7 +270,13 @@ impl<'text> SbsReader<'text, '_> {
|
||||
Some(port) => port,
|
||||
None if to.open_inputs => {
|
||||
if let Some(node) = graph.nodes.get_mut(to.index) {
|
||||
add_placeholder_input(node, sbs_input);
|
||||
if let Some(instance) = &mut node.instance {
|
||||
if !instance.inputs.iter().any(|port| port.identifier == sbs_input) {
|
||||
instance.inputs.push(materialcraft_core::GraphPort { identifier: sbs_input.into(), type_code: 1 });
|
||||
}
|
||||
} else {
|
||||
add_placeholder_input(node, sbs_input);
|
||||
}
|
||||
}
|
||||
sbs_input.to_string()
|
||||
}
|
||||
@@ -402,10 +408,35 @@ impl<'text> SbsReader<'text, '_> {
|
||||
|| params.into_iter().flat_map(|p| children_named(p, "parameter")).filter(|p| !BASE_PARAMS.contains(&child_value(*p, "name").unwrap_or("")));
|
||||
|
||||
let own_graph = dependency.as_deref().is_none_or(|d| d == SELF_DEPENDENCY).then(|| self.graph_names.get(&path).cloned()).flatten();
|
||||
if let Some(sub_graph) = own_graph {
|
||||
let external = dependency.as_deref().filter(|file| *file != SELF_DEPENDENCY && !file.contains("://") && file.to_ascii_lowercase().ends_with(".sbs"));
|
||||
if own_graph.is_some() || external.is_some() {
|
||||
let (id, index, node) = push_node(graph, "graph_instance", pos);
|
||||
if let Some(node) = node {
|
||||
node.params.insert("graph".into(), Value::Text(sub_graph));
|
||||
if let Some(sub_graph) = own_graph {
|
||||
node.params.insert("graph".into(), Value::Text(sub_graph));
|
||||
} else {
|
||||
let comp_node = implementation.parent().and_then(|parent| parent.parent());
|
||||
let types: HashMap<_, _> = comp_node
|
||||
.and_then(|node| child_element(node, "compOutputs"))
|
||||
.into_iter()
|
||||
.flat_map(|outputs| children_named(outputs, "compOutput"))
|
||||
.map(|output| {
|
||||
(child_value(output, "uid").unwrap_or(""), child_value(output, "comptype").and_then(|code| code.parse().ok()).unwrap_or(1))
|
||||
})
|
||||
.collect();
|
||||
node.instance = Some(materialcraft_core::GraphInstance {
|
||||
package: external.unwrap_or("").into(),
|
||||
graph: path.clone(),
|
||||
inputs: Vec::new(),
|
||||
outputs: bridgings
|
||||
.iter()
|
||||
.map(|(uid, identifier)| materialcraft_core::GraphPort {
|
||||
identifier: identifier.clone(),
|
||||
type_code: types.get(uid.as_str()).copied().unwrap_or(1),
|
||||
})
|
||||
.collect(),
|
||||
});
|
||||
}
|
||||
for param in instance_params() {
|
||||
if let Some(value) = child_element(param, "paramValue").and_then(sbs_constant) {
|
||||
node.params.insert(child_value(param, "name").unwrap_or("").to_string(), to_natural_value(&value));
|
||||
|
||||
@@ -40,6 +40,9 @@ impl SbsWriter {
|
||||
match node.def.as_str() {
|
||||
"output" => Vec::new(),
|
||||
"graph_instance" => {
|
||||
if let Some(instance) = &node.instance {
|
||||
return instance.outputs.iter().map(|port| (port.identifier.clone(), port.type_code)).collect();
|
||||
}
|
||||
let sub_graph = match node.params.get("graph") {
|
||||
Some(Value::Text(name)) => pkg.graph_by_name(name).map(|(_, g)| g),
|
||||
_ => None,
|
||||
@@ -244,13 +247,18 @@ impl SbsWriter {
|
||||
let _ = write!(out, "<compInputBridge><entry v=\"{}\"/><parameters>{base}</parameters></compInputBridge>", ids.input_entry.unwrap_or(0));
|
||||
}
|
||||
"graph_instance" => {
|
||||
let sub_graph = text_param(node, "graph", "");
|
||||
let sub_graph = node.instance.as_ref().map(|instance| instance.graph.clone()).unwrap_or_else(|| text_param(node, "graph", ""));
|
||||
let dependency = node
|
||||
.instance
|
||||
.as_ref()
|
||||
.filter(|instance| !instance.package.is_empty())
|
||||
.map_or(ids.self_uid, |instance| self.external_package_uid(&instance.package));
|
||||
let params: String = node.params.iter().filter(|(k, _)| *k != "graph").filter_map(|(k, v)| param_xml(k, v)).collect();
|
||||
let _ = write!(
|
||||
out,
|
||||
"<compInstance><path v=\"pkg:///{}?dependency={}\"/><parameters>{base}{params}</parameters>",
|
||||
xml_escape(&sub_graph),
|
||||
ids.self_uid
|
||||
dependency
|
||||
);
|
||||
write_bridgings(out, outputs);
|
||||
out.push_str("</compInstance>");
|
||||
|
||||
@@ -3,10 +3,14 @@ use materialcraft_core::FunctionGraph;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
pub fn compile(graph: &FunctionGraph, seed: u32) -> Result<Program, String> {
|
||||
if graph.nodes.len() > 4096 {
|
||||
return Err("function exceeds the 4096-node execution limit".into());
|
||||
}
|
||||
compile_nested(graph, seed, 0, &mut 4096)
|
||||
}
|
||||
|
||||
fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mut usize) -> Result<Program, String> {
|
||||
if depth > 8 || graph.nodes.len() > *remaining {
|
||||
return Err("function exceeds the 8-level or 4096-node execution limit".into());
|
||||
}
|
||||
*remaining -= graph.nodes.len();
|
||||
let output = super::output_node(graph).ok_or("function has no result node")?.id;
|
||||
let mut nodes = BTreeMap::new();
|
||||
for node in &graph.nodes {
|
||||
@@ -25,9 +29,14 @@ pub fn compile(graph: &FunctionGraph, seed: u32) -> Result<Program, String> {
|
||||
}
|
||||
|
||||
let node = nodes.get(&id).ok_or_else(|| format!("missing function node {id}"))?;
|
||||
let definition = catalog::definition(&node.op).ok_or_else(|| format!("unknown function `{}`", node.op))?;
|
||||
if node.inputs.len() != definition.inputs.len() || node.inputs.iter().any(Option::is_none) {
|
||||
return Err(format!("{} ({id}) requires connected inputs: {}", definition.label, definition.inputs.join(", ")));
|
||||
let definition = if node.instance.is_some() {
|
||||
&super::function_call::DEFINITION
|
||||
} else {
|
||||
catalog::definition(&node.op).ok_or_else(|| format!("unknown function `{}`", node.op))?
|
||||
};
|
||||
let names: Vec<_> = super::input_identifiers(node).collect();
|
||||
if node.inputs.len() != names.len() || node.inputs.iter().any(Option::is_none) {
|
||||
return Err(format!("{} ({id}) requires connected inputs: {}", super::node_label(node), names.join(", ")));
|
||||
}
|
||||
|
||||
if !visited {
|
||||
@@ -43,15 +52,28 @@ pub fn compile(graph: &FunctionGraph, seed: u32) -> Result<Program, String> {
|
||||
active.remove(&id);
|
||||
let inputs: Vec<usize> =
|
||||
node.inputs.iter().flatten().map(|input| indices.get(input).copied().ok_or("unresolved function input".to_string())).collect::<Result<_, _>>()?;
|
||||
let call = if let Some(instance) = &node.instance {
|
||||
let source = node.source.as_ref().ok_or_else(|| format!("unresolved Function Graph `{}`", instance.graph))?.as_ref().map_err(Clone::clone)?;
|
||||
let program = compile_nested(source, seed, depth + 1, remaining)?;
|
||||
for (port, index) in instance.inputs.iter().zip(&inputs) {
|
||||
if instructions.get(*index).is_some_and(|instruction| instruction.type_code != port.type_code) {
|
||||
return Err(format!("{}: input `{}` has the wrong type", instance.graph, port.identifier));
|
||||
}
|
||||
}
|
||||
Some(super::function_call::FunctionCall { program, inputs: names.iter().map(|name| (*name).to_string()).collect() })
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let value = node.value.clone().or_else(|| catalog::default_value(&node.op));
|
||||
let input_type = |index: usize| inputs.get(index).and_then(|index| instructions.get(*index)).map_or(256, |instruction| instruction.type_code);
|
||||
let inferred = super::inferred_type::inferred_type(&node.op, value.as_ref(), input_type);
|
||||
let type_code = if node.type_code == 0 { inferred } else { node.type_code };
|
||||
let pure = !matches!(node.op.as_str(), "set" | "sequence" | "while" | "rand" | "isdefined")
|
||||
let type_code = call.as_ref().map_or_else(|| if node.type_code == 0 { inferred } else { node.type_code }, |call| call.program.output_type());
|
||||
let pure = call.is_none()
|
||||
&& !matches!(node.op.as_str(), "set" | "sequence" | "while" | "rand" | "isdefined")
|
||||
&& !node.op.starts_with("get_")
|
||||
&& inputs.iter().all(|index| instructions.get(*index).is_some_and(|instruction| instruction.pure));
|
||||
indices.insert(id, instructions.len());
|
||||
instructions.push(Instruction { id, definition, inputs, value, type_code, pure });
|
||||
instructions.push(Instruction { id, definition, inputs, value, type_code, pure, call });
|
||||
}
|
||||
|
||||
let output = indices.get(&output).copied().ok_or("missing result instruction")?;
|
||||
|
||||
@@ -10,6 +10,7 @@ pub struct EvaluationState {
|
||||
pub(crate) registers: Vec<Option<Value>>,
|
||||
pub(crate) variables: BTreeMap<String, Value>,
|
||||
pub(crate) random: u32,
|
||||
pub(crate) calls: BTreeMap<usize, EvaluationState>,
|
||||
}
|
||||
|
||||
impl EvaluationState {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
use super::{FunctionDefinition, Program};
|
||||
|
||||
pub(crate) const DEFINITION: FunctionDefinition = FunctionDefinition { id: "instance", label: "Function instance", inputs: &[], category: "Graph" };
|
||||
|
||||
pub(crate) struct FunctionCall {
|
||||
pub program: Program,
|
||||
pub inputs: Vec<String>,
|
||||
}
|
||||
@@ -8,4 +8,5 @@ pub(crate) struct Instruction {
|
||||
pub value: Option<Value>,
|
||||
pub type_code: u32,
|
||||
pub pure: bool,
|
||||
pub call: Option<super::function_call::FunctionCall>,
|
||||
}
|
||||
@@ -6,14 +6,17 @@ mod compile;
|
||||
mod evaluation_context;
|
||||
mod evaluation_state;
|
||||
mod evaluation_step;
|
||||
mod function_call;
|
||||
mod function_definition;
|
||||
mod inferred_type;
|
||||
mod instruction;
|
||||
mod node_interface;
|
||||
mod node_types;
|
||||
mod operation;
|
||||
mod output;
|
||||
mod processor;
|
||||
mod program;
|
||||
mod resolve_instances;
|
||||
pub mod value_type;
|
||||
mod vector;
|
||||
mod warnings;
|
||||
@@ -23,11 +26,13 @@ pub use compile::compile;
|
||||
pub use evaluation_context::EvaluationContext;
|
||||
pub use evaluation_state::EvaluationState;
|
||||
pub use function_definition::FunctionDefinition;
|
||||
pub use node_interface::{graph_ports, input_identifiers, node_label};
|
||||
pub use node_types::node_types;
|
||||
pub use output::{can_be_output, output_node};
|
||||
pub use processor::default_pixel_function;
|
||||
pub(crate) use processor::pixel_processor;
|
||||
pub use program::Program;
|
||||
pub use resolve_instances::resolve_instances;
|
||||
pub use warnings::warnings;
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
use materialcraft_core::{FunctionNode, Graph, GraphPort};
|
||||
|
||||
pub fn input_identifiers(node: &FunctionNode) -> impl Iterator<Item = &str> {
|
||||
node.instance
|
||||
.iter()
|
||||
.flat_map(|instance| instance.inputs.iter().map(|port| port.identifier.as_str()))
|
||||
.chain(super::definition(&node.op).filter(|_| node.instance.is_none()).into_iter().flat_map(|definition| definition.inputs.iter().copied()))
|
||||
}
|
||||
|
||||
pub fn node_label(node: &FunctionNode) -> &str {
|
||||
node.instance
|
||||
.as_ref()
|
||||
.map(|instance| instance.graph.as_str())
|
||||
.unwrap_or_else(|| super::definition(&node.op).map_or("Unknown function", |definition| definition.label))
|
||||
}
|
||||
|
||||
pub fn graph_ports(graph: &Graph) -> Result<(Vec<GraphPort>, Vec<GraphPort>), String> {
|
||||
let function = graph.function.as_ref().ok_or("the source must be a Function Graph")?;
|
||||
let output = super::output_node(function).ok_or("the source graph needs an output before it can be instanced")?;
|
||||
let types = super::node_types(function)?;
|
||||
if graph.exposed.len() > 4096 {
|
||||
return Err("Function instance exceeds the 4096-input limit".into());
|
||||
}
|
||||
let inputs: Vec<_> = graph
|
||||
.exposed
|
||||
.iter()
|
||||
.map(|parameter| GraphPort { identifier: parameter.id.clone(), type_code: super::value_type::type_code(¶meter.default) })
|
||||
.collect();
|
||||
if inputs.iter().any(|port| port.type_code == 0) {
|
||||
return Err("Function inputs must be scalar or vector values".into());
|
||||
}
|
||||
Ok((inputs, vec![GraphPort { identifier: "output".into(), type_code: types.get(&output.id).copied().unwrap_or(256) }]))
|
||||
}
|
||||
@@ -23,8 +23,13 @@ pub fn node_types(graph: &FunctionGraph) -> Result<BTreeMap<u32, u32>, String> {
|
||||
}
|
||||
|
||||
let node = nodes.get(&id).ok_or_else(|| format!("missing function node {id}"))?;
|
||||
let definition = catalog::definition(&node.op).ok_or_else(|| format!("unknown function `{}`", node.op))?;
|
||||
if node.inputs.len() > definition.inputs.len() {
|
||||
if node.instance.is_some() && node.op != "instance" {
|
||||
return Err("graph references require the instance operation".into());
|
||||
}
|
||||
if node.instance.is_none() && catalog::definition(&node.op).is_none() {
|
||||
return Err(format!("unknown function `{}`", node.op));
|
||||
}
|
||||
if node.inputs.len() > super::input_identifiers(node).count() {
|
||||
return Err(format!("{} has too many inputs", node.op));
|
||||
}
|
||||
if !visited {
|
||||
@@ -39,7 +44,11 @@ pub fn node_types(graph: &FunctionGraph) -> Result<BTreeMap<u32, u32>, String> {
|
||||
|
||||
active.remove(&id);
|
||||
let input_type = |slot| node.inputs.get(slot).copied().flatten().and_then(|id| types.get(&id).copied()).unwrap_or(256);
|
||||
let inferred = inferred_type(&node.op, node.value.as_ref(), input_type);
|
||||
let inferred = node
|
||||
.instance
|
||||
.as_ref()
|
||||
.and_then(|instance| instance.outputs.first())
|
||||
.map_or_else(|| inferred_type(&node.op, node.value.as_ref(), input_type), |port| port.type_code);
|
||||
types.insert(id, if node.type_code == 0 { inferred } else { node.type_code });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use materialcraft_core::{FunctionGraph, FunctionNode};
|
||||
|
||||
pub fn can_be_output(operation: &str) -> bool {
|
||||
operation != "passthrough" && super::definition(operation).is_some()
|
||||
operation == "instance" || (operation != "passthrough" && super::definition(operation).is_some())
|
||||
}
|
||||
|
||||
pub fn output_node(graph: &FunctionGraph) -> Option<&FunctionNode> {
|
||||
|
||||
@@ -29,16 +29,18 @@ impl Program {
|
||||
|
||||
pub fn evaluate(&self, context: &EvaluationContext<'_>, state: &mut EvaluationState) -> Result<Value, String> {
|
||||
state.variables.clone_from(context.variables);
|
||||
self.run(context, state)
|
||||
let mut remaining = MAX_EVALUATION_STEPS;
|
||||
self.run(context, state, &mut remaining)
|
||||
}
|
||||
|
||||
// Pixel assignments are local; only the general evaluator exports the complete environment
|
||||
pub(super) fn evaluate_pixel(&self, context: &EvaluationContext<'_>, state: &mut EvaluationState) -> Result<Value, String> {
|
||||
state.variables.clear();
|
||||
self.run(context, state)
|
||||
let mut remaining = MAX_EVALUATION_STEPS;
|
||||
self.run(context, state, &mut remaining)
|
||||
}
|
||||
|
||||
fn run(&self, context: &EvaluationContext<'_>, state: &mut EvaluationState) -> Result<Value, String> {
|
||||
fn run(&self, context: &EvaluationContext<'_>, state: &mut EvaluationState, remaining_steps: &mut usize) -> Result<Value, String> {
|
||||
use EvaluationStep::*;
|
||||
|
||||
state.steps.clear();
|
||||
@@ -47,10 +49,9 @@ impl Program {
|
||||
state.registers.resize(self.instructions.len(), None);
|
||||
state.random = context.variables.get("$randomseed").and_then(Value::as_i32).map_or(self.seed, |seed| seed as u32);
|
||||
state.steps.push(Enter(self.output));
|
||||
let mut remaining_steps = MAX_EVALUATION_STEPS;
|
||||
|
||||
while let Some(step) = state.steps.pop() {
|
||||
remaining_steps = remaining_steps.checked_sub(1).ok_or("function exceeded its execution budget; check loop termination")?;
|
||||
*remaining_steps = remaining_steps.checked_sub(1).ok_or("function exceeded its execution budget; check loop termination")?;
|
||||
match step {
|
||||
Enter(index) => {
|
||||
let instruction = self.instruction(index)?;
|
||||
@@ -81,8 +82,17 @@ impl Program {
|
||||
Apply { instruction: index, base } => {
|
||||
let instruction = self.instruction(index)?;
|
||||
let arguments = state.values.get(base..).ok_or("invalid function operands")?;
|
||||
let result = operation::evaluate(instruction, arguments, &mut state.variables, &mut state.random, context)
|
||||
.map_err(|error| format!("{} ({}): {error}", instruction.definition.label, instruction.id))?;
|
||||
let result = if let Some(call) = &instruction.call {
|
||||
let storage = state.calls.entry(index).or_default();
|
||||
storage.variables.clone_from(&state.variables);
|
||||
for (name, value) in call.inputs.iter().zip(arguments) {
|
||||
storage.variables.insert(name.clone(), value.clone());
|
||||
}
|
||||
call.program.run(context, storage, remaining_steps)
|
||||
} else {
|
||||
operation::evaluate(instruction, arguments, &mut state.variables, &mut state.random, context)
|
||||
}
|
||||
.map_err(|error| format!("{} ({}): {error}", instruction.definition.label, instruction.id))?;
|
||||
|
||||
state.values.truncate(base);
|
||||
if instruction.pure
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
use materialcraft_core::{FunctionGraph, GraphPort, Package};
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
/// Resolve calls once per document edit, with bounded source expansion
|
||||
pub fn resolve_instances(function: &mut FunctionGraph, local: &Package, sources: &BTreeMap<String, Arc<Package>>) {
|
||||
resolve(function, local, sources, &mut Vec::new(), &mut 4096);
|
||||
}
|
||||
|
||||
fn resolve(function: &mut FunctionGraph, local: &Package, sources: &BTreeMap<String, Arc<Package>>, stack: &mut Vec<(usize, String)>, remaining: &mut usize) {
|
||||
*remaining = remaining.saturating_sub(function.nodes.len());
|
||||
for node in &mut function.nodes {
|
||||
let Some(instance) = &mut node.instance else {
|
||||
node.source = None;
|
||||
continue;
|
||||
};
|
||||
|
||||
let source = if instance.package.is_empty() { Some(local) } else { sources.get(&instance.package).map(AsRef::as_ref) };
|
||||
let resolved = (|| {
|
||||
let source = source.ok_or_else(|| format!("Missing source file for `{}`", instance.graph))?;
|
||||
let (_, graph) = source.graph_by_name(&instance.graph).ok_or_else(|| format!("Missing Function Graph `{}`", instance.graph))?;
|
||||
let body = graph.function.as_ref().ok_or("Only Function Graphs can be used in a function")?;
|
||||
let key = (std::ptr::from_ref(source) as usize, graph.name.clone());
|
||||
if stack.contains(&key) {
|
||||
return Err("Function Graph dependency cycle".into());
|
||||
}
|
||||
if stack.len() >= 8 || body.nodes.len() > *remaining {
|
||||
return Err("Function instances exceed the 8-level or 4096-node limit".into());
|
||||
}
|
||||
if graph.exposed.len() > 4096
|
||||
|| graph.exposed.iter().map(|parameter| ¶meter.id).collect::<std::collections::BTreeSet<_>>().len() != graph.exposed.len()
|
||||
{
|
||||
return Err("Function inputs must have unique identifiers and at most 4096 entries".into());
|
||||
}
|
||||
let previous: BTreeMap<_, _> = instance.inputs.iter().zip(&node.inputs).map(|(port, value)| (port.identifier.clone(), *value)).collect();
|
||||
instance.inputs = graph
|
||||
.exposed
|
||||
.iter()
|
||||
.map(|parameter| GraphPort { identifier: parameter.id.clone(), type_code: super::value_type::type_code(¶meter.default) })
|
||||
.collect();
|
||||
// Keep connected removed inputs visible so saving never discards a wire
|
||||
for (name, value) in &previous {
|
||||
if value.is_some() && !instance.inputs.iter().any(|port| port.identifier == *name) {
|
||||
instance.inputs.push(GraphPort { identifier: name.clone(), type_code: 0 });
|
||||
}
|
||||
}
|
||||
node.inputs = instance.inputs.iter().map(|port| previous.get(&port.identifier).copied().flatten()).collect();
|
||||
let mut body = body.clone();
|
||||
stack.push(key);
|
||||
resolve(&mut body, source, sources, stack, remaining);
|
||||
stack.pop();
|
||||
let output = super::output_node(&body).ok_or_else(|| format!("{}: no output node defined", instance.graph))?;
|
||||
let types = super::node_types(&body)?;
|
||||
let type_code = types.get(&output.id).copied().unwrap_or(256);
|
||||
instance.outputs = vec![GraphPort { identifier: "output".into(), type_code }];
|
||||
node.type_code = type_code;
|
||||
if instance.inputs.iter().any(|port| port.type_code == 0) {
|
||||
return Err(format!("{}: a connected input no longer exists in the source", instance.graph));
|
||||
}
|
||||
Ok(Arc::new(body))
|
||||
})();
|
||||
node.source = Some(resolved);
|
||||
}
|
||||
}
|
||||
@@ -4,28 +4,38 @@ use std::collections::BTreeMap;
|
||||
pub fn warnings(graph: &FunctionGraph) -> Vec<String> {
|
||||
let Some(output) = super::output_node(graph) else { return vec!["No output node defined".into()] };
|
||||
|
||||
if has_value_source(graph, output.id) { Vec::new() } else { vec!["Output node has no connected value source".into()] }
|
||||
}
|
||||
|
||||
fn has_value_source(graph: &FunctionGraph, output: u32) -> bool {
|
||||
// Match the evaluator's node budget to bound work on malformed graphs
|
||||
if graph.nodes.len() > 4096 {
|
||||
return false;
|
||||
return vec!["Function exceeds the 4096-node limit".into()];
|
||||
}
|
||||
|
||||
let mut remaining: BTreeMap<_, _> = graph.nodes.iter().map(|node| (node.id, node)).collect();
|
||||
let mut pending = vec![output];
|
||||
let mut pending = vec![output.id];
|
||||
let mut has_value_source = false;
|
||||
while let Some(id) = pending.pop() {
|
||||
let Some(node) = remaining.remove(&id) else { continue };
|
||||
let Some(definition) = super::definition(&node.op) else { continue };
|
||||
|
||||
if definition.inputs.is_empty() {
|
||||
return true;
|
||||
if let Some(instance) = &node.instance {
|
||||
if node.inputs.len() != instance.inputs.len() || node.inputs.iter().any(Option::is_none) {
|
||||
return vec![format!("{}: missing input connection", instance.graph)];
|
||||
}
|
||||
match &node.source {
|
||||
Some(Err(error)) => return vec![error.clone()],
|
||||
None => return vec![format!("{}: source is unavailable", instance.graph)],
|
||||
Some(Ok(source)) => {
|
||||
let warnings = warnings(source);
|
||||
if !warnings.is_empty() {
|
||||
return warnings;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if super::definition(&node.op).is_none() {
|
||||
continue;
|
||||
}
|
||||
|
||||
pending.extend(node.inputs.iter().take(definition.inputs.len()).flatten().copied());
|
||||
let inputs = super::input_identifiers(node).count();
|
||||
has_value_source |= inputs == 0;
|
||||
pending.extend(node.inputs.iter().take(inputs).flatten().copied());
|
||||
}
|
||||
false
|
||||
if has_value_source { Vec::new() } else { vec!["Output node has no connected value source".into()] }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
use crate::{PortKind, find};
|
||||
use materialcraft_core::{Graph, GraphPort, NodeId, Value, ValueType};
|
||||
|
||||
pub fn graph_ports(graph: &Graph) -> (Vec<GraphPort>, Vec<GraphPort>) {
|
||||
let mut inputs = Vec::new();
|
||||
let mut outputs = Vec::new();
|
||||
for node in &graph.nodes {
|
||||
let type_code = match node.def.as_str() {
|
||||
"input_color" => 1,
|
||||
"input_grayscale" => 2,
|
||||
"input_value" => node.params.get("default").and_then(ValueType::of).unwrap_or(ValueType::Float) as u32,
|
||||
"output" => output_type(graph, node.id, "output"),
|
||||
_ => continue,
|
||||
};
|
||||
let identifier = match node.params.get("identifier") {
|
||||
Some(Value::Text(name)) if !name.is_empty() => name.clone(),
|
||||
_ => if node.def == "output" { "output" } else { "input" }.into(),
|
||||
};
|
||||
let port = GraphPort { identifier, type_code };
|
||||
if node.def == "output" {
|
||||
outputs.push(port);
|
||||
} else {
|
||||
inputs.push(port);
|
||||
}
|
||||
}
|
||||
(inputs, outputs)
|
||||
}
|
||||
|
||||
pub fn output_type(graph: &Graph, id: NodeId, port: &str) -> u32 {
|
||||
if let Ok(Some(kind)) = crate::output_port_value_type(graph, id, port) {
|
||||
return kind as u32;
|
||||
}
|
||||
let mut pending = vec![(id, port.to_string())];
|
||||
let mut visited = std::collections::BTreeSet::new();
|
||||
while let Some((id, port)) = pending.pop() {
|
||||
if !visited.insert((id, port.clone())) {
|
||||
continue;
|
||||
}
|
||||
let Ok(node) = graph.node(id) else { continue };
|
||||
if let Some(instance) = &node.instance {
|
||||
if instance.outputs.iter().any(|output| output.identifier == port && output.type_code == 1) {
|
||||
return 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let Some(definition) = find(&node.def) else { return 1 };
|
||||
if let Some(parameter) = definition.param("color_mode") {
|
||||
if node.params.get("color_mode").unwrap_or(¶meter.default).as_bool() == Some(true) {
|
||||
return 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
match definition.outputs.iter().find(|output| output.id == port).map(|output| output.kind) {
|
||||
Some(PortKind::Color) => return 1,
|
||||
Some(PortKind::Gray) => continue,
|
||||
_ => {}
|
||||
}
|
||||
for source in definition.inputs.iter().filter(|input| input.kind != PortKind::Gray).filter_map(|input| graph.input_of(id, input.id)) {
|
||||
pending.push((source.from, source.from_port.clone()));
|
||||
if node.def != "blend" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
|
||||
}
|
||||
@@ -9,6 +9,7 @@ pub mod pattern;
|
||||
mod raster;
|
||||
|
||||
mod cook_context;
|
||||
pub mod graph_interface;
|
||||
mod image_input;
|
||||
mod node_definition;
|
||||
mod node_definition_builder;
|
||||
@@ -24,7 +25,7 @@ pub use cook_context::CookContext;
|
||||
pub use image_input::{MAX_IMAGE_INPUTS, image_input_prefix};
|
||||
pub use node_definition::NodeDefinition;
|
||||
pub(crate) use node_definition_builder::node;
|
||||
pub use output_value_type::output_value_type;
|
||||
pub use output_value_type::{output_port_value_type, output_value_type};
|
||||
pub use parameter::{clamp_params, resolve_params};
|
||||
pub use parameter_definition::ParameterDefinition;
|
||||
pub use port_definition::PortDefinition;
|
||||
|
||||
@@ -3,17 +3,28 @@ use std::collections::BTreeSet;
|
||||
|
||||
use crate::find;
|
||||
|
||||
pub fn output_value_type(graph: &Graph, mut id: NodeId) -> Result<Option<ValueType>, String> {
|
||||
pub fn output_value_type(graph: &Graph, id: NodeId) -> Result<Option<ValueType>, String> {
|
||||
output_port_value_type(graph, id, "output")
|
||||
}
|
||||
|
||||
pub fn output_port_value_type(graph: &Graph, mut id: NodeId, port: &str) -> Result<Option<ValueType>, String> {
|
||||
let mut port = port;
|
||||
let mut visited = BTreeSet::new();
|
||||
loop {
|
||||
if !visited.insert(id) {
|
||||
if !visited.insert((id, port)) {
|
||||
return Err("cycle while resolving a value connection".into());
|
||||
}
|
||||
|
||||
let node = graph.node(id).map_err(|error| error.to_string())?;
|
||||
if let Some(instance) = &node.instance {
|
||||
return Ok(instance.outputs.iter().find(|output| output.identifier == port).and_then(|output| ValueType::from_code(output.type_code)));
|
||||
}
|
||||
match node.def.as_str() {
|
||||
"dot" | "output" => match graph.input_of(id, "input") {
|
||||
Some(connection) => id = connection.from,
|
||||
Some(connection) => {
|
||||
id = connection.from;
|
||||
port = &connection.from_port;
|
||||
}
|
||||
None => return Ok(None),
|
||||
},
|
||||
"input_value" => return Ok(Some(node.params.get("default").and_then(ValueType::of).unwrap_or(ValueType::Float))),
|
||||
|
||||
+119
-93
@@ -32,99 +32,124 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
});
|
||||
ui.add_space(2.0);
|
||||
egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
|
||||
let graph_warnings: Vec<String> = app
|
||||
.session
|
||||
.pkg
|
||||
.graphs
|
||||
.iter()
|
||||
.map(|graph| {
|
||||
materialcraft_engine::function_inspection::warnings(graph).into_iter().flat_map(|(_, messages)| messages).collect::<Vec<_>>().join("\n")
|
||||
})
|
||||
.collect();
|
||||
let package_warnings = app
|
||||
.session
|
||||
.pkg
|
||||
.graphs
|
||||
.iter()
|
||||
.zip(&graph_warnings)
|
||||
.filter(|(_, warning)| !warning.is_empty())
|
||||
.map(|(graph, warning)| format!("{}: {warning}", graph.name))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
let name = format!("{}{}", app.session.pkg.name, if app.session.modified { "*" } else { "" });
|
||||
tree_row::show(ui, &name, icon::PACKAGE, false, (!package_warnings.is_empty()).then_some(package_warnings.as_str()));
|
||||
let mut open: Option<usize> = None;
|
||||
let mut action: Option<(&str, usize)> = None;
|
||||
let mut open = None;
|
||||
let mut action = None;
|
||||
let mut selected_node = None;
|
||||
let mut assignment = None;
|
||||
for (index, graph) in app.session.pkg.graphs.iter().enumerate() {
|
||||
let active = index == app.session.graph;
|
||||
let function = graph.function.is_some();
|
||||
let mut header = |ui: &mut Ui| {
|
||||
let symbol = if function { icon::FUNCTION } else { icon::TREE_STRUCTURE };
|
||||
let warning = graph_warnings.get(index).map(String::as_str).unwrap_or("");
|
||||
let response = tree_row::show(ui, &graph.name, symbol, active, (!warning.is_empty()).then_some(warning));
|
||||
if !function {
|
||||
response.interact(egui::Sense::drag()).dnd_set_drag_payload(crate::graph_drag_payload::GraphDragPayload { name: graph.name.clone() });
|
||||
for (document, package, path, modified) in app.session.open_packages() {
|
||||
ui.push_id(document, |ui| {
|
||||
let graph_warnings: Vec<String> = package
|
||||
.graphs
|
||||
.iter()
|
||||
.map(|graph| {
|
||||
materialcraft_engine::function_inspection::warnings(graph).into_iter().flat_map(|(_, messages)| messages).collect::<Vec<_>>().join("\n")
|
||||
})
|
||||
.collect();
|
||||
let package_warnings = package
|
||||
.graphs
|
||||
.iter()
|
||||
.zip(&graph_warnings)
|
||||
.filter(|(_, warning)| !warning.is_empty())
|
||||
.map(|(graph, warning)| format!("{}: {warning}", graph.name))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
let name = format!("{}{}", package.name, if modified { "*" } else { "" });
|
||||
let package_row = tree_row::show(ui, &name, icon::PACKAGE, false, (!package_warnings.is_empty()).then_some(package_warnings.as_str()));
|
||||
if package_row.clicked() {
|
||||
action = Some(("package", document, 0));
|
||||
}
|
||||
if response.clicked() {
|
||||
open = Some(index);
|
||||
if let Some(path) = path {
|
||||
package_row.on_hover_text(path);
|
||||
}
|
||||
response.context_menu(|ui| {
|
||||
if !function {
|
||||
for (label, command) in [("View outputs in 3D view", "view3d"), ("Reset and view outputs in 3D view", "reset3d")] {
|
||||
if ui.button(tr(label)).clicked() {
|
||||
action = Some((command, index));
|
||||
ui.close();
|
||||
}
|
||||
for (index, graph) in package.graphs.iter().enumerate() {
|
||||
let active = document == app.session.document_id && index == app.session.graph;
|
||||
let function = graph.function.is_some();
|
||||
let mut header = |ui: &mut Ui| {
|
||||
let symbol = if function { icon::FUNCTION } else { icon::TREE_STRUCTURE };
|
||||
let warning = graph_warnings.get(index).map(String::as_str).unwrap_or("");
|
||||
let response = tree_row::show(ui, &graph.name, symbol, active, (!warning.is_empty()).then_some(warning));
|
||||
if graph.function.as_ref().map_or_else(
|
||||
|| graph.nodes.iter().any(|node| node.def == "output"),
|
||||
|function| materialcraft_engine::nodes::function::output_node(function).is_some(),
|
||||
) {
|
||||
response
|
||||
.interact(egui::Sense::drag())
|
||||
.dnd_set_drag_payload(crate::graph_drag_payload::GraphDragPayload { document, name: graph.name.clone() });
|
||||
}
|
||||
}
|
||||
for (label, command) in [("Open", "open"), ("Export outputs...", "export"), ("Duplicate", "duplicate"), ("Delete", "delete")] {
|
||||
if ui.button(tr(label)).clicked() {
|
||||
action = Some((command, index));
|
||||
ui.close();
|
||||
if response.clicked() {
|
||||
open = Some((document, index));
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
let mut ports =
|
||||
graph.nodes.iter().filter(|node| matches!(node.def.as_str(), "output" | "input_color" | "input_grayscale" | "input_value")).peekable();
|
||||
if function || ports.peek().is_none() {
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(ui.spacing().indent);
|
||||
header(ui);
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
let id = ui.make_persistent_id(("graph", index, &graph.name));
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true).show_header(ui, header).body(|ui| {
|
||||
ui.indent("graph-ports", |ui| {
|
||||
for node in ports {
|
||||
ui.push_id(node.id, |ui| {
|
||||
let name = crate::graph::node_label(node);
|
||||
let selected = active && app.session.selection.contains(&node.id);
|
||||
let symbol = if node.def == "input_value" { icon::FUNCTION } else { icon::IMAGE };
|
||||
let response = tree_row::show(ui, &name, symbol, selected, None);
|
||||
|
||||
response.context_menu(|ui| {
|
||||
if let Some(parameters) = crate::material_preview_menu::show(ui, &app.session, index, node.id) {
|
||||
assignment = Some(parameters);
|
||||
response.context_menu(|ui| {
|
||||
if !function {
|
||||
for (label, command) in [("View outputs in 3D view", "view3d"), ("Reset and view outputs in 3D view", "reset3d")] {
|
||||
if ui.button(tr(label)).clicked() {
|
||||
action = Some((command, document, index));
|
||||
ui.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
for (label, command) in [("Open", "open"), ("Export outputs...", "export"), ("Duplicate", "duplicate"), ("Delete", "delete")] {
|
||||
if ui.button(tr(label)).clicked() {
|
||||
action = Some((command, document, index));
|
||||
ui.close();
|
||||
}
|
||||
});
|
||||
if response.clicked() || response.double_clicked() {
|
||||
selected_node = Some((index, node.id, response.double_clicked()));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
let mut ports =
|
||||
graph.nodes.iter().filter(|node| matches!(node.def.as_str(), "output" | "input_color" | "input_grayscale" | "input_value")).peekable();
|
||||
if function || ports.peek().is_none() {
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(ui.spacing().indent);
|
||||
header(ui);
|
||||
});
|
||||
continue;
|
||||
}
|
||||
});
|
||||
|
||||
let id = ui.make_persistent_id(("graph", index, &graph.name));
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true).show_header(ui, header).body(|ui| {
|
||||
ui.indent("graph-ports", |ui| {
|
||||
for node in ports {
|
||||
ui.push_id(node.id, |ui| {
|
||||
let name = crate::graph::node_label(node);
|
||||
let selected = active && app.session.selection.contains(&node.id);
|
||||
let symbol = if node.def == "input_value" { icon::FUNCTION } else { icon::IMAGE };
|
||||
let response = tree_row::show(ui, &name, symbol, selected, None);
|
||||
|
||||
response.context_menu(|ui| {
|
||||
if document == app.session.document_id
|
||||
&& let Some(parameters) = crate::material_preview_menu::show(ui, &app.session, index, node.id)
|
||||
{
|
||||
assignment = Some(parameters);
|
||||
}
|
||||
});
|
||||
if response.clicked() || response.double_clicked() {
|
||||
selected_node = Some((document, index, node.id, response.double_clicked()));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
if !package.resources.is_empty() {
|
||||
ui.add_space(6.0);
|
||||
ui.label(RichText::new(tr("Resources")).color(tk.text_dim).size(11.0));
|
||||
for resource in &package.resources {
|
||||
ui.label(format!("{} {} ({} KB)", icon::IMAGE, resource.name, resource.bytes.len() / 1024));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if let Some(i) = open {
|
||||
app.run("graph.open", json!({"graph": i}));
|
||||
if let Some((document, index)) = open
|
||||
&& activate(app, document)
|
||||
{
|
||||
app.run("graph.open", json!({"graph": index}));
|
||||
}
|
||||
if let Some((index, node, preview)) = selected_node {
|
||||
if let Some((document, index, node, preview)) = selected_node {
|
||||
if !activate(app, document) {
|
||||
return;
|
||||
}
|
||||
if index != app.session.graph && app.run("graph.open", json!({"graph": index})).is_none() {
|
||||
return;
|
||||
}
|
||||
@@ -144,33 +169,34 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
if let Some(parameters) = assignment {
|
||||
app.run("view.3d.assign", parameters);
|
||||
}
|
||||
match action {
|
||||
Some((command @ ("view3d" | "reset3d"), graph)) => {
|
||||
let Some((command, document, index)) = action else { return };
|
||||
if !activate(app, document) {
|
||||
return;
|
||||
}
|
||||
match (command, index) {
|
||||
(command @ ("view3d" | "reset3d"), graph) => {
|
||||
app.run("view.3d.outputs", json!({"graph": graph, "reset": command == "reset3d"}));
|
||||
}
|
||||
Some(("open", i)) => {
|
||||
("open", i) => {
|
||||
app.run("graph.open", json!({"graph": i}));
|
||||
}
|
||||
Some(("export", i)) => {
|
||||
("export", i) => {
|
||||
// The export window works on the open graph
|
||||
if app.run("graph.open", json!({"graph": i})).is_some() {
|
||||
app.ui.windows.export = true;
|
||||
}
|
||||
}
|
||||
Some(("duplicate", i)) => {
|
||||
("duplicate", i) => {
|
||||
app.run("graph.duplicate", json!({"graph": i}));
|
||||
}
|
||||
Some(("delete", i)) => {
|
||||
("delete", i) => {
|
||||
app.run("graph.delete", json!({"graph": i}));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if !app.session.pkg.resources.is_empty() {
|
||||
ui.add_space(6.0);
|
||||
ui.label(RichText::new(tr("Resources")).color(tk.text_dim).size(11.0));
|
||||
for r in &app.session.pkg.resources {
|
||||
ui.label(format!("{} {} ({} KB)", icon::IMAGE, r.name, r.bytes.len() / 1024));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn activate(app: &mut MaterialCraftApp, document: u64) -> bool {
|
||||
document == app.session.document_id || app.run("package.activate", json!({"document": document})).is_some()
|
||||
}
|
||||
@@ -2,6 +2,12 @@ use super::{FunctionEditor, FunctionTarget};
|
||||
use crate::MaterialCraftApp;
|
||||
|
||||
pub(super) fn editor_enabled(app: &MaterialCraftApp, editor: &FunctionEditor, command: &str) -> Option<bool> {
|
||||
if command == "graph.node.edit" {
|
||||
return Some(
|
||||
editor.selection.len() == 1
|
||||
&& editor.current(app).zip(editor.selection.first()).and_then(|(graph, id)| graph.node(*id)).is_some_and(|node| node.instance.is_some()),
|
||||
);
|
||||
}
|
||||
let target = editor.command_target().map(materialcraft_engine::SelectionTarget::Function);
|
||||
if let Some(enabled) = crate::graph::wire_actions::enabled(app, command, &editor.selected_wires, target.as_ref()) {
|
||||
return Some(enabled && editor.current(app).is_some());
|
||||
@@ -46,6 +52,10 @@ pub(super) fn invoke_editor(app: &mut MaterialCraftApp, editor: &mut FunctionEdi
|
||||
return true;
|
||||
}
|
||||
|
||||
if command == "graph.node.edit" {
|
||||
editor.open_reference = editor.selection.first().copied();
|
||||
return true;
|
||||
}
|
||||
let parameters = editor.selection_parameters(command);
|
||||
crate::graph::selection_commands::invoke(app, &mut editor.selection, command, parameters);
|
||||
if matches!(command, "edit.selectAll" | "edit.selectNone" | "edit.paste" | "edit.pasteWithoutLinks" | "edit.duplicate" | "edit.duplicateWithoutLinks") {
|
||||
@@ -58,6 +68,17 @@ pub(crate) fn invoke(app: &mut MaterialCraftApp, command: &str) -> bool {
|
||||
let Some(mut editor) = app.function_editor.take() else { return false };
|
||||
|
||||
let handled = invoke_editor(app, &mut editor, command);
|
||||
app.function_editor = Some(editor);
|
||||
finish_editor(app, editor);
|
||||
handled
|
||||
}
|
||||
|
||||
pub(super) fn finish_editor(app: &mut MaterialCraftApp, mut editor: FunctionEditor) {
|
||||
let reference =
|
||||
editor.open_reference.take().and_then(|id| app.run("graph.instance.reference", serde_json::json!({"id": id, "function": editor.command_target()})));
|
||||
app.function_editor = Some(editor);
|
||||
if let Some(reference) = reference
|
||||
&& let Some(graph) = reference.get("graph").and_then(serde_json::Value::as_u64).and_then(|graph| usize::try_from(graph).ok())
|
||||
{
|
||||
crate::graph::subgraph_navigation::open_external_instance(app, &reference, graph);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,11 @@ fn node_actions(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, ui: &mu
|
||||
ui.close();
|
||||
}
|
||||
ui.separator();
|
||||
if editor.current(app).and_then(|graph| graph.node(node)).is_some_and(|node| node.instance.is_some())
|
||||
&& edit_menu::button(ui, "Open reference", "graph.node.edit", true)
|
||||
{
|
||||
editor.open_reference = Some(node);
|
||||
}
|
||||
if let Some(graph) = editor.current(app)
|
||||
&& graph.node(node).is_some_and(|node| materialcraft_engine::nodes::function::can_be_output(&node.op))
|
||||
{
|
||||
|
||||
@@ -18,6 +18,7 @@ pub struct FunctionEditor {
|
||||
pub(super) drag: Option<FunctionDrag>,
|
||||
pub(super) menu_at: Pos2,
|
||||
pub(crate) framed: bool,
|
||||
pub(super) open_reference: Option<u32>,
|
||||
}
|
||||
|
||||
impl FunctionEditor {
|
||||
@@ -47,6 +48,7 @@ impl FunctionEditor {
|
||||
drag: None,
|
||||
menu_at: Pos2::ZERO,
|
||||
framed: false,
|
||||
open_reference: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,5 +5,5 @@ pub(super) struct FunctionPort {
|
||||
pub slot: Option<usize>,
|
||||
pub position: Pos2,
|
||||
pub radius: f32,
|
||||
pub label: &'static str,
|
||||
pub label: String,
|
||||
}
|
||||
@@ -16,7 +16,7 @@ pub(super) fn category_color(category: &str) -> Color32 {
|
||||
}
|
||||
|
||||
fn input_count(node: &FunctionNode) -> usize {
|
||||
materialcraft_engine::nodes::function::definition(&node.op).map_or(0, |definition| definition.inputs.len())
|
||||
materialcraft_engine::nodes::function::input_identifiers(node).count()
|
||||
}
|
||||
|
||||
pub(super) fn node_rect(node: &FunctionNode, previews: bool) -> Rect {
|
||||
|
||||
@@ -124,6 +124,23 @@ pub(super) fn handle(
|
||||
|
||||
let camera = GraphCamera { origin: canvas_bounds.min, pan: editor.pan, zoom: editor.zoom };
|
||||
|
||||
if let Some(payload) = crate::widgets::dnd_release::<crate::graph_drag_payload::GraphDragPayload>(response)
|
||||
&& let Some(pointer) = pointer
|
||||
&& let Some(target) = editor.command_target()
|
||||
{
|
||||
let at = camera.to_graph(pointer);
|
||||
if let Some(result) = app.run(
|
||||
"graph.instance.add",
|
||||
serde_json::json!({"document": payload.document, "graph": payload.name,
|
||||
"function": target, "at": [at.x - crate::graph::NODE_W * 0.5, at.y - 20.0], "snapToGrid": snap}),
|
||||
) && let Some(id) = result.get("id").and_then(serde_json::Value::as_u64).and_then(|id| u32::try_from(id).ok())
|
||||
{
|
||||
editor.selection = vec![id];
|
||||
editor.selected_wires.clear();
|
||||
editor.inspected_node = Some(id);
|
||||
}
|
||||
}
|
||||
|
||||
// Adding operations
|
||||
if response.secondary_clicked()
|
||||
&& let Some(pointer_position) = pointer
|
||||
|
||||
@@ -29,9 +29,10 @@ pub(super) fn paint(
|
||||
return;
|
||||
}
|
||||
|
||||
let label = definition.map_or("?", |definition| definition.label);
|
||||
let label = materialcraft_engine::nodes::function::node_label(node);
|
||||
let category = definition.map_or("", |definition| definition.category);
|
||||
node_card::paint(painter, bounds, zoom, label, category_color(category));
|
||||
let color = if node.instance.is_some() { crate::graph::header_color("graph_instance") } else { category_color(category) };
|
||||
node_card::paint(painter, bounds, zoom, label, color);
|
||||
let body = node_card::body(bounds, zoom);
|
||||
if output {
|
||||
painter.rect_filled(body, 0.0, OUTPUT_COLOR);
|
||||
|
||||
@@ -15,5 +15,5 @@ pub(crate) fn show_graph(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
super::warning_banner::show(ui, document, &target, graph);
|
||||
}
|
||||
canvas(app, &mut editor, ui);
|
||||
app.function_editor = Some(editor);
|
||||
super::actions::finish_editor(app, editor);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use egui::Ui;
|
||||
use materialcraft_engine::{core::FunctionGraph, nodes::function::definition};
|
||||
use materialcraft_engine::{core::FunctionGraph, nodes::function::node_label};
|
||||
|
||||
use crate::{MaterialCraftApp, graph::node_selection, i18n::tr};
|
||||
|
||||
@@ -14,7 +14,7 @@ fn current(app: &MaterialCraftApp) -> Option<(&FunctionGraph, u32)> {
|
||||
pub(crate) fn title(app: &MaterialCraftApp) -> Option<String> {
|
||||
let (graph, id) = current(app)?;
|
||||
let node = graph.node(id)?;
|
||||
let label = definition(&node.op).map_or(node.op.as_str(), |definition| definition.label);
|
||||
let label = node_label(node);
|
||||
Some(format!("{label} - {}", tr("Properties")))
|
||||
}
|
||||
|
||||
@@ -23,12 +23,15 @@ pub(crate) fn show(app: &mut MaterialCraftApp, ui: &mut Ui) -> bool {
|
||||
|
||||
let Some(node) = graph.node(id) else { return false };
|
||||
|
||||
let label = definition(&node.op).map_or(node.op.as_str(), |definition| definition.label);
|
||||
let label = node_label(node);
|
||||
ui.heading(tr(label));
|
||||
if materialcraft_engine::nodes::function::output_node(graph).is_some_and(|node| node.id == id) {
|
||||
ui.label(tr("Output node"));
|
||||
}
|
||||
|
||||
if let Some(instance) = &node.instance {
|
||||
ui.label(format!("Source: {}", if instance.package.is_empty() { "This package" } else { &instance.package }));
|
||||
}
|
||||
let mut value = node.value.clone();
|
||||
let changed = value.as_mut().is_some_and(|value| crate::value_editor::show(ui, value));
|
||||
if changed
|
||||
|
||||
@@ -94,15 +94,15 @@ pub(super) fn paint(app: &MaterialCraftApp, editor: &mut FunctionEditor, ui: &mu
|
||||
geometry.rects.push((materialcraft_engine::core::NodeId(u64::from(id)), screen_rect));
|
||||
|
||||
let definition = materialcraft_engine::nodes::function::definition(&node.op);
|
||||
let label = definition.map_or("?", |definition| definition.label);
|
||||
let label = materialcraft_engine::nodes::function::node_label(node);
|
||||
let dot = node.op == "passthrough";
|
||||
let show_labels = crate::graph::port_label::visible(ui, screen_rect);
|
||||
super::node_canvas::paint(&painter, screen_rect, editor.zoom, node, definition, selected.contains(&id), graph.output == Some(id), &tokens);
|
||||
|
||||
let port_radius = (4.5 * editor.zoom).clamp(2.5, 7.0);
|
||||
let port_color = crate::graph::port_color(materialcraft_engine::nodes::PortKind::Value);
|
||||
if let Some(definition) = definition {
|
||||
for (input_index, identifier) in definition.inputs.iter().enumerate() {
|
||||
{
|
||||
for (input_index, identifier) in materialcraft_engine::nodes::function::input_identifiers(node).enumerate() {
|
||||
let position = camera.to_screen(in_port(node, bounds, input_index));
|
||||
painter.circle(position, port_radius, port_color, Stroke::new(1.0, Color32::from_gray(0x18)));
|
||||
if show_labels {
|
||||
@@ -120,7 +120,7 @@ pub(super) fn paint(app: &MaterialCraftApp, editor: &mut FunctionEditor, ui: &mu
|
||||
slot: Some(input_index),
|
||||
position,
|
||||
radius: if dot { (3.5 * editor.zoom).max(2.0) } else { 9.0 },
|
||||
label: port_label(identifier),
|
||||
label: port_label(identifier).into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -142,7 +142,7 @@ pub(super) fn paint(app: &MaterialCraftApp, editor: &mut FunctionEditor, ui: &mu
|
||||
slot: None,
|
||||
position: output_position,
|
||||
radius: if dot { (3.5 * editor.zoom).max(2.0) } else { 9.0 },
|
||||
label: port_label("output"),
|
||||
label: port_label("output").into(),
|
||||
});
|
||||
|
||||
let body = ui.interact(screen_rect, ui.scope_id().with(("fn-node", id)), Sense::click_and_drag());
|
||||
@@ -185,7 +185,7 @@ pub(super) fn paint(app: &MaterialCraftApp, editor: &mut FunctionEditor, ui: &mu
|
||||
.map(|port| crate::graph::PortHit {
|
||||
node: materialcraft_engine::core::NodeId(u64::from(port.node)),
|
||||
port: port.slot.map_or_else(|| "output".into(), |slot| slot.to_string()),
|
||||
label: port.label.into(),
|
||||
label: port.label.clone(),
|
||||
output: port.slot.is_none(),
|
||||
kind: materialcraft_engine::nodes::PortKind::Value,
|
||||
pos: port.position,
|
||||
|
||||
@@ -40,7 +40,11 @@ pub(crate) fn invoke(app: &mut MaterialCraftApp, command: &str) -> bool {
|
||||
}
|
||||
Some("graph") => {
|
||||
if let Some(index) = result.get("graph").and_then(serde_json::Value::as_u64).and_then(|index| usize::try_from(index).ok()) {
|
||||
super::subgraph_navigation::open_instance(app, index);
|
||||
if result.get("document").and_then(serde_json::Value::as_u64) == Some(app.session.document_id) {
|
||||
super::subgraph_navigation::open_instance(app, index);
|
||||
} else {
|
||||
super::subgraph_navigation::open_external_instance(app, &result, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -19,6 +19,12 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2, node: Opti
|
||||
}
|
||||
|
||||
ui.set_min_width(290.0);
|
||||
if let Some(node) = node
|
||||
&& app.session.graph().ok().and_then(|graph| graph.node(node).ok()).is_some_and(|node| node.def == "graph_instance")
|
||||
{
|
||||
super::instance_context_menu::show(app, ui, at, node);
|
||||
return;
|
||||
}
|
||||
if node.is_some() && menus::enabled(app, "graph.node.edit") {
|
||||
action(app, ui, "Edit graph", "graph.node.edit", Value::Null);
|
||||
ui.separator();
|
||||
@@ -55,7 +61,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2, node: Opti
|
||||
action(app, ui, "Next pin", "ui.graph.nextPin", Value::Null);
|
||||
}
|
||||
|
||||
fn add_nodes(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2) {
|
||||
pub(super) fn add_nodes(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2) {
|
||||
let definitions = materialcraft_engine::nodes::catalog().iter().filter(|definition| definition.id != "dot");
|
||||
let entries = definitions.map(|definition| (definition.id, definition.label, definition.category));
|
||||
if let Some(definition) = super::add_node_menu::show(ui, entries) {
|
||||
@@ -63,7 +69,7 @@ fn add_nodes(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2) {
|
||||
}
|
||||
}
|
||||
|
||||
fn outputs(app: &mut MaterialCraftApp, ui: &mut Ui, node: Option<NodeId>) {
|
||||
pub(super) fn outputs(app: &mut MaterialCraftApp, ui: &mut Ui, node: Option<NodeId>) {
|
||||
let targets: Vec<_> = app
|
||||
.session
|
||||
.graph()
|
||||
@@ -97,7 +103,7 @@ fn outputs(app: &mut MaterialCraftApp, ui: &mut Ui, node: Option<NodeId>) {
|
||||
});
|
||||
}
|
||||
|
||||
fn action(app: &mut MaterialCraftApp, ui: &mut Ui, label: &str, command: &str, parameters: Value) {
|
||||
pub(super) fn action(app: &mut MaterialCraftApp, ui: &mut Ui, label: &str, command: &str, parameters: Value) {
|
||||
if super::edit_menu::button(ui, label, command, menus::enabled(app, command)) {
|
||||
if command.starts_with("ui.") || matches!(command, "file.save" | "graph.node.edit") {
|
||||
menus::invoke(app, ui.ctx(), command);
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
use materialcraft_engine::core::NodeId;
|
||||
|
||||
pub(crate) struct GraphParent {
|
||||
pub document: u64,
|
||||
pub graph: usize,
|
||||
pub selection: Vec<NodeId>,
|
||||
pub function: Option<crate::function::FunctionEditor>,
|
||||
pub pattern: Option<crate::pattern::PatternEditor>,
|
||||
pub camera: crate::state::GraphCamera,
|
||||
}
|
||||
|
||||
impl GraphParent {
|
||||
pub(super) fn capture(app: &mut crate::MaterialCraftApp) -> Self {
|
||||
Self {
|
||||
document: app.session.document_id,
|
||||
graph: app.session.graph,
|
||||
selection: app.session.selection.clone(),
|
||||
camera: app.ui.graph_camera,
|
||||
function: app.function_editor.take(),
|
||||
pattern: app.pattern_editor.take(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,12 @@ pub struct GraphView {
|
||||
}
|
||||
|
||||
impl GraphView {
|
||||
pub(crate) fn cancel_gesture(&mut self) {
|
||||
self.drag = None;
|
||||
self.search = None;
|
||||
self.menu_wire = None;
|
||||
self.framed_graph = None;
|
||||
}
|
||||
pub(crate) fn is_dragging(&self) -> bool {
|
||||
self.drag.is_some()
|
||||
}
|
||||
|
||||
@@ -141,9 +141,12 @@ pub(super) fn handle(
|
||||
// Clicks
|
||||
for (id, r) in nodes {
|
||||
if r.double_clicked() {
|
||||
app.run("view.2d", json!({"node": id.0, "port": 0}));
|
||||
if crate::menus::enabled(app, "graph.node.edit") {
|
||||
crate::menus::invoke(app, ui.ctx(), "graph.node.edit");
|
||||
// Instances open their sources only through Open reference
|
||||
if app.session.graph().ok().and_then(|graph| graph.node(*id).ok()).is_some_and(|node| node.def != "graph_instance") {
|
||||
app.run("view.2d", json!({"node": id.0, "port": 0}));
|
||||
if crate::menus::enabled(app, "graph.node.edit") {
|
||||
crate::menus::invoke(app, ui.ctx(), "graph.node.edit");
|
||||
}
|
||||
}
|
||||
} else if r.clicked() && app.session.selection.contains(id) && app.session.view2d.node != Some(*id) {
|
||||
// Keep the current output when deselecting or clicking the node already shown
|
||||
@@ -203,16 +206,20 @@ pub(super) fn handle(
|
||||
None => super::context_menu::show(app, ui, at, None),
|
||||
});
|
||||
|
||||
if let Some(payload) = crate::widgets::dnd_release::<crate::graph_drag_payload::GraphDragPayload>(canvas)
|
||||
&& let Some(pointer) = pointer
|
||||
{
|
||||
let at = cam.to_graph(pointer);
|
||||
app.run("graph.instance.add", json!({"document": payload.document, "graph": payload.name, "at": [at.x - NODE_W * 0.5, at.y - 20.0]}));
|
||||
}
|
||||
|
||||
// Library drops
|
||||
if let Some(payload) = crate::widgets::dnd_release::<LibraryDrag>(canvas)
|
||||
&& let Some(p) = pointer
|
||||
{
|
||||
let gp = cam.to_graph(p);
|
||||
let at = [gp.x - NODE_W * 0.5, gp.y - 20.0];
|
||||
match payload.0.strip_prefix('@') {
|
||||
Some(graph) => app.run("graph.node.add", json!({"def": "graph_instance", "at": at, "params": {"graph": graph}})),
|
||||
None => app.run("graph.node.add", json!({"def": payload.0, "at": at})),
|
||||
};
|
||||
app.run("graph.node.add", json!({"def": payload.0, "at": at}));
|
||||
}
|
||||
|
||||
// Keys over the canvas
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
use crate::{MaterialCraftApp, i18n::tr};
|
||||
use egui::{Pos2, Ui};
|
||||
use materialcraft_engine::core::NodeId;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::context_menu::{action, add_nodes, outputs};
|
||||
|
||||
pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2, node: NodeId) {
|
||||
outputs(app, ui, Some(node));
|
||||
if let Some(parameters) = crate::material_preview_menu::show(ui, &app.session, app.session.graph, node) {
|
||||
app.run("view.3d.assign", parameters);
|
||||
}
|
||||
ui.separator();
|
||||
action(app, ui, "Add Comment", "graph.comment.add", json!({"at": [at.x, at.y]}));
|
||||
action(app, ui, "Add Frame", "graph.frame.add", json!({"rect": [at.x, at.y, 400.0, 260.0]}));
|
||||
if ui.button(tr("Copy info to clipboard")).clicked() {
|
||||
if let Some(info) = app.run("graph.node.info", json!({"id": node}))
|
||||
&& let Some(text) = info.get("text").and_then(Value::as_str)
|
||||
{
|
||||
ui.ctx().copy_text(text.into());
|
||||
}
|
||||
ui.close();
|
||||
}
|
||||
ui.separator();
|
||||
ui.menu_button(tr("Manage parameters"), |ui| {
|
||||
action(app, ui, "Reset parameters", "graph.node.reset", json!({"id": node}));
|
||||
});
|
||||
ui.separator();
|
||||
ui.menu_button(tr("Create"), |ui| {
|
||||
ui.menu_button(tr("Add node"), |ui| add_nodes(app, ui, at));
|
||||
action(app, ui, "Add Dot node", "graph.node.add", json!({"def": "dot", "at": [at.x, at.y]}));
|
||||
action(app, ui, "Add Navigation pin", "graph.pin.add", json!({"at": [at.x, at.y]}));
|
||||
});
|
||||
ui.separator();
|
||||
action(app, ui, "Open reference", "graph.node.edit", Value::Null);
|
||||
ui.separator();
|
||||
let label = format!("{} ({})", tr("Create graph from selection"), app.session.selection.len());
|
||||
action(app, ui, &label, "graph.createFromSelection", Value::Null);
|
||||
for (label, command) in [
|
||||
("Copy selection", "edit.copy"),
|
||||
("Delete selection", "edit.delete"),
|
||||
("Delete and relink", "edit.deleteRelink"),
|
||||
("Duplicate selection", "edit.duplicate"),
|
||||
("Duplicate selection without link", "edit.duplicateWithoutLinks"),
|
||||
("Select upstream nodes", "graph.selectUpstream"),
|
||||
("Select downstream nodes", "graph.selectDownstream"),
|
||||
("Disable selection", "graph.node.disable"),
|
||||
] {
|
||||
action(app, ui, label, command, Value::Null);
|
||||
}
|
||||
if super::wire_actions::swap_button(app, ui, &app.graph.selected_wires, None) {
|
||||
super::wire_actions::swap_selected_wires(app);
|
||||
ui.close();
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ mod graph_camera;
|
||||
mod graph_drag;
|
||||
pub(crate) mod graph_parent;
|
||||
mod graph_view;
|
||||
mod instance_context_menu;
|
||||
#[cfg(test)]
|
||||
mod interaction_tests;
|
||||
mod library_drag;
|
||||
|
||||
@@ -14,7 +14,7 @@ pub fn header_color(def: &str) -> Color32 {
|
||||
match def {
|
||||
"output" => Color32::from_rgb(0x2c, 0x8f, 0x5e),
|
||||
"input_color" | "input_grayscale" | "input_value" => Color32::from_rgb(0x6d, 0x52, 0xb5),
|
||||
"graph_instance" => Color32::from_rgb(0x1e, 0x8a, 0xa0),
|
||||
"graph_instance" => Color32::from_rgb(0xa2, 0x3f, 0x4a),
|
||||
_ if cat.starts_with("Atomic") => Color32::from_rgb(0x5c, 0x62, 0x6e),
|
||||
_ if cat.contains("Noises") => Color32::from_rgb(0xa8, 0x3c, 0x34),
|
||||
_ if cat.contains("Patterns") => Color32::from_rgb(0xb3, 0x5a, 0x2c),
|
||||
@@ -38,6 +38,9 @@ pub fn node_label(n: &Node) -> String {
|
||||
if !n.label.is_empty() {
|
||||
return n.label.clone();
|
||||
}
|
||||
if let Some(instance) = &n.instance {
|
||||
return instance.graph.clone();
|
||||
}
|
||||
let label = match n.def.as_str() {
|
||||
"graph_instance" => match n.params.get("graph") {
|
||||
Some(Value::Text(g)) => g.clone(),
|
||||
|
||||
@@ -17,9 +17,23 @@ pub(crate) fn parent(app: &mut MaterialCraftApp) {
|
||||
} else if crate::pattern::active(app) {
|
||||
app.pattern_editor = None;
|
||||
} else if let Some(parent) = app.graph_parents.pop()
|
||||
&& open_graph(app, parent.graph)
|
||||
&& open_document_graph(app, parent.document, parent.graph)
|
||||
{
|
||||
app.session.selection = parent.selection.into_iter().filter(|id| app.session.graph().is_ok_and(|graph| graph.node(*id).is_ok())).collect();
|
||||
app.session.selection = parent
|
||||
.selection
|
||||
.into_iter()
|
||||
.filter(|id| {
|
||||
app.session.graph().is_ok_and(|graph| {
|
||||
if let Some(function) = &graph.function {
|
||||
u32::try_from(id.0).ok().is_some_and(|id| function.node(id).is_some())
|
||||
} else {
|
||||
graph.node(*id).is_ok()
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
app.function_editor = parent.function;
|
||||
app.pattern_editor = parent.pattern;
|
||||
app.ui.graph_camera = parent.camera;
|
||||
app.graph.framed_graph = Some(parent.graph);
|
||||
app.ui.frame_all = false;
|
||||
@@ -37,13 +51,39 @@ pub(crate) fn open_function(app: &mut MaterialCraftApp, node: materialcraft_engi
|
||||
}
|
||||
|
||||
pub(crate) fn open_instance(app: &mut MaterialCraftApp, graph: usize) {
|
||||
let parent = GraphParent { graph: app.session.graph, selection: app.session.selection.clone(), camera: app.ui.graph_camera };
|
||||
let parent = GraphParent::capture(app);
|
||||
if open_graph(app, graph) {
|
||||
app.graph_parents.push(parent);
|
||||
app.ui.dock.focus(Panel::Graph);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn open_external_instance(app: &mut MaterialCraftApp, result: &serde_json::Value, graph: usize) {
|
||||
let parent = GraphParent::capture(app);
|
||||
let parents = std::mem::take(&mut app.graph_parents);
|
||||
let opened = if let Some(document) = result.get("document").and_then(serde_json::Value::as_u64) {
|
||||
open_document_graph(app, document, graph)
|
||||
} else if let Some(path) = result.get("path").and_then(serde_json::Value::as_str) {
|
||||
app.run("file.open", json!({"path": path})).is_some() && open_graph(app, graph)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
app.graph_parents = parents;
|
||||
if opened {
|
||||
app.graph_parents.push(parent);
|
||||
} else {
|
||||
app.function_editor = parent.function;
|
||||
app.pattern_editor = parent.pattern;
|
||||
}
|
||||
}
|
||||
|
||||
fn open_document_graph(app: &mut MaterialCraftApp, document: u64, graph: usize) -> bool {
|
||||
let parents = std::mem::take(&mut app.graph_parents);
|
||||
let opened = (document == app.session.document_id || app.run("package.activate", json!({"document": document})).is_some()) && open_graph(app, graph);
|
||||
app.graph_parents = parents;
|
||||
opened
|
||||
}
|
||||
|
||||
fn open_graph(app: &mut MaterialCraftApp, index: usize) -> bool {
|
||||
let parents = std::mem::take(&mut app.graph_parents);
|
||||
let material = app.session.material_preview.clone();
|
||||
@@ -54,8 +94,18 @@ fn open_graph(app: &mut MaterialCraftApp, index: usize) -> bool {
|
||||
}
|
||||
|
||||
pub(crate) fn breadcrumb(app: &MaterialCraftApp) -> String {
|
||||
let mut path: Vec<String> =
|
||||
app.graph_parents.iter().filter_map(|parent| app.session.pkg.graphs.get(parent.graph).map(|graph| graph.name.clone())).collect();
|
||||
let mut path: Vec<String> = app
|
||||
.graph_parents
|
||||
.iter()
|
||||
.filter_map(|parent| {
|
||||
app.session
|
||||
.open_packages()
|
||||
.into_iter()
|
||||
.find(|entry| entry.0 == parent.document)
|
||||
.and_then(|entry| entry.1.graphs.get(parent.graph))
|
||||
.map(|graph| graph.name.clone())
|
||||
})
|
||||
.collect();
|
||||
if let Ok(graph) = app.session.graph() {
|
||||
path.push(graph.name.clone());
|
||||
let owner = app.function_editor.as_ref().and_then(|editor| editor.node).or_else(|| app.pattern_editor.as_ref().map(|editor| editor.node));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/// A graph resource dragged from Explorer to a material preview
|
||||
/// A graph resource dragged from an open Explorer package
|
||||
pub(crate) struct GraphDragPayload {
|
||||
pub document: u64,
|
||||
pub name: String,
|
||||
}
|
||||
@@ -96,9 +96,6 @@ fn category_tree(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
}
|
||||
};
|
||||
row(ui, format!("{} {}", icon::SQUARES_FOUR, tr("All nodes")), "", 4.0);
|
||||
if app.session.pkg.graphs.len() > 1 {
|
||||
row(ui, format!("{} {}", icon::TREE_STRUCTURE, tr("This package")), "@package", 4.0);
|
||||
}
|
||||
for (top, subs) in tree() {
|
||||
row(ui, format!("{} {}", category_icon(top), tr(top)), top, 4.0);
|
||||
for sub in subs {
|
||||
@@ -108,21 +105,12 @@ fn category_tree(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Library entries for the current filter: (id, label, category, is a package graph).
|
||||
fn entries(app: &MaterialCraftApp) -> Vec<(String, String, &'static str, bool)> {
|
||||
/// Atomic library entries matching the current filter
|
||||
fn entries(app: &MaterialCraftApp) -> Vec<(String, String, &'static str)> {
|
||||
let search = app.ui.library_search.to_ascii_lowercase();
|
||||
let cat = &app.ui.library_category;
|
||||
let mut out: Vec<(String, String, &'static str, bool)> = Vec::new();
|
||||
if cat == "@package" || !search.is_empty() {
|
||||
for (i, g) in app.session.pkg.graphs.iter().enumerate() {
|
||||
if i != app.session.graph && (search.is_empty() || g.name.to_ascii_lowercase().contains(&search)) {
|
||||
out.push((g.name.clone(), g.name.clone(), "This package", true));
|
||||
}
|
||||
}
|
||||
if cat == "@package" {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
let category = app.ui.library_category.as_str();
|
||||
let cat = if category == "@package" { "" } else { category };
|
||||
let mut out = Vec::new();
|
||||
for d in materialcraft_engine::nodes::catalog() {
|
||||
if d.id == "graph_instance" {
|
||||
continue;
|
||||
@@ -133,7 +121,7 @@ fn entries(app: &MaterialCraftApp) -> Vec<(String, String, &'static str, bool)>
|
||||
if !(search.is_empty() || d.label.to_ascii_lowercase().contains(&search) || d.id.contains(&search)) {
|
||||
continue;
|
||||
}
|
||||
out.push((d.id.to_string(), d.label.to_string(), d.category, false));
|
||||
out.push((d.id.to_string(), d.label.to_string(), d.category));
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -149,19 +137,18 @@ fn items(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
let as_list = app.ui.prefs.library_list;
|
||||
let tile = vec2(78.0, 98.0);
|
||||
let cols = if as_list { 1 } else { ((ui.available_width() + 4.0) / (tile.x + 4.0)).floor().max(1.0) as usize };
|
||||
let mut add: Option<(String, bool)> = None;
|
||||
let mut add = None;
|
||||
for chunk in list.chunks(cols) {
|
||||
ui.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing = vec2(4.0, 4.0);
|
||||
for (id, label, cat, is_graph) in chunk {
|
||||
for (id, label, cat) in chunk {
|
||||
let size = if as_list { vec2(ui.available_width(), 26.0) } else { tile };
|
||||
let (r, resp) = ui.allocate_exact_size(size, Sense::click_and_drag());
|
||||
crate::widgets::name(&resp, egui::Role::ListItem, label);
|
||||
let def_id = if *is_graph { "graph_instance" } else { id.as_str() };
|
||||
if resp.hovered() {
|
||||
ui.painter().rect_filled(r, 6.0, tk.row_hover);
|
||||
}
|
||||
let tex = if *is_graph { None } else { app.thumbs.library(&ctx, id) };
|
||||
let tex = app.thumbs.library(&ctx, id);
|
||||
let img_rect = if as_list {
|
||||
Rect::from_min_size(r.min + vec2(2.0, 2.0), vec2(22.0, 22.0))
|
||||
} else {
|
||||
@@ -172,11 +159,11 @@ fn items(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
ui.painter().image(t, img_rect, Rect::from_min_max(egui::Pos2::ZERO, egui::pos2(1.0, 1.0)), Color32::WHITE);
|
||||
}
|
||||
None => {
|
||||
ui.painter().rect_filled(img_rect, 4.0, header_color(def_id).gamma_multiply(0.7));
|
||||
ui.painter().rect_filled(img_rect, 4.0, header_color(id).gamma_multiply(0.7));
|
||||
ui.painter().text(
|
||||
img_rect.center(),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
if *is_graph { icon::TREE_STRUCTURE } else { icon::CUBE },
|
||||
icon::CUBE,
|
||||
egui::FontId::proportional(img_rect.height() * 0.4),
|
||||
Color32::WHITE,
|
||||
);
|
||||
@@ -184,7 +171,7 @@ fn items(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
}
|
||||
// The category color as a strip, like the node headers.
|
||||
let strip = Rect::from_min_size(img_rect.left_bottom() - vec2(0.0, 3.0), vec2(img_rect.width(), 3.0));
|
||||
ui.painter().rect_filled(strip, 0.0, header_color(def_id));
|
||||
ui.painter().rect_filled(strip, 0.0, header_color(id));
|
||||
if as_list {
|
||||
let g = crate::widgets::elided(ui.painter(), label.clone(), 11.5, tk.text, r.width() - 34.0);
|
||||
ui.painter().galley(r.left_center() + vec2(30.0, -g.size().y * 0.5), g, tk.text);
|
||||
@@ -195,20 +182,33 @@ fn items(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
}
|
||||
let resp = resp.on_hover_text(format!("{label}\n{cat}"));
|
||||
if resp.drag_started() {
|
||||
let payload = if *is_graph { format!("@{id}") } else { id.clone() };
|
||||
egui::DragAndDrop::set_payload(&ctx, LibraryDrag(payload));
|
||||
egui::DragAndDrop::set_payload(&ctx, LibraryDrag(id.clone()));
|
||||
}
|
||||
if resp.double_clicked() {
|
||||
add = Some((id.clone(), *is_graph));
|
||||
add = Some(id.clone());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if let Some((id, is_graph)) = add {
|
||||
if is_graph {
|
||||
app.run("graph.node.add", serde_json::json!({"def": "graph_instance", "params": {"graph": id}}));
|
||||
} else {
|
||||
crate::graph::add_node_near_selection(app, &id);
|
||||
}
|
||||
if let Some(id) = add {
|
||||
crate::graph::add_node_near_selection(app, &id);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn open_package_graphs_are_explorer_resources_not_library_entries() {
|
||||
let mut app = MaterialCraftApp::headless(materialcraft_engine::new_session(), crate::UiState::default());
|
||||
let mut graph = materialcraft_engine::core::Graph::new("Reusable source");
|
||||
graph.add_node("output", [0.0; 2]);
|
||||
app.session.pkg.graphs.push(graph);
|
||||
app.ui.library_search = "Reusable source".into();
|
||||
assert!(entries(&app).is_empty());
|
||||
app.ui.library_search.clear();
|
||||
app.ui.library_category = "@package".into();
|
||||
assert!(entries(&app).iter().any(|entry| entry.0 == "uniform"));
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,7 @@ impl MaterialCraftApp {
|
||||
if matches!(
|
||||
id,
|
||||
"graph.node.add"
|
||||
| "graph.instance.add"
|
||||
| "graph.node.move"
|
||||
| "graph.frame.move"
|
||||
| "graph.wire.insertDot"
|
||||
@@ -145,11 +146,16 @@ impl MaterialCraftApp {
|
||||
}
|
||||
|
||||
pub(crate) fn close_subgraph_after(&mut self, command: &str) {
|
||||
if command.starts_with("file.new") || matches!(command, "file.open" | "graph.open" | "graph.delete" | "graph.duplicate") {
|
||||
if command.starts_with("file.new")
|
||||
|| command.starts_with("package.")
|
||||
|| matches!(command, "file.open" | "graph.open" | "graph.delete" | "graph.duplicate")
|
||||
{
|
||||
self.graph.inspected_node = None;
|
||||
self.graph_parents.clear();
|
||||
self.pattern_editor = None;
|
||||
self.function_editor = None;
|
||||
self.graph.selected_wires.clear();
|
||||
self.graph.cancel_gesture();
|
||||
}
|
||||
self.reconcile_editors();
|
||||
if (materialcraft_engine::selection_commands::supports(command) || matches!(command, "edit.undo" | "edit.redo"))
|
||||
|
||||
@@ -46,7 +46,8 @@ mod unsaved_state_tests {
|
||||
let mut a = app(false);
|
||||
guard(&mut a, Guarded::Menu { id: "ui.closeAll".into() });
|
||||
assert!(a.ui.windows.unsaved.is_none());
|
||||
assert_eq!(a.session.pkg.graphs.first().map(|g| g.name.as_str()), Some("New_graph"), "the empty package replaced it");
|
||||
assert!(a.session.pkg.graphs.is_empty());
|
||||
assert_eq!(a.session.open_packages().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -150,8 +150,10 @@ pub fn invoke(app: &mut MaterialCraftApp, ctx: &egui::Context, id: &str) {
|
||||
"ui.preferences" => app.ui.windows.preferences = true,
|
||||
"ui.view.graph" => {}
|
||||
"ui.resetLayout" => app.ui.dock = crate::docking::Dock::default(),
|
||||
// These replace the open package: ask first when it has unsaved changes.
|
||||
"ui.closeAll" | "file.newPackage" => crate::unsaved::guard(app, crate::unsaved::Guarded::Menu { id: id.to_string() }),
|
||||
"ui.closeAll" => crate::unsaved::guard(app, crate::unsaved::Guarded::Menu { id: id.to_string() }),
|
||||
"file.newPackage" => {
|
||||
app.run("file.newPackage", serde_json::json!({}));
|
||||
}
|
||||
"ui.reloadResources" | "ui.clearCache" => {
|
||||
if let Ok(mut c) = app.session.cache.lock() {
|
||||
c.clear();
|
||||
|
||||
@@ -135,14 +135,15 @@ fn base_params(app: &mut MaterialCraftApp, ui: &mut Ui, node: &materialcraft_eng
|
||||
|
||||
fn instance_params(app: &mut MaterialCraftApp, ui: &mut Ui, node: &materialcraft_engine::core::Node) {
|
||||
let tokens = Tokens::get(ui.ctx());
|
||||
let Some((graph_index, subgraph)) = materialcraft_engine::eval::instance_graph(&app.session.pkg, node) else {
|
||||
let Some((_, subgraph)) = materialcraft_engine::eval::instance_graph(&app.session.pkg, node) else {
|
||||
ui.label(RichText::new(tr("The instanced graph is missing")).color(tokens.danger));
|
||||
return;
|
||||
};
|
||||
|
||||
let exposed = subgraph.exposed.clone();
|
||||
if ui.button(format!("{} {}", icon::ARROW_SQUARE_OUT, tr("Open graph"))).clicked() {
|
||||
crate::graph::subgraph_navigation::open_instance(app, graph_index);
|
||||
app.run("graph.select", json!({"ids": [node.id.0]}));
|
||||
crate::menus::invoke(app, ui.ctx(), "graph.node.edit");
|
||||
return;
|
||||
}
|
||||
if exposed.is_empty() {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
//! Ask before unsaved work is thrown away
|
||||
//!
|
||||
//! Opening a package, closing all, making an empty package, dropping a file on the window and
|
||||
//! closing the window or quitting all replace or end the open package. When it has unsaved
|
||||
//! changes, those paths ask first (Save / Discard / Cancel). Only these UI paths ask: engine
|
||||
//! commands run by the control channel, MCP, the CLI and scripts never open a dialog
|
||||
//! Closing all packages or quitting asks Save / Discard / Cancel for each modified package
|
||||
//! Opening or creating another package retains the existing documents without asking
|
||||
//! Engine commands run by the control channel, MCP, the CLI and scripts never open a dialog
|
||||
//!
|
||||
//! The decision is a pure state machine ([`request`], [`answer`], [`after_save`]) over the
|
||||
//! serde [`Guarded`] action held in `Windows::unsaved`, so tests and the control channel
|
||||
@@ -24,7 +23,7 @@ pub use step::Step;
|
||||
|
||||
/// Does throwing this session away lose work?
|
||||
pub fn needs_confirmation(session: &materialcraft_engine::Session) -> bool {
|
||||
session.modified
|
||||
session.open_packages().iter().any(|entry| entry.3)
|
||||
}
|
||||
|
||||
/// Ask to run `action`. Returns it when it can run now; otherwise holds it in `pending` for the
|
||||
@@ -61,6 +60,11 @@ pub fn holds_close(modified: bool, confirmed: bool) -> bool {
|
||||
|
||||
/// Run `action`, asking first when the package has unsaved changes
|
||||
pub fn guard(app: &mut MaterialCraftApp, action: Guarded) {
|
||||
if app.ui.windows.unsaved.is_none()
|
||||
&& let Some((document, ..)) = app.session.open_packages().into_iter().find(|entry| entry.3)
|
||||
{
|
||||
app.run("package.activate", serde_json::json!({"document": document}));
|
||||
}
|
||||
let modified = needs_confirmation(&app.session);
|
||||
if let Some(a) = request(&mut app.ui.windows.unsaved, modified, action) {
|
||||
perform(app, a);
|
||||
@@ -70,8 +74,17 @@ pub fn guard(app: &mut MaterialCraftApp, action: Guarded) {
|
||||
/// Run an allowed action
|
||||
fn perform(app: &mut MaterialCraftApp, action: Guarded) {
|
||||
match action {
|
||||
Guarded::Menu { .. } => {
|
||||
app.run("file.newPackage", Value::Null);
|
||||
action @ (Guarded::Menu { .. } | Guarded::Quit) => {
|
||||
if app.session.modified {
|
||||
app.run("package.close", serde_json::json!({"discard": true}));
|
||||
}
|
||||
if needs_confirmation(&app.session) {
|
||||
guard(app, action);
|
||||
} else if action == Guarded::Quit {
|
||||
app.quit_confirmed = true;
|
||||
} else {
|
||||
app.run("package.closeAll", Value::Null);
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
Guarded::OpenPath { path } => crate::windows::open_path_now(app, &path),
|
||||
@@ -82,7 +95,6 @@ fn perform(app: &mut MaterialCraftApp, action: Guarded) {
|
||||
crate::web_files::load_now(app, &name, &bytes);
|
||||
}
|
||||
}
|
||||
Guarded::Quit => app.quit_confirmed = true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +145,7 @@ pub fn show(app: &mut MaterialCraftApp, ctx: &Context) {
|
||||
Step::Run(a) => perform(app, a),
|
||||
Step::Save(a) => {
|
||||
crate::windows::file_action(app, ctx, "file.save");
|
||||
match after_save(needs_confirmation(&app.session), a) {
|
||||
match after_save(app.session.modified, a) {
|
||||
Some(a) => perform(app, a),
|
||||
None => app.pending_load = None,
|
||||
}
|
||||
|
||||
@@ -25,23 +25,39 @@ mod unsaved_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_second_package_cannot_replace_the_payload_waiting_for_confirmation() {
|
||||
use crate::unsaved::{Choice, Guarded, Step};
|
||||
fn opening_packages_preserves_the_existing_unsaved_document() {
|
||||
let mut app = crate::MaterialCraftApp::headless(materialcraft_engine::sample_session(), crate::UiState::default());
|
||||
app.session.modified = true;
|
||||
let bytes = |name: &str| materialcraft_engine::formats::sbs::write_sbs(&materialcraft_engine::core::Package::new(name)).unwrap();
|
||||
crate::web_files::load(&mut app, "first.sbs", bytes("First"), false);
|
||||
crate::web_files::load(&mut app, "second.sbs", bytes("Second"), false);
|
||||
assert_eq!(app.ui.windows.unsaved, Some(Guarded::LoadBytes { name: "first.sbs".into() }));
|
||||
assert!(app.ui.windows.unsaved.is_none());
|
||||
assert_eq!(app.session.open_packages().len(), 3);
|
||||
let result = crate::control::handle(&mut app, &egui::Context::default(), "ui.set", &serde_json::json!({"prefs": {"thumbnails": false}}));
|
||||
assert_eq!(result["ok"], true);
|
||||
assert!(app.ui.windows.unsaved.is_some());
|
||||
if let Step::Run(action) = crate::unsaved::answer(&mut app.ui.windows.unsaved, Choice::Discard) {
|
||||
crate::unsaved::perform(&mut app, action);
|
||||
} else {
|
||||
panic!("missing held action");
|
||||
}
|
||||
assert_eq!(app.session.pkg.name, "First");
|
||||
app.run("package.activate", serde_json::json!({"document": 0})).unwrap();
|
||||
assert!(app.session.modified);
|
||||
assert_ne!(app.session.pkg.name, "First");
|
||||
assert_ne!(app.session.pkg.name, "Second");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quitting_asks_about_each_modified_package() {
|
||||
use crate::unsaved::{Guarded, guard, perform};
|
||||
let mut app = crate::MaterialCraftApp::headless(materialcraft_engine::sample_session(), crate::UiState::default());
|
||||
app.session.modified = true;
|
||||
app.run("file.newPackage", serde_json::json!({"name": "Second"})).unwrap();
|
||||
app.session.modified = true;
|
||||
guard(&mut app, Guarded::Quit);
|
||||
assert_eq!(app.session.document_id, 0);
|
||||
app.ui.windows.unsaved = None;
|
||||
perform(&mut app, Guarded::Quit);
|
||||
assert!(!app.quit_confirmed);
|
||||
assert_eq!(app.session.pkg.name, "Second");
|
||||
assert_eq!(app.ui.windows.unsaved, Some(Guarded::Quit));
|
||||
app.ui.windows.unsaved = None;
|
||||
perform(&mut app, Guarded::Quit);
|
||||
assert!(app.quit_confirmed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -215,7 +215,9 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(avail.x, (avail.y - 22.0).max(40.0)), Sense::click_and_drag());
|
||||
crate::widgets::name(&resp, egui::Role::Image, "3D view");
|
||||
let graph_hover = resp.dnd_hover_payload::<crate::graph_drag_payload::GraphDragPayload>().is_some();
|
||||
if let Some(graph) = crate::widgets::dnd_release::<crate::graph_drag_payload::GraphDragPayload>(&resp) {
|
||||
if let Some(graph) = crate::widgets::dnd_release::<crate::graph_drag_payload::GraphDragPayload>(&resp)
|
||||
&& (graph.document == app.session.document_id || app.run("package.activate", json!({"document": graph.document})).is_some())
|
||||
{
|
||||
app.run("view.3d.outputs", json!({"graph": graph.name}));
|
||||
}
|
||||
|
||||
|
||||
@@ -65,8 +65,7 @@ pub fn load(app: &mut MaterialCraftApp, name: &str, bytes: Vec<u8>, bitmap: bool
|
||||
if app.ui.windows.unsaved.is_some() {
|
||||
return;
|
||||
}
|
||||
app.pending_load = Some((name.to_string(), bytes));
|
||||
crate::unsaved::guard(app, crate::unsaved::Guarded::LoadBytes { name: name.to_string() });
|
||||
load_now(app, name, &bytes);
|
||||
}
|
||||
|
||||
pub(crate) fn load_now(app: &mut MaterialCraftApp, name: &str, bytes: &[u8]) {
|
||||
@@ -247,14 +246,15 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_package_picked_over_unsaved_work_is_held_not_loaded() {
|
||||
fn an_invalid_package_reports_an_error_without_discarding_unsaved_work() {
|
||||
let mut app = app(true);
|
||||
let before = app.session.pkg.name.clone();
|
||||
load(&mut app, "other.sbs", b"not a package".to_vec(), false);
|
||||
assert_eq!(app.ui.windows.unsaved, Some(crate::unsaved::Guarded::LoadBytes { name: "other.sbs".into() }));
|
||||
assert_eq!(app.pending_load.as_ref().map(|(n, _)| n.as_str()), Some("other.sbs"));
|
||||
assert!(app.ui.windows.unsaved.is_none());
|
||||
assert!(app.pending_load.is_none());
|
||||
assert_eq!(app.session.pkg.name, before);
|
||||
assert!(app.toasts.is_empty());
|
||||
assert!(app.session.modified);
|
||||
assert!(!app.toasts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -591,7 +591,7 @@ pub fn file_action(app: &mut MaterialCraftApp, ctx: &egui::Context, id: &str) ->
|
||||
/// Open a package from a path (recent list, command line)
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn open_path(app: &mut MaterialCraftApp, path: &str) {
|
||||
crate::unsaved::guard(app, crate::unsaved::Guarded::OpenPath { path: path.to_string() });
|
||||
open_path_now(app, path);
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
#[cfg(test)]
|
||||
mod open_guard_tests {
|
||||
use crate::unsaved::Guarded;
|
||||
use crate::{MaterialCraftApp, state::UiState};
|
||||
|
||||
#[test]
|
||||
fn opening_a_path_over_unsaved_work_asks_first() {
|
||||
fn a_failed_open_keeps_unsaved_work_without_a_discard_prompt() {
|
||||
let mut s = materialcraft_engine::sample_session();
|
||||
s.modified = true;
|
||||
let mut app = MaterialCraftApp::headless(s, UiState::default());
|
||||
let before = app.session.pkg.clone();
|
||||
crate::windows::open_path(&mut app, "does-not-exist.sbs");
|
||||
assert_eq!(app.ui.windows.unsaved, Some(Guarded::OpenPath { path: "does-not-exist.sbs".into() }));
|
||||
assert!(app.toasts.is_empty(), "nothing was attempted yet");
|
||||
assert!(app.ui.windows.unsaved.is_none());
|
||||
assert!(!app.toasts.is_empty());
|
||||
assert_eq!(app.session.pkg, before);
|
||||
assert!(app.session.modified);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#[path = "snapshot/function_instances.rs"]
|
||||
mod function_instances;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use egui::vec2;
|
||||
@@ -32,6 +35,96 @@ fn save(h: &mut Harness<'static, MaterialCraftApp>, dir: &std::path::Path, name:
|
||||
println!("wrote {}", dir.join(format!("{name}.png")).display());
|
||||
}
|
||||
|
||||
fn double_click(harness: &mut Harness<'static, MaterialCraftApp>, position: egui::Pos2) {
|
||||
harness.event(egui::Event::PointerMoved(position));
|
||||
harness.step();
|
||||
let start = harness.ctx.input(|input| input.time) + 1.0;
|
||||
for (index, pressed) in [true, false, true, false].into_iter().enumerate() {
|
||||
harness.input_mut().time = Some(start + index as f64 * 0.05);
|
||||
harness.event(egui::Event::PointerButton { pos: position, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE });
|
||||
harness.step();
|
||||
}
|
||||
settle(harness, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires an offscreen GPU; run explicitly with --ignored"]
|
||||
fn explorer_drags_a_linked_graph_between_open_packages() {
|
||||
use materialcraft_engine::{
|
||||
Session,
|
||||
core::{Graph, Package, Value},
|
||||
file_cmds, formats,
|
||||
};
|
||||
let directory = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").expect("set MC_SNAPSHOT_DIR"));
|
||||
std::fs::create_dir_all(&directory).unwrap();
|
||||
let mut source = Package::new("Source file");
|
||||
let mut graph = Graph::new("Reusable tint");
|
||||
graph.size = [4, 4];
|
||||
let color = graph.add_node("uniform", [0.0; 2]);
|
||||
graph.node_mut(color).unwrap().params.insert("color".into(), Value::Color([0.2, 0.6, 0.8, 1.0]));
|
||||
let output = graph.add_node("output", [256.0, 0.0]);
|
||||
graph.connect(color, "output", output, "input").unwrap();
|
||||
source.graphs.push(graph);
|
||||
let mut session = Session::new(source);
|
||||
session.path = Some(directory.join("source.sbs").to_string_lossy().into_owned());
|
||||
let mut target = Package::new("Target file");
|
||||
target.graphs.push(Graph::new("Target canvas"));
|
||||
file_cmds::open_bytes(&mut session, &formats::sbs::write_sbs(&target).unwrap(), Some(directory.join("target.sbs").to_string_lossy().into_owned())).unwrap();
|
||||
session.preview_size = Some(16);
|
||||
let target_document = session.document_id;
|
||||
let mut app = MaterialCraftApp::headless(session, UiState::default());
|
||||
app.ui.dock.close(materialcraft_ui_egui::docking::Panel::Library);
|
||||
let mut harness = harness(app, 1600.0, 960.0);
|
||||
settle(&mut harness, 6);
|
||||
let start = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Reusable tint")).rect().center();
|
||||
let end = harness.state().graph.view.center();
|
||||
harness.event(egui::Event::PointerMoved(start));
|
||||
harness.step();
|
||||
harness.event(egui::Event::PointerButton { pos: start, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE });
|
||||
harness.step();
|
||||
for position in [start + vec2(12.0, 0.0), end] {
|
||||
harness.event(egui::Event::PointerMoved(position));
|
||||
harness.step();
|
||||
}
|
||||
harness.event(egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE });
|
||||
settle(&mut harness, 8);
|
||||
assert_eq!(harness.state().session.document_id, target_document);
|
||||
assert_eq!(harness.state().session.graph().unwrap().nodes.len(), 1);
|
||||
let instance = harness.state().session.graph().unwrap().nodes[0].id;
|
||||
assert_eq!(harness.state().session.graph().unwrap().node(instance).unwrap().instance.as_ref().unwrap().graph, "Reusable tint");
|
||||
assert!(harness.state().session.cooked.nodes[&instance].error.is_none());
|
||||
save(&mut harness, &directory, "linked-instance-explorer-drop");
|
||||
harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Reusable tint")).click();
|
||||
settle(&mut harness, 3);
|
||||
assert_eq!(harness.state().session.document_id, 0);
|
||||
harness.state_mut().run("graph.select", json!({"ids": [color.0]})).unwrap();
|
||||
harness.state_mut().run("graph.node.param", json!({"id": color.0, "param": "color", "value": [0.9, 0.3, 0.1, 1.0]})).unwrap();
|
||||
harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Target canvas")).click();
|
||||
settle(&mut harness, 8);
|
||||
assert_eq!(harness.state().session.document_id, target_document);
|
||||
assert_eq!(harness.state().session.history(), ["Add graph instance"]);
|
||||
let value = harness.state().session.cooked.image(instance, 0).unwrap().get(0, 0, false);
|
||||
assert!(value.into_iter().zip([0.9, 0.3, 0.1, 1.0]).all(|(actual, expected)| (actual - expected).abs() < 1e-6));
|
||||
assert!(harness.state().session.modified);
|
||||
save(&mut harness, &directory, "linked-instance-source-edit");
|
||||
let position = harness.get(By::new().role(egui::accesskit::Role::Button).label("Reusable tint")).rect().center();
|
||||
double_click(&mut harness, position);
|
||||
assert_eq!(harness.state().session.document_id, target_document, "double-click must not open the instance source");
|
||||
assert_eq!(harness.state().session.graph().unwrap().name, "Target canvas");
|
||||
assert_eq!(harness.state().session.history(), ["Add graph instance"]);
|
||||
harness.get(By::new().role(egui::accesskit::Role::Button).label("Reusable tint")).click_secondary();
|
||||
settle(&mut harness, 2);
|
||||
for label in ["View output in 2D view", "Copy info to clipboard", "Open reference Ctrl+E", "Create graph from selection (1)", "Duplicate selection Ctrl+D"]
|
||||
{
|
||||
assert!(harness.query(By::new().label(label)).is_some(), "missing instance action: {label}");
|
||||
}
|
||||
save(&mut harness, &directory, "linked-instance-context-menu");
|
||||
harness.get(By::new().label("Open reference Ctrl+E")).click();
|
||||
settle(&mut harness, 4);
|
||||
assert_eq!(harness.state().session.document_id, 0);
|
||||
assert_eq!(harness.state().session.graph().unwrap().name, "Reusable tint");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires an offscreen GPU; run explicitly with --ignored"]
|
||||
fn function_output_highlight_follows_context_menu_and_undo() {
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
use super::{By, Harness, MaterialCraftApp, Queryable, UiState, double_click, harness, json, save, settle, vec2};
|
||||
use materialcraft_engine::{
|
||||
FunctionGraphTarget, Session,
|
||||
core::{FunctionGraph, Graph, NodeId, Package, Value},
|
||||
file_cmds, formats,
|
||||
};
|
||||
|
||||
fn drag(harness: &mut Harness<'static, MaterialCraftApp>, name: &str) {
|
||||
let start = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label(name)).rect().center();
|
||||
let end = harness.state().graph.view.center();
|
||||
harness.event(egui::Event::PointerMoved(start));
|
||||
harness.step();
|
||||
harness.event(egui::Event::PointerButton { pos: start, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE });
|
||||
harness.step();
|
||||
for position in [start + vec2(12.0, 0.0), end] {
|
||||
harness.event(egui::Event::PointerMoved(position));
|
||||
harness.step();
|
||||
}
|
||||
harness.event(egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE });
|
||||
settle(harness, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires an offscreen GPU; run explicitly with --ignored"]
|
||||
fn function_instances_drop_into_compatible_canvases_and_return_from_their_source() {
|
||||
let directory = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").unwrap());
|
||||
std::fs::create_dir_all(&directory).unwrap();
|
||||
for kind in ["function", "pixel_processor", "value_processor", "fx_map", "material"] {
|
||||
let mut source = Package::new("Function sources");
|
||||
let mut tint = Graph::new("Reusable color");
|
||||
tint.function = Some(FunctionGraph::constant(Value::Float4([0.2, 0.5, 0.8, 1.0])));
|
||||
source.graphs.push(tint);
|
||||
let mut incomplete = Graph::new("Incomplete source");
|
||||
incomplete.function = Some(FunctionGraph::default());
|
||||
source.graphs.push(incomplete);
|
||||
let mut session = Session::new(source);
|
||||
session.path = Some(directory.join(format!("source-{kind}.sbs")).to_string_lossy().into());
|
||||
let mut target = Package::new("Instance destination");
|
||||
let mut graph = Graph::new("Instance canvas");
|
||||
if kind == "function" {
|
||||
graph.function = Some(FunctionGraph::default());
|
||||
}
|
||||
target.graphs.push(graph);
|
||||
file_cmds::open_bytes(
|
||||
&mut session,
|
||||
&formats::sbs::write_sbs(&target).unwrap(),
|
||||
Some(directory.join(format!("target-{kind}.sbs")).to_string_lossy().into()),
|
||||
)
|
||||
.unwrap();
|
||||
let target_document = session.document_id;
|
||||
let mut app = MaterialCraftApp::headless(session, UiState::default());
|
||||
app.ui.dock.close(materialcraft_ui_egui::docking::Panel::Library);
|
||||
let owner = if matches!(kind, "pixel_processor" | "value_processor" | "fx_map") {
|
||||
Some(NodeId(app.run("graph.node.add", json!({"def": kind})).unwrap()["id"].as_u64().unwrap()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut harness = harness(app, 1600.0, 960.0);
|
||||
settle(&mut harness, 5);
|
||||
let context = harness.ctx.clone();
|
||||
if owner.is_some() {
|
||||
materialcraft_ui_egui::menus::invoke(harness.state_mut(), &context, "graph.node.edit");
|
||||
settle(&mut harness, 4);
|
||||
}
|
||||
let original = harness.state().session.pkg.clone();
|
||||
drag(&mut harness, "Incomplete source");
|
||||
assert_eq!(harness.state().session.pkg, original, "an outputless graph must not produce a drag payload");
|
||||
drag(&mut harness, "Reusable color");
|
||||
if matches!(kind, "fx_map" | "material") {
|
||||
assert_eq!(harness.state().session.pkg, original, "{kind} must reject Function instances");
|
||||
continue;
|
||||
}
|
||||
let target = owner.map_or(FunctionGraphTarget::Graph, |node| FunctionGraphTarget::Parameter { node, parameter: "function".into() });
|
||||
let instance = target.current(&harness.state().session).unwrap().nodes.iter().find(|node| node.instance.is_some()).unwrap().id;
|
||||
assert_eq!(harness.state().session.document_id, target_document);
|
||||
assert_eq!(harness.state().session.graph, 0);
|
||||
harness.get(By::new().role(egui::accesskit::Role::Button).label("Reusable color")).click_secondary();
|
||||
settle(&mut harness, 2);
|
||||
harness.get(By::new().label("Set as output node")).click();
|
||||
settle(&mut harness, 3);
|
||||
assert_eq!(target.current(&harness.state().session).unwrap().output, Some(instance));
|
||||
save(&mut harness, &directory, &format!("function-instance-{kind}"));
|
||||
let position = harness.get(By::new().role(egui::accesskit::Role::Button).label("Reusable color")).rect().center();
|
||||
let history = harness.state().session.history();
|
||||
double_click(&mut harness, position);
|
||||
assert_eq!(harness.state().session.document_id, target_document, "double-click must not open the {kind} instance source");
|
||||
assert_eq!(harness.state().function_editor.as_ref().unwrap().node, owner);
|
||||
assert_eq!(target.current(&harness.state().session).unwrap().output, Some(instance));
|
||||
assert_eq!(harness.state().session.history(), history);
|
||||
harness.get(By::new().role(egui::accesskit::Role::Button).label("Reusable color")).click_secondary();
|
||||
settle(&mut harness, 2);
|
||||
save(&mut harness, &directory, &format!("function-instance-{kind}-menu"));
|
||||
harness.get(By::new().label("Open reference Ctrl+E")).click();
|
||||
settle(&mut harness, 4);
|
||||
assert_eq!(harness.state().session.document_id, 0);
|
||||
assert_eq!(harness.state().session.graph().unwrap().name, "Reusable color");
|
||||
harness.state_mut().run("graph.function.edit", json!({"edit": {"action": "value", "id": 1, "value": Value::Float4([0.7,0.3,0.2,1.0])}})).unwrap();
|
||||
harness.get(By::new().role(egui::accesskit::Role::Button).label("Return to parent graph")).click();
|
||||
settle(&mut harness, 5);
|
||||
assert_eq!(harness.state().session.document_id, target_document);
|
||||
assert!(harness.state().session.modified);
|
||||
assert_eq!(harness.state().function_editor.as_ref().unwrap().node, owner);
|
||||
assert_eq!(target.current(&harness.state().session).unwrap().output, Some(instance));
|
||||
assert_eq!(harness.state().session.history().last().unwrap(), "Edit Function Graph");
|
||||
save(&mut harness, &directory, &format!("function-instance-{kind}-return"));
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -19,7 +19,7 @@ L2 eval cooking: topological order, output sizes (relative to input / pare
|
||||
content-addressed LRU cache, graph instances (sub-graphs, recursion-guarded)
|
||||
L2 formats native .sbs packages, .mcmat portable materials, bitmap/SVG decode,
|
||||
texture encode (PNG/JPEG/TGA/TIFF/BMP/WEBP/EXR/HDR), atomic writes
|
||||
L3 engine Session (package, open graph, selection, 2D/3D view state, snapshot undo, cache),
|
||||
L3 engine Session (open packages, active graph, selection, 2D/3D view state, snapshot undo, cache),
|
||||
the command registry, inspect, sample materials and graph templates
|
||||
L3 render the 3D view: BVH ray casts, the height output displacing a subdivided mesh, GGX PBR with
|
||||
prefiltered IBL + SH diffuse, alpha transparency (layered ray compositing) and an
|
||||
@@ -67,6 +67,19 @@ keeps drawing the last results; the browser cooks on the UI thread.
|
||||
|
||||
## Commands and automation
|
||||
|
||||
`Session` exposes the active package and keeps inactive editing state in `OpenDocument`. Switching
|
||||
packages retains their undo histories, selections and previews and invalidates pending cook results.
|
||||
Linked main and Function graph instances store a source path, graph name and cached connector interface.
|
||||
`linked_packages` publishes source snapshots and refreshes dependent packages; `linked_files`
|
||||
resolves saved references on native open. Source snapshots share a flat registry of `Arc<Package>`
|
||||
values whose own registries are empty, so cross-file references cannot create ownership cycles.
|
||||
The registry is runtime state; SBS serializes the file references and connectors instead.
|
||||
Function instances resolve to bounded runtime call bodies before cooking. Each compiled call owns
|
||||
its program and reuses separate evaluation storage, so source lookup and compilation never happen
|
||||
per pixel. Calls share the execution-step budget and are capped at eight levels and 4096 nodes.
|
||||
SBS Function nodes store the source URI in their instance data; a `materialcraftInterface` cache
|
||||
preserves unconnected input pins when a source file is unavailable.
|
||||
|
||||
Every user-visible action is a `CommandSpec` (`engine/src/*_cmds.rs`): id, label, menu, shortcut,
|
||||
params doc, `enabled`, `run`. The UI calls them through `MaterialCraftApp::run`; the CLI, the JSON
|
||||
control channel and MCP dispatch the same ids. `Session::edit` snapshots the package before running
|
||||
|
||||
@@ -73,7 +73,11 @@ Types: `move`, `down`, `up`, `click`, `doubleClick`, `drag`, `scroll`, `key` (eg
|
||||
| Command | Params |
|
||||
|---|---|
|
||||
| `file.new` | `{template?, name?, size?}`; templates from `graph.templates` (`empty`, `pbr`, `bricks`, `stone_tiles`, `wood`, `rusty_metal`, `fabric`, `ground`) |
|
||||
| `graph.node.edit` | `{}` resolves the selected FX-Map, processor or local graph instance to `{kind, node, parameter?, graph?}`; use `ui.menu.invoke` to enter it in the graph panel |
|
||||
| `graph.node.edit` | `{}` resolves the selected FX-Map, processor or graph instance to `{kind, node, parameter?, graph?, document?, path?}`; use `ui.menu.invoke` to open its reference in the graph panel |
|
||||
| `graph.node.info` | `{id}` returns `{text}` describing a main-graph node without changing the document or node clipboard |
|
||||
| `graph.instance.add` | `{document, graph, function?: FunctionGraphTarget, at?, snapToGrid?}` adds a linked graph instance from an open package; requires a source output and rejects dependency cycles |
|
||||
| `package.activate` | `{document}` activates an open package while retaining each package's selection, views and undo history |
|
||||
| `package.close` / `package.closeAll` | `{document?, discard?}` / `{discard?}` closes one/all packages; modified packages require explicit discard |
|
||||
| `graph.pattern.set` | `{id, pattern}` replaces an FX-Map subgraph with cycle validation and undo |
|
||||
| `graph.node.add` | `{def, at?, params?, connectFrom?: {node, port?}, label?}` |
|
||||
| `graph.connect` / `graph.disconnect` | `{from, fromPort?, to, toPort?}` / `{to, toPort}` |
|
||||
@@ -89,6 +93,35 @@ Types: `move`, `down`, `up`, `click`, `doubleClick`, `drag`, `scroll`, `key` (eg
|
||||
| `view.3d.loadMesh` | `{path}` shows an OBJ, glTF or GLB mesh (scene `custom`) |
|
||||
| `library.list` | `{filter?, category?}`: every node with ports and parameters |
|
||||
|
||||
## Linked packages
|
||||
|
||||
Opening an SBS file or creating a package retains the other open packages. `session.inspect`
|
||||
includes `documents` with each package's document id, path, active state, modified state and graphs.
|
||||
Explorer graph drags use that document id and graph name with `graph.instance.add`.
|
||||
|
||||
Main graphs with outputs can be instanced in other main graphs. Cross-file sources must be saved
|
||||
first; the target stores a file reference and connector metadata, not a copy of the source nodes.
|
||||
Unsaved source edits update open instances and mark dependent packages modified, including
|
||||
transitive dependencies. Each package keeps its own undo history. Native reopening resolves saved
|
||||
file references even when the sources are not open in Explorer. Missing source files retain their
|
||||
cached connectors and wires and report a cook error.
|
||||
|
||||
Function Graphs with designated outputs can be instanced in Function canvases, including Pixel
|
||||
Processor, Value Processor and parameter functions. Pass `function` for embedded canvases; the
|
||||
active standalone Function Graph is inferred when it is omitted. FX-Map canvases reject instances.
|
||||
The source's exposed inputs become typed connectors; missing connections warn when the instance
|
||||
feeds the designated output. An instance can itself be designated as the output. Function calls
|
||||
retain separate variable scopes and share the evaluator's execution budget.
|
||||
|
||||
`graph.instance.reference {id, function?: FunctionGraphTarget}` resolves the source document, path
|
||||
and graph index without changing the package. The UI's Open reference action (Ctrl+E) uses this
|
||||
command and retains the parent Function/processor canvas for return navigation. Double-clicking
|
||||
an instance has no additional action.
|
||||
|
||||
Source-file watching and global Library SBS assets are not implemented yet.
|
||||
References currently identify graphs by name; renaming an external source graph requires recreating
|
||||
its instances. Save As creates a separate file and does not redirect references to the original.
|
||||
|
||||
## 3D material assignment
|
||||
|
||||
`view.3d.outputs {graph?, reset?}` applies a material graph's image outputs by their usage.
|
||||
@@ -182,7 +215,8 @@ cycle, capacity, ID and position validation.
|
||||
|
||||
|
||||
A function has `nodes` and an optional `output` node id. Each node has `id`, native `op`,
|
||||
`inputs` (source ids or null for unwired ports), optional `value`, `type_code` and `pos`.
|
||||
`inputs` (source ids or null for unwired ports), optional `value`, `type_code` and `pos`. Function instances use `op: "instance"` and
|
||||
`instance: {package, graph, inputs, outputs}`; resolved call bodies are runtime-only.
|
||||
For example, `const_float1` supplies a scalar and `mul` multiplies its `a` and `b` inputs.
|
||||
The catalog in `crates/nodes/src/function/catalog.rs` defines all native operation and port ids.
|
||||
Processors and dynamic parameters use the same function representation. Graph validation and
|
||||
|
||||
Reference in new issue
Block a user