Skip to main content

egui_graph_edit/
graph.rs

1use super::*;
2
3#[cfg(feature = "persistence")]
4use serde::{Deserialize, Serialize};
5
6/// A node inside the [`Graph`]. Nodes have input and output parameters, stored
7/// as ids. They also contain a custom `NodeData` struct with whatever data the
8/// user wants to store per-node.
9#[derive(Debug, Clone)]
10#[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))]
11pub struct Node<NodeData> {
12    pub id: NodeId,
13    pub label: String,
14    pub inputs: Vec<(String, InputId)>,
15    pub outputs: Vec<(String, OutputId)>,
16    pub user_data: NodeData,
17}
18
19/// The three kinds of input params. These describe how the graph must behave
20/// with respect to inline widgets and connections for this parameter.
21#[derive(Debug, Clone, Copy)]
22#[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))]
23pub enum InputParamKind {
24    /// No constant value can be set. Only incoming connections can produce it
25    ConnectionOnly,
26    /// Only a constant value can be set. No incoming connections accepted.
27    ConstantOnly,
28    /// Both incoming connections and constants are accepted. Connections take
29    /// precedence over the constant values.
30    ConnectionOrConstant,
31}
32
33#[cfg(feature = "persistence")]
34fn shown_inline_default() -> bool {
35    true
36}
37
38/// An input parameter. Input parameters are inside a node, and represent data
39/// that this node receives. Unlike their [`OutputParam`] counterparts, input
40/// parameters also display an inline widget which allows setting its "value".
41/// The `DataType` generic parameter is used to restrict the range of input
42/// connections for this parameter, and the `ValueType` is use to represent the
43/// data for the inline widget (i.e. constant) value.
44#[derive(Debug, Clone)]
45#[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))]
46pub struct InputParam<DataType, ValueType> {
47    pub id: InputId,
48    /// The data type of this node. Used to determine incoming connections. This
49    /// should always match the type of the InputParamValue, but the property is
50    /// not actually enforced.
51    pub typ: DataType,
52    /// The constant value stored in this parameter.
53    pub value: ValueType,
54    /// The input kind. See [`InputParamKind`]
55    pub kind: InputParamKind,
56    /// Back-reference to the node containing this parameter.
57    pub node: NodeId,
58    /// When true, the node is shown inline inside the node graph.
59    #[cfg_attr(feature = "persistence", serde(default = "shown_inline_default"))]
60    pub shown_inline: bool,
61}
62
63/// An output parameter. Output parameters are inside a node, and represent the
64/// data that the node produces. Output parameters can be linked to the input
65/// parameters of other nodes. Unlike an [`InputParam`], output parameters
66/// cannot have a constant inline value.
67#[derive(Debug, Clone)]
68#[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))]
69pub struct OutputParam<DataType> {
70    pub id: OutputId,
71    /// Back-reference to the node containing this parameter.
72    pub node: NodeId,
73    pub typ: DataType,
74}
75
76/// The graph, containing nodes, input parameters and output parameters. Because
77/// graphs are full of self-referential structures, this type uses the `slotmap`
78/// crate to represent all the inner references in the data.
79#[derive(Debug, Clone)]
80#[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))]
81pub struct Graph<NodeData, DataType, ValueType> {
82    /// The [`Node`]s of the graph
83    pub nodes: SlotMap<NodeId, Node<NodeData>>,
84    /// The [`InputParam`]s of the graph
85    pub inputs: SlotMap<InputId, InputParam<DataType, ValueType>>,
86    /// The [`OutputParam`]s of the graph
87    pub outputs: SlotMap<OutputId, OutputParam<DataType>>,
88    // Connects the input of a node, to the output of its predecessor that
89    // produces it
90    pub connections: SecondaryMap<InputId, OutputId>,
91}