| 1 | //! Which graph level the Flow canvas shows: the root, or the inside of a nested |
| 2 | //! `MacroOperator` / `GroupOperator`. |
| 3 | //! |
| 4 | //! `Comp` stores every unpacked tool in one flat `nodes` list; a group's children are the |
| 5 | //! internal tools parsed immediately before it, named by the group's raw `Tools` table. |
| 6 | |
| 7 | use model::{Comp, LuaKey, Node, NodeId}; |
| 8 | use std::collections::HashSet; |
| 9 | |
| 10 | /// Direct children of `owner` and the first `NodeId` of its whole (nested) block. |
| 11 | fn scan(comp: &Comp, owner: NodeId) -> (Vec<NodeId>, usize) { |
| 12 | let mut children = Vec::new(); |
| 13 | let mut cursor = owner.0; |
| 14 | if let Some(tools) = comp.nodes[owner.0].raw.get_table("Tools") { |
| 15 | for (key, value) in tools.entries.iter().rev() { |
| 16 | let (LuaKey::Str(name), Some(tool)) = (key, value.as_table()) else { |
| 17 | continue; |
| 18 | }; |
| 19 | let kind = tool.tag.as_deref().unwrap_or("Unknown"); |
| 20 | let child = (0..cursor).rev().find(|&i| { |
| 21 | let n = &comp.nodes[i]; |
| 22 | n.internal && n.name == *name && n.kind == kind |
| 23 | }); |
| 24 | // Modifiers (splines, paths) own no node. |
| 25 | let Some(child) = child else { continue }; |
| 26 | children.push(NodeId(child)); |
| 27 | cursor = scan(comp, NodeId(child)).1; |
| 28 | } |
| 29 | } |
| 30 | children.reverse(); |
| 31 | (children, cursor) |
| 32 | } |
| 33 | |
| 34 | /// Tools shown inside `group`, in `comp.nodes` order. |
| 35 | pub fn group_children(comp: &Comp, group: NodeId) -> Vec<NodeId> { |
| 36 | if group.0 >= comp.nodes.len() { |
| 37 | return Vec::new(); |
| 38 | } |
| 39 | scan(comp, group).0 |
| 40 | } |
| 41 | |
| 42 | pub fn is_group(node: &Node) -> bool { |
| 43 | matches!(node.kind.as_str(), "MacroOperator" | "GroupOperator") |
| 44 | } |
| 45 | |
| 46 | #[derive(Default)] |
| 47 | pub struct Scope { |
| 48 | path: Vec<NodeId>, |
| 49 | members: HashSet<NodeId>, |
| 50 | } |
| 51 | |
| 52 | impl Scope { |
| 53 | /// Groups entered from the root, outermost first. |
| 54 | pub fn path(&self) -> &[NodeId] { |
| 55 | &self.path |
| 56 | } |
| 57 | |
| 58 | pub fn is_root(&self) -> bool { |
| 59 | self.path.is_empty() |
| 60 | } |
| 61 | |
| 62 | pub fn shows(&self, node: &Node) -> bool { |
| 63 | if self.is_root() { |
| 64 | !node.internal |
| 65 | } else { |
| 66 | self.members.contains(&node.id) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// The shown node called `name`; `Comp::find_node` can pick a same-named tool of another level. |
| 71 | pub fn resolve(&self, comp: &Comp, name: &str) -> Option<NodeId> { |
| 72 | if self.is_root() { |
| 73 | comp.find_node(name).filter(|&id| !comp.node(id).internal) |
| 74 | } else { |
| 75 | self.members |
| 76 | .iter() |
| 77 | .copied() |
| 78 | .find(|&id| comp.nodes.get(id.0).is_some_and(|n| n.name == name)) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Every shown node, in `comp.nodes` order. |
| 83 | pub fn visible(&self, comp: &Comp) -> Vec<NodeId> { |
| 84 | comp.nodes.iter().filter(|n| self.shows(n)).map(|n| n.id).collect() |
| 85 | } |
| 86 | |
| 87 | /// Descends into `group` when it is a shown macro/group node. |
| 88 | pub fn enter(&mut self, comp: &Comp, group: NodeId) -> bool { |
| 89 | if !comp.nodes.get(group.0).is_some_and(|n| is_group(n) && self.shows(n)) { |
| 90 | return false; |
| 91 | } |
| 92 | self.path.push(group); |
| 93 | self.members = group_children(comp, group).into_iter().collect(); |
| 94 | true |
| 95 | } |
| 96 | |
| 97 | /// Keeps the outermost `depth` levels; returns the first group left. |
| 98 | pub fn truncate(&mut self, comp: &Comp, depth: usize) -> Option<NodeId> { |
| 99 | let left = self.path.get(depth).copied()?; |
| 100 | self.path.truncate(depth); |
| 101 | self.reload_members(comp); |
| 102 | Some(left) |
| 103 | } |
| 104 | |
| 105 | /// Drops levels that no longer exist after an undo, redo or delete, and refreshes membership. |
| 106 | /// Returns whether the path changed. |
| 107 | pub fn refresh(&mut self, comp: &Comp) -> bool { |
| 108 | let mut valid = 0; |
| 109 | let mut parent_children: Option<HashSet<NodeId>> = None; |
| 110 | for &group in &self.path { |
| 111 | let ok = comp.nodes.get(group.0).is_some_and(|n| { |
| 112 | is_group(n) |
| 113 | && match &parent_children { |
| 114 | Some(siblings) => siblings.contains(&group), |
| 115 | None => !n.internal, |
| 116 | } |
| 117 | }); |
| 118 | if !ok { |
| 119 | break; |
| 120 | } |
| 121 | parent_children = Some(group_children(comp, group).into_iter().collect()); |
| 122 | valid += 1; |
| 123 | } |
| 124 | let changed = valid != self.path.len(); |
| 125 | self.path.truncate(valid); |
| 126 | self.reload_members(comp); |
| 127 | changed |
| 128 | } |
| 129 | |
| 130 | fn reload_members(&mut self, comp: &Comp) { |
| 131 | self.members = self |
| 132 | .path |
| 133 | .last() |
| 134 | .map(|&g| group_children(comp, g).into_iter().collect()) |
| 135 | .unwrap_or_default(); |
| 136 | } |
| 137 | } |