From 1bb780347812c9000d95f932b2314715fa16b569 Mon Sep 17 00:00:00 2001 From: Firestar99 Date: Tue, 1 Jul 2025 17:07:50 +0200 Subject: [PATCH] move `IndexNode` to `gelement-nodes` --- .../messages/portfolio/document_migration.rs | 1 + node-graph/gcore/src/graphic_element.rs | 50 ---------------- node-graph/gelement-nodes/src/index.rs | 57 +++++++++++++++++++ node-graph/gelement-nodes/src/lib.rs | 1 + 4 files changed, 59 insertions(+), 50 deletions(-) create mode 100644 node-graph/gelement-nodes/src/index.rs diff --git a/editor/src/messages/portfolio/document_migration.rs b/editor/src/messages/portfolio/document_migration.rs index 2f112eb8c..0020518f6 100644 --- a/editor/src/messages/portfolio/document_migration.rs +++ b/editor/src/messages/portfolio/document_migration.rs @@ -139,6 +139,7 @@ const REPLACEMENTS: &[(&str, &str)] = &[ ("graphene_core::ConstructArtboardNode", "graphene_element_nodes::conversion::ToArtboardNode"), ("graphene_core::graphic_element::AppendArtboardNode", "graphene_element_nodes::conversion::AppendArtboardNode"), ("graphene_core::AddArtboardNode", "graphene_element_nodes::conversion::AppendArtboardNode"), + ("graphene_core::graphic_element::IndexNode", "graphene_element_nodes::index::IndexNode"), ]; pub fn document_migration_string_preprocessing(document_serialized_content: String) -> String { diff --git a/node-graph/gcore/src/graphic_element.rs b/node-graph/gcore/src/graphic_element.rs index 39862b4ff..c8df19248 100644 --- a/node-graph/gcore/src/graphic_element.rs +++ b/node-graph/gcore/src/graphic_element.rs @@ -368,53 +368,3 @@ impl From for GraphicElement { pub trait ToGraphicElement { fn to_graphic_element(&self) -> GraphicElement; } - -/// Returns the value at the specified index in the collection. -/// If that index has no value, the type's default value is returned. -#[node_macro::node(category("General"))] -fn index( - _: impl Ctx, - /// The collection of data, such as a list or table. - #[implementations( - Vec, - Vec>, - Vec, Vec, - Vec, - VectorDataTable, - RasterDataTable, - GraphicGroupTable, - )] - collection: T, - /// The index of the item to retrieve, starting from 0 for the first item. - index: u32, -) -> T::Output -where - T::Output: Clone + Default, -{ - collection.at_index(index as usize).unwrap_or_default() -} - -pub trait AtIndex { - type Output; - fn at_index(&self, index: usize) -> Option; -} -impl AtIndex for Vec { - type Output = T; - - fn at_index(&self, index: usize) -> Option { - self.get(index).cloned() - } -} -impl AtIndex for Instances { - type Output = Instances; - - fn at_index(&self, index: usize) -> Option { - let mut result_table = Self::default(); - if let Some(row) = self.instance_ref_iter().nth(index) { - result_table.push(row.to_instance_cloned()); - Some(result_table) - } else { - None - } - } -} diff --git a/node-graph/gelement-nodes/src/index.rs b/node-graph/gelement-nodes/src/index.rs new file mode 100644 index 000000000..9f356f993 --- /dev/null +++ b/node-graph/gelement-nodes/src/index.rs @@ -0,0 +1,57 @@ +use glam::DVec2; +use graphene_core::GraphicGroupTable; +use graphene_core::color::Color; +use graphene_core::context::Ctx; +use graphene_core::instances::Instances; +use graphene_core::raster_types::{CPU, RasterDataTable}; +use graphene_core::vector::VectorDataTable; + +/// Returns the value at the specified index in the collection. +/// If that index has no value, the type's default value is returned. +#[node_macro::node(category("General"))] +fn index( + _: impl Ctx, + /// The collection of data, such as a list or table. + #[implementations( + Vec, + Vec>, + Vec, Vec, + Vec, + VectorDataTable, + RasterDataTable, + GraphicGroupTable, + )] + collection: T, + /// The index of the item to retrieve, starting from 0 for the first item. + index: u32, +) -> T::Output +where + T::Output: Clone + Default, +{ + collection.at_index(index as usize).unwrap_or_default() +} + +pub trait AtIndex { + type Output; + fn at_index(&self, index: usize) -> Option; +} +impl AtIndex for Vec { + type Output = T; + + fn at_index(&self, index: usize) -> Option { + self.get(index).cloned() + } +} +impl AtIndex for Instances { + type Output = Instances; + + fn at_index(&self, index: usize) -> Option { + let mut result_table = Self::default(); + if let Some(row) = self.instance_ref_iter().nth(index) { + result_table.push(row.to_instance_cloned()); + Some(result_table) + } else { + None + } + } +} diff --git a/node-graph/gelement-nodes/src/lib.rs b/node-graph/gelement-nodes/src/lib.rs index e45e23ca1..68a68dcbd 100644 --- a/node-graph/gelement-nodes/src/lib.rs +++ b/node-graph/gelement-nodes/src/lib.rs @@ -1,6 +1,7 @@ pub mod animation; pub mod blending_nodes; pub mod conversion; +pub mod index; pub mod instance; pub mod logic; pub mod transform_nodes;