Skip to content

Commit 6a3b098

Browse files
authored
Add automatic type conversion from number to Vec2 by splatting (#3394)
1 parent 57b0b9c commit 6a3b098

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

node-graph/interpreted-executor/src/node_registry.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
5757
// into_node!(from: Table<Raster<CPU>>, to: Table<Raster<SRGBA8>>),
5858
#[cfg(feature = "gpu")]
5959
into_node!(from: &WasmEditorApi, to: &WgpuExecutor),
60+
convert_node!(from: DVec2, to: DVec2),
6061
convert_node!(from: String, to: String),
6162
convert_node!(from: bool, to: String),
6263
convert_node!(from: DVec2, to: String),
@@ -275,6 +276,8 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
275276
convert_node!(from: u128, to: numbers),
276277
convert_node!(from: isize, to: numbers),
277278
convert_node!(from: usize, to: numbers),
279+
convert_node!(from: numbers, to: DVec2),
280+
convert_node!(from: numbers, to: String),
278281
]
279282
.into_iter()
280283
.flatten(),
@@ -386,7 +389,25 @@ mod node_registry_macros {
386389
convert_node!(from: $from, to: u128),
387390
convert_node!(from: $from, to: isize),
388391
convert_node!(from: $from, to: usize),
389-
convert_node!(from: $from, to: String),
392+
];
393+
x
394+
}};
395+
(from: numbers, to: $to:ty) => {{
396+
let x: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
397+
convert_node!(from: f32, to: $to),
398+
convert_node!(from: f64, to: $to),
399+
convert_node!(from: i8, to: $to),
400+
convert_node!(from: u8, to: $to),
401+
convert_node!(from: u16, to: $to),
402+
convert_node!(from: i16, to: $to),
403+
convert_node!(from: i32, to: $to),
404+
convert_node!(from: u32, to: $to),
405+
convert_node!(from: i64, to: $to),
406+
convert_node!(from: u64, to: $to),
407+
convert_node!(from: i128, to: $to),
408+
convert_node!(from: u128, to: $to),
409+
convert_node!(from: isize, to: $to),
410+
convert_node!(from: usize, to: $to),
390411
];
391412
x
392413
}};

node-graph/libraries/core-types/src/ops.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::{
2-
Node,
3-
table::{Table, TableRow},
4-
transform::Footprint,
5-
};
1+
use crate::Node;
2+
use crate::table::{Table, TableRow};
3+
use crate::transform::Footprint;
4+
use glam::DVec2;
65
use std::future::Future;
76
use std::marker::PhantomData;
87

@@ -55,12 +54,10 @@ impl<T: ToString + Send> Convert<String, ()> for T {
5554
}
5655
}
5756

58-
// trait mentioning inner type in args
5957
pub trait TableConvert<U> {
6058
fn convert_row(self) -> U;
6159
}
6260

63-
//
6461
impl<U, T: TableConvert<U> + Send> Convert<Table<U>, ()> for Table<T> {
6562
async fn convert(self, _: Footprint, _: ()) -> Table<U> {
6663
let table: Table<U> = self
@@ -76,6 +73,12 @@ impl<U, T: TableConvert<U> + Send> Convert<Table<U>, ()> for Table<T> {
7673
}
7774
}
7875

76+
impl Convert<DVec2, ()> for DVec2 {
77+
async fn convert(self, _: Footprint, _: ()) -> DVec2 {
78+
self
79+
}
80+
}
81+
7982
/// Implements the [`Convert`] trait for conversion between the cartesian product of Rust's primitive numeric types.
8083
macro_rules! impl_convert {
8184
($from:ty, $to:ty) => {
@@ -100,6 +103,12 @@ macro_rules! impl_convert {
100103
impl_convert!(u128, $to);
101104
impl_convert!(isize, $to);
102105
impl_convert!(usize, $to);
106+
107+
impl Convert<DVec2, ()> for $to {
108+
async fn convert(self, _: Footprint, _: ()) -> DVec2 {
109+
DVec2::splat(self as f64)
110+
}
111+
}
103112
};
104113
}
105114
impl_convert!(f32);

0 commit comments

Comments
 (0)