diff --git a/docs/simple_electronics.wasm b/docs/simple_electronics.wasm index 5bb0b30..a64a518 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 5179dcc..aad2ff6 100644 --- a/src/components/nodes.rs +++ b/src/components/nodes.rs @@ -38,10 +38,6 @@ impl Node<0, 1> for OnNode { fn calculate_state(&self, _: [bool; 0]) -> [bool; 1] { [true] } - - fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] - } } #[derive(Default)] @@ -50,10 +46,6 @@ impl Node<0, 1> for OffNode { fn calculate_state(&self, _: [bool; 0]) -> [bool; 1] { [false] } - - fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] - } } #[derive(Default)] @@ -68,7 +60,7 @@ impl Node<1, 1> for NotNode { } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(25.0, 0.0)] + [Vec2::new(22.5, 0.0)] } } @@ -80,11 +72,11 @@ impl Node<2, 1> for AndNode { } fn input_offsets() -> [Vec2; 2] { - [Vec2::new(-30.0, -20.0), Vec2::new(-30.0, 20.0)] + [Vec2::new(-35.0, -20.0), Vec2::new(-35.0, 20.0)] } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] + [Vec2::new(35.0, 0.0)] } } @@ -96,11 +88,11 @@ impl Node<2, 1> for OrNode { } fn input_offsets() -> [Vec2; 2] { - [Vec2::new(-30.0, -20.0), Vec2::new(-30.0, 20.0)] + [Vec2::new(-35.0, -20.0), Vec2::new(-35.0, 20.0)] } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] + [Vec2::new(35.0, 0.0)] } } @@ -112,11 +104,11 @@ impl Node<2, 1> for NandNode { } fn input_offsets() -> [Vec2; 2] { - [Vec2::new(-30.0, -20.0), Vec2::new(-30.0, 20.0)] + [Vec2::new(-35.0, -20.0), Vec2::new(-35.0, 20.0)] } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] + [Vec2::new(35.0, 0.0)] } } @@ -128,11 +120,11 @@ impl Node<2, 1> for NorNode { } fn input_offsets() -> [Vec2; 2] { - [Vec2::new(-30.0, -20.0), Vec2::new(-30.0, 20.0)] + [Vec2::new(-35.0, -20.0), Vec2::new(-35.0, 20.0)] } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] + [Vec2::new(35.0, 0.0)] } } @@ -144,11 +136,11 @@ impl Node<2, 1> for XorNode { } fn input_offsets() -> [Vec2; 2] { - [Vec2::new(-30.0, -20.0), Vec2::new(-30.0, 20.0)] + [Vec2::new(-35.0, -20.0), Vec2::new(-35.0, 20.0)] } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] + [Vec2::new(35.0, 0.0)] } } @@ -160,11 +152,11 @@ impl Node<2, 1> for XnorNode { } fn input_offsets() -> [Vec2; 2] { - [Vec2::new(-30.0, -20.0), Vec2::new(-30.0, 20.0)] + [Vec2::new(-35.0, -20.0), Vec2::new(-35.0, 20.0)] } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] + [Vec2::new(35.0, 0.0)] } } @@ -179,7 +171,7 @@ impl Node<0, 1> for SwitchNode { } fn output_offsets() -> [Vec2; 1] { - [Vec2::new(30.0, 0.0)] + [Vec2::new(35.0, 0.0)] } } diff --git a/src/systems/draw_systems.rs b/src/systems/draw_systems.rs index 1b0452d..830cc78 100644 --- a/src/systems/draw_systems.rs +++ b/src/systems/draw_systems.rs @@ -25,7 +25,6 @@ where { node: PhantomData, draw_fn: Arc, - input_offsets: [Vec2; I], } impl<'a, N, const I: usize, const O: usize> System<'a> for DrawNodeSys @@ -45,6 +44,9 @@ where &mut self, (positions, nodes, connections, wires, tick_progress, textures): Self::SystemData, ) { + let input_offsets = N::input_offsets(); + let output_offsets = N::output_offsets(); + (&positions, &nodes).join().for_each(|(self_pos, node)| { let pos = self_pos.pos; node.inputs @@ -56,7 +58,7 @@ where let wire = wires.get(e).unwrap(); let sp = *wire_pos; - let ep = Vec2::new(pos.x, pos.y) + self.input_offsets[i]; + let ep = Vec2::new(pos.x, pos.y) + input_offsets[i]; if wire.output_state != wire.input_state { let delta = ((tick_progress.0 - 0.5) * 2.0).clamp(0.0, 1.0) as f32; @@ -284,9 +286,22 @@ impl<'a> System<'a> for DrawConnectionSys { type SystemData = (ReadStorage<'a, Connection>, ReadStorage<'a, Pos>); fn run(&mut self, (connections, positions): Self::SystemData) { + let mouse_pos = { + let (mx, my) = mouse_position(); + Vec2::new(mx, my) + }; + + let color = |pos: Vec2| { + if (pos - mouse_pos).length() > 10.0 { + Color::from_rgba(180, 180, 180, 215) + } else { + DARKGRAY + } + }; + (&connections, &positions) .join() - .for_each(|(_, Pos { pos, .. })| draw_circle(pos.x, pos.y, 10.0, LIGHTGRAY)); + .for_each(|(_, Pos { pos, .. })| draw_circle(pos.x, pos.y, 10.0, color(*pos))); } } @@ -372,14 +387,12 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher draw_fn: Arc::new(|_, Pos { pos, .. }, _| { draw_circle(pos.x, pos.y, 25.0, RED); }), - input_offsets: [], }) .with_thread_local(DrawNodeSys { node: PhantomData::, draw_fn: Arc::new(|_, Pos { pos, .. }, _| { draw_circle(pos.x, pos.y, 25.0, WHITE); }), - input_offsets: [], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -398,7 +411,6 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher }, ); }), - input_offsets: [Vec2::new(-25.0, 0.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -417,7 +429,6 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher }, ); }), - input_offsets: [Vec2::new(-25.0, -15.0), Vec2::new(-25.0, 15.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -436,7 +447,6 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher }, ); }), - input_offsets: [Vec2::new(-25.0, -15.0), Vec2::new(-25.0, 15.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -455,7 +465,6 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher }, ); }), - input_offsets: [Vec2::new(-25.0, -15.0), Vec2::new(-25.0, 15.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -474,7 +483,6 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher }, ); }), - input_offsets: [Vec2::new(-25.0, -15.0), Vec2::new(-25.0, 15.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -493,7 +501,6 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher }, ); }), - input_offsets: [Vec2::new(-26.0, -15.0), Vec2::new(-26.0, 15.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -512,14 +519,12 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher }, ); }), - input_offsets: [Vec2::new(-26.5, -15.0), Vec2::new(-26.5, 15.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, draw_fn: Arc::new(|_, Pos { pos, .. }, _: &Textures| { draw_circle(pos.x, pos.y, 10.0, WHITE); }), - input_offsets: [Vec2::new(0.0, 0.0)], }) .with_thread_local(DrawNodeSys { node: PhantomData::, @@ -529,7 +534,6 @@ pub fn add_draw_system<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatcher draw_circle(pos.x, pos.y, 25.0, color); draw_circle_lines(pos.x, pos.y, 25.0, 2.5, BLACK); }), - input_offsets: [], }) .with_thread_local(DrawConnectionSys) }