Skip to content

Commit 84431ec

Browse files
committed
Broken layer tree
Bye
1 parent 8451e49 commit 84431ec

File tree

10 files changed

+460
-36
lines changed

10 files changed

+460
-36
lines changed

document-legacy/src/document.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::document_metadata::DocumentMetadata;
12
use crate::intersection::Quad;
23
use crate::layers::folder_layer::FolderLayer;
34
use crate::layers::layer_info::{Layer, LayerData, LayerDataType, LayerDataTypeDiscriminant};
@@ -28,6 +29,8 @@ pub struct Document {
2829
pub state_identifier: DefaultHasher,
2930
#[serde(default)]
3031
pub document_network: graph_craft::document::NodeNetwork,
32+
#[serde(skip)]
33+
pub metadata: DocumentMetadata,
3134
#[serde(default)]
3235
pub commit_hash: String,
3336
}
@@ -56,6 +59,7 @@ impl Default for Document {
5659
network.push_node(node, false);
5760
network
5861
},
62+
metadata: Default::default(),
5963
commit_hash: String::new(),
6064
}
6165
}
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
use glam::DAffine2;
2+
use std::collections::HashMap;
3+
use std::num::NonZeroU64;
4+
5+
use graph_craft::document::{NodeId, NodeNetwork};
6+
7+
#[derive(Debug, Clone)]
8+
pub struct DocumentMetadata {
9+
transforms: HashMap<NodeId, DAffine2>,
10+
structure: HashMap<LayerNodeIdentifier, NodeRelations>,
11+
}
12+
13+
impl Default for DocumentMetadata {
14+
fn default() -> Self {
15+
Self {
16+
transforms: HashMap::new(),
17+
structure: HashMap::from_iter([(LayerNodeIdentifier::ROOT, NodeRelations::default())]),
18+
}
19+
}
20+
}
21+
22+
impl DocumentMetadata {
23+
pub const fn root(&self) -> LayerNodeIdentifier {
24+
LayerNodeIdentifier::ROOT
25+
}
26+
fn get_structure(&self, node_identifier: LayerNodeIdentifier) -> Option<&NodeRelations> {
27+
self.structure.get(&node_identifier)
28+
}
29+
fn get_structure_mut(&mut self, node_identifier: LayerNodeIdentifier) -> &mut NodeRelations {
30+
self.structure.entry(node_identifier).or_default()
31+
}
32+
}
33+
34+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
35+
pub struct LayerNodeIdentifier(NonZeroU64);
36+
impl LayerNodeIdentifier {
37+
const ROOT: Self = LayerNodeIdentifier::new_unchecked(0);
38+
const fn new_unchecked(node_id: NodeId) -> Self {
39+
// Safety: will always be >=1
40+
Self(unsafe { NonZeroU64::new_unchecked(node_id + 1) })
41+
}
42+
pub fn new(node_id: NodeId, network: &NodeNetwork) -> Self {
43+
debug_assert!(
44+
is_layer_node(node_id, network),
45+
"Layer identifer constructed from non layer node {node_id}: {:#?}",
46+
network.nodes.get(&node_id)
47+
);
48+
Self::new_unchecked(node_id)
49+
}
50+
pub fn to_node(self) -> NodeId {
51+
u64::from(self.0) - 1
52+
}
53+
pub fn parent(self, document_metadata: &DocumentMetadata) -> Option<LayerNodeIdentifier> {
54+
document_metadata.get_structure(self).and_then(|relations| relations.parent)
55+
}
56+
pub fn previous_sibling(self, document_metadata: &DocumentMetadata) -> Option<LayerNodeIdentifier> {
57+
document_metadata.get_structure(self).and_then(|relations| relations.previous_sibling)
58+
}
59+
pub fn next_sibling(self, document_metadata: &DocumentMetadata) -> Option<LayerNodeIdentifier> {
60+
document_metadata.get_structure(self).and_then(|relations| relations.next_sibling)
61+
}
62+
pub fn first_child(self, document_metadata: &DocumentMetadata) -> Option<LayerNodeIdentifier> {
63+
document_metadata.get_structure(self).and_then(|relations| relations.first_child)
64+
}
65+
pub fn last_child(self, document_metadata: &DocumentMetadata) -> Option<LayerNodeIdentifier> {
66+
document_metadata.get_structure(self).and_then(|relations| relations.last_child)
67+
}
68+
pub fn has_children(self, document_metadata: &DocumentMetadata) -> bool {
69+
self.first_child(document_metadata).is_some()
70+
}
71+
pub fn children(self, document_metadata: &DocumentMetadata) -> AxisIter {
72+
AxisIter {
73+
layer_node: self.first_child(document_metadata),
74+
next_node: Self::next_sibling,
75+
document_metadata,
76+
}
77+
}
78+
pub fn ancestors(self, document_metadata: &DocumentMetadata) -> AxisIter {
79+
AxisIter {
80+
layer_node: Some(self),
81+
next_node: Self::parent,
82+
document_metadata,
83+
}
84+
}
85+
pub fn last_children(self, document_metadata: &DocumentMetadata) -> AxisIter {
86+
AxisIter {
87+
layer_node: Some(self),
88+
next_node: Self::last_child,
89+
document_metadata,
90+
}
91+
}
92+
pub fn decendants(self, document_metadata: &DocumentMetadata) -> DecendantsIter {
93+
DecendantsIter {
94+
front: self.first_child(document_metadata),
95+
back: self.last_child(document_metadata),
96+
document_metadata,
97+
}
98+
}
99+
pub fn push_front_child(self, document_metadata: &mut DocumentMetadata, new: LayerNodeIdentifier) {
100+
let parent = document_metadata.get_structure_mut(self);
101+
let old_first_child = parent.first_child.replace(new);
102+
parent.last_child.get_or_insert(new);
103+
if let Some(old_first_child) = old_first_child {
104+
document_metadata.get_structure_mut(old_first_child).previous_sibling = Some(new);
105+
document_metadata.get_structure_mut(new).next_sibling = Some(old_first_child);
106+
}
107+
document_metadata.get_structure_mut(new).parent = Some(self);
108+
}
109+
pub fn push_child(self, document_metadata: &mut DocumentMetadata, new: LayerNodeIdentifier) {
110+
let parent = document_metadata.get_structure_mut(self);
111+
let old_last_child = parent.last_child.replace(new);
112+
parent.first_child.get_or_insert(new);
113+
if let Some(old_last_child) = old_last_child {
114+
document_metadata.get_structure_mut(old_last_child).next_sibling = Some(new);
115+
document_metadata.get_structure_mut(new).previous_sibling = Some(old_last_child);
116+
}
117+
document_metadata.get_structure_mut(new).parent = Some(self);
118+
}
119+
pub fn add_before(self, document_metadata: &mut DocumentMetadata, new: LayerNodeIdentifier) {
120+
document_metadata.get_structure_mut(new).next_sibling = Some(self);
121+
document_metadata.get_structure_mut(new).parent = self.parent(document_metadata);
122+
let old_previous_sibling = document_metadata.get_structure_mut(self).previous_sibling.replace(new);
123+
if let Some(old_previous_sibling) = old_previous_sibling {
124+
document_metadata.get_structure_mut(old_previous_sibling).next_sibling = Some(new);
125+
document_metadata.get_structure_mut(new).previous_sibling = Some(old_previous_sibling);
126+
} else {
127+
self.parent(document_metadata)
128+
.map(|parent| document_metadata.get_structure_mut(parent))
129+
.filter(|structure| structure.first_child == Some(self))
130+
.map(|structure| structure.first_child = Some(new));
131+
}
132+
}
133+
pub fn add_after(self, document_metadata: &mut DocumentMetadata, new: LayerNodeIdentifier) {
134+
document_metadata.get_structure_mut(new).previous_sibling = Some(self);
135+
document_metadata.get_structure_mut(new).parent = self.parent(document_metadata);
136+
let old_next_sibling = document_metadata.get_structure_mut(self).next_sibling.replace(new);
137+
if let Some(old_next_sibling) = old_next_sibling {
138+
document_metadata.get_structure_mut(old_next_sibling).previous_sibling = Some(new);
139+
document_metadata.get_structure_mut(new).next_sibling = Some(old_next_sibling);
140+
} else {
141+
self.parent(document_metadata)
142+
.map(|parent| document_metadata.get_structure_mut(parent))
143+
.filter(|structure| structure.last_child == Some(self))
144+
.map(|structure| structure.last_child = Some(new));
145+
}
146+
}
147+
pub fn delete(self, document_metadata: &mut DocumentMetadata) {
148+
let previous_sibling = self.previous_sibling(document_metadata);
149+
let next_sibling = self.next_sibling(document_metadata);
150+
151+
if let Some(previous_sibling) = previous_sibling.map(|node| document_metadata.get_structure_mut(node)) {
152+
previous_sibling.next_sibling = next_sibling;
153+
}
154+
155+
if let Some(next_sibling) = next_sibling.map(|node| document_metadata.get_structure_mut(node)) {
156+
next_sibling.previous_sibling = previous_sibling;
157+
}
158+
let mut parent = self.parent(document_metadata).map(|parent| document_metadata.get_structure_mut(parent));
159+
parent
160+
.as_mut()
161+
.filter(|structure| structure.first_child == Some(self))
162+
.map(|structure| structure.first_child = next_sibling);
163+
parent.filter(|structure| structure.last_child == Some(self)).map(|structure| structure.last_child = previous_sibling);
164+
165+
let mut delete = vec![self];
166+
delete.extend(self.decendants(document_metadata));
167+
for node in delete {
168+
document_metadata.structure.remove(&node);
169+
}
170+
}
171+
}
172+
impl From<NodeId> for LayerNodeIdentifier {
173+
fn from(node_id: NodeId) -> Self {
174+
Self::new_unchecked(node_id)
175+
}
176+
}
177+
impl From<LayerNodeIdentifier> for NodeId {
178+
fn from(identifer: LayerNodeIdentifier) -> Self {
179+
identifer.to_node()
180+
}
181+
}
182+
/// Iterator over specified axis.
183+
#[derive(Clone)]
184+
pub struct AxisIter<'a> {
185+
layer_node: Option<LayerNodeIdentifier>,
186+
next_node: fn(LayerNodeIdentifier, &DocumentMetadata) -> Option<LayerNodeIdentifier>,
187+
document_metadata: &'a DocumentMetadata,
188+
}
189+
190+
impl<'a> Iterator for AxisIter<'a> {
191+
type Item = LayerNodeIdentifier;
192+
193+
fn next(&mut self) -> Option<Self::Item> {
194+
let layer_node = self.layer_node.take();
195+
self.layer_node = layer_node.and_then(|node| (self.next_node)(node, self.document_metadata));
196+
layer_node
197+
}
198+
}
199+
200+
#[derive(Clone)]
201+
pub struct DecendantsIter<'a> {
202+
front: Option<LayerNodeIdentifier>,
203+
back: Option<LayerNodeIdentifier>,
204+
document_metadata: &'a DocumentMetadata,
205+
}
206+
207+
impl<'a> Iterator for DecendantsIter<'a> {
208+
type Item = LayerNodeIdentifier;
209+
210+
fn next(&mut self) -> Option<Self::Item> {
211+
if self.front == self.back {
212+
self.back = None;
213+
self.front.take()
214+
} else {
215+
let layer_node = self.front.take();
216+
if let Some(layer_node) = layer_node {
217+
self.front = layer_node
218+
.first_child(self.document_metadata)
219+
.or_else(|| layer_node.ancestors(self.document_metadata).find_map(|ancestor| ancestor.next_sibling(self.document_metadata)));
220+
}
221+
layer_node
222+
}
223+
}
224+
}
225+
impl<'a> DoubleEndedIterator for DecendantsIter<'a> {
226+
fn next_back(&mut self) -> Option<Self::Item> {
227+
if self.front == self.back {
228+
self.front = None;
229+
self.back.take()
230+
} else {
231+
let layer_node = self.back.take();
232+
if let Some(layer_node) = layer_node {
233+
self.back = layer_node
234+
.previous_sibling(self.document_metadata)
235+
.and_then(|sibling| sibling.last_children(self.document_metadata).last())
236+
.or_else(|| layer_node.parent(self.document_metadata));
237+
}
238+
239+
layer_node
240+
}
241+
}
242+
}
243+
244+
#[derive(Debug, Clone, Copy, Default)]
245+
pub struct NodeRelations {
246+
parent: Option<LayerNodeIdentifier>,
247+
previous_sibling: Option<LayerNodeIdentifier>,
248+
next_sibling: Option<LayerNodeIdentifier>,
249+
first_child: Option<LayerNodeIdentifier>,
250+
last_child: Option<LayerNodeIdentifier>,
251+
}
252+
253+
fn is_layer_node(node: NodeId, network: &NodeNetwork) -> bool {
254+
node == LayerNodeIdentifier::ROOT.to_node() || network.nodes.get(&node).is_some_and(|node| node.name == "Layer")
255+
}
256+
257+
#[test]
258+
fn test_tree() {
259+
let mut document_metadata = DocumentMetadata::default();
260+
let root = document_metadata.root();
261+
let document_metadata = &mut document_metadata;
262+
root.push_child(document_metadata, LayerNodeIdentifier::new_unchecked(3));
263+
assert_eq!(root.children(document_metadata).collect::<Vec<_>>(), vec![LayerNodeIdentifier::new_unchecked(3)]);
264+
root.push_child(document_metadata, LayerNodeIdentifier::new_unchecked(6));
265+
assert_eq!(root.children(document_metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![3, 6]);
266+
assert_eq!(root.decendants(document_metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![3, 6]);
267+
LayerNodeIdentifier::new_unchecked(3).add_after(document_metadata, LayerNodeIdentifier::new_unchecked(4));
268+
LayerNodeIdentifier::new_unchecked(3).add_before(document_metadata, LayerNodeIdentifier::new_unchecked(2));
269+
LayerNodeIdentifier::new_unchecked(6).add_before(document_metadata, LayerNodeIdentifier::new_unchecked(5));
270+
LayerNodeIdentifier::new_unchecked(6).add_after(document_metadata, LayerNodeIdentifier::new_unchecked(9));
271+
LayerNodeIdentifier::new_unchecked(6).push_child(document_metadata, LayerNodeIdentifier::new_unchecked(8));
272+
LayerNodeIdentifier::new_unchecked(6).push_front_child(document_metadata, LayerNodeIdentifier::new_unchecked(7));
273+
root.push_front_child(document_metadata, LayerNodeIdentifier::new_unchecked(1));
274+
assert_eq!(root.children(document_metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![1, 2, 3, 4, 5, 6, 9]);
275+
assert_eq!(
276+
root.decendants(document_metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
277+
vec![1, 2, 3, 4, 5, 6, 7, 8, 9]
278+
);
279+
assert_eq!(
280+
root.decendants(document_metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(),
281+
vec![9, 8, 7, 6, 5, 4, 3, 2, 1]
282+
);
283+
assert!(root.children(document_metadata).all(|child| child.parent(document_metadata) == Some(root)));
284+
LayerNodeIdentifier::new_unchecked(6).delete(document_metadata);
285+
LayerNodeIdentifier::new_unchecked(1).delete(document_metadata);
286+
assert_eq!(root.children(document_metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![2, 3, 4, 5, 9]);
287+
assert_eq!(root.decendants(document_metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![2, 3, 4, 5, 9]);
288+
assert_eq!(root.decendants(document_metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(), vec![9, 5, 4, 3, 2]);
289+
}

document-legacy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod boolean_ops;
66
/// Contains constant values used by this crate.
77
pub mod consts;
88
pub mod document;
9+
pub mod document_metadata;
910
/// Defines errors that can occur when using this crate.
1011
pub mod error;
1112
/// Utilities for computing intersections.

editor/src/messages/portfolio/document/document_message_handler.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::messages::tool::utility_types::ToolType;
1717
use crate::node_graph_executor::NodeGraphExecutor;
1818

1919
use document_legacy::document::Document as DocumentLegacy;
20+
use document_legacy::document_metadata::LayerNodeIdentifier;
2021
use document_legacy::layers::blend_mode::BlendMode;
2122
use document_legacy::layers::folder_layer::FolderLayer;
2223
use document_legacy::layers::layer_info::{LayerDataType, LayerDataTypeDiscriminant};
@@ -1209,13 +1210,14 @@ impl DocumentMessageHandler {
12091210
)
12101211
}
12111212

1212-
fn serialize_structure(&self, folder: &FolderLayer, structure: &mut Vec<u64>, data: &mut Vec<LayerId>, path: &mut Vec<LayerId>) {
1213+
fn serialize_structure(&self, folder: LayerNodeIdentifier, structure: &mut Vec<u64>, data: &mut Vec<LayerId>, path: &mut Vec<LayerId>) {
12131214
let mut space = 0;
1214-
for (id, layer) in folder.layer_ids.iter().zip(folder.layers()).rev() {
1215-
data.push(*id);
1215+
for layer_node in folder.children(&self.document_legacy.metadata) {
1216+
data.push(layer_node.to_node());
1217+
info!("Pushed child");
12161218
space += 1;
1217-
if let LayerDataType::Folder(ref folder) = layer.data {
1218-
path.push(*id);
1219+
if layer_node.has_children(&self.document_legacy.metadata) {
1220+
path.push(layer_node.to_node());
12191221
if self.layer_metadata(path).expanded {
12201222
structure.push(space);
12211223
self.serialize_structure(folder, structure, data, path);
@@ -1261,7 +1263,7 @@ impl DocumentMessageHandler {
12611263
/// ```
12621264
pub fn serialize_root(&self) -> Vec<u64> {
12631265
let (mut structure, mut data) = (vec![0], Vec::new());
1264-
self.serialize_structure(self.document_legacy.root.as_folder().unwrap(), &mut structure, &mut data, &mut vec![]);
1266+
self.serialize_structure(self.document_legacy.metadata.root(), &mut structure, &mut data, &mut vec![]);
12651267
structure[0] = structure.len() as u64 - 1;
12661268
structure.extend(data);
12671269

0 commit comments

Comments
 (0)