Skip to content

Commit 37758ed

Browse files
committed
Don't simulate on clicks
1 parent 2941923 commit 37758ed

3 files changed

Lines changed: 38 additions & 64 deletions

File tree

src/app.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,10 @@ impl DB {
924924
}
925925
}
926926

927+
pub fn current_dirty() -> bool {
928+
true
929+
}
930+
927931
#[derive(serde::Deserialize, serde::Serialize)]
928932
pub struct App {
929933
pub canvas_config: CanvasConfig,
@@ -936,7 +940,7 @@ pub struct App {
936940
// possible connections while dragging
937941
pub potential_connections: HashSet<Connection>,
938942
// mark when current needs recomputation
939-
#[serde(skip)]
943+
#[serde(skip, default = "current_dirty")]
940944
pub current_dirty: bool,
941945
pub show_debug: bool,
942946
// selection set and move preview
@@ -1403,7 +1407,7 @@ impl App {
14031407
self.handle_drag_end(mouse);
14041408

14051409
if self.connection_manager.update_connections(&mut self.db) {
1406-
self.current_dirty = true;
1410+
// self.current_dirty = true;
14071411
}
14081412
if !drag_had_movement {
14091413
self.selected.clear();
@@ -1886,7 +1890,7 @@ impl App {
18861890
} => {
18871891
return Some(Hover::Instance(original_wire_id));
18881892
}
1889-
Drag::Panel { .. } | Drag::Selecting { .. } | Drag::Label { .. } => {}
1893+
Drag::Selecting { .. } | Drag::Label { .. } => {}
18901894
}
18911895
}
18921896
for selected in &self.selected {
@@ -2155,6 +2159,7 @@ impl App {
21552159

21562160
// Simulation status
21572161
writeln!(out, "\n=== Simulation Status ===").ok();
2162+
writeln!(out, "needs update {}", self.current_dirty).ok();
21582163
match self.simulator.status {
21592164
SimulationStatus::Stable { iterations } => {
21602165
writeln!(out, "Status: STABLE (after {iterations} iterations)").ok();

src/drag.rs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ pub enum CanvasDrag {
2222

2323
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Copy)]
2424
pub enum Drag {
25-
Panel {
26-
pos: Pos2,
27-
kind: InstanceKind,
28-
},
2925
Canvas(CanvasDrag),
3026
Label {
3127
id: LabelId,
@@ -141,17 +137,6 @@ impl App {
141137

142138
pub fn handle_dragging(&mut self, ui: &mut Ui, mouse: Pos2) {
143139
match self.drag {
144-
Some(Drag::Panel { pos: _, kind }) => match kind {
145-
InstanceKind::Gate(gate_kind) => self.draw_gate_preview(ui, gate_kind, mouse),
146-
InstanceKind::Power => self.draw_power_preview(ui, mouse),
147-
InstanceKind::Wire => {
148-
self.draw_wire(ui, &Wire::new_at(mouse), false, false);
149-
}
150-
InstanceKind::Lamp => self.draw_lamp_preview(ui, mouse),
151-
InstanceKind::CustomCircuit(def) => {
152-
self.draw_custom_circuit_preview(ui, def, mouse);
153-
}
154-
},
155140
Some(Drag::Selecting { start }) => {
156141
let start_screen = start - self.viewport_offset;
157142
let mouse_screen = mouse - self.viewport_offset;
@@ -321,9 +306,6 @@ impl App {
321306
None => {}
322307
}
323308

324-
if let Some(Drag::Panel { pos, kind: _ }) = self.drag.as_mut() {
325-
*pos = mouse;
326-
}
327309
self.compute_potential_connections();
328310
}
329311

@@ -332,41 +314,15 @@ impl App {
332314
return;
333315
};
334316
match drag {
335-
Drag::Panel { pos, kind } => {
336-
if let InstanceKind::CustomCircuit(definition_index) = kind
337-
&& definition_index >= self.db.custom_circuit_definitions.len()
338-
{
339-
return;
340-
}
341-
342-
let id = match kind {
343-
InstanceKind::Gate(gate_kind) => self.db.new_gate(Gate {
344-
kind: gate_kind,
345-
pos,
346-
}),
347-
InstanceKind::Power => self.db.new_power(Power { pos, on: true }),
348-
InstanceKind::Wire => {
349-
let w = Wire::new_at(pos);
350-
self.db.new_wire(w)
351-
}
352-
InstanceKind::Lamp => self.db.new_lamp(Lamp { pos }),
353-
InstanceKind::CustomCircuit(definition_index) => {
354-
self.db
355-
.new_custom_circuit(crate::custom_circuit::CustomCircuit {
356-
pos,
357-
definition_index,
358-
})
359-
}
360-
};
361-
self.connection_manager.mark_instance_dirty(id);
362-
}
363317
Drag::Canvas(canvas_drag) => match canvas_drag {
364318
CanvasDrag::Single { id, offset: _ } => {
365319
self.connection_manager.mark_instance_dirty(id);
320+
self.current_dirty = true;
366321
}
367322
CanvasDrag::Selected { start: _ } => {
368323
let selected: Vec<InstanceId> = self.selected.iter().copied().collect();
369324
self.connection_manager.mark_instances_dirty(&selected);
325+
self.current_dirty = true;
370326
}
371327
},
372328
Drag::Selecting { start } => {
@@ -401,6 +357,7 @@ impl App {
401357
}
402358
Drag::Resize { id, start: _ } => {
403359
self.connection_manager.mark_instance_dirty(id);
360+
self.current_dirty = true;
404361
}
405362
Drag::PinToWire {
406363
source_pin: _,
@@ -418,9 +375,9 @@ impl App {
418375
} => {
419376
self.connection_manager
420377
.mark_instance_dirty(original_wire_id);
378+
self.current_dirty = true;
421379
}
422380
}
423-
self.current_dirty = true;
424381
self.connection_manager.rebuild_spatial_index(&self.db);
425382
self.potential_connections.clear();
426383
self.drag_had_movement = false;

src/simulator.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ impl Value {
8080
pub struct Simulator {
8181
/// Final result - maps each pin to its current value
8282
pub current: HashMap<Pin, Value>,
83+
/// Keep what has been already evaluated
84+
pub evaluated: HashSet<InstanceId>,
8385
/// Number of iterations taken in last compute
8486
pub last_iterations: usize,
8587
/// Current status of the simulation
@@ -114,23 +116,13 @@ impl Simulator {
114116
self.evaluate_power(db, id);
115117
}
116118

119+
let sorted_instances = self.rebuild_sorted_instances(db);
120+
117121
while self.current_iteration < MAX_ITERATIONS {
118122
previous_state = self.current.clone();
119123

120-
let sorted_instances = self.rebuild_sorted_instances(db);
121124
for &id in &sorted_instances {
122-
match db.ty(id) {
123-
InstanceKind::Wire => {
124-
self.evaluate_wire(db, id);
125-
}
126-
InstanceKind::Gate(_) => {
127-
self.evaluate_gate(db, id);
128-
}
129-
InstanceKind::Lamp => {
130-
self.evaluate_lamp(db, id);
131-
}
132-
InstanceKind::Power | InstanceKind::CustomCircuit(_) => {}
133-
}
125+
self.evaluate(db, id);
134126
}
135127

136128
self.current_iteration += 1;
@@ -165,6 +157,22 @@ impl Simulator {
165157
.collect()
166158
}
167159

160+
fn evaluate(&mut self, db: &DB, id: InstanceId) {
161+
self.evaluated.insert(id);
162+
match db.ty(id) {
163+
InstanceKind::Wire => {
164+
self.evaluate_wire(db, id);
165+
}
166+
InstanceKind::Gate(_) => {
167+
self.evaluate_gate(db, id);
168+
}
169+
InstanceKind::Lamp => {
170+
self.evaluate_lamp(db, id);
171+
}
172+
InstanceKind::Power | InstanceKind::CustomCircuit(_) => {}
173+
}
174+
}
175+
168176
fn evaluate_power(&mut self, db: &DB, id: InstanceId) {
169177
let p = db.get_power(id);
170178
let out = db.power_output(id);
@@ -225,7 +233,7 @@ impl Simulator {
225233
self.current.insert(inp, val);
226234
}
227235

228-
fn get_pin_value(&self, db: &DB, pin: Pin) -> Value {
236+
fn get_pin_value(&mut self, db: &DB, pin: Pin) -> Value {
229237
let mut connected = db.connected_pins(pin);
230238
connected.push(pin);
231239
connected.sort_unstable();
@@ -236,6 +244,10 @@ impl Simulator {
236244
if db.pin_info(other).kind != PinKind::Output {
237245
continue;
238246
}
247+
248+
if !self.evaluated.contains(&other.ins) {
249+
self.evaluate(db, other.ins);
250+
}
239251
if let Some(&val) = self.current.get(&other) {
240252
result = result.or(val);
241253
}

0 commit comments

Comments
 (0)