Skip to content

Commit c671705

Browse files
committed
New node: 'Index'
1 parent 83773ba commit c671705

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

node-graph/gcore/src/graphic_element.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,53 @@ impl From<GraphicGroupTable> for GraphicElement {
551551
pub trait ToGraphicElement {
552552
fn to_graphic_element(&self) -> GraphicElement;
553553
}
554+
555+
/// Returns the value at the specified index in the collection.
556+
/// If that index has no value, the type's default value is returned.
557+
#[node_macro::node(category("General"))]
558+
fn index<T: AtIndex + Clone + Default>(
559+
_: impl Ctx,
560+
/// The collection of data, such as a list or table.
561+
#[implementations(
562+
Vec<Color>,
563+
Vec<Option<Color>>,
564+
Vec<f64>, Vec<u64>,
565+
Vec<DVec2>,
566+
VectorDataTable,
567+
RasterDataTable<CPU>,
568+
GraphicGroupTable,
569+
)]
570+
collection: T,
571+
/// The index of the item to retrieve, starting from 0 for the first item.
572+
index: u32,
573+
) -> T::Output
574+
where
575+
T::Output: Clone + Default,
576+
{
577+
collection.at_index(index as usize).unwrap_or_default()
578+
}
579+
580+
pub trait AtIndex {
581+
type Output;
582+
fn at_index(&self, index: usize) -> Option<Self::Output>;
583+
}
584+
impl<T: Clone> AtIndex for Vec<T> {
585+
type Output = T;
586+
587+
fn at_index(&self, index: usize) -> Option<Self::Output> {
588+
self.get(index).cloned()
589+
}
590+
}
591+
impl<T: Clone> AtIndex for Instances<T> {
592+
type Output = Instances<T>;
593+
594+
fn at_index(&self, index: usize) -> Option<Self::Output> {
595+
let mut result_table = Self::default();
596+
if let Some(row) = self.instance_ref_iter().nth(index) {
597+
result_table.push(row.to_instance_cloned());
598+
Some(result_table)
599+
} else {
600+
None
601+
}
602+
}
603+
}

0 commit comments

Comments
 (0)