Skip to content

Commit

Permalink
improved connection drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhan45 committed Apr 11, 2021
1 parent 10895d0 commit 195374a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
Binary file modified docs/simple_electronics.wasm
Binary file not shown.
36 changes: 14 additions & 22 deletions src/components/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
}
}

Expand All @@ -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)]
}
}

Expand All @@ -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)]
}
}

Expand All @@ -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)]
}
}

Expand All @@ -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)]
}
}

Expand All @@ -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)]
}
}

Expand All @@ -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)]
}
}

Expand All @@ -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)]
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/systems/draw_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ where
{
node: PhantomData<N>,
draw_fn: Arc<dyn Fn(&N, Pos, &Textures)>,
input_offsets: [Vec2; I],
}

impl<'a, N, const I: usize, const O: usize> System<'a> for DrawNodeSys<N, I, O>
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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)));
}
}

Expand Down Expand Up @@ -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::<OffNode>,
draw_fn: Arc::new(|_, Pos { pos, .. }, _| {
draw_circle(pos.x, pos.y, 25.0, WHITE);
}),
input_offsets: [],
})
.with_thread_local(DrawNodeSys {
node: PhantomData::<NotNode>,
Expand All @@ -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::<AndNode>,
Expand All @@ -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::<OrNode>,
Expand All @@ -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::<NandNode>,
Expand All @@ -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::<NorNode>,
Expand All @@ -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::<XorNode>,
Expand All @@ -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::<XnorNode>,
Expand All @@ -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::<Wire>,
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::<SwitchNode>,
Expand All @@ -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)
}

0 comments on commit 195374a

Please sign in to comment.