Skip to content

Commit

Permalink
Implement new node_fn macro
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueDoctor committed Aug 20, 2024
1 parent 93ac6a6 commit 2c3000b
Show file tree
Hide file tree
Showing 13 changed files with 946 additions and 126 deletions.
20 changes: 19 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
category: "Value",
node_template: NodeTemplate {
document_node: DocumentNode {
implementation: DocumentNodeImplementation::proto("graphene_core::ops::ConstructVector2<_, _>"),
implementation: DocumentNodeImplementation::proto("graphene_core::ops::construct_vector2::ConstructVector2"),
inputs: vec![
NodeInput::value(TaggedValue::None, false),
NodeInput::value(TaggedValue::F64(0.), false),
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/wasm-communication/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ export async function initWasm() {

// Import the WASM module JS bindings and wrap them in the panic proxy
// eslint-disable-next-line import/no-cycle
await init();
let wasm = await init();

Check failure on line 22 in frontend/src/wasm-communication/editor.ts

View workflow job for this annotation

GitHub Actions / build

'wasm' is never reassigned. Use 'const' instead
// console.log(wasm);
for (const [name, f] of Object.entries(wasm)) {
if (name.startsWith("__node_registry")) {
console.log(f);

Check failure on line 26 in frontend/src/wasm-communication/editor.ts

View workflow job for this annotation

GitHub Actions / build

Delete `↹`

Check warning on line 26 in frontend/src/wasm-communication/editor.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
f();

Check failure on line 27 in frontend/src/wasm-communication/editor.ts

View workflow job for this annotation

GitHub Actions / build

Delete `↹`
}
}

Check failure on line 30 in frontend/src/wasm-communication/editor.ts

View workflow job for this annotation

GitHub Actions / build

Delete `↹`
wasmImport = await wasmMemory();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).imageCanvases = {};
Expand Down
1 change: 1 addition & 0 deletions frontend/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod editor_api;
pub mod helpers;

use editor::messages::prelude::*;
use js_sys::{Array, Function, Object, Reflect};

use std::panic;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down
2 changes: 1 addition & 1 deletion node-graph/gcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std = [
]
reflections = [
"alloc",
"ctor"
"ctor",
]
serde = [
"dep:serde",
Expand Down
2 changes: 1 addition & 1 deletion node-graph/gcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub mod vector;
#[cfg(feature = "alloc")]
pub mod application_io;

#[cfg(feature = "alloc")]
#[cfg(feature = "reflections")]
pub mod registry;

pub mod quantization;
Expand Down
8 changes: 2 additions & 6 deletions node-graph/gcore/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,8 @@ where
first % second
}

pub struct ConstructVector2<X, Y> {
x: X,
y: Y,
}
#[node_macro::node_fn(ConstructVector2)]
fn construct_vector2(_primary: (), x: f64, y: f64) -> glam::DVec2 {
#[node_macro::new_node_fn]
fn construct_vector2(_: (), x: f64, y: f64) -> glam::DVec2 {
glam::DVec2::new(x, y)
}

Expand Down
113 changes: 0 additions & 113 deletions node-graph/gcore/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,116 +233,3 @@ where
}
}
}

async fn construct_vector2<'n, IY: Into<f64> + Send + 'n, IC: Into<u64> + Send + 'n>(_: (), x: f64, y: IY, c: impl Node<'n, (), Output: core::future::Future<Output = IC>>) -> glam::DVec2 {
glam::DVec2::new(x, y.into())
}
mod construct_vector2 {
use super::*;
use crate as gcore;
use ctor::ctor;
use gcore::ops::TypeNode;
use gcore::registry::{DowncastBothNode, DynAnyNode, DynFuture, FieldMetadata, NodeMetadata, TypeErasedBox, NODE_METADATA, NODE_REGISTRY};
use gcore::value::ClonedNode;
use gcore::{concrete, fn_type, Node, NodeIOTypes, ProtoNodeIdentifier, WasmNotSync};
pub struct ConstructVector2<Node0, Node1, Node2> {
x: Node0,
y: Node1,
c: Node2,
}
#[automatically_derived]
impl<'n, IY: Into<f64> + Send + 'n, IC: Into<u64> + Send + 'n, Node0, Node1, Node2> Node<'n, ()> for ConstructVector2<Node0, Node1, Node2>
where
Node0: Node<'n, (), Output = f64>,
Node1: Node<'n, (), Output = IY>,
Node2: Node<'n, (), Output: core::future::Future<Output = IC>> + WasmNotSync + 'n,
{
type Output = DynFuture<'n, glam::DVec2>;
fn eval(&'n self, input: ()) -> Self::Output {
let x = self.x.eval(());
let y = self.y.eval(());
let c = &self.c;
Box::pin(construct_vector2(input, x, y, c))
}
}
#[automatically_derived]
impl<'n, Node0, Node1, Node2> ConstructVector2<Node0, Node1, Node2> {
pub fn new(x: Node0, y: Node1, c: Node2) -> Self {
Self { x, y, c }
}
}
#[ctor]
fn register_node() {
let mut registry = NODE_REGISTRY.lock().unwrap();
registry.insert(
ProtoNodeIdentifier::new(concat![std::module_path!(), "::", stringify!(ConstructVector2)]),
vec![
(
|args| {
Box::pin(async move {
let x: DowncastBothNode<(), f64> = DowncastBothNode::new(args[0usize].clone());
let value = x.eval(()).await;
let x = ClonedNode::new(value);
let x: TypeNode<_, (), f64> = TypeNode::new(x);
let y: DowncastBothNode<(), f32> = DowncastBothNode::new(args[1usize].clone());
let value = y.eval(()).await;
let y = ClonedNode::new(value);
let y: TypeNode<_, (), f32> = TypeNode::new(y);
let c: DowncastBothNode<(), u32> = DowncastBothNode::new(args[2usize].clone());
let node = ConstructVector2::new(x, y, c);
let any: DynAnyNode<(), _, _> = DynAnyNode::new(node);
Box::new(any) as TypeErasedBox<'_>
})
},
NodeIOTypes::new(concrete!(()), concrete!(glam::DVec2), vec![fn_type!((), f64), fn_type!((), f32), fn_type!((), u32)]),
),
(
|args| {
Box::pin(async move {
let x: DowncastBothNode<(), f64> = DowncastBothNode::new(args[0usize].clone());
let value = x.eval(()).await;
let x = ClonedNode::new(value);
let x: TypeNode<_, (), f64> = TypeNode::new(x);
let y: DowncastBothNode<(), f64> = DowncastBothNode::new(args[1usize].clone());
let value = y.eval(()).await;
let y = ClonedNode::new(value);
let y: TypeNode<_, (), f64> = TypeNode::new(y);
let c: DowncastBothNode<(), u64> = DowncastBothNode::new(args[2usize].clone());
let node = ConstructVector2::new(x, y, c);
let any: DynAnyNode<(), _, _> = DynAnyNode::new(node);
Box::new(any) as TypeErasedBox<'_>
})
},
NodeIOTypes::new(concrete!(()), concrete!(glam::DVec2), vec![fn_type!((), f64), fn_type!((), f64), fn_type!((), u64)]),
),
],
);
}
#[ctor]
fn register_metadata() {
let metadata = NodeMetadata {
identifier: ProtoNodeIdentifier::new(concat![std::module_path!(), "::", stringify!(ConstructVector2)]),
category: Some("Value"),
input_type: concrete!(()),
output_type: concrete!(glam::DVec2),
fields: vec![
FieldMetadata {
name: stringify!(x).to_string(),
default_value: Some(stringify!(1.3)),
},
FieldMetadata {
name: stringify!(y).to_string(),
default_value: None,
},
FieldMetadata {
name: stringify!(c).to_string(),
default_value: None,
},
],
};
NODE_METADATA
.lock()
.unwrap()
.insert(ProtoNodeIdentifier::new(concat![std::module_path!(), "::", stringify!(ConstructVector2)]), metadata);
}
}
7 changes: 6 additions & 1 deletion node-graph/interpreted-executor/src/node_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
register_node!(graphene_core::ops::ModuloNode<_>, input: &f64, params: [f64]),
register_node!(graphene_core::ops::ModuloNode<_>, input: f64, params: [&f64]),
register_node!(graphene_core::ops::ModuloNode<_>, input: &f64, params: [&f64]),
register_node!(graphene_core::ops::ConstructVector2<_, _>, input: (), params: [f64, f64]),
register_node!(graphene_core::ops::SomeNode, input: &WasmEditorApi, params: []),
register_node!(graphene_core::ops::UnwrapNode, input: Option<Color>, params: []),
register_node!(graphene_core::logic::LogToConsoleNode, input: bool, params: []),
Expand Down Expand Up @@ -806,6 +805,12 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
async_node!(graphene_core::AddArtboardNode<_, _>, input: Footprint, output: ArtboardGroup, fn_params: [Footprint => ArtboardGroup, Footprint => Artboard]),
];
let mut map: HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeConstructor>> = HashMap::new();
for (id, entry) in graphene_core::registry::NODE_REGISTRY.lock().unwrap().drain() {
log::debug!("id: {id:?}");
for (constructor, types) in entry.into_iter() {
map.entry(id.clone()).or_default().insert(types, constructor);
}
}
for (id, c, types) in node_types.into_iter().flatten() {
// TODO: this is a hack to remove the newline from the node new_name
// This occurs for the ChannelMixerNode presumably because of the long name.
Expand Down
7 changes: 6 additions & 1 deletion node-graph/node-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ proc-macro = true

[dependencies]
# Workspace dependencies
syn = { workspace = true }
syn = { workspace = true, features = ["extra-traits", "full"] }
proc-macro2 = { workspace = true }
quote = { workspace = true }

convert_case = "0.6.0"
indoc = "2.0.5"
proc-macro-crate = "3.1.0"

Loading

0 comments on commit 2c3000b

Please sign in to comment.