Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion document/migrations/core/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{Compiler, Declaration, DeclarationResolver, InputConnector, MigrationError, NewNode, OutputConnector};
use crate::{Compiler, Declaration, DeclarationResolver, InputConnector, MigrationError, NewNode, OutputConnector, encode};
use document_graph_storage::attributes::attr::node::REFLECTION_METADATA;
use document_graph_storage::{Implementation, InputSlot, NetworkId, NodeId, NodeInput, Registry, TimeStamp};
use serde::Serialize;
use std::ops::Deref;

/// Which part of the registry around an anchor a view reads or edits. A migration's footprint is the union of its views' regions.
Expand Down Expand Up @@ -329,6 +331,16 @@ impl Inputs {
self.origins.push(None);
}

/// Add a slot the compiler fills in with what `metadata` names, a frozen mirror value such as the node's path.
pub fn push_reflection<T: Serialize>(&mut self, metadata: T) {
let mut slot = new_slot(NodeInput::Reflection);
slot.attributes.set(REFLECTION_METADATA, encode(&metadata), TimeStamp::ORIGIN);

self.edited = true;
self.slots.push(slot);
self.origins.push(None);
}

pub fn remove(&mut self, index: u32) {
if !self.check(index, self.slots.len()) {
return;
Expand Down
1 change: 1 addition & 0 deletions document/migrations/nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
mod graphene_core;
mod math_nodes;
mod raster_nodes;
mod vector_nodes;
4 changes: 4 additions & 0 deletions document/migrations/nodes/src/vector_nodes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Migrations of the nodes in `vector_nodes`.

mod path_from_path_network;
mod renames;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use migration_core::{Inputs, migration};
use serde::Serialize;

/// Frozen mirror of the `DocumentNodeMetadata` variant this migration uses.
#[derive(Serialize)]
enum Reflected {
DocumentNodePath,
}

/// The Path subgraph became the node it held, which takes the instance's inputs and is given its own path as that node was.
#[migration(network "Path"@1 => "vector_nodes::vector_modification_nodes::PathNode"@2)]
pub fn path_from_path_network(inputs: &mut Inputs) {
inputs.push_reflection(Reflected::DocumentNodePath);
}

#[cfg(test)]
mod tests {
use super::*;
use document_graph_storage::NodeInput;
use document_graph_storage::attributes::attr::node::REFLECTION_METADATA;
use migration_core::{decl, literal};
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};

fn contract() -> SourceContract {
SourceContract::new(2, |random, _slot| literal(random.below(10) as f64))
}

#[test]
fn rewrites_history() {
check_commutation(&PathFromPathNetwork, &contract());
check_undo(&PathFromPathNetwork, &contract());
}

#[test]
fn collapses_into_the_node_and_gives_it_its_path() {
let (content, modification) = (literal(1.), literal(2.));

let mut fixture = Fixture::new(&PathFromPathNetwork);
let (path, network) = fixture.add_network(decl!(network "Path"@1), vec![content.clone(), modification.clone()]);
let migrated = fixture.migrate(&PathFromPathNetwork).unwrap();

assert_eq!(fixture.declaration_of(&migrated, path), Some(decl!("vector_nodes::vector_modification_nodes::PathNode"@2)));
assert!(!migrated.networks.contains_key(&network));
assert_eq!(fixture.inputs(&migrated, path), [content, modification, NodeInput::Reflection]);

let own_path = &migrated.node_instances[&path].inputs()[2];
assert_eq!(own_path.attributes.get_typed::<String>(REFLECTION_METADATA).as_deref(), Some("DocumentNodePath"));
}
}
5 changes: 5 additions & 0 deletions document/migrations/nodes/src/vector_nodes/renames.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Nodes that only moved to a new identifier, or changed version with stored nodes staying as they are.

use migration_core::rename;

rename!("vector_nodes::vector_modification_nodes::PathModifyNode"@1 => "vector_nodes::vector_modification_nodes::PathNode"@2);
12 changes: 12 additions & 0 deletions document/migrations/runner/tests/node_migrations/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ pub fn opacity_toggles(inputs: &mut Inputs) {
inputs.push(literal(Tagged::Number(100.)));
}

/// Frozen mirror of the `DocumentNodeMetadata` variant the migration below uses.
#[derive(Serialize)]
pub enum Reflected {
DocumentNodePath,
}

/// Gives the node its own path.
#[migration("vector_nodes::bounding_box"@1 => @2)]
pub fn bounding_box_own_path(inputs: &mut Inputs) {
inputs.push_reflection(Reflected::DocumentNodePath);
}

pub const ROTATION: u32 = 2;

/// Rotation is now stored in degrees.
Expand Down
16 changes: 15 additions & 1 deletion document/migrations/runner/tests/node_migrations/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ mod networks;
mod registration;
mod rewrite;

use document_graph_storage::attributes::attr::node::REFLECTION_METADATA;
use document_graph_storage::{NodeId, NodeInput, ROOT_NETWORK, Registry};
use examples::{AngleBetweenDegrees, Axis, ExtractXyToSplit, OpacityToggles, ROTATION, Tagged, TransformDegrees};
use examples::{AngleBetweenDegrees, Axis, BoundingBoxOwnPath, ExtractXyToSplit, OpacityToggles, ROTATION, Tagged, TransformDegrees};
use migration_core::{decl, literal, literal_of};
use migration_runner::RunnerError;
use migration_runner::testing::{Fixture, registered};
Expand Down Expand Up @@ -57,6 +58,19 @@ fn opacity_toggles_build_the_target_layout() {
assert_eq!(fixture.declaration_of(&migrated, opacity), Some(decl!("blending_nodes::opacity"@2)));
}

#[test]
fn reflection_input_stores_what_it_reflects() {
let mut fixture = Fixture::new(&BoundingBoxOwnPath);
let bounding_box = fixture.add(decl!("vector_nodes::bounding_box"@1), vec![literal(Tagged::Number(1.))]);

let migrated = fixture.migrate(&BoundingBoxOwnPath).unwrap();

let slots = migrated.node_instances[&bounding_box].inputs();
assert_eq!(slots.len(), 2);
assert_eq!(slots[1].input, NodeInput::Reflection);
assert_eq!(slots[1].attributes.get_typed::<String>(REFLECTION_METADATA).as_deref(), Some("DocumentNodePath"));
}

#[test]
fn transform_converts_a_literal_rotation() {
let mut fixture = Fixture::new(&TransformDegrees);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn every_migration_registers_once() {
decl!("order::static_late"@1),
decl!("transform_nodes::transform"@1),
decl!("vector_nodes::angle_between"@1),
decl!("vector_nodes::bounding_box"@1),
decl!("vector_nodes::extract_xy"@1),
decl!("vector_nodes::split_vec2"@2),
];
Expand Down
7 changes: 6 additions & 1 deletion document/migrations/runner/tests/node_migrations/rewrite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The history properties of `migration_runner::testing` on the examples.

use crate::examples::{AngleBetweenDegrees, Axis, ExtractXyToSplit, OpacityToggles, Tagged, TransformDegrees};
use crate::examples::{AngleBetweenDegrees, Axis, BoundingBoxOwnPath, ExtractXyToSplit, OpacityToggles, Tagged, TransformDegrees};
use migration_core::{decl, literal};
use migration_runner::testing::{Random, SourceContract, check_commutation, check_undo, registered};

Expand All @@ -18,6 +18,11 @@ fn opacity_toggles_commute() {
check_commutation(&OpacityToggles, &numbers(2));
}

#[test]
fn bounding_box_own_path_commutes() {
check_commutation(&BoundingBoxOwnPath, &numbers(1));
}

#[test]
fn transform_degrees_commutes() {
check_commutation(&TransformDegrees, &numbers(3));
Expand Down
4 changes: 2 additions & 2 deletions editor/src/messages/clipboard/clipboard_message_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::consts::DEFAULT_STROKE_WIDTH;
use crate::messages::portfolio::document::SelectionParts;
use crate::messages::portfolio::document::graph_operation::utility_types::TransformIn;
use crate::messages::portfolio::document::node_graph::document_node_definitions::resolve_network_node_type;
use crate::messages::portfolio::document::node_graph::document_node_definitions::resolve_proto_node_type;
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
use crate::messages::portfolio::document::utility_types::network_interface;
use crate::messages::portfolio::document::utility_types::nodes::SelectedNodes;
Expand Down Expand Up @@ -429,7 +429,7 @@ impl MessageHandler<ClipboardMessage, ClipboardMessageContext<'_>> for Clipboard
let mut layers = Vec::new();

for (_, new_vector, transform) in paths {
let Some(node_type) = resolve_network_node_type("Path") else {
let Some(node_type) = resolve_proto_node_type(graphene_std::vector::path::IDENTIFIER) else {
error!("Path node does not exist");
continue;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ impl<'a> ModifyInputsContext<'a> {
self.network_interface.move_node_to_chain_start(&smooth_anchors_id, layer, &[], self.import);

// Add Path node to chain start (closest to the Merge node)
let path = resolve_network_node_type("Path").expect("Path node does not exist").default_node_template();
let path = resolve_proto_node_type(graphene_std::vector::path::IDENTIFIER)
.expect("Path node does not exist")
.default_node_template();
let path_id = NodeId::new();
self.network_interface.insert_node(path_id, path, &[]);
self.network_interface.move_node_to_chain_start(&path_id, layer, &[], self.import);
Expand All @@ -150,7 +152,7 @@ impl<'a> ModifyInputsContext<'a> {
let (vector, paths) = Vector::from_bezpath(&bezpath);
let modification = Box::new(VectorModification::create_from_vector(&vector, &paths));

let shape = resolve_network_node_type("Path")
let shape = resolve_proto_node_type(graphene_std::vector::path::IDENTIFIER)
.expect("Path node does not exist")
.node_template_input_override([None, Some(NodeInput::value(TaggedValue::VectorModification(modification), false))]);
let shape_id = NodeId::new();
Expand Down Expand Up @@ -410,7 +412,7 @@ impl<'a> ModifyInputsContext<'a> {

// If inserting a 'Path' node, insert a 'Combine Paths' node if the type is `Graphic`.
// TODO: Allow the 'Path' node to operate on `List` data by utilizing the reference (index or ID?) for each item.
if node_definition.identifier == "Path" {
if *reference == DefinitionIdentifier::ProtoNode(graphene_std::vector::path::IDENTIFIER) {
let layer_input_type = self.network_interface.input_type(&InputConnector::layer_secondary_input(output_layer.to_node()), &[]);
if layer_input_type.compiled_element_name().as_deref() == Some("Graphic") {
let Some(combine_paths_definition) = resolve_proto_node_type(graphene_std::vector_nodes::combine_paths::IDENTIFIER) else {
Expand Down Expand Up @@ -994,7 +996,7 @@ impl<'a> ModifyInputsContext<'a> {
}

pub fn vector_modify(&mut self, modification_type: VectorModificationType) {
let Some(path_node_id) = self.existing_network_node_id("Path", true) else {
let Some(path_node_id) = self.existing_proto_node_id(graphene_std::vector::path::IDENTIFIER, true) else {
return;
};
self.network_interface.vector_modify(&path_node_id, modification_type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,56 +769,6 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
),
inspector: None,
},
DocumentNodeDefinition {
identifier: "Path",
version: 1,
category: "Vector: Geometry",
icon: Some("NodePath"),
node_template: NodeTemplate {
implementation: NodeTemplateImplementation::Network(NodeNetworkTemplate {
exports: vec![NodeInput::node(NodeId(1), 0)],
nodes: [
NodeTemplate {
inputs: vec![NodeInput::import(generic!(T), 0)],
implementation: NodeTemplateImplementation::ProtoNode(memo::monitor::IDENTIFIER),
call_argument: generic!(T),
skip_deduplication: true,
node_type_metadata: NodeTypePersistentMetadata::node(IVec2::new(0, 0)),
..Default::default()
},
NodeTemplate {
inputs: vec![
NodeInput::node(NodeId(0), 0),
NodeInput::import(concrete!(graphene_std::vector::VectorModification), 1),
NodeInput::Reflection(graph_craft::document::DocumentNodeMetadata::DocumentNodePath),
],
call_argument: generic!(T),
implementation: NodeTemplateImplementation::ProtoNode(vector::path_modify::IDENTIFIER),
node_type_metadata: NodeTypePersistentMetadata::node(IVec2::new(7, 0)),
..Default::default()
},
]
.into_iter()
.enumerate()
.map(|(id, node)| (NodeId(id as u64), node))
.collect(),
..Default::default()
}),
inputs: vec![
NodeInput::type_default(item!(Vector), true),
NodeInput::value(TaggedValue::VectorModification(Default::default()), false),
],
input_metadata: vec![
("Content", "The original shape that edits made with the Pen and Path tools are applied to.").into(),
("Modification", "The edits made with the Pen and Path tools, stored as changes to points and segments.").into(),
],
output_names: vec!["Modified".to_string()],
output_descriptions: vec!["The shape with the Pen and Path tool edits applied.".to_string()],
..Default::default()
},
description: Cow::Borrowed("Holds the edits made with the Pen and Path tools, applying them to the incoming shape."),
inspector: None,
},
];

document_node_derive::post_process_nodes(custom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphMessageContext<'a>> for NodeG
NodeGraphMessage::AddPathNode => {
if let Some(layer) = make_path_editable_is_allowed(network_interface) {
responses.add(NodeGraphMessage::CreateNodeInLayerWithTransaction {
node_type: DefinitionIdentifier::Network("Path".into()),
node_type: DefinitionIdentifier::ProtoNode(graphene_std::vector::path::IDENTIFIER),
layer,
});
responses.add(EventMessage::SelectionChanged);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3143,8 +3143,8 @@ pub(crate) fn generate_node_inspector(node_id: NodeId, context: &mut NodeInspect
} else {
let number_of_inputs = context.network_interface.number_of_inputs(&node_id, context.selection_network_path);
for input_index in 1..number_of_inputs {
// Hide inputs that are connected to a scope
if let Some(NodeInput::Scope(_)) = context
// Hide inputs that are connected to a scope or filled in by the compiler
if let Some(NodeInput::Scope(_) | NodeInput::Reflection(_)) = context
.network_interface
.input_from_connector(&InputConnector::node_at_index(node_id, input_index), context.selection_network_path)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ async fn demo_artwork_vector_modifications_still_apply() {

let document = editor.active_document();
let network = &document.network_interface;
let path_definition = DefinitionIdentifier::Network("Path".into());
let path_definition = DefinitionIdentifier::ProtoNode(graphene_std::vector::path::IDENTIFIER);
let mut path_layers = 0;
let mut any_curve = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl DocumentMetadata {

let mut use_local = true;
let graph_layer = graph_modification_utils::NodeGraphLayer::new(layer, network_interface);
let identifier = DefinitionIdentifier::Network("Path".into());
let identifier = DefinitionIdentifier::ProtoNode(graphene_std::vector::path::IDENTIFIER);
if let Some(path_node) = graph_layer.upstream_visible_node_id_from_name_in_layer(&identifier)
&& let Some(&source) = self.first_element_source_ids.get(&layer.to_node())
&& !network_interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ impl NodeNetworkInterface {
layer_paths
}

/// The path of every node in the document's networks made from the definition.
pub fn node_paths_with_reference(&self, reference: &DefinitionIdentifier) -> Vec<Vec<NodeId>> {
let mut node_paths = Vec::new();
self.for_each_network(&mut |network_path, network_metadata| {
for node_id in network_metadata.persistent_metadata.node_metadata.keys() {
if self.reference(node_id, network_path).as_ref() == Some(reference) {
node_paths.push([network_path, &[*node_id]].concat());
}
}
});
node_paths
}

fn for_each_layer(&self, visit: &mut impl FnMut(&[NodeId], NodeId)) {
self.for_each_network(&mut |network_path, network_metadata| {
for (node_id, node_metadata) in &network_metadata.persistent_metadata.node_metadata {
Expand Down
Loading
Loading