use egui::Rect;
use re_chunk::EntityPath;
use crate::graph::{EdgeId, NodeId};
use super::EdgeGeometry;
#[derive(Debug)]
pub struct Layout {
pub(super) nodes: ahash::HashMap<NodeId, Rect>,
pub(super) edges: ahash::HashMap<EdgeId, Vec<EdgeGeometry>>,
pub(super) entities: Vec<(EntityPath, Rect)>,
}
fn bounding_rect_from_iter(rectangles: impl Iterator<Item = egui::Rect>) -> egui::Rect {
rectangles.fold(egui::Rect::NOTHING, |acc, rect| acc.union(rect))
}
impl Layout {
pub fn empty() -> Self {
Self {
nodes: ahash::HashMap::default(),
edges: ahash::HashMap::default(),
entities: Vec::new(),
}
}
pub fn bounding_rect(&self) -> Rect {
bounding_rect_from_iter(self.nodes.values().copied())
}
pub fn get_node(&self, node: &NodeId) -> Option<Rect> {
self.nodes.get(node).copied()
}
pub fn get_edge(&self, edge: &EdgeId) -> Option<&[EdgeGeometry]> {
self.edges.get(edge).map(|es| es.as_slice())
}
pub fn edges(&self) -> impl Iterator<Item = (EdgeId, &[EdgeGeometry])> {
self.edges.iter().map(|(id, es)| (*id, es.as_slice()))
}
pub fn num_entities(&self) -> usize {
self.entities.len()
}
pub fn entities(&self) -> impl Iterator<Item = &(EntityPath, Rect)> {
self.entities.iter()
}
}