Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions crates/rectree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl<N: RectNodes> NodeContext for N {
fn set_translation(&mut self, id: &Self::Id, translation: Vec2) {
if let Some(n) = self.get_node_mut(id) {
n.translation = translation;
n.state.needs_reposition();
}
}
}
Expand Down Expand Up @@ -220,15 +221,19 @@ pub fn layout<
}

// 3. Propagate translation, seeding from `bubbled_id`'s parent
// world translation. Seeding from `bubbled_id`'s own translation
// would offset a non-root node by its own translation each pass;
// the root has no parent and seeds from zero.
// world translation.
let parent_world = nodes
.get_node(&bubbled_id)
.and_then(|node| node.parent_id)
.and_then(|parent| nodes.get_node(&parent))
.map_or(Vec2::ZERO, |node| node.world_translation);
propagate_translation(tree, nodes, &bubbled_id, parent_world);
propagate_translation(
tree,
nodes,
&bubbled_id,
parent_world,
false,
);
}

/// Propagates a constraint top-down through the subtree rooted
Expand Down Expand Up @@ -417,25 +422,36 @@ pub fn propagate_translation<
nodes: &mut N,
id: &T::Id,
parent_world: Vec2,
parent_translation_changed: bool,
) {
let node = nodes
.get_node(id)
.expect("propagate_translation: Id is invalid!");

// Already up-to-date; skip this entire subtree.
if node.state.is_positioned() {
if !parent_translation_changed && node.state.is_positioned() {
return;
}

let world = parent_world + node.translation;

let mut translation_changed = false;
if let Some(n) = nodes.get_node_mut(id) {
n.world_translation = world;
translation_changed = n.world_translation != world;
if translation_changed {
n.world_translation = world;
}
n.state.has_repositioned();
}

tree.for_each_child(id, nodes, |child, nodes| {
propagate_translation(tree, nodes, child, world);
propagate_translation(
tree,
nodes,
child,
world,
translation_changed,
);
});
}

Expand Down Expand Up @@ -598,6 +614,7 @@ mod tests {
&mut wt.nodes,
&0,
Vec2::ZERO,
false,
);

assert_eq!(wt.nodes.0[&0].world_translation, Vec2::ZERO);
Expand All @@ -621,6 +638,7 @@ mod tests {
&mut wt.nodes,
&0,
Vec2::ZERO,
false,
);

assert_eq!(wt.nodes.0[&1].world_translation, Vec2::ZERO);
Expand All @@ -641,6 +659,7 @@ mod tests {
&mut wt.nodes,
&0,
Vec2::ZERO,
false,
);
let calls = wt.tree.for_each_child_calls.get();

Expand All @@ -650,6 +669,7 @@ mod tests {
&mut wt.nodes,
&0,
Vec2::ZERO,
false,
);
assert_eq!(wt.tree.for_each_child_calls.get(), calls);
}
Expand Down
56 changes: 30 additions & 26 deletions crates/rectree/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ bitflags! {
/// | Translation propagated | `POSITIONED` set |
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct NodeState: u8 {
/// Set by [`crate::propagate_translation`] when
/// `world_translation` is current.
const POSITIONED = 1;

/// Set by [`crate::constrain`] when the stored constraint
/// matches the value last propagated from the parent.
const CONSTRAINED = 1 << 1;
const CONSTRAINED = 1;

/// Set by [`crate::build`] or [`crate::build_up`] when
/// `size` and child translations are current.
const BUILT = 1 << 2;
const BUILT = 1 << 1;

/// Set by [`crate::propagate_translation`] when
/// `world_translation` is current.
const POSITIONED = 1 << 2;
}
}

Expand All @@ -113,11 +113,6 @@ impl NodeState {
*self == Self::all()
}

/// Returns `true` if the `POSITIONED` flag is set.
pub fn is_positioned(&self) -> bool {
self.intersects(Self::POSITIONED)
}

/// Returns `true` if the `CONSTRAINED` flag is set.
pub fn is_constrained(&self) -> bool {
self.intersects(Self::CONSTRAINED)
Expand All @@ -128,9 +123,9 @@ impl NodeState {
self.intersects(Self::BUILT)
}

/// Clears `POSITIONED`, marking translation as stale.
pub fn needs_reposition(&mut self) {
self.remove(Self::POSITIONED);
/// Returns `true` if the `POSITIONED` flag is set.
pub fn is_positioned(&self) -> bool {
self.intersects(Self::POSITIONED)
}

/// Clears `CONSTRAINED`, marking constraint as stale.
Expand All @@ -143,9 +138,9 @@ impl NodeState {
self.remove(Self::BUILT);
}

/// Sets `POSITIONED`, marking translation as current.
pub fn has_repositioned(&mut self) {
self.insert(Self::POSITIONED);
/// Clears `POSITIONED`, marking translation as stale.
pub fn needs_reposition(&mut self) {
self.remove(Self::POSITIONED);
}

/// Sets `CONSTRAINED`, marking constraint as current.
Expand All @@ -157,6 +152,11 @@ impl NodeState {
pub fn has_rebuilt(&mut self) {
self.insert(Self::BUILT);
}

/// Sets `POSITIONED`, marking translation as current.
pub fn has_repositioned(&mut self) {
self.insert(Self::POSITIONED);
}
}

#[cfg(test)]
Expand Down Expand Up @@ -186,13 +186,26 @@ mod tests {
#[test]
fn test_reset_clears_all_flags() {
let mut s = NodeState::all();
s.has_reconstrained();
s.has_rebuilt();
s.has_repositioned();
assert!(s.is_ready());
s.reset();
assert!(!s.is_constrained());
assert!(!s.is_built());
assert!(!s.is_positioned());
assert!(!s.is_ready());
}

#[test]
fn test_needs_reconstrain_clears_constrained() {
let mut s = NodeState::default();
s.has_reconstrained();
assert!(s.is_constrained());
s.needs_reconstrain();
assert!(!s.is_constrained());
}

#[test]
fn test_needs_rebuild_clears_built() {
let mut s = NodeState::default();
Expand All @@ -210,13 +223,4 @@ mod tests {
s.needs_reposition();
assert!(!s.is_positioned());
}

#[test]
fn test_needs_reconstrain_clears_constrained() {
let mut s = NodeState::default();
s.has_reconstrained();
assert!(s.is_constrained());
s.needs_reconstrain();
assert!(!s.is_constrained());
}
}
Loading