autorouter: put autorouter in new `Invoker` object for Command pattern

This commit is contained in:
Mikolaj Wielgus 2024-05-13 03:20:57 +02:00
parent d9ff08477c
commit 7bcb3926d4
4 changed files with 79 additions and 9 deletions

View File

@ -161,6 +161,10 @@ impl<R: RulesTrait> Autorouter<R> {
)
}
pub fn undo_autoroute(&mut self, selection: &Selection) {
todo!();
}
pub fn layout(&self) -> &Arc<Mutex<Layout<R>>> {
&self.layout
}

59
src/autorouter/invoker.rs Normal file
View File

@ -0,0 +1,59 @@
use crate::{
autorouter::{selection::Selection, Autoroute, Autorouter},
drawing::rules::RulesTrait,
router::RouterObserverTrait,
};
pub enum Command {
Autoroute(Selection),
}
pub enum Execute {
Autoroute(Autoroute),
}
impl Execute {
pub fn next<R: RulesTrait>(
&mut self,
invoker: &mut Invoker<R>,
observer: &mut impl RouterObserverTrait<R>,
) -> bool {
match self {
Execute::Autoroute(autoroute) => autoroute.next(&mut invoker.autorouter, observer),
}
}
}
pub struct Invoker<R: RulesTrait> {
autorouter: Autorouter<R>,
}
impl<R: RulesTrait> Invoker<R> {
pub fn new(autorouter: Autorouter<R>) -> Self {
Self { autorouter }
}
pub fn execute(&mut self, command: &Command, observer: &mut impl RouterObserverTrait<R>) {
let mut execute = self.execute_walk(command);
while execute.next(self, observer) {
//
}
}
pub fn execute_walk(&mut self, command: &Command) -> Execute {
match command {
Command::Autoroute(selection) => {
Execute::Autoroute(self.autorouter.autoroute_walk(&selection).unwrap())
}
}
}
pub fn undo(&mut self) {
todo!();
}
pub fn redo(&mut self) {
todo!();
}
}

View File

@ -1,4 +1,5 @@
mod autorouter;
pub mod invoker;
pub mod ratsnest;
pub mod selection;

View File

@ -10,7 +10,10 @@ use std::{
};
use topola::{
autorouter::Autorouter,
autorouter::{
invoker::{Command, Execute, Invoker},
Autorouter,
},
drawing::{
dot::FixedDotIndex,
graph::{MakePrimitive, PrimitiveIndex},
@ -202,9 +205,10 @@ impl eframe::App for App {
let selection = overlay.selection().clone();
execute(async move {
let mut autorouter = Autorouter::new(layout).unwrap();
let mut invoker = Invoker::new(Autorouter::new(layout).unwrap());
let mut execute = invoker.execute_walk(&Command::Autoroute(selection));
if let Some(mut autoroute) = autorouter.autoroute_walk(&selection) {
if let Execute::Autoroute(ref mut autoroute) = execute {
let from = autoroute.navmesh().as_ref().unwrap().from();
let to = autoroute.navmesh().as_ref().unwrap().to();
@ -214,13 +218,15 @@ impl eframe::App for App {
shared_data.to = Some(to);
shared_data.navmesh = autoroute.navmesh().clone();
}
}
while autoroute.next(
&mut autorouter,
&mut DebugRouterObserver {
shared_data: shared_data_arc_mutex.clone(),
},
) {
while execute.next(
&mut invoker,
&mut DebugRouterObserver {
shared_data: shared_data_arc_mutex.clone(),
},
) {
if let Execute::Autoroute(ref mut autoroute) = execute {
shared_data_arc_mutex.lock().unwrap().navmesh =
autoroute.navmesh().clone();
}