mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
* Multiple node outputs * Add new nodes * gcore use std by default to allow for testing * Allow multiple node outputs * Multiple outputs to frontend * Add ImageFrameNode to node registry * Minor cleanup * Basic transform implementation * Add some logging to image encoding * Fix ImageFrameNode * Add transform input to Imaginate node (#1014) * Add transform input to imaginate node * Force the resolution to be edited with no transform * Add transform to imaginate generation * Fix compilation --------- Co-authored-by: Keavon Chambers <keavon@keavon.com> * Add labels to node outputs * Fix seed; disable mask when transform is disconnected; add Imaginate tooltips * Rename 'Input Multiple' node to 'Input' * Code review * Replicate to Svelte * Show only the primary input chain in the Properties panel --------- Co-authored-by: Dennis Kobert <dennis@kobert.dev> Co-authored-by: Keavon Chambers <keavon@keavon.com>
200 lines
6.3 KiB
Rust
200 lines
6.3 KiB
Rust
use dyn_any::{DynAny, StaticType};
|
|
pub use graphene_core::{generic, ops, Node};
|
|
use std::{marker::PhantomData, pin::Pin};
|
|
|
|
pub struct DynAnyNode<I, O, Node> {
|
|
node: Node,
|
|
_i: PhantomData<I>,
|
|
_o: PhantomData<O>,
|
|
}
|
|
#[node_macro::node_fn(DynAnyNode<_I, _O>)]
|
|
fn any_node<_I: StaticType, _O: StaticType, N>(input: Any<'input>, node: &'any_input N) -> Any<'input>
|
|
where
|
|
N: for<'any_input> Node<'any_input, _I, Output = _O>,
|
|
{
|
|
let node_name = core::any::type_name::<N>();
|
|
let input: Box<_I> = dyn_any::downcast(input).unwrap_or_else(|e| panic!("DynAnyNode Input, {e} in:\n{node_name}"));
|
|
Box::new(node.eval(*input))
|
|
}
|
|
pub struct DynAnyRefNode<I, O, Node> {
|
|
node: Node,
|
|
_i: PhantomData<(I, O)>,
|
|
}
|
|
impl<'input, _I: 'input + StaticType, _O: 'input + StaticType, N: 'input> Node<'input, Any<'input>> for DynAnyRefNode<_I, _O, N>
|
|
where
|
|
N: for<'any_input> Node<'any_input, _I, Output = &'any_input _O>,
|
|
{
|
|
type Output = Any<'input>;
|
|
fn eval<'node: 'input>(&'node self, input: Any<'input>) -> Self::Output {
|
|
{
|
|
let node_name = core::any::type_name::<N>();
|
|
let input: Box<_I> = dyn_any::downcast(input).unwrap_or_else(|e| panic!("DynAnyNode Input, {e} in:\n{node_name}"));
|
|
Box::new(self.node.eval(*input))
|
|
}
|
|
}
|
|
}
|
|
impl<_I, _O, S0> DynAnyRefNode<_I, _O, S0> {
|
|
pub const fn new(node: S0) -> Self {
|
|
Self { node, _i: core::marker::PhantomData }
|
|
}
|
|
}
|
|
|
|
pub type TypeErasedNode<'n> = dyn for<'i> Node<'i, Any<'i>, Output = Any<'i>> + 'n + Send + Sync;
|
|
pub type TypeErasedPinnedRef<'n> = Pin<&'n (dyn for<'i> Node<'i, Any<'i>, Output = Any<'i>> + 'n + Send + Sync)>;
|
|
pub type TypeErasedPinned<'n> = Pin<Box<dyn for<'i> Node<'i, Any<'i>, Output = Any<'i>> + 'n + Send + Sync>>;
|
|
|
|
pub trait IntoTypeErasedNode<'n> {
|
|
fn into_type_erased(self) -> TypeErasedPinned<'n>;
|
|
}
|
|
|
|
impl<'n, N: 'n> IntoTypeErasedNode<'n> for N
|
|
where
|
|
N: for<'i> Node<'i, Any<'i>, Output = Any<'i>> + Send + Sync + 'n,
|
|
{
|
|
fn into_type_erased(self) -> TypeErasedPinned<'n> {
|
|
Box::pin(self)
|
|
}
|
|
}
|
|
|
|
pub struct DowncastNode<O, Node> {
|
|
node: Node,
|
|
_o: PhantomData<O>,
|
|
}
|
|
impl<N: Clone, O: StaticType> Clone for DowncastNode<O, N> {
|
|
fn clone(&self) -> Self {
|
|
Self { node: self.node.clone(), _o: self._o }
|
|
}
|
|
}
|
|
impl<N: Copy, O: StaticType> Copy for DowncastNode<O, N> {}
|
|
|
|
#[node_macro::node_fn(DowncastNode<_O>)]
|
|
fn downcast<N, _O: StaticType>(input: Any<'input>, node: &'input N) -> _O
|
|
where
|
|
N: Node<'input, Any<'input>, Output = Any<'input>>,
|
|
{
|
|
let node_name = core::any::type_name::<N>();
|
|
let out = dyn_any::downcast(node.eval(input)).unwrap_or_else(|e| panic!("DowncastNode Input {e} in:\n{node_name}"));
|
|
*out
|
|
}
|
|
|
|
/// Boxes the input and downcasts the output.
|
|
/// Wraps around a node taking Box<dyn DynAny> and returning Box<dyn DynAny>
|
|
#[derive(Clone, Copy)]
|
|
pub struct DowncastBothNode<'a, I, O> {
|
|
node: TypeErasedPinnedRef<'a>,
|
|
_i: PhantomData<I>,
|
|
_o: PhantomData<O>,
|
|
}
|
|
impl<'n: 'input, 'input, O: 'input + StaticType, I: 'input + StaticType> Node<'input, I> for DowncastBothNode<'n, I, O> {
|
|
type Output = O;
|
|
#[inline]
|
|
fn eval<'node: 'input>(&'node self, input: I) -> Self::Output {
|
|
{
|
|
let input = Box::new(input);
|
|
let out = dyn_any::downcast(self.node.eval(input)).unwrap_or_else(|e| panic!("DowncastBothNode Input {e}"));
|
|
*out
|
|
}
|
|
}
|
|
}
|
|
impl<'n, I, O> DowncastBothNode<'n, I, O> {
|
|
pub const fn new(node: TypeErasedPinnedRef<'n>) -> Self {
|
|
Self {
|
|
node,
|
|
_i: core::marker::PhantomData,
|
|
_o: core::marker::PhantomData,
|
|
}
|
|
}
|
|
}
|
|
/// Boxes the input and downcasts the output.
|
|
/// Wraps around a node taking Box<dyn DynAny> and returning Box<dyn DynAny>
|
|
#[derive(Clone, Copy)]
|
|
pub struct DowncastBothRefNode<'a, I, O> {
|
|
node: TypeErasedPinnedRef<'a>,
|
|
_i: PhantomData<(I, O)>,
|
|
}
|
|
impl<'n: 'input, 'input, O: 'input + StaticType, I: 'input + StaticType> Node<'input, I> for DowncastBothRefNode<'n, I, O> {
|
|
type Output = &'input O;
|
|
#[inline]
|
|
fn eval<'node: 'input>(&'node self, input: I) -> Self::Output {
|
|
{
|
|
let input = Box::new(input);
|
|
let out: Box<&_> = dyn_any::downcast::<&O>(self.node.eval(input)).unwrap_or_else(|e| panic!("DowncastBothRefNode Input {e}"));
|
|
*out
|
|
}
|
|
}
|
|
}
|
|
impl<'n, I, O> DowncastBothRefNode<'n, I, O> {
|
|
pub const fn new(node: TypeErasedPinnedRef<'n>) -> Self {
|
|
Self { node, _i: core::marker::PhantomData }
|
|
}
|
|
}
|
|
|
|
pub struct ComposeTypeErased<'a> {
|
|
first: TypeErasedPinnedRef<'a>,
|
|
second: TypeErasedPinnedRef<'a>,
|
|
}
|
|
|
|
impl<'i, 'a: 'i> Node<'i, Any<'i>> for ComposeTypeErased<'a> {
|
|
type Output = Any<'i>;
|
|
fn eval<'s: 'i>(&'s self, input: Any<'i>) -> Self::Output {
|
|
let arg = self.first.eval(input);
|
|
self.second.eval(arg)
|
|
}
|
|
}
|
|
|
|
impl<'a> ComposeTypeErased<'a> {
|
|
pub const fn new(first: TypeErasedPinnedRef<'a>, second: TypeErasedPinnedRef<'a>) -> Self {
|
|
ComposeTypeErased { first, second }
|
|
}
|
|
}
|
|
|
|
pub type Any<'n> = Box<dyn DynAny<'n> + 'n>;
|
|
|
|
pub fn input_node<O: StaticType>(n: TypeErasedPinnedRef) -> DowncastBothNode<(), O> {
|
|
DowncastBothNode::new(n)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use graphene_core::{ops::AddNode, ops::IdNode, value::ValueNode};
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
pub fn dyn_input_invalid_eval_panic() {
|
|
//let add = DynAnyNode::new(AddNode::new()).into_type_erased();
|
|
//add.eval(Box::new(&("32", 32u32)));
|
|
let dyn_any = DynAnyNode::<(u32, u32), u32, _>::new(ValueNode::new(AddNode::new()));
|
|
let type_erased = dyn_any.into_type_erased();
|
|
let _ref_type_erased = type_erased.as_ref();
|
|
//let type_erased = Box::pin(dyn_any) as TypeErasedPinned<'_>;
|
|
type_erased.eval(Box::new(&("32", 32u32)));
|
|
}
|
|
|
|
#[test]
|
|
pub fn dyn_input_invalid_eval_panic_() {
|
|
//let add = DynAnyNode::new(AddNode::new()).into_type_erased();
|
|
//add.eval(Box::new(&("32", 32u32)));
|
|
let dyn_any = DynAnyNode::<(u32, u32), u32, _>::new(ValueNode::new(AddNode::new()));
|
|
let type_erased = Box::pin(dyn_any) as TypeErasedPinned<'_>;
|
|
type_erased.eval(Box::new((4u32, 2u32)));
|
|
let id_node = IdNode::new();
|
|
let type_erased_id = Box::pin(id_node) as TypeErasedPinned;
|
|
let type_erased = ComposeTypeErased::new(type_erased.as_ref(), type_erased_id.as_ref());
|
|
type_erased.eval(Box::new((4u32, 2u32)));
|
|
//let downcast: DowncastBothNode<(u32, u32), u32> = DowncastBothNode::new(type_erased.as_ref());
|
|
//downcast.eval((4u32, 2u32));
|
|
}
|
|
|
|
// TODO: Fix this test
|
|
/*
|
|
#[test]
|
|
pub fn dyn_input_storage_composition() {
|
|
// todo readd test
|
|
let node = <graphene_core::ops::IdNode>::new();
|
|
let any: DynAnyNode<Any<'_>, Any<'_>, _> = DynAnyNode::new(ValueNode::new(node));
|
|
any.into_type_erased();
|
|
}
|
|
*/
|
|
}
|