From c74e69a5e95496789eb04c4a5f5b150cf51c8243 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Sat, 27 Apr 2024 01:12:01 +0200 Subject: [PATCH] autorouter: move ratsnest to `Autorouter`, move overlay to egui module --- src/autorouter/autorouter.rs | 29 ++-------- src/autorouter/mod.rs | 1 - src/bin/topola-egui/app.rs | 57 +++++++++++-------- src/bin/topola-egui/main.rs | 1 + .../topola-egui}/overlay.rs | 2 +- 5 files changed, 40 insertions(+), 50 deletions(-) rename src/{autorouter => bin/topola-egui}/overlay.rs (99%) diff --git a/src/autorouter/autorouter.rs b/src/autorouter/autorouter.rs index f61581e..c9a61d4 100644 --- a/src/autorouter/autorouter.rs +++ b/src/autorouter/autorouter.rs @@ -3,7 +3,7 @@ use petgraph::visit::{EdgeRef, IntoEdgeReferences}; use spade::InsertionError; use crate::{ - autorouter::{overlay::Overlay, ratsnest::RatsnestVertexIndex}, + autorouter::ratsnest::{Ratsnest, RatsnestVertexIndex}, drawing::rules::RulesTrait, layout::Layout, router::Router, @@ -11,14 +11,14 @@ use crate::{ }; pub struct Autorouter { - overlay: Overlay, + ratsnest: Ratsnest, router: Router, } impl Autorouter { pub fn new(layout: Layout) -> Result { Ok(Self { - overlay: Overlay::new(&layout)?, + ratsnest: Ratsnest::new(&layout)?, router: Router::new(layout), }) } @@ -27,23 +27,15 @@ impl Autorouter { //for ratline in self.overlay.ratsnest().graph().edge_references() { // For now, let's only take the first ratline. - let ratline = self - .overlay - .ratsnest() - .graph() - .edge_references() - .next() - .unwrap(); + let ratline = self.ratsnest.graph().edge_references().next().unwrap(); self.route( - self.overlay - .ratsnest() + self.ratsnest .graph() .node_weight(ratline.source()) .unwrap() .vertex_index(), - self.overlay - .ratsnest() + self.ratsnest .graph() .node_weight(ratline.target()) .unwrap() @@ -57,15 +49,6 @@ impl Autorouter { todo!(); } - // TODO: Move somewhere higher in abstraction. - pub fn click(&mut self, at: Point) { - self.overlay.click(self.router.layout(), at); - } - - pub fn overlay(&self) -> &Overlay { - &self.overlay - } - pub fn router(&self) -> &Router { &self.router } diff --git a/src/autorouter/mod.rs b/src/autorouter/mod.rs index fa4b03d..d9f3b2f 100644 --- a/src/autorouter/mod.rs +++ b/src/autorouter/mod.rs @@ -1,5 +1,4 @@ mod autorouter; -pub mod overlay; pub mod ratsnest; pub use autorouter::*; diff --git a/src/bin/topola-egui/app.rs b/src/bin/topola-egui/app.rs index 6152c7b..3076f83 100644 --- a/src/bin/topola-egui/app.rs +++ b/src/bin/topola-egui/app.rs @@ -7,7 +7,7 @@ use std::{ }; use topola::{ - autorouter::{overlay::Overlay, Autorouter}, + autorouter::Autorouter, drawing::{graph::MakePrimitive, primitive::MakePrimitiveShape, Drawing}, dsn::{design::DsnDesign, rules::DsnRules}, geometry::{ @@ -19,12 +19,15 @@ use topola::{ math::Circle, }; -use crate::painter::Painter; +use crate::{overlay::Overlay, painter::Painter}; /// Deserialize/Serialize is needed to persist app state between restarts. #[derive(serde::Deserialize, serde::Serialize)] #[serde(default)] pub struct App { + #[serde(skip)] + overlay: Option, + #[serde(skip)] autorouter: Option>, @@ -38,6 +41,7 @@ pub struct App { impl Default for App { fn default() -> Self { Self { + overlay: None, autorouter: None, text_channel: channel(), from_rect: egui::Rect::from_x_y_ranges(0.0..=1000000.0, 0.0..=500000.0), @@ -69,13 +73,15 @@ impl eframe::App for App { if let Ok(file_contents) = self.text_channel.1.try_recv() { let design = DsnDesign::load_from_string(file_contents).unwrap(); let layout = design.make_layout(); - self.autorouter = Some(Autorouter::new(design.make_layout()).unwrap()); + self.overlay = Some(Overlay::new(&layout).unwrap()); + self.autorouter = Some(Autorouter::new(layout).unwrap()); } } else { if let Ok(path) = self.text_channel.1.try_recv() { let design = DsnDesign::load_from_file(&path).unwrap(); let layout = design.make_layout(); - self.autorouter = Some(Autorouter::new(design.make_layout()).unwrap()); + self.overlay = Some(Overlay::new(&layout).unwrap()); + self.autorouter = Some(Autorouter::new(layout).unwrap()); } } @@ -143,9 +149,12 @@ impl eframe::App for App { let transform = egui::emath::RectTransform::from_to(self.from_rect, viewport_rect); let mut painter = Painter::new(ui, transform); - if let Some(ref mut autorouter) = &mut self.autorouter { + if let (Some(autorouter), Some(overlay)) = (&self.autorouter, &mut self.overlay) { if ctx.input(|i| i.pointer.any_click()) { - autorouter.click(point! {x: latest_pos.x as f64, y: -latest_pos.y as f64}); + overlay.click( + autorouter.router().layout(), + point! {x: latest_pos.x as f64, y: -latest_pos.y as f64}, + ); } for primitive in autorouter @@ -158,8 +167,7 @@ impl eframe::App for App { .primitive(autorouter.router().layout().drawing()) .shape(); - let color = if autorouter - .overlay() + let color = if overlay .selection() .contains(&GenericNode::Primitive(primitive)) { @@ -171,11 +179,7 @@ impl eframe::App for App { } for zone in autorouter.router().layout().layer_zones(1) { - let color = if autorouter - .overlay() - .selection() - .contains(&GenericNode::Compound(zone)) - { + let color = if overlay.selection().contains(&GenericNode::Compound(zone)) { egui::Color32::from_rgb(100, 100, 255) } else { egui::Color32::from_rgb(52, 52, 200) @@ -201,8 +205,7 @@ impl eframe::App for App { .primitive(autorouter.router().layout().drawing()) .shape(); - let color = if autorouter - .overlay() + let color = if overlay .selection() .contains(&GenericNode::Primitive(primitive)) { @@ -214,11 +217,7 @@ impl eframe::App for App { } for zone in autorouter.router().layout().layer_zones(0) { - let color = if autorouter - .overlay() - .selection() - .contains(&GenericNode::Compound(zone)) - { + let color = if overlay.selection().contains(&GenericNode::Compound(zone)) { egui::Color32::from_rgb(255, 100, 100) } else { egui::Color32::from_rgb(200, 52, 52) @@ -234,11 +233,19 @@ impl eframe::App for App { ) } - let ratsnest = autorouter.overlay().ratsnest().graph(); - - for ratline in ratsnest.edge_references() { - let from = ratsnest.node_weight(ratline.source()).unwrap().pos; - let to = ratsnest.node_weight(ratline.target()).unwrap().pos; + for edge in overlay.ratsnest().graph().edge_references() { + let from = overlay + .ratsnest() + .graph() + .node_weight(edge.source()) + .unwrap() + .pos; + let to = overlay + .ratsnest() + .graph() + .node_weight(edge.target()) + .unwrap() + .pos; painter.paint_edge(from, to, egui::Color32::from_rgb(90, 90, 200)); } diff --git a/src/bin/topola-egui/main.rs b/src/bin/topola-egui/main.rs index 5ea573a..6ccf968 100644 --- a/src/bin/topola-egui/main.rs +++ b/src/bin/topola-egui/main.rs @@ -1,6 +1,7 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release mod app; +mod overlay; mod painter; use app::App; diff --git a/src/autorouter/overlay.rs b/src/bin/topola-egui/overlay.rs similarity index 99% rename from src/autorouter/overlay.rs rename to src/bin/topola-egui/overlay.rs index c3a2ba9..e1f406c 100644 --- a/src/autorouter/overlay.rs +++ b/src/bin/topola-egui/overlay.rs @@ -4,7 +4,7 @@ use geo::Point; use rstar::AABB; use spade::InsertionError; -use crate::{ +use topola::{ autorouter::ratsnest::Ratsnest, drawing::{ graph::{GetLayer, MakePrimitive},