-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
240 lines (193 loc) · 5.98 KB
/
mod.rs
File metadata and controls
240 lines (193 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
mod add_node_popup;
mod draw_cut;
mod frame;
mod node_graph_editor;
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use frame::NodeFrameState;
pub use node_graph_editor::*;
use egui::{pos2, Align, Color32, InnerResponse, Label, Layout, Pos2, Response, Vec2, WidgetText};
use serde::{Deserialize, Serialize};
use crate::node_graph::*;
/// Contains every information about nodes that is only relevant to the editing
/// of a node graph, like node positions and their drawing order.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NodeGraphEditState {
node_states: HashMap<NodeId, NodeFrameState>,
node_order: Vec<NodeId>,
}
impl NodeGraphEditState {
pub fn new() -> Self {
Self {
node_states: HashMap::new(),
node_order: Vec::new(),
}
}
/// Ensures this state contains a state for every node, possibly creating or
/// deleting entries.
pub fn sync_state(&mut self, node_ids: &[NodeId]) {
// Delete non-existing nodes
self.node_states
.retain(|node_id, _| node_ids.contains(node_id));
self.node_order.retain(|node_id| node_ids.contains(node_id));
// Find the best position to add new nodes
let mut cursor = self
.node_states
.values()
.map(|n| pos2(n.position.x + 230.0, n.position.y))
.reduce(|a, b| pos2(a.x.max(b.x), a.y.min(b.y)))
.unwrap_or_default();
// Creating new nodes
for node_id in node_ids {
self.node_states.entry(*node_id).or_insert_with(|| {
let state = NodeFrameState {
position: egui::Pos2::new(cursor.x, cursor.y),
};
cursor.x += 230.0;
state
});
if !self.node_order.contains(node_id) {
self.node_order.push(*node_id);
}
}
}
pub fn to_top(&mut self, node_id: NodeId) {
self.node_order.retain(|id| id != &node_id);
self.node_order.push(node_id);
}
}
/// Trait describing a node graph to the [NodeGraphEditor].
pub trait EditNodeGraph {
fn get_node_ids(&self) -> Vec<NodeId>;
fn get_node_mut(&mut self, node_id: NodeId) -> Option<&mut (dyn DynEditNode)>;
fn remove_node(&mut self, node_id: NodeId);
fn add_node(&mut self, path: &str) -> NodeId;
fn addable_nodes(&self) -> Vec<&'static str>;
}
/// Trait describing a node that is part of a node graph to the [NodeGraphEditor].
pub trait EditNode {
type OutputId: Into<OutputId> + From<OutputId>;
type InputId: Into<InputId> + From<InputId>;
fn name(&self) -> &str;
fn color(&self) -> Color32;
fn connect(&mut self, input: Self::InputId, connection: NodeOutput);
fn disconnect(&mut self, input: Self::InputId);
fn ui(&mut self, ui: &mut NodeUi);
}
/// Auto-trait
pub trait DynEditNode {
fn name(&self) -> &str;
fn color(&self) -> Color32;
fn connect(&mut self, input: InputId, connection: NodeOutput);
fn disconnect(&mut self, input: InputId);
fn ui(&mut self, ui: &mut NodeUi);
}
impl<T: EditNode> DynEditNode for T {
fn name(&self) -> &str {
self.name()
}
fn color(&self) -> Color32 {
self.color()
}
fn connect(&mut self, input: InputId, connection: NodeOutput) {
self.connect(input.into(), connection)
}
fn disconnect(&mut self, input: InputId) {
self.disconnect(input.into())
}
fn ui(&mut self, ui: &mut NodeUi) {
self.ui(ui)
}
}
/// Wrapper around [egui::Ui], additionally describing the node inputs and
/// outputs.
pub struct NodeUi<'a> {
ui: &'a mut egui::Ui,
inputs: &'a mut Vec<CollectedInput>,
outputs: &'a mut Vec<CollectedOutput>,
}
pub(super) struct CollectedInput {
pub id: InputId,
pub pos: Pos2,
pub color: Color32,
pub connection: Option<NodeOutput>,
}
pub(super) struct CollectedOutput {
pub id: OutputId,
pub type_: TypeId,
pub pos: Pos2,
pub color: Color32,
}
impl NodeUi<'_> {
pub fn input(
&mut self,
id: impl Into<InputId>,
connection: Option<NodeOutput>,
color: impl Into<Color32>,
add_contents: impl FnOnce(&mut egui::Ui),
) {
let InnerResponse {
response: Response { rect, .. },
..
} = self
.ui
.allocate_ui(Vec2::new(self.ui.available_width(), 18.0), add_contents);
let pin_pos = rect.left_top() + Vec2::new(-10.0, 9.0);
self.inputs.push(CollectedInput {
id: id.into(),
pos: pin_pos,
color: color.into(),
connection,
});
}
pub fn output(
&mut self,
id: impl Into<OutputId>,
type_: impl Into<TypeId>,
color: impl Into<Color32>,
add_contents: impl FnOnce(&mut egui::Ui),
) {
let rect = self
.ui
.allocate_ui(Vec2::new(self.ui.available_width(), 18.0), |ui| {
ui.with_layout(
Layout {
cross_align: Align::Max,
..*ui.layout()
},
add_contents,
);
})
.response
.rect;
let pin_pos = rect.right_top() + Vec2::new(10.0, 9.0);
self.outputs.push(CollectedOutput {
id: id.into(),
type_: type_.into(),
pos: pin_pos,
color: color.into(),
});
}
}
impl Deref for NodeUi<'_> {
type Target = egui::Ui;
fn deref(&self) -> &Self::Target {
self.ui
}
}
impl DerefMut for NodeUi<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ui
}
}
pub trait UiNodeExt {
fn node_label(&mut self, text: impl Into<WidgetText>) -> Response;
}
impl UiNodeExt for egui::Ui {
fn node_label(&mut self, text: impl Into<WidgetText>) -> Response {
self.add_space(3.5);
self.add(Label::new(text).selectable(false))
}
}