Skip to content

Commit 0610655

Browse files
committed
More snapping logic
1 parent cdf8064 commit 0610655

File tree

1 file changed

+93
-40
lines changed

1 file changed

+93
-40
lines changed

src/app.rs

Lines changed: 93 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use egui::{
44
Align, Button, Color32, Image, Layout, Pos2, Rect, Sense, Stroke, Ui, Vec2, Widget, pos2, vec2,
55
};
66

7-
use crate::{assets, config::CanvasConfig};
7+
use crate::{
8+
assets::{self, PinInfo},
9+
config::CanvasConfig,
10+
};
811

912
// TODO Direction is not used anymore. I can calculate it from current positions?
1013
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Copy, Default, Eq, PartialEq)]
@@ -78,11 +81,13 @@ pub struct GateInstance {
7881
pos: Pos2,
7982
}
8083

81-
impl GateKind {
82-
fn graphics(&self) -> &assets::InstanceGraphics {
83-
match self {
84-
Self::Nand => &assets::NAND_GRAPHICS,
84+
impl GateInstance {
85+
fn pins(&self) -> Vec<Pos2> {
86+
let mut pins_pos = Vec::new();
87+
for pin in self.kind.graphics().pins {
88+
pins_pos.push(self.pos + pin.offset);
8589
}
90+
pins_pos
8691
}
8792
}
8893

@@ -91,6 +96,18 @@ pub enum GateKind {
9196
Nand,
9297
}
9398

99+
impl GateKind {
100+
fn graphics(&self) -> &assets::InstanceGraphics {
101+
match self {
102+
Self::Nand => &assets::NAND_GRAPHICS,
103+
}
104+
}
105+
106+
fn pins(&self) -> &[PinInfo] {
107+
self.graphics().pins
108+
}
109+
}
110+
94111
#[derive(serde::Deserialize, serde::Serialize, Copy, Debug, Clone)]
95112
pub struct WireInstance {
96113
pub start: Pos2,
@@ -245,17 +262,18 @@ impl TemplateApp {
245262
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
246263
// Register all supported image loaders
247264
egui_extras::install_image_loaders(&cc.egui_ctx);
248-
if let Some(storage) = cc.storage {
249-
eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default()
250-
} else {
251-
Default::default()
252-
}
265+
// if let Some(storage) = cc.storage {
266+
// eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default()
267+
// } else {
268+
// Default::default()
269+
// }
270+
Default::default()
253271
}
254272

255273
pub fn main_layout(&mut self, ui: &mut Ui) {
256-
egui::Window::new("Debug logs").show(ui.ctx(), |ui| {
257-
egui_logger::logger_ui().show(ui);
258-
});
274+
// egui::Window::new("Debug logs").show(ui.ctx(), |ui| {
275+
// egui_logger::logger_ui().show(ui);
276+
// });
259277
ui.with_layout(Layout::left_to_right(Align::Min), |ui| {
260278
self.canvas_config = CanvasConfig::default();
261279
ui.add(egui::TextEdit::multiline(&mut format!(
@@ -320,6 +338,7 @@ impl TemplateApp {
320338
let mut mutations: Vec<MoveMutation> = Vec::new();
321339

322340
// handle dragging from panel
341+
// TODO probably should add panel_drag to the world so rendering is easier
323342
if let Some(panel_drag) = &self.panel_drag {
324343
if inside_rect(&canvas_rect, &panel_drag.ty) {
325344
log::debug!("drag inside rect");
@@ -454,40 +473,74 @@ impl TemplateApp {
454473
}
455474
let mut new_connections: HashSet<Connection> = HashSet::new();
456475

476+
let threshold = 10.0;
477+
// TODO: Only need to check this on placement and moving.
478+
// Also use a better way than iterating on everything.
457479
for instance in self.instances.iter() {
458480
match instance.ty {
459-
InstanceType::Gate(gate) => {
481+
InstanceType::Gate(self_gate) => {
460482
for other in self.instances.iter() {
461-
if let InstanceType::Wire(wire_instance) = other.ty {
462-
for pin in gate.kind.graphics().pins {
463-
let pin_pos = gate.pos + pin.offset;
464-
let d_start = wire_instance.start.distance(pin_pos);
465-
let d_end = wire_instance.end.distance(pin_pos);
466-
let threshold = 10.0;
467-
if d_start < threshold {
468-
ui.painter()
469-
.circle_filled(pin_pos, 10.0, Color32::LIGHT_BLUE);
470-
new_connections.insert(Connection::new(instance.id, other.id));
471-
mutations.push(MoveMutation {
472-
ins: other.id,
473-
old_pos: wire_instance.start,
474-
new_pos: pin_pos,
475-
});
476-
} else if d_end < threshold {
477-
ui.painter()
478-
.circle_filled(pin_pos, 10.0, Color32::LIGHT_BLUE);
479-
new_connections.insert(Connection::new(instance.id, other.id));
480-
mutations.push(MoveMutation {
481-
ins: other.id,
482-
old_pos: wire_instance.end,
483-
new_pos: pin_pos,
484-
});
483+
if other.id == instance.id {
484+
continue;
485+
}
486+
match other.ty {
487+
InstanceType::Wire(other_wire) => {
488+
for pin_pos in self_gate.pins() {
489+
let d_start = other_wire.start.distance(pin_pos);
490+
let d_end = other_wire.end.distance(pin_pos);
491+
if d_start < threshold {
492+
ui.painter().circle_filled(
493+
pin_pos,
494+
10.0,
495+
Color32::LIGHT_BLUE,
496+
);
497+
new_connections
498+
.insert(Connection::new(instance.id, other.id));
499+
mutations.push(MoveMutation {
500+
ins: other.id,
501+
old_pos: other_wire.start,
502+
new_pos: pin_pos,
503+
});
504+
} else if d_end < threshold {
505+
ui.painter().circle_filled(
506+
pin_pos,
507+
10.0,
508+
Color32::LIGHT_BLUE,
509+
);
510+
new_connections
511+
.insert(Connection::new(instance.id, other.id));
512+
mutations.push(MoveMutation {
513+
ins: other.id,
514+
old_pos: other_wire.end,
515+
new_pos: pin_pos,
516+
});
517+
}
518+
}
519+
}
520+
// TODO: Right now once self is moved to other and once other is moved
521+
// to self. This logic is not correct
522+
InstanceType::Gate(other_gate) => {
523+
for self_pin in self_gate.pins() {
524+
for other_pin in other_gate.pins() {
525+
if self_pin.distance(other_pin) < threshold {
526+
ui.painter().circle_filled(
527+
self_pin,
528+
10.0,
529+
Color32::LIGHT_BLUE,
530+
);
531+
mutations.push(MoveMutation {
532+
ins: other.id,
533+
old_pos: other_pin,
534+
new_pos: self_pin,
535+
});
536+
}
537+
}
485538
}
486539
}
487540
}
488541
}
489542
}
490-
InstanceType::Wire(_) => {}
543+
InstanceType::Wire(wire_instance) => {}
491544
}
492545
}
493546

@@ -497,7 +550,7 @@ impl TemplateApp {
497550
let instance = self.get_instance_mut(mutation.ins);
498551
match &mut instance.ty {
499552
InstanceType::Gate(gate_instance) => {
500-
// TODO implement gates moving to snap
553+
gate_instance.pos += mutation.new_pos - mutation.old_pos
501554
}
502555
InstanceType::Wire(wire_instance) => {
503556
if wire_instance.start == mutation.old_pos {

0 commit comments

Comments
 (0)