@@ -108,33 +108,27 @@ impl Instance {
108108
109109 match self . ty {
110110 InstanceType :: Gate ( gate_instance) => {
111- for ( i, pin) in gate_instance. kind . graphics ( ) . pins . iter ( ) . enumerate ( ) {
112- let pin_pos = gate_instance. pos + pin. offset ;
111+ for ( i, _) in gate_instance. kind . graphics ( ) . pins . iter ( ) . enumerate ( ) {
113112 pins. push ( Pin {
114- pos : pin_pos,
115113 ins : self . id ,
116114 index : i as u32 ,
117115 } ) ;
118116 }
119117 }
120118 InstanceType :: Power ( power_instance) => {
121- for ( i, pin) in power_instance. graphics ( ) . pins . iter ( ) . enumerate ( ) {
122- let pin_pos = power_instance. pos + pin. offset ;
119+ for ( i, _) in power_instance. graphics ( ) . pins . iter ( ) . enumerate ( ) {
123120 pins. push ( Pin {
124- pos : pin_pos,
125121 ins : self . id ,
126122 index : i as u32 ,
127123 } ) ;
128124 }
129125 }
130- InstanceType :: Wire ( wire_instance ) => {
126+ InstanceType :: Wire ( _ ) => {
131127 pins. push ( Pin {
132- pos : wire_instance. start ,
133128 ins : self . id ,
134129 index : 0 ,
135130 } ) ;
136131 pins. push ( Pin {
137- pos : wire_instance. end ,
138132 ins : self . id ,
139133 index : 1 ,
140134 } ) ;
@@ -158,20 +152,6 @@ impl Instance {
158152 }
159153 }
160154 }
161-
162- fn move_pin_delta ( & self , pin : Pin , new_pos : Pos2 ) -> Vec2 {
163- match & self . ty {
164- InstanceType :: Wire ( wire) => {
165- if pin. index == 0 {
166- new_pos - wire. start
167- } else {
168- new_pos - wire. end
169- }
170- }
171- InstanceType :: Gate ( _gate) => new_pos - pin. pos ,
172- InstanceType :: Power ( _power) => new_pos - pin. pos ,
173- }
174- }
175155}
176156
177157#[ derive( serde:: Deserialize , serde:: Serialize , Copy , Debug , Clone ) ]
@@ -332,15 +312,13 @@ impl CanvasDrag {
332312
333313#[ derive( serde:: Deserialize , serde:: Serialize , Debug , Clone , Copy , PartialEq , Eq ) ]
334314pub struct Pin {
335- pub pos : Pos2 ,
336315 pub ins : InstanceId ,
337316 pub index : u32 ,
338317}
339318
340319impl Pin {
341320 /// Compute this pin's current world position from its instance.
342- /// This ignores the stored `pos` and derives from the instance's data.
343- pub fn _position_from ( & self , ins : & Instance ) -> Pos2 {
321+ pub fn position_from ( & self , ins : & Instance ) -> Pos2 {
344322 match ins. ty {
345323 InstanceType :: Gate ( g) => {
346324 let info = g. kind . graphics ( ) . pins [ self . index as usize ] ;
@@ -384,6 +362,10 @@ impl Connection {
384362 Self { pin1 : a, pin2 : b }
385363 }
386364
365+ fn involves ( & self , ins : InstanceId ) -> bool {
366+ self . pin1 . ins == ins || self . pin2 . ins == ins
367+ }
368+
387369 /// Return pins in the order that starts with pin from instance id
388370 fn get_pin ( & self , moving_instance_id : InstanceId ) -> Option < ( Pin , Pin ) > {
389371 if self . pin1 . ins == moving_instance_id {
@@ -489,6 +471,7 @@ impl TemplateApp {
489471 ui. separator ( ) ;
490472 ui. vertical ( |ui| {
491473 ui. heading ( "Canvas" ) ;
474+ ui. label ( "press d to remove object" ) ;
492475 self . draw_canvas ( ui) ;
493476 } ) ;
494477 } ) ;
@@ -712,7 +695,11 @@ impl TemplateApp {
712695 continue ;
713696 }
714697 for other_pin in other_ins. pins ( ) {
715- if self_pin. pos . distance ( other_pin. pos ) > EDGE_THRESHOLD {
698+ if self
699+ . get_pin_pos ( self_pin)
700+ . distance ( self . get_pin_pos ( other_pin) )
701+ > EDGE_THRESHOLD
702+ {
716703 continue ;
717704 }
718705
@@ -731,8 +718,11 @@ impl TemplateApp {
731718 // object.
732719 for conn in & possible_connections {
733720 if let Some ( ( _, other) ) = conn. get_pin ( moving_instance_id) {
734- ui. painter ( )
735- . circle_filled ( other. pos , EDGE_THRESHOLD , Color32 :: LIGHT_YELLOW ) ;
721+ ui. painter ( ) . circle_filled (
722+ self . get_pin_pos ( other) ,
723+ EDGE_THRESHOLD ,
724+ Color32 :: LIGHT_YELLOW ,
725+ ) ;
736726 }
737727 }
738728 if mouse_up {
@@ -744,16 +734,17 @@ impl TemplateApp {
744734 ( conn. pin1 , conn. pin2 )
745735 } ;
746736 let instance = self . get_instance ( pin1. ins ) ;
737+ let pin_pos = self . get_pin_pos ( pin2) ;
747738 if let InstanceType :: Wire ( _) = instance. ty {
748739 let wire = self . get_wire_mut ( instance. id ) ;
749740 if pin1. index == 0 {
750- wire. start = pin2 . pos ;
741+ wire. start = pin_pos ;
751742 } else {
752- wire. end = pin2 . pos ;
743+ wire. end = pin_pos ;
753744 } ;
754745 self . resize = None ;
755746 } else {
756- let delta = { instance . move_pin_delta ( pin1, pin2 . pos ) } ;
747+ let delta = { self . move_pin_delta ( pin1, self . get_pin_pos ( pin2 ) ) } ;
757748 self . mov_component_with_connected ( pin1. ins , delta, canvas_rect) ;
758749 }
759750 }
@@ -796,6 +787,22 @@ impl TemplateApp {
796787 wire. rotate_cw ( ) ;
797788 }
798789 }
790+
791+ let d_pressed = ui. input ( |i| i. key_pressed ( egui:: Key :: D ) ) ;
792+ if d_pressed {
793+ if let Some ( canvas_drag) = & self . canvas_drag {
794+ self . remove_instance ( canvas_drag. id ) ;
795+ self . canvas_drag = None ;
796+ } else if self . panel_drag . is_some ( ) {
797+ self . panel_drag = None ;
798+ // rotate instance when dragging from panel
799+ } else if let Some ( mouse_pos) = pointer_pos
800+ && let Some ( i) = self . interacted_instance ( mouse_pos)
801+ {
802+ self . remove_instance ( i. id ) ;
803+ }
804+ }
805+
799806 for instance in & self . instances {
800807 match instance. ty {
801808 InstanceType :: Gate ( gate) => {
@@ -887,6 +894,37 @@ impl TemplateApp {
887894 }
888895 i
889896 }
897+
898+ fn move_pin_delta ( & self , pin : Pin , new_pos : Pos2 ) -> Vec2 {
899+ let instance = self . get_instance ( pin. ins ) ;
900+ let pin_pos = self . get_pin_pos ( pin) ;
901+ match instance. ty {
902+ InstanceType :: Wire ( wire) => {
903+ if pin. index == 0 {
904+ new_pos - wire. start
905+ } else {
906+ new_pos - wire. end
907+ }
908+ }
909+ InstanceType :: Gate ( _gate) => new_pos - pin_pos,
910+ InstanceType :: Power ( _power) => new_pos - pin_pos,
911+ }
912+ }
913+
914+ fn remove_instance ( & mut self , id : InstanceId ) {
915+ self . connections . retain ( |f| !f. involves ( id) ) ;
916+ self . instances . remove ( id. usize ( ) ) ;
917+ if let Some ( d) = & self . canvas_drag
918+ && d. id == id
919+ {
920+ self . canvas_drag = None ;
921+ }
922+ if let Some ( r) = & self . resize
923+ && r. id == id
924+ {
925+ self . resize = None ;
926+ }
927+ }
890928}
891929
892930fn inside_rect ( canvas_rect : & Rect , ty : & InstanceType ) -> bool {
@@ -917,18 +955,13 @@ impl TemplateApp {
917955 self . instances . get ( id. usize ( ) ) . expect ( "should not happen" )
918956 }
919957
958+ fn get_pin_pos ( & self , pin : Pin ) -> Pos2 {
959+ pin. position_from ( self . get_instance ( pin. ins ) )
960+ }
961+
920962 fn get_connected_instances ( & self , id : InstanceId ) -> Vec < InstanceId > {
921963 let mut connecteds = Vec :: new ( ) ;
922- let mut conns: Vec < _ > = self . connections . iter ( ) . collect ( ) ;
923- conns. sort_by_key ( |c| {
924- (
925- c. pin1 . pos . x . to_bits ( ) ,
926- c. pin1 . pos . y . to_bits ( ) ,
927- c. pin2 . pos . x . to_bits ( ) ,
928- c. pin2 . pos . y . to_bits ( ) ,
929- )
930- } ) ;
931- for con in conns {
964+ for con in & self . connections {
932965 if con. pin1 . ins == id {
933966 connecteds. push ( con. pin2 . ins ) ;
934967 }
@@ -1081,12 +1114,10 @@ mod tests {
10811114 #[ test]
10821115 fn connection_normalization_and_hash_eq ( ) {
10831116 let a = Pin {
1084- pos : pos2 ( 0.0 , 0.0 ) ,
10851117 ins : InstanceId ( 1 ) ,
10861118 index : 0 ,
10871119 } ;
10881120 let b = Pin {
1089- pos : pos2 ( 10.0 , 0.0 ) ,
10901121 ins : InstanceId ( 2 ) ,
10911122 index : 1 ,
10921123 } ;
0 commit comments