diff --git a/src/board/edit.rs b/src/board/edit.rs index af12b00..b56c6c2 100644 --- a/src/board/edit.rs +++ b/src/board/edit.rs @@ -4,11 +4,15 @@ use std::collections::BTreeMap; -use crate::{board::BandName, drawing::band::BandUid, geometry::edit::Edit, layout::LayoutEdit}; +use crate::{ + board::BandName, drawing::band::BandUid, geometry::edit::Edit, layout::LayoutEdit, + router::ng::EtchedPath, +}; #[derive(Debug, Clone, Default)] pub struct BoardDataEdit { - pub(super) bands: BTreeMap, Option)>, + pub(super) bands_by_id: BTreeMap, Option)>, + pub(super) bands_by_name: BTreeMap, Option)>, } impl BoardDataEdit { @@ -19,11 +23,13 @@ impl BoardDataEdit { impl Edit for BoardDataEdit { fn reverse_inplace(&mut self) { - self.bands.reverse_inplace(); + self.bands_by_id.reverse_inplace(); + self.bands_by_name.reverse_inplace(); } fn merge(&mut self, edit: Self) { - self.bands.merge(edit.bands); + self.bands_by_id.merge(edit.bands_by_id); + self.bands_by_name.merge(edit.bands_by_name); } } diff --git a/src/board/mod.rs b/src/board/mod.rs index 23e27b8..8a52543 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -262,10 +262,13 @@ impl Board { let ep = EtchedPath { end_points: (source, target).into(), }; - self.bands_by_id.insert(ep, band); - self.band_bandname.insert(band, bandname.clone()); - recorder.bands.insert(bandname, (None, Some(band))); + self.bands_by_id.insert(ep, band); + recorder.bands_by_id.insert(ep, (None, Some(band))); + + self.band_bandname.insert(band, bandname.clone()); + recorder.bands_by_name.insert(bandname, (None, Some(band))); + true } } @@ -310,12 +313,19 @@ impl Board { if let Some((_, uid)) = self.bands_by_id.remove_by_left(&ep) { let (from, _) = uid.into(); self.layout.remove_band(&mut recorder.layout_edit, from)?; + + recorder + .board_data_edit + .bands_by_id + .insert(ep, (Some(uid), None)); } - recorder - .board_data_edit - .bands - .insert(bandname, (maybe_band, None)); + if let Some(uid) = maybe_band { + recorder + .board_data_edit + .bands_by_name + .insert(bandname, (Some(uid), None)); + } Ok(()) } @@ -346,19 +356,31 @@ impl Board { } pub fn apply_edit(&mut self, edit: &BoardEdit) { - for (bandname, (maybe_old_band_uid, ..)) in &edit.board_data_edit.bands { + for (bandname, (maybe_old_band_uid, _)) in &edit.board_data_edit.bands_by_name { if maybe_old_band_uid.is_some() { self.band_bandname.remove_by_right(bandname); } } + for (ep, (maybe_old_band_uid, _)) in &edit.board_data_edit.bands_by_id { + if maybe_old_band_uid.is_some() { + self.bands_by_id.remove_by_left(ep); + } + } + self.layout_mut().apply(&edit.layout_edit); - for (bandname, (.., maybe_new_band_uid)) in &edit.board_data_edit.bands { + for (bandname, (_, maybe_new_band_uid)) in &edit.board_data_edit.bands_by_name { if let Some(band_uid) = maybe_new_band_uid { self.band_bandname.insert(*band_uid, bandname.clone()); } } + + for (ep, (_, maybe_new_band_uid)) in &edit.board_data_edit.bands_by_id { + if let Some(band_uid) = maybe_new_band_uid { + self.bands_by_id.insert(*ep, *band_uid); + } + } } /// Returns the mesadata associated with the layout's drawing rules.