1- use std:: { collections:: HashSet , hash:: Hash , usize } ;
1+ use std:: { collections:: HashSet , hash:: Hash } ;
22
33use egui:: {
4- Align , Button , Color32 , Image , Layout , Pos2 , Rect , Sense , Stroke , Ui , Vec2 , Widget , pos2, vec2,
4+ Align , Button , Color32 , Image , Layout , Pos2 , Rect , Sense , Stroke , Ui , Vec2 , Widget as _, pos2,
5+ vec2,
56} ;
67
7- use crate :: {
8- assets:: { self , PinInfo } ,
9- config:: CanvasConfig ,
10- } ;
8+ use crate :: { assets, config:: CanvasConfig } ;
119
1210// TODO Direction is not used anymore. I can calculate it from current positions?
1311#[ derive( serde:: Deserialize , serde:: Serialize , Debug , Clone , Copy , Default , Eq , PartialEq ) ]
@@ -22,10 +20,10 @@ pub enum Direction {
2220impl Direction {
2321 fn _rotate_cw ( self ) -> Self {
2422 match self {
25- Direction :: Up => Direction :: Right ,
26- Direction :: Right => Direction :: Down ,
27- Direction :: Down => Direction :: Left ,
28- Direction :: Left => Direction :: Up ,
23+ Self :: Up => Self :: Right ,
24+ Self :: Right => Self :: Down ,
25+ Self :: Down => Self :: Left ,
26+ Self :: Left => Self :: Up ,
2927 }
3028 }
3129}
@@ -83,7 +81,7 @@ impl Instance {
8381 }
8482 }
8583
86- return pins;
84+ pins
8785 }
8886
8987 fn mov ( & mut self , move_vec : Vec2 ) {
@@ -128,7 +126,7 @@ impl InstanceType {
128126 }
129127
130128 pub fn new_wire_from_point ( p : Pos2 ) -> Self {
131- return Self :: new_wire ( p, pos2 ( p. x + 30.0 , p. y ) ) ;
129+ Self :: new_wire ( p, pos2 ( p. x + 30.0 , p. y ) )
132130 }
133131 fn new_gate ( kind : GateKind , pos : Pos2 ) -> Self {
134132 Self :: Gate ( GateInstance { kind, pos } )
@@ -142,15 +140,7 @@ pub struct GateInstance {
142140 pos : Pos2 ,
143141}
144142
145- impl GateInstance {
146- fn pins ( & self ) -> Vec < Pos2 > {
147- let mut pins_pos = Vec :: new ( ) ;
148- for pin in self . kind . graphics ( ) . pins {
149- pins_pos. push ( self . pos + pin. offset ) ;
150- }
151- pins_pos
152- }
153- }
143+ impl GateInstance { }
154144
155145#[ derive( serde:: Deserialize , serde:: Serialize , Copy , Debug , Clone ) ]
156146pub enum GateKind {
@@ -163,10 +153,6 @@ impl GateKind {
163153 Self :: Nand => & assets:: NAND_GRAPHICS ,
164154 }
165155 }
166-
167- fn pins ( & self ) -> & [ PinInfo ] {
168- self . graphics ( ) . pins
169- }
170156}
171157
172158#[ derive( serde:: Deserialize , serde:: Serialize , Copy , Debug , Clone ) ]
@@ -212,19 +198,19 @@ impl InstanceId {
212198
213199impl From < u32 > for InstanceId {
214200 fn from ( v : u32 ) -> Self {
215- InstanceId ( v)
201+ Self ( v)
216202 }
217203}
218204
219- impl Into < u32 > for InstanceId {
220- fn into ( self ) -> u32 {
221- self . 0
205+ impl From < InstanceId > for u32 {
206+ fn from ( val : InstanceId ) -> Self {
207+ val . 0
222208 }
223209}
224210
225- impl Into < usize > for InstanceId {
226- fn into ( self ) -> usize {
227- self . 0 as usize
211+ impl From < InstanceId > for usize {
212+ fn from ( val : InstanceId ) -> Self {
213+ val . 0 as Self
228214 }
229215}
230216
@@ -289,11 +275,7 @@ impl Connection {
289275 }
290276}
291277
292- /// Define what instance is moving
293- pub struct MoveMutation {
294- ins : InstanceId ,
295- move_vec : Vec2 ,
296- }
278+ //
297279
298280#[ derive( serde:: Deserialize , serde:: Serialize ) ]
299281pub struct TemplateApp {
@@ -410,6 +392,7 @@ impl TemplateApp {
410392 }
411393 }
412394
395+ #[ expect( clippy:: too_many_lines) ]
413396 fn draw_canvas ( & mut self , ui : & mut Ui ) {
414397 let ( resp, _painter) = ui. allocate_painter ( ui. available_size ( ) , Sense :: hover ( ) ) ;
415398 let canvas_rect = resp. rect ;
@@ -425,7 +408,7 @@ impl TemplateApp {
425408 self . draw_gate ( ui, & gate) ;
426409 }
427410 InstanceType :: Wire ( wire) => {
428- self . draw_wire ( ui, & wire) ;
411+ Self :: draw_wire ( ui, & wire) ;
429412 }
430413 }
431414 }
@@ -435,13 +418,12 @@ impl TemplateApp {
435418 // spawn a new gate
436419 if let Some ( panel_drag) = & self . panel_drag
437420 && mouse_up
421+ && inside_rect ( & canvas_rect, & panel_drag. ty )
438422 {
439- if inside_rect ( & canvas_rect, & panel_drag. ty ) {
440- self . instances
441- . push ( Instance :: new ( self . next_instance_id , panel_drag. ty ) ) ;
442- self . next_instance_id . incr ( ) ;
443- self . panel_drag = None ;
444- }
423+ self . instances
424+ . push ( Instance :: new ( self . next_instance_id , panel_drag. ty ) ) ;
425+ self . next_instance_id . incr ( ) ;
426+ self . panel_drag = None ;
445427 }
446428 let pointer_pos = ui. input ( |i| i. pointer . interact_pos ( ) ) ;
447429 let pointer_pressed = ui. input ( |i| i. pointer . primary_down ( ) ) ;
@@ -454,7 +436,7 @@ impl TemplateApp {
454436 {
455437 let i = self . interacted_instance ( mouse_pos) ;
456438 if let Some ( instance) = i {
457- log:: debug!( "canvas drag on {:?}" , instance ) ;
439+ log:: debug!( "canvas drag on {instance :?}" ) ;
458440 match instance. ty {
459441 InstanceType :: Gate ( gate) => {
460442 self . canvas_drag = Some ( CanvasDrag :: new (
@@ -527,11 +509,11 @@ impl TemplateApp {
527509 // TODO: Only need to check this on placement and moving.
528510 // Also use a better way than iterating on everything.
529511 let mut possible_connections = HashSet :: new ( ) ;
530- for self_ins in self . instances . iter ( ) {
512+ for self_ins in & self . instances {
531513 log:: info!( "{self_ins:#?}" ) ;
532514 let self_pins = self_ins. pins ( ) ;
533515 for self_pin in self_pins {
534- for other_ins in self . instances . iter ( ) {
516+ for other_ins in & self . instances {
535517 if self_ins. id == other_ins. id {
536518 continue ;
537519 }
@@ -554,15 +536,33 @@ impl TemplateApp {
554536 }
555537 }
556538 }
557- // paint connected pins
558- for conn in & possible_connections {
539+ // paint connected pins (iterate in a stable order)
540+ let mut conns: Vec < _ > = possible_connections. iter ( ) . collect ( ) ;
541+ conns. sort_by_key ( |c| {
542+ (
543+ c. pin1 . pos . x . to_bits ( ) ,
544+ c. pin1 . pos . y . to_bits ( ) ,
545+ c. pin2 . pos . x . to_bits ( ) ,
546+ c. pin2 . pos . y . to_bits ( ) ,
547+ )
548+ } ) ;
549+ for conn in conns {
559550 ui. painter ( )
560551 . circle_filled ( conn. pin1 . pos , 10.0 , Color32 :: LIGHT_YELLOW ) ;
561552 }
562553 // snap connections together
563554 if mouse_up {
564- // TODO: Remove clone
565- for conn in & possible_connections {
555+ // snap in a stable order
556+ let mut conns: Vec < _ > = possible_connections. iter ( ) . collect ( ) ;
557+ conns. sort_by_key ( |c| {
558+ (
559+ c. pin1 . pos . x . to_bits ( ) ,
560+ c. pin1 . pos . y . to_bits ( ) ,
561+ c. pin2 . pos . x . to_bits ( ) ,
562+ c. pin2 . pos . y . to_bits ( ) ,
563+ )
564+ } ) ;
565+ for conn in conns {
566566 let instance = self . get_instance_mut ( conn. pin1 . ins ) ;
567567 instance. move_pin ( conn. pin1 , conn. pin2 . pos ) ;
568568 }
@@ -597,13 +597,13 @@ impl TemplateApp {
597597 }
598598 }
599599 }
600- for instance in self . instances . iter ( ) {
600+ for instance in & self . instances {
601601 match instance. ty {
602602 InstanceType :: Gate ( gate) => {
603603 self . draw_gate ( ui, & gate) ;
604604 }
605605 InstanceType :: Wire ( wire) => {
606- self . draw_wire ( ui, & wire) ;
606+ Self :: draw_wire ( ui, & wire) ;
607607 }
608608 }
609609 }
@@ -613,7 +613,7 @@ impl TemplateApp {
613613 }
614614 }
615615
616- fn draw_wire ( & self , ui : & mut Ui , wire : & WireInstance ) {
616+ fn draw_wire ( ui : & Ui , wire : & WireInstance ) {
617617 let thickness = 6.0 ;
618618 ui. painter ( ) . line_segment (
619619 [ wire. start , wire. end ] ,
@@ -635,7 +635,16 @@ impl TemplateApp {
635635 ui. painter ( )
636636 . circle_filled ( pin_pos, self . canvas_config . base_pin_size , color) ;
637637 // paint connected pins
638- for conn in & self . connections {
638+ let mut conns: Vec < _ > = self . connections . iter ( ) . collect ( ) ;
639+ conns. sort_by_key ( |c| {
640+ (
641+ c. pin1 . pos . x . to_bits ( ) ,
642+ c. pin1 . pos . y . to_bits ( ) ,
643+ c. pin2 . pos . x . to_bits ( ) ,
644+ c. pin2 . pos . y . to_bits ( ) ,
645+ )
646+ } ) ;
647+ for conn in conns {
639648 if conn. pin1 . pos == pin_pos || conn. pin2 . pos == pin_pos {
640649 // ui.painter()
641650 // .circle_filled(pin_pos, 10.0, Color32::LIGHT_YELLOW);
@@ -646,7 +655,7 @@ impl TemplateApp {
646655
647656 fn interacted_instance ( & self , mouse_pos : Pos2 ) -> Option < & Instance > {
648657 let mut i: Option < & Instance > = None ;
649- for instance in self . instances . iter ( ) {
658+ for instance in & self . instances {
650659 match instance. ty {
651660 InstanceType :: Gate ( gate) => {
652661 let size = self . canvas_config . base_gate_size ;
@@ -679,30 +688,13 @@ fn inside_rect(canvas_rect: &Rect, ty: &InstanceType) -> bool {
679688}
680689
681690impl TemplateApp {
682- fn _get_wire ( & self , id : InstanceId ) -> WireInstance {
683- match self . get_instance ( id) . ty {
684- InstanceType :: Gate ( _) => panic ! ( "Should not happen" ) ,
685- InstanceType :: Wire ( wire_instance) => wire_instance,
686- }
687- }
688691 fn get_wire_mut ( & mut self , id : InstanceId ) -> & mut WireInstance {
689692 match & mut self . get_instance_mut ( id) . ty {
690693 InstanceType :: Gate ( _) => panic ! ( "Should not happen" ) ,
691694 InstanceType :: Wire ( wire_instance) => wire_instance,
692695 }
693696 }
694697
695- fn _get_gate ( & self , id : InstanceId ) -> GateInstance {
696- match self . get_instance ( id) . ty {
697- InstanceType :: Gate ( gate) => gate,
698- InstanceType :: Wire ( _) => panic ! ( "Should not happen" ) ,
699- }
700- }
701-
702- fn get_instance ( & self , id : InstanceId ) -> & Instance {
703- self . instances . get ( id. usize ( ) ) . expect ( "should not happen" )
704- }
705-
706698 fn get_instance_mut ( & mut self , id : InstanceId ) -> & mut Instance {
707699 self . instances
708700 . get_mut ( id. usize ( ) )
@@ -711,7 +703,16 @@ impl TemplateApp {
711703
712704 fn get_connected_instances ( & self , id : InstanceId ) -> Vec < InstanceId > {
713705 let mut connecteds = Vec :: new ( ) ;
714- for con in & self . connections {
706+ let mut conns: Vec < _ > = self . connections . iter ( ) . collect ( ) ;
707+ conns. sort_by_key ( |c| {
708+ (
709+ c. pin1 . pos . x . to_bits ( ) ,
710+ c. pin1 . pos . y . to_bits ( ) ,
711+ c. pin2 . pos . x . to_bits ( ) ,
712+ c. pin2 . pos . y . to_bits ( ) ,
713+ )
714+ } ) ;
715+ for con in conns {
715716 if con. pin1 . ins == id {
716717 connecteds. push ( con. pin2 . ins ) ;
717718 }
0 commit comments