summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-08-18 21:43:08 +0300
committeromagdy7 <omar.professional8777@gmail.com>2023-08-18 21:43:08 +0300
commit1e5dc994b7c7317a8c5be5c600dcd2dc90b0ac07 (patch)
tree12b82d91929e430b22ac08dedecb36fa19af466d /src
parent10bd1676edd9417c8ec8466e585c13bd109d687c (diff)
downloadlgsim-1e5dc994b7c7317a8c5be5c600dcd2dc90b0ac07.tar.xz
lgsim-1e5dc994b7c7317a8c5be5c600dcd2dc90b0ac07.zip
Cleaned the code + added output_layer for displaying outputs
Diffstat (limited to 'src')
-rw-r--r--src/gate.rs56
-rw-r--r--src/main.rs12
-rw-r--r--src/output_layout.rs26
-rw-r--r--src/wire.rs7
4 files changed, 70 insertions, 31 deletions
diff --git a/src/gate.rs b/src/gate.rs
index 2b2952c..1d869e1 100644
--- a/src/gate.rs
+++ b/src/gate.rs
@@ -1,3 +1,4 @@
+use crate::wire::*;
use macroquad::prelude::*;
pub struct Rec {
@@ -19,7 +20,8 @@ enum State {
#[derive(Debug, Clone)]
pub struct Connection {
- dst: Option<Box<Connection>>,
+ wire: Option<Box<Wire>>,
+ pub pos: (f32, f32),
pub radius: f32,
pub color: Color,
pub on: bool,
@@ -33,17 +35,31 @@ impl Rec {
fn contains(&self, pos: (f32, f32)) -> bool {
pos.0 >= self.x && pos.0 <= self.x + self.w && pos.1 >= self.y && pos.1 <= self.y + self.h
}
+
+ fn draw(&self) {
+ draw_rectangle(self.x, self.y, self.w, self.h, self.color);
+ }
}
impl Connection {
- pub fn new(radius: f32, color: Color, on: bool) -> Connection {
+ pub fn new(radius: f32, pos: (f32, f32), color: Color, on: bool) -> Connection {
Connection {
- dst: None,
+ wire: None,
+ pos,
radius,
color,
on,
}
}
+
+ pub fn draw(&self) {
+ draw_circle(
+ self.pos.0,
+ self.pos.1,
+ self.radius,
+ if self.on { self.color } else { GRAY },
+ );
+ }
}
pub struct Gate<'a> {
@@ -92,23 +108,16 @@ impl<'a> Gate<'a> {
}
fn draw_helper(&mut self) {
- draw_rectangle(
- self.rect.x,
- self.rect.y,
- self.rect.w,
- self.rect.h,
- self.rect.color,
- );
+ // draws the structure of the gate
+ self.rect.draw();
- for (cnt, node) in self.input.iter().enumerate() {
- draw_circle(
- self.rect.x,
- self.rect.y + 25. + (30. * cnt as f32),
- node.radius,
- if node.on { node.color } else { GRAY },
- );
+ // draws input
+ for (cnt, node) in self.input.iter_mut().enumerate() {
+ node.pos = (self.rect.x, self.rect.y + 25. + (30. * cnt as f32));
+ node.draw();
}
+ // draws label
draw_text(
self.label,
self.rect.x + self.rect.w / 2.0,
@@ -117,15 +126,8 @@ impl<'a> Gate<'a> {
WHITE,
);
- draw_circle(
- self.rect.x + self.rect.w,
- self.rect.y + self.rect.h / 2.,
- self.output.radius,
- if self.output.on {
- self.output.color
- } else {
- GRAY
- },
- );
+ // draws output
+ self.output.pos = (self.rect.x + self.rect.w, self.rect.y + self.rect.h / 2.);
+ self.output.draw();
}
}
diff --git a/src/main.rs b/src/main.rs
index 3e1b2e7..6b95a83 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,19 +1,24 @@
use macroquad::prelude::*;
mod gate;
mod input_layout;
+mod output_layout;
+mod wire;
use gate::*;
use input_layout::*;
+use output_layout::*;
+use wire::*;
#[macroquad::main("lgsim")]
async fn main() {
let mut gate = Gate::new(
Rec::new(500., 500., 120., 80., RED),
- vec![Connection::new(10., GREEN, false); 2],
+ vec![Connection::new(10., (0.0, 0.0), GREEN, false); 2],
"GATE",
- Connection::new(10., GREEN, false),
+ Connection::new(10., (0.0, 0.0), GREEN, false),
);
- let input_layout = InputLayout::new(vec![Connection::new(10., GREEN, false); 16]);
+ let input_layout = InputLayout::new(vec![Connection::new(10., (0.0, 0.0), GREEN, false); 16]);
+ let output_layout = OutputLayout::new(vec![Connection::new(10., (0.0, 0.0), GREEN, false); 16]);
loop {
let playground_offset_x: f32 = screen_width() / 20.;
@@ -35,6 +40,7 @@ async fn main() {
// Draw things before egui
input_layout.draw();
+ output_layout.draw();
gate.draw();
// egui_macroquad::draw();
diff --git a/src/output_layout.rs b/src/output_layout.rs
index 0c7101b..efe4875 100644
--- a/src/output_layout.rs
+++ b/src/output_layout.rs
@@ -1 +1,25 @@
-struct OutputLayout {}
+use crate::gate::*;
+use macroquad::prelude::*;
+
+pub struct OutputLayout {
+ outputs: Vec<Connection>,
+}
+
+impl OutputLayout {
+ pub fn new(outputs: Vec<Connection>) -> Self {
+ OutputLayout { outputs }
+ }
+ pub fn draw(&self) {
+ let playground_offset_x: f32 = screen_width() / 20.;
+ let playground_offset_y: f32 = screen_height() / 14.;
+ let playground_height: f32 = screen_height() - playground_offset_y * 2.0;
+ for (cnt, node) in self.outputs.iter().enumerate() {
+ draw_circle(
+ screen_width() - playground_offset_x,
+ playground_height / 3.0 + 25. + (30. * cnt as f32),
+ node.radius,
+ if node.on { node.color } else { GRAY },
+ );
+ }
+ }
+}
diff --git a/src/wire.rs b/src/wire.rs
index e69de29..70e2a2c 100644
--- a/src/wire.rs
+++ b/src/wire.rs
@@ -0,0 +1,7 @@
+use crate::gate::*;
+
+#[derive(Clone, Debug)]
+pub struct Wire {
+ src: Connection,
+ dst: Connection,
+}