Skip to content

Commit

Permalink
Add pin / crossover toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
agausmann committed May 2, 2021
1 parent 6b6465f commit 29c199c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ impl Circuit {
self.insert_pin(position);
}

pub fn delete_pin(&mut self, position: IVec2) {
if let Some(&tile) = self.tiles.get(&position) {
if let Some(pin_id) = tile.pin {
self.remove_pin(pin_id);
}
let mut endpoints = Vec::new();
for &wire_id in tile.wires.iter().flatten() {
let wire = self.remove_wire(wire_id);
if wire.start == position {
endpoints.push(wire.end);
} else if wire.end == position {
endpoints.push(wire.start);
} else {
panic!("wire is not connected to this tile");
}
}
for i in 1..endpoints.len() {
for j in 0..i {
if endpoints[i].x == endpoints[j].x
|| endpoints[i].y == endpoints[j].y
{
self.insert_wire(endpoints[i], endpoints[j]);
}
}
}
}
}

pub fn delete_all_at(&mut self, position: IVec2) {
if let Some(&tile) = self.tiles.get(&position) {
if let Some(pin_id) = tile.pin {
Expand Down
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,16 @@ impl State {
..
} => {
if start_position == end_position {
self.circuit.place_pin(start_position);
if self
.circuit
.tile(start_position)
.and_then(|tile| tile.pin)
.is_some()
{
self.circuit.delete_pin(start_position);
} else {
self.circuit.place_pin(start_position);
}
} else {
self.circuit.place_wire(
start_position,
Expand Down

0 comments on commit 29c199c

Please sign in to comment.