stainless_script/stdlib/
flow_nodes.rs1use crate::{
2 class::Class,
3 node::Node,
4 socket::{InputSocket, OutputSocket},
5 ExecutionContext,
6};
7use std::rc::Rc;
8
9pub fn start_node_class() -> Class {
10 Class {
11 name: "start".into(),
12 nodes: vec![Rc::new(StartNode {
13 outputs: vec![],
14 name: "default".into(),
15 }) as Rc<dyn Node>],
16 obj_from_str: None,
17 }
18}
19
20pub fn end_node_class() -> Class {
21 Class {
22 name: "end".into(),
23 nodes: vec![Rc::new(EndNode(vec![])) as Rc<dyn Node>],
24 obj_from_str: None,
25 }
26}
27
28#[derive(Debug, Clone)]
30pub struct StartNode {
31 outputs: Vec<OutputSocket>,
32 name: String,
33}
34
35impl Node for StartNode {
36 fn execute(&self, _context: &mut ExecutionContext) -> usize {
37 0
38 }
39
40 fn class(&self) -> Class {
41 start_node_class()
42 }
43
44 fn variants(&self) -> Vec<std::borrow::Cow<'_, str>> {
45 vec!["start#default#[]".into(), self.current_variant()]
46 }
47
48 fn current_variant(&self) -> std::borrow::Cow<'_, str> {
49 format!(
50 "start#{}#{}",
51 self.name,
52 ron::to_string(&self.outputs).unwrap()
53 )
54 .into()
55 }
56
57 fn set_variant(&mut self, variant: &str) {
58 let mut parts = variant.split('#');
59 parts.next();
60 let name = String::from(parts.next().unwrap());
61 let outputs = ron::from_str(parts.next().unwrap()).unwrap();
62 self.name = name;
63 self.outputs = outputs
64 }
65
66 fn inputs(&self) -> Vec<InputSocket> {
67 vec![]
68 }
69
70 fn outputs(&self) -> Vec<OutputSocket> {
71 self.outputs.clone()
72 }
73
74 fn clone_node(&self) -> Rc<dyn Node> {
75 Rc::new(self.clone()) as Rc<dyn Node>
76 }
77
78 fn accepts_arbitrary_variants(&self) -> bool {
79 true
80 }
81}
82
83#[derive(Debug, Clone)]
85pub struct EndNode(Vec<InputSocket>);
86
87impl Node for EndNode {
88 fn execute(&self, context: &mut ExecutionContext) -> usize {
89 let inputs = context.get_inputs();
90 context.finish_subroutine(inputs);
91 0
92 }
93
94 fn class(&self) -> Class {
95 end_node_class()
96 }
97
98 fn variants(&self) -> Vec<std::borrow::Cow<'_, str>> {
99 vec!["end[]".into(), self.current_variant()]
100 }
101
102 fn current_variant(&self) -> std::borrow::Cow<'_, str> {
103 format!("end{}", ron::to_string(&self.0).unwrap()).into()
104 }
105
106 fn set_variant(&mut self, variant: &str) {
107 self.0 = ron::from_str(variant.strip_prefix("end").unwrap()).unwrap()
108 }
109
110 fn inputs(&self) -> Vec<InputSocket> {
111 self.0.clone()
112 }
113
114 fn outputs(&self) -> Vec<OutputSocket> {
115 vec![]
116 }
117
118 fn clone_node(&self) -> Rc<dyn Node> {
119 Rc::new(self.clone()) as Rc<dyn Node>
120 }
121
122 fn accepts_arbitrary_variants(&self) -> bool {
123 true
124 }
125}