From 4c8f7cb804caf7c7a0d33fd0d898c4759a254459 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Wed, 17 Apr 2024 03:36:15 +0000 Subject: [PATCH] overlay: toggle only one node at a time, prioritize active layer --- src/overlay/overlay.rs | 63 +++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/src/overlay/overlay.rs b/src/overlay/overlay.rs index 770cc11..4521e0c 100644 --- a/src/overlay/overlay.rs +++ b/src/overlay/overlay.rs @@ -5,7 +5,7 @@ use rstar::AABB; use crate::{ drawing::{ - graph::{MakePrimitive, PrimitiveIndex}, + graph::{GetLayer, MakePrimitive, PrimitiveIndex}, primitive::MakeShape, rules::RulesTrait, }, @@ -16,35 +16,66 @@ use crate::{ pub struct Overlay { selection: HashSet>>, + active_layer: u64, } impl Overlay { pub fn new() -> Self { Self { selection: HashSet::new(), + active_layer: 0, } } pub fn click(&mut self, layout: &Layout, at: Point) { - for geom in layout.drawing().rtree().locate_in_envelope_intersecting( - &AABB::<[f64; 3]>::from_corners( + let geoms: Vec<_> = layout + .drawing() + .rtree() + .locate_in_envelope_intersecting(&AABB::<[f64; 3]>::from_corners( [at.x(), at.y(), -f64::INFINITY], [at.x(), at.y(), f64::INFINITY], - ), - ) { - match geom.data { - Node::Primitive(primitive) => { - if primitive - .primitive(layout.drawing()) - .shape() - .contains_point(at) - { - self.toggle_selection(geom.data); - } - } - Node::Compound(compound) => (), // TODO. + )) + .collect(); + + if let Some(geom) = geoms.iter().find(|&&geom| match geom.data { + Node::Primitive(primitive) => { + primitive.primitive(layout.drawing()).layer() == self.active_layer + } + Node::Compound(compound) => false, + }) { + if self.toggle_selection_if_contains_point(layout, geom.data, at) { + return; } } + + for geom in geoms { + if self.toggle_selection_if_contains_point(layout, geom.data, at) { + return; + } + } + } + + fn toggle_selection_if_contains_point( + &mut self, + layout: &Layout, + node: Node>, + p: Point, + ) -> bool { + match node { + Node::Primitive(primitive) => { + if primitive + .primitive(layout.drawing()) + .shape() + .contains_point(p) + { + self.toggle_selection(node); + return true; + } + } + Node::Compound(compound) => (), // TODO. + } + + false } fn toggle_selection(&mut self, node: Node>) {