@@ -18,7 +18,6 @@ use crate::{
1818 config:: CanvasConfig ,
1919 connection_manager:: { Connection , ConnectionManager } ,
2020 drag:: Drag ,
21- module:: Module ,
2221} ;
2322
2423pub const PANEL_BUTTON_MAX_HEIGHT : f32 = 50.0 ;
@@ -325,6 +324,7 @@ impl App {
325324 }
326325
327326 fn is_on ( & self , pin : Pin ) -> bool {
327+ let pin = pin. is_passthrough ( & self . db ) . unwrap_or ( pin) ;
328328 let Some ( v) = self . simulator . current . get ( & pin) else {
329329 return false ;
330330 } ;
@@ -449,6 +449,7 @@ impl App {
449449 self . draw_panel_button ( ui, InstanceKind :: Gate ( GateKind :: Nor ) ) ;
450450 self . draw_panel_button ( ui, InstanceKind :: Gate ( GateKind :: Xor ) ) ;
451451 self . draw_panel_button ( ui, InstanceKind :: Gate ( GateKind :: Xnor ) ) ;
452+ self . draw_panel_button ( ui, InstanceKind :: Gate ( GateKind :: Not ) ) ;
452453 self . draw_panel_button ( ui, InstanceKind :: Power ) ;
453454 self . draw_panel_button ( ui, InstanceKind :: Lamp ) ;
454455 self . draw_panel_button ( ui, InstanceKind :: Clock ) ;
@@ -538,10 +539,7 @@ impl App {
538539 InstanceKind :: Wire => self . db . circuit . new_wire ( Wire :: new_at ( pos) ) ,
539540 InstanceKind :: Lamp => self . db . circuit . new_lamp ( Lamp { pos } ) ,
540541 InstanceKind :: Clock => self . db . circuit . new_clock ( Clock { pos } ) ,
541- InstanceKind :: Module ( c) => self . db . new_module_with_flattening ( Module {
542- pos,
543- definition_index : c,
544- } ) ,
542+ InstanceKind :: Module ( c) => self . db . new_module ( c, pos) ,
545543 } ;
546544 self . set_drag ( Drag :: Canvas ( crate :: drag:: CanvasDrag :: Single {
547545 id,
@@ -556,7 +554,7 @@ impl App {
556554 {
557555 let mut ids = Vec :: new ( ) ;
558556 for ( id, m) in & self . circuit ( ) . modules {
559- if m. definition_index == i {
557+ if m. definition_id == i {
560558 ids. push ( id) ;
561559 }
562560 }
@@ -730,7 +728,7 @@ impl App {
730728 Self :: draw_grid ( ui, canvas_rect, self . viewport_offset ) ;
731729
732730 let mouse_clicked_canvas = resp. clicked ( ) ;
733- let mouse_dragging_canvas = resp. dragged ( ) ;
731+ let mouse_dragging_canvas = resp. dragged_by ( egui :: PointerButton :: Primary ) ;
734732 let double_clicked = ui. input ( |i| {
735733 i. pointer
736734 . button_double_clicked ( egui:: PointerButton :: Primary )
@@ -953,7 +951,7 @@ impl App {
953951 }
954952 }
955953
956- fn draw_instance_graphics_new (
954+ fn draw_instance_graphics (
957955 & mut self ,
958956 ui : & mut Ui ,
959957 graphics : assets:: InstanceGraphics ,
@@ -1028,15 +1026,15 @@ impl App {
10281026 let gate = self . db . circuit . get_gate ( id) ;
10291027 ( gate. pos , gate. kind )
10301028 } ;
1031- self . draw_instance_graphics_new ( ui, kind. graphics ( ) , self . adjusted_pos ( pos) , id) ;
1029+ self . draw_instance_graphics ( ui, kind. graphics ( ) , self . adjusted_pos ( pos) , id) ;
10321030 }
10331031
10341032 fn draw_power ( & mut self , ui : & mut Ui , id : InstanceId ) {
10351033 let ( pos, graphics) = {
10361034 let power = self . db . circuit . get_power ( id) ;
10371035 ( power. pos , power. graphics ( ) )
10381036 } ;
1039- self . draw_instance_graphics_new ( ui, graphics, self . adjusted_pos ( pos) , id) ;
1037+ self . draw_instance_graphics ( ui, graphics, self . adjusted_pos ( pos) , id) ;
10401038 }
10411039
10421040 fn draw_lamp ( & mut self , ui : & mut Ui , id : InstanceId ) {
@@ -1062,7 +1060,7 @@ impl App {
10621060 }
10631061 }
10641062
1065- self . draw_instance_graphics_new ( ui, graphics, pos, id) ;
1063+ self . draw_instance_graphics ( ui, graphics, pos, id) ;
10661064 }
10671065
10681066 fn draw_clock ( & mut self , ui : & mut Ui , id : InstanceId ) {
@@ -1071,24 +1069,25 @@ impl App {
10711069 ( clock. pos , clock. graphics ( ) )
10721070 } ;
10731071 let pos = self . adjusted_pos ( pos) ;
1074- self . draw_instance_graphics_new ( ui, graphics, pos, id) ;
1072+ self . draw_instance_graphics ( ui, graphics, pos, id) ;
10751073 }
10761074
10771075 fn draw_module ( & mut self , ui : & mut Ui , id : InstanceId ) {
10781076 let ( pos, definition_index) = {
10791077 let module = self . db . circuit . get_module ( id) ;
1080- ( module. pos , module. definition_index )
1078+ ( module. pos , module. definition_id )
10811079 } ;
10821080 let screen_center = pos - self . viewport_offset ;
10831081
10841082 let ( name, pins, pin_offsets) = {
10851083 let definition = self . db . circuit . get_module ( id) . definition ( & self . db ) ;
10861084 let name = definition. name . clone ( ) ;
1087- let pins = definition. get_unconnected_pins ( & self . db , id) ;
1088-
1085+ let pins = self . db . circuit . get_module ( id) . pins ( ) ;
10891086 let pin_offsets: Vec < Vec2 > = pins
10901087 . iter ( )
1091- . map ( |pin| definition. calculate_pin_offset ( & self . db , pin, & self . canvas_config ) )
1088+ . map ( |pin| {
1089+ definition. calculate_pin_offset ( & self . db , & pins, pin, & self . canvas_config )
1090+ } )
10921091 . collect ( ) ;
10931092
10941093 ( name, pins, pin_offsets)
@@ -1128,7 +1127,7 @@ impl App {
11281127 } ) ) ;
11291128 }
11301129
1131- for ( pin_index , ( & pin, & pin_offset) ) in pins. iter ( ) . zip ( pin_offsets. iter ( ) ) . enumerate ( ) {
1130+ for ( & pin, & pin_offset) in pins. iter ( ) . zip ( pin_offsets. iter ( ) ) {
11321131 let pin_pos_world = pos + pin_offset;
11331132 let pin_screen_pos = self . adjusted_pos ( pin_pos_world) ;
11341133
@@ -1140,7 +1139,7 @@ impl App {
11401139 ui. painter ( )
11411140 . circle_filled ( pin_screen_pos, self . canvas_config . base_pin_size , pin_color) ;
11421141
1143- let has_current = self . is_on ( Pin :: new ( id , pin_index as u32 , pin. kind ) ) ;
1142+ let has_current = self . is_on ( pin) ;
11441143
11451144 if has_current {
11461145 ui. painter ( ) . circle_stroke (
@@ -1155,15 +1154,12 @@ impl App {
11551154 Vec2 :: splat ( self . canvas_config . base_pin_size + PIN_HOVER_THRESHOLD ) ,
11561155 ) ;
11571156 let pin_resp = ui. allocate_rect ( pin_rect, Sense :: drag ( ) ) ;
1158- let pin_obj = Pin :: new ( id, pin_index as u32 , pin. kind ) ;
11591157 if pin_resp. hovered ( ) {
1160- self . hovered = Some ( Hover :: Pin ( pin_obj ) ) ;
1158+ self . hovered = Some ( Hover :: Pin ( pin ) ) ;
11611159 }
11621160 if pin_resp. dragged ( ) {
11631161 self . selected . clear ( ) ;
1164- self . set_drag ( Drag :: PinToWire {
1165- source_pin : pin_obj,
1166- } ) ;
1162+ self . set_drag ( Drag :: PinToWire { source_pin : pin } ) ;
11671163 }
11681164 }
11691165 }
@@ -1259,7 +1255,7 @@ impl App {
12591255 && let Some ( split_point) = self . wire_branching_action_point ( mouse, id)
12601256 {
12611257 ui. painter ( ) . circle_filled (
1262- split_point,
1258+ self . adjusted_pos ( split_point) ,
12631259 PIN_HOVER_THRESHOLD ,
12641260 COLOR_HOVER_PIN_TO_WIRE ,
12651261 ) ;
@@ -1273,7 +1269,7 @@ impl App {
12731269 && let Some ( split_point) = self . wire_branching_action_point ( mouse, id)
12741270 {
12751271 ui. painter ( ) . circle_filled (
1276- split_point,
1272+ self . adjusted_pos ( split_point) ,
12771273 PIN_HOVER_THRESHOLD ,
12781274 COLOR_HOVER_PIN_TO_WIRE ,
12791275 ) ;
@@ -1551,30 +1547,31 @@ impl App {
15511547
15521548 fn debug_string ( & self , ui : & Ui ) -> String {
15531549 let mut out = String :: new ( ) ;
1550+
1551+ // App state (compact)
1552+ writeln ! ( out, "======================================" ) . ok ( ) ;
1553+ writeln ! ( out, " APP STATE" ) . ok ( ) ;
1554+ writeln ! ( out, "======================================" ) . ok ( ) ;
15541555 let mouse_pos_world = self . mouse_pos_world ( ui) ;
15551556 writeln ! ( out, "mouse: {mouse_pos_world:?}" ) . ok ( ) ;
1556-
15571557 writeln ! ( out, "hovered: {:?}" , self . hovered) . ok ( ) ;
15581558 writeln ! ( out, "drag: {:?}" , self . drag) . ok ( ) ;
1559- writeln ! ( out, "viewport_offset: {:?}" , self . viewport_offset) . ok ( ) ;
1560- writeln ! ( out, "potential_conns: {}" , self . potential_connections. len( ) ) . ok ( ) ;
1561- writeln ! ( out, "clipboard: {:?}" , self . clipboard) . ok ( ) ;
15621559 writeln ! ( out, "selected: {:?}" , self . selected) . ok ( ) ;
1563- writeln ! ( out, "editing_label: {:?}" , self . editing_label) . ok ( ) ;
1564- writeln ! ( out, "label_edit_buffer: {}" , self . label_edit_buffer) . ok ( ) ;
15651560 writeln ! ( out, "viewing_module: {:?}" , self . viewing_module) . ok ( ) ;
15661561
15671562 // Simulation status
1568- writeln ! ( out, "\n === Simulation Status ===" ) . ok ( ) ;
1569- writeln ! ( out, "needs update {}" , self . current_dirty) . ok ( ) ;
1563+ writeln ! ( out) . ok ( ) ;
1564+ writeln ! ( out, "======================================" ) . ok ( ) ;
1565+ writeln ! ( out, " SIMULATION" ) . ok ( ) ;
1566+ writeln ! ( out, "======================================" ) . ok ( ) ;
15701567 match self . simulator . status {
15711568 SimulationStatus :: Stable { iterations } => {
1572- writeln ! ( out, "Status: STABLE (after {iterations} iterations )" ) . ok ( ) ;
1569+ writeln ! ( out, "Status: STABLE ({iterations} iters )" ) . ok ( ) ;
15731570 }
15741571 SimulationStatus :: Unstable { max_reached } => {
15751572 if max_reached {
15761573 let iters = self . simulator . last_iterations ;
1577- writeln ! ( out, "Status: UNSTABLE (max iterations : {iters})" ) . ok ( ) ;
1574+ writeln ! ( out, "Status: UNSTABLE (max: {iters})" ) . ok ( ) ;
15781575 } else {
15791576 writeln ! ( out, "Status: UNSTABLE" ) . ok ( ) ;
15801577 }
@@ -1583,44 +1580,31 @@ impl App {
15831580 writeln ! ( out, "Status: RUNNING..." ) . ok ( ) ;
15841581 }
15851582 }
1586- let iters = self . simulator . last_iterations ;
1587- writeln ! ( out, "Iterations: {iters}" ) . ok ( ) ;
1588-
1589- // Clock controller state
1590- writeln ! ( out, "\n --- Clock Controller ---" ) . ok ( ) ;
1591- writeln ! ( out, "State: {:?}" , self . clock_controller. state) . ok ( ) ;
15921583 writeln ! (
15931584 out,
1594- "Tick interval: {:.2}s" ,
1595- self . clock_controller. tick_interval
1585+ "Clock: {:?}, interval: {:.2}s" ,
1586+ self . clock_controller. state , self . clock_controller . tick_interval
15961587 )
15971588 . ok ( ) ;
1598- writeln ! ( out, "Voltage: {}" , self . clock_controller. voltage) . ok ( ) ;
1599-
1600- if self . potential_connections . is_empty ( ) {
1601- writeln ! ( out, "\n Potential Connections: none" ) . ok ( ) ;
1602- } else {
1603- writeln ! ( out, "\n Potential Connections:" ) . ok ( ) ;
1604- for c in & self . potential_connections {
1605- writeln ! ( out, " {}" , c. display( self . circuit( ) ) ) . ok ( ) ;
1606- }
1607- }
1608-
1609- writeln ! ( out, "\n {}" , self . connection_manager. debug_info( ) ) . ok ( ) ;
1610-
1611- writeln ! ( out, "\n " ) . ok ( ) ;
16121589
1613- out. write_str ( & self . circuit ( ) . display ( & self . db ) ) . ok ( ) ;
1590+ // Circuit instances (main content)
1591+ writeln ! ( out) . ok ( ) ;
1592+ out. write_str ( & self . circuit ( ) . display ( & self . db , Some ( & self . simulator ) ) )
1593+ . ok ( ) ;
16141594
1595+ // Module definitions (at the bottom, summary only)
16151596 if !self . db . module_definitions . is_empty ( ) {
1616- writeln ! ( out, "\n Module Def:" ) . ok ( ) ;
1617- let mut iter = self . db . module_definitions . iter ( ) ;
1618- if let Some ( ( id, first) ) = iter. next ( ) {
1619- writeln ! ( out, " {}" , first. display_definition( & self . db, id) ) . ok ( ) ;
1620- }
1621- for ( id, m) in iter {
1622- writeln ! ( out) . ok ( ) ;
1623- writeln ! ( out, " {}" , m. display_definition( & self . db, id) ) . ok ( ) ;
1597+ writeln ! ( out) . ok ( ) ;
1598+ writeln ! ( out, "======================================" ) . ok ( ) ;
1599+ writeln ! (
1600+ out,
1601+ " MODULE DEFINITIONS ({} total)" ,
1602+ self . db. module_definitions. len( )
1603+ )
1604+ . ok ( ) ;
1605+ writeln ! ( out, "======================================" ) . ok ( ) ;
1606+ for ( id, m) in & self . db . module_definitions {
1607+ writeln ! ( out, "{}" , m. display_definition( & self . db, id) ) . ok ( ) ;
16241608 }
16251609 }
16261610
@@ -1691,7 +1675,7 @@ impl App {
16911675 }
16921676 InstanceKind :: Module ( _) => {
16931677 let cc = self . db . circuit . get_module ( id) ;
1694- object_pos. push ( ClipBoardItem :: Module ( cc. definition_index , center - cc. pos ) ) ;
1678+ object_pos. push ( ClipBoardItem :: Module ( cc. definition_id , center - cc. pos ) ) ;
16951679 }
16961680 }
16971681 }
@@ -1742,12 +1726,13 @@ impl App {
17421726 self . selected . insert ( id) ;
17431727 }
17441728 ClipBoardItem :: Module ( def_index, offset) => {
1745- let id = self . db . new_module_with_flattening ( Module {
1746- pos : mouse - offset,
1747- definition_index : def_index,
1748- } ) ;
1749- self . connection_manager . mark_instance_dirty ( id) ;
1750- self . selected . insert ( id) ;
1729+ // TODO: Modules
1730+ // let id = self.db.new_module_with_flattening(Module {
1731+ // pos: mouse - offset,
1732+ // definition_index: def_index,
1733+ // });
1734+ // self.connection_manager.mark_instance_dirty(id);
1735+ // self.selected.insert(id);
17511736 }
17521737 ClipBoardItem :: Lamp ( offset) => {
17531738 let id = self . db . circuit . new_lamp ( Lamp {
@@ -1886,7 +1871,7 @@ impl App {
18861871 pos - self . viewport_offset
18871872 }
18881873
1889- fn mouse_pos_world ( & self , ui : & Ui ) -> Option < Pos2 > {
1874+ pub fn mouse_pos_world ( & self , ui : & Ui ) -> Option < Pos2 > {
18901875 ui. ctx ( )
18911876 . pointer_interact_pos ( )
18921877 . map ( |p| p + self . viewport_offset )
0 commit comments