diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-08-10 20:45:56 +0300 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-08-10 20:45:56 +0300 |
| commit | 7cdd18e6b03711c5e70874a092b94affc8b7d586 (patch) | |
| tree | 4e2be890f1fb32143f1a9981d466d9afd3c2ae35 /src | |
| download | lgsim-7cdd18e6b03711c5e70874a092b94affc8b7d586.tar.xz lgsim-7cdd18e6b03711c5e70874a092b94affc8b7d586.zip | |
Intial commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/gate.rs | 111 | ||||
| -rw-r--r-- | src/main.rs | 38 |
2 files changed, 149 insertions, 0 deletions
diff --git a/src/gate.rs b/src/gate.rs new file mode 100644 index 0000000..bb27a6d --- /dev/null +++ b/src/gate.rs @@ -0,0 +1,111 @@ +use macroquad::prelude::*; + +pub struct Rec { + x: f32, + y: f32, + w: f32, + h: f32, + color: Color, +} + +#[derive(Default, Debug, PartialEq)] +enum State { + #[default] + Idle, + DragStart, + Dragging, + DragStop, +} + +pub struct Connection { + radius: f32, + color: Color, +} + +impl Rec { + pub fn new(x: f32, y: f32, w: f32, h: f32, color: Color) -> Self { + Rec { x, y, w, h, color } + } + + 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 + } +} + +impl Connection { + pub fn new(radius: f32, color: Color) -> Connection { + Connection { radius, color } + } +} + +pub struct Gate { + rect: Rec, + input: Vec<Connection>, + output: Connection, + state: State, +} + +impl Gate { + pub fn new(rect: Rec, input: Vec<Connection>, output: Connection) -> Gate { + Gate { + rect, + input, + output, + state: State::default(), + } + } + + pub fn draw(&mut self) { + if is_mouse_button_pressed(MouseButton::Left) && self.rect.contains(mouse_position()) { + self.state = State::DragStart; + } else if is_mouse_button_down(MouseButton::Left) + && self.rect.contains(mouse_position()) + && self.state == State::DragStart + || self.state == State::Dragging + { + self.state = State::Dragging; + } + if is_mouse_button_released(MouseButton::Left) { + self.state = State::DragStop; + } + + println!("{:?}", self.state); + + match self.state { + State::Dragging => { + self.rect.x = mouse_position().0; + self.rect.y = mouse_position().1; + self.draw_helper(); + } + _ => { + self.draw_helper(); + } + } + } + + fn draw_helper(&mut self) { + draw_rectangle( + self.rect.x, + self.rect.y, + self.rect.w, + self.rect.h, + self.rect.color, + ); + + for (cnt, node) in self.input.iter().enumerate() { + draw_circle( + self.rect.x, + self.rect.y + 25. + (30. * cnt as f32), + node.radius, + node.color, + ); + } + + draw_circle( + self.rect.x + self.rect.w, + self.rect.y + self.rect.h / 2., + self.output.radius, + self.output.color, + ); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1759a20 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,38 @@ +use macroquad::prelude::*; +mod gate; +use gate::*; + +#[macroquad::main("egui with macroquad")] +async fn main() { + let mut gate = Gate::new( + Rec::new(500., 500., 120., 80., RED), + vec![Connection::new(10., GOLD), Connection::new(10., GOLD)], + Connection::new(10., GOLD), + ); + + loop { + clear_background(color_u8!(27, 27, 27, 255)); + + // Process keys, mouse etc. + + egui_macroquad::ui(|egui_ctx| { + egui::Window::new("egui ❤ macroquad").show(egui_ctx, |ui| { + ui.label("Test"); + }); + }); + + // Draw things before egui + + // let gate2 = Gate::new( + // Rec::new(700., 700., 120., 80., RED), + // vec![Connection::new(10., GOLD), Connection::new(10., GOLD)], + // ); + gate.draw(); + // gate2.draw(); + egui_macroquad::draw(); + + // Draw things after egui + + next_frame().await; + } +} |
