diff --git a/docs/simple_electronics.wasm b/docs/simple_electronics.wasm index 864654d..1f99f17 100755 Binary files a/docs/simple_electronics.wasm and b/docs/simple_electronics.wasm differ diff --git a/src/components/nodes.rs b/src/components/nodes.rs index ae84046..55f114d 100644 --- a/src/components/nodes.rs +++ b/src/components/nodes.rs @@ -102,6 +102,24 @@ impl Node<2, 1> for XnorNode { } } +#[macro_export] +macro_rules! all_nodes { + ($macro:ident) => { + $macro!( + [OnNode, 0, 1], + [OffNode, 0, 1], + [Wire, 1, 1], + [NotNode, 1, 1], + [AndNode, 2, 1], + [OrNode, 2, 1], + [NandNode, 2, 1], + [NorNode, 2, 1], + [XorNode, 2, 1], + [XnorNode, 2, 1], + ) + }; +} + pub fn add_node_systems<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> DispatcherBuilder<'a, 'b> { macro_rules! add_systems { ( $([$node:ident, $i:expr, $o:expr]),* $(,)? ) => { @@ -113,16 +131,5 @@ pub fn add_node_systems<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatche }; } - add_systems!( - [OnNode, 0, 1], - [OffNode, 0, 1], - [Wire, 1, 1], - [NotNode, 1, 1], - [AndNode, 2, 1], - [OrNode, 2, 1], - [NandNode, 2, 1], - [NorNode, 2, 1], - [XorNode, 2, 1], - [XnorNode, 2, 1], - ) + all_nodes!(add_systems) } diff --git a/src/systems.rs b/src/systems.rs index 1b2de82..1f47810 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -1,3 +1,4 @@ +pub mod cleanup_sys; pub mod draw_systems; pub mod place_node_sys; pub mod place_wire_sys; diff --git a/src/systems/cleanup_sys.rs b/src/systems/cleanup_sys.rs new file mode 100644 index 0000000..5259434 --- /dev/null +++ b/src/systems/cleanup_sys.rs @@ -0,0 +1,48 @@ +use crate::Connected; +use std::marker::PhantomData; + +use crate::components::Node; +use specs::prelude::*; + +#[derive(Default)] +pub struct CleanupWires +where + N: Node + 'static, +{ + node: PhantomData, +} +impl<'a, N, const I: usize, const O: usize> System<'a> for CleanupWires +where + N: Node + 'static, +{ + type SystemData = (WriteStorage<'a, Connected>, Entities<'a>); + + fn run(&mut self, (mut nodes, entities): Self::SystemData) { + (&mut nodes).join().for_each(|node| { + node.inputs + .iter_mut() + .chain(node.outputs.iter_mut()) + .filter(|wire_opt| wire_opt.is_some()) + .for_each(|o: &mut Option| { + let wire_e = o.unwrap(); + dbg!(wire_e); + if !entities.is_alive(wire_e) { + *o = None; + } + }); + }); + } +} + +pub fn run_cleanup_sys(world: &mut World) { + use crate::nodes::*; + + macro_rules! cleanup_nodes { + ( $([$node:ident, $i:expr, $o:expr]),*, $(,)? ) => { + $(CleanupWires::<$node, $i, $o>::default().run_now(world);)* + }; + } + + use crate::all_nodes; + all_nodes!(cleanup_nodes); +} diff --git a/src/ui/mouse_click.rs b/src/ui/mouse_click.rs index fda2fb3..e715244 100644 --- a/src/ui/mouse_click.rs +++ b/src/ui/mouse_click.rs @@ -8,7 +8,19 @@ use crate::nodes; use crate::resources; pub fn handle_mouse_click(world: &mut World) { - world.insert(resources::AddingWire(None)); + // clear adding wire including removing the wire entity + { + let adding_wire_state = world.fetch::().0; + if let Some((_, wire_entity, _, _)) = adding_wire_state { + dbg!(wire_entity); + world.delete_entity(wire_entity).unwrap(); + + crate::systems::cleanup_sys::run_cleanup_sys(world); + + std::mem::drop(adding_wire_state); + world.insert(AddingWire(None)); + } + } let node = world.fetch::(); if let resources::AddingNode(Some(n)) = &*node { @@ -24,18 +36,8 @@ pub fn handle_mouse_click(world: &mut World) { }; } - place_node_systems!( - [OnNode, 0, 1], - [OffNode, 0, 1], - [Wire, 1, 1], - [NotNode, 1, 1], - [AndNode, 2, 1], - [OrNode, 2, 1], - [NandNode, 2, 1], - [NorNode, 2, 1], - [XorNode, 2, 1], - [XnorNode, 2, 1] - ); + use crate::all_nodes; + all_nodes!(place_node_systems); std::mem::drop(node); world.insert(resources::AddingNode(None)); @@ -73,18 +75,8 @@ pub fn handle_mouse_right_click(world: &mut World) { }; } - place_wire_systems!( - [OnNode, 0, 1], - [OffNode, 0, 1], - [Wire, 1, 1], - [NotNode, 1, 1], - [AndNode, 2, 1], - [OrNode, 2, 1], - [NandNode, 2, 1], - [NorNode, 2, 1], - [XorNode, 2, 1], - [XnorNode, 2, 1] - ); + use crate::all_nodes; + all_nodes!(place_wire_systems); } }; }