Skip to content

Commit ba177c4

Browse files
TrueDoctorKeavon
andauthored
Generalize the 'Map Vector' node as 'Map' with support for all graphical types (#3793)
* Rename Map Vector to Map * Fix compilation errors * Move to the Graphic module and add Read {Graphic, Raster, Color} nodes --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
1 parent bd1c549 commit ba177c4

File tree

5 files changed

+74
-37
lines changed

5 files changed

+74
-37
lines changed

editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
479479
// "true: bool" -> 1[6:Reset Transform]
480480
// "false: bool" -> 2[6:Reset Transform]
481481
// "false: bool" -> 3[6:Reset Transform]
482-
// [12:Flatten Vector]0 -> 0[7:Map Vector]
483-
// [6:Reset Transform]0 -> 1[7:Map Vector]
484-
// [7:Map Vector]0 -> 0[8:Morph]
482+
// [12:Flatten Vector]0 -> 0[7:Map]
483+
// [6:Reset Transform]0 -> 1[7:Map]
484+
// [7:Map]0 -> 0[8:Morph]
485485
// [15:Multiply]0 -> 1[8:Morph]
486486
// [8:Morph]0 -> 0[9:Transform]
487487
// [4:Position on Path]0 -> 1[9:Transform]
@@ -563,9 +563,9 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
563563
],
564564
..Default::default()
565565
},
566-
// 7: Map Vector
566+
// 7: Map
567567
DocumentNode {
568-
implementation: DocumentNodeImplementation::ProtoNode(vector_nodes::map_vector::IDENTIFIER),
568+
implementation: DocumentNodeImplementation::ProtoNode(graphic::map::IDENTIFIER),
569569
inputs: vec![NodeInput::node(NodeId(12), 0), NodeInput::node(NodeId(6), 0)],
570570
..Default::default()
571571
},
@@ -719,7 +719,7 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
719719
},
720720
..Default::default()
721721
},
722-
// 7: Map Vector
722+
// 7: Map
723723
DocumentNodeMetadata {
724724
persistent_metadata: DocumentNodePersistentMetadata {
725725
node_type_metadata: NodeTypePersistentMetadata::node(IVec2::new(21, 1)),
@@ -838,9 +838,9 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
838838
// [1:Extract Transform]0 -> 0[2:Decompose Translation]
839839
// [2:Decompose Translation]0 -> 0[3:Vec2 to Point]
840840
// [IMPORTS]0 -> 0[4:Flatten Vector]
841-
// [4:Flatten Vector]0 -> 0[5:Map Vector]
842-
// [3:Vec2 to Point]0 -> 1[5:Map Vector]
843-
// [5:Map Vector]0 -> 0[6: Flatten Path]
841+
// [4:Flatten Vector]0 -> 0[5:Map]
842+
// [3:Vec2 to Point]0 -> 1[5:Map]
843+
// [5:Map]0 -> 0[6: Flatten Path]
844844
// [6:Flatten Path]0 -> 0[7:Points to Polyline]
845845
// "false: bool" -> 1[7:Points to Polyline]
846846
// [7:Points to Polyline]0 -> 0[EXPORTS]
@@ -879,9 +879,9 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
879879
inputs: vec![NodeInput::import(generic!(T), 0)],
880880
..Default::default()
881881
},
882-
// 5: Map Vector
882+
// 5: Map
883883
DocumentNode {
884-
implementation: DocumentNodeImplementation::ProtoNode(vector_nodes::map_vector::IDENTIFIER),
884+
implementation: DocumentNodeImplementation::ProtoNode(graphic::map::IDENTIFIER),
885885
inputs: vec![NodeInput::node(NodeId(4), 0), NodeInput::node(NodeId(3), 0)],
886886
..Default::default()
887887
},
@@ -954,7 +954,7 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
954954
},
955955
..Default::default()
956956
},
957-
// 5: Map Vector
957+
// 5: Map
958958
DocumentNodeMetadata {
959959
persistent_metadata: DocumentNodePersistentMetadata {
960960
node_type_metadata: NodeTypePersistentMetadata::node(IVec2::new(28, 0)),

editor/src/messages/portfolio/document_migration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ const NODE_REPLACEMENTS: &[NodeReplacement<'static>] = &[
838838
aliases: &["graphene_core::vector::InstanceIndexNode", "core_types::vector::InstanceIndexNode"],
839839
},
840840
NodeReplacement {
841-
node: graphene_std::vector::map_vector::IDENTIFIER,
841+
node: graphene_std::graphic::map::IDENTIFIER,
842842
aliases: &["graphene_core::vector::InstanceMapNode"],
843843
},
844844
NodeReplacement {

node-graph/nodes/gcore/src/context.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
use core_types::ExtractVarArgs;
21
use core_types::table::Table;
2+
use core_types::{Color, ExtractVarArgs};
33
use core_types::{Ctx, ExtractIndex, ExtractPosition};
44
use glam::DVec2;
5-
use graphic_types::Vector;
5+
use graphic_types::{Graphic, Vector};
6+
use raster_types::{CPU, Raster};
7+
8+
#[node_macro::node(category("Context"), path(graphene_core::vector))]
9+
fn read_graphic(ctx: impl Ctx + ExtractVarArgs) -> Table<Graphic> {
10+
let Ok(var_arg) = ctx.vararg(0) else { return Default::default() };
11+
let var_arg = var_arg as &dyn std::any::Any;
12+
13+
var_arg.downcast_ref().cloned().unwrap_or_default()
14+
}
615

7-
// TODO: Call this "Read Context" once it's fully generic
816
#[node_macro::node(category("Context"), path(graphene_core::vector))]
917
fn read_vector(ctx: impl Ctx + ExtractVarArgs) -> Table<Vector> {
1018
let Ok(var_arg) = ctx.vararg(0) else { return Default::default() };
@@ -13,6 +21,22 @@ fn read_vector(ctx: impl Ctx + ExtractVarArgs) -> Table<Vector> {
1321
var_arg.downcast_ref().cloned().unwrap_or_default()
1422
}
1523

24+
#[node_macro::node(category("Context"), path(graphene_core::vector))]
25+
fn read_raster(ctx: impl Ctx + ExtractVarArgs) -> Table<Raster<CPU>> {
26+
let Ok(var_arg) = ctx.vararg(0) else { return Default::default() };
27+
let var_arg = var_arg as &dyn std::any::Any;
28+
29+
var_arg.downcast_ref().cloned().unwrap_or_default()
30+
}
31+
32+
#[node_macro::node(category("Context"), path(graphene_core::vector))]
33+
fn read_color(ctx: impl Ctx + ExtractVarArgs) -> Table<Color> {
34+
let Ok(var_arg) = ctx.vararg(0) else { return Default::default() };
35+
let var_arg = var_arg as &dyn std::any::Any;
36+
37+
var_arg.downcast_ref().cloned().unwrap_or_default()
38+
}
39+
1640
#[node_macro::node(category("Context"), path(core_types::vector))]
1741
async fn read_position(
1842
ctx: impl Ctx + ExtractPosition,

node-graph/nodes/graphic/src/graphic.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1-
use core_types::Color;
2-
use core_types::Ctx;
31
use core_types::registry::types::SignedInteger;
42
use core_types::table::{Table, TableRow};
53
use core_types::uuid::NodeId;
4+
use core_types::{AnyHash, CloneVarArgs, Color, Context, Ctx, ExtractAll, OwnedContextImpl};
65
use glam::{DAffine2, DVec2};
76
use graphic_types::graphic::{Graphic, IntoGraphicTable};
87
use graphic_types::{Artboard, Vector};
98
use raster_types::{CPU, GPU, Raster};
109
use vector_types::GradientStops;
1110

11+
#[node_macro::node(category("General"), path(graphene_core::vector))]
12+
async fn map<Item: AnyHash + Send + Sync + std::hash::Hash>(
13+
ctx: impl Ctx + CloneVarArgs + ExtractAll,
14+
#[implementations(
15+
Table<Graphic>,
16+
Table<Vector>,
17+
Table<Raster<CPU>>,
18+
Table<Color>,
19+
Table<GradientStops>,
20+
)]
21+
content: Table<Item>,
22+
#[implementations(
23+
Context -> Table<Graphic>,
24+
Context -> Table<Vector>,
25+
Context -> Table<Raster<CPU>>,
26+
Context -> Table<Color>,
27+
Context -> Table<GradientStops>,
28+
)]
29+
mapped: impl Node<Context<'static>, Output = Table<Item>>,
30+
) -> Table<Item> {
31+
let mut rows = Table::new();
32+
33+
for (i, row) in content.into_iter().enumerate() {
34+
let owned_ctx = OwnedContextImpl::from(ctx.clone());
35+
let owned_ctx = owned_ctx.with_vararg(Box::new(Table::new_from_row(row))).with_index(i);
36+
let table = mapped.eval(owned_ctx.into_context()).await;
37+
38+
rows.extend(table);
39+
}
40+
41+
rows
42+
}
43+
1244
/// Performs internal editor record-keeping that enables tools to target this network's layer.
1345
/// This node associates the ID of the network's parent layer to every element of output data.
1446
/// This technical detail may be ignored by users, and will be phased out in the future.

node-graph/nodes/vector/src/vector_nodes.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,25 +1325,6 @@ async fn separate_subpaths(_: impl Ctx, content: Table<Vector>) -> Table<Vector>
13251325
.collect()
13261326
}
13271327

1328-
// TODO: Call this "Map" once it's fully generic
1329-
#[node_macro::node(category("Vector"), path(graphene_core::vector))]
1330-
async fn map_vector(ctx: impl Ctx + CloneVarArgs + ExtractAll, content: Table<Vector>, mapped: impl Node<Context<'static>, Output = Table<Vector>>) -> Table<Vector> {
1331-
let mut rows = Vec::new();
1332-
1333-
for (i, row) in content.into_iter().enumerate() {
1334-
let owned_ctx = OwnedContextImpl::from(ctx.clone());
1335-
let owned_ctx = owned_ctx.with_vararg(Box::new(Table::new_from_row(row))).with_index(i);
1336-
let table = mapped.eval(owned_ctx.into_context()).await;
1337-
1338-
for inner_row in table {
1339-
rows.push(inner_row);
1340-
}
1341-
}
1342-
1343-
rows.into_iter().collect()
1344-
}
1345-
1346-
// TODO: Call this "Map" once it's fully generic
13471328
#[node_macro::node(category("Vector"), path(graphene_core::vector))]
13481329
async fn map_points(ctx: impl Ctx + CloneVarArgs + ExtractAll, content: Table<Vector>, mapped: impl Node<Context<'static>, Output = DVec2>) -> Table<Vector> {
13491330
let mut content = content;

0 commit comments

Comments
 (0)