77 using System . Drawing . Drawing2D ;
88 using System . Linq ;
99 using System . Windows . Forms ;
10+ using Maxstupo . LogicSandbox . Logic ;
1011 using Maxstupo . LogicSandbox . Logic . Components ;
1112 using Maxstupo . LogicSandbox . Properties ;
1213 using Maxstupo . LogicSandbox . Shapes ;
@@ -17,15 +18,16 @@ public partial class FormMain : Form {
1718 private Keys AdditiveKey { get ; set ; } = Keys . Control ;
1819 private Keys InclusiveKey { get ; set ; } = Keys . Shift ;
1920
20- private readonly List < DigitalComponent > digitalComponents = new List < DigitalComponent > ( ) ;
21-
21+ private Circuit circuit = new Circuit ( ) ;
2222 private readonly Selector < DigitalComponent > selector ;
2323 private readonly Transformer < DigitalComponent > transformer = new Transformer < DigitalComponent > ( ) ;
2424
25+ private Pin selectedPin ;
26+
2527 public FormMain ( ) {
2628 InitializeComponent ( ) ;
2729
28- selector = new Selector < DigitalComponent > ( digitalComponents ) ;
30+ selector = new Selector < DigitalComponent > ( circuit . Components ) ;
2931 selector . OnDragging += ( s , e ) => canvas . Refresh ( ) ;
3032 selector . OnEndDrag += Selector_OnEndDrag ;
3133
@@ -40,14 +42,15 @@ public FormMain() {
4042 }
4143
4244 private void FormMain_Load ( object sender , EventArgs e ) {
45+ canvas . Zoom = 2 ;
4346 canvas . Center ( ) ;
4447
4548
46- digitalComponents . Add ( new NotGate ( "not_gate0" , 10 , 60 ) ) ;
47- digitalComponents . Add ( new NotGate ( "not_gate0" , - 40 , 20 ) ) ;
48- digitalComponents . Add ( new NotGate ( "not_gate0" , 50 , - 20 ) ) ;
49- digitalComponents . Add ( new NotGate ( "not_gate0" , - 20 , - 50 ) ) ;
50- digitalComponents . Add ( new NotGate ( "not_gate0" , 90 , - 90 ) ) ;
49+ circuit . Components . Add ( new NotGate ( "not_gate0" , 10 , 60 ) ) ;
50+ circuit . Components . Add ( new NotGate ( "not_gate0" , - 40 , 20 ) ) ;
51+ circuit . Components . Add ( new NotGate ( "not_gate0" , 50 , - 20 ) ) ;
52+ circuit . Components . Add ( new NotGate ( "not_gate0" , - 20 , - 50 ) ) ;
53+ circuit . Components . Add ( new NotGate ( "not_gate0" , 90 , - 90 ) ) ;
5154 }
5255
5356 private void Canvas_Paint ( object sender , PaintEventArgs e ) {
@@ -57,33 +60,47 @@ private void Canvas_Paint(object sender, PaintEventArgs e) {
5760 g . InterpolationMode = InterpolationMode . HighQualityBicubic ;
5861 g . PixelOffsetMode = PixelOffsetMode . HighQuality ;
5962
60- foreach ( DigitalComponent component in digitalComponents )
61- component . Draw ( g ) ;
63+ circuit . Draw ( g ) ;
64+
65+ if ( selectedPin != null )
66+ g . DrawLine ( Pens . Blue , canvas . MouseWorldX , canvas . MouseWorldY , selectedPin . GlobalX , selectedPin . GlobalY ) ;
6267
6368 selector . Draw ( g ) ;
6469 }
6570
6671 private void Canvas_MouseDown ( object sender , MouseEventArgs e ) {
6772 if ( e . Button == MouseButtons . Left ) {
6873
69- bool additiveMode = ModifierKeys . HasFlag ( AdditiveKey ) ; // Previous selection not cleared.
74+ selectedPin = circuit . GetPinOver ( ) ;
75+ if ( selectedPin == null ) {
7076
71- if ( ! selector . Start ( canvas . MouseWorldX , canvas . MouseWorldY , additiveMode , GetItemOver ) ) {
77+ bool additiveMode = ModifierKeys . HasFlag ( AdditiveKey ) ; // Previous selection not cleared.
7278
73- transformer . Clear ( ) ;
74- transformer . AddItems ( selector . SelectedItems ) ;
75- transformer . StartDrag ( canvas . MouseWorldX , canvas . MouseWorldY ) ;
79+ if ( ! selector . Start ( canvas . MouseWorldX , canvas . MouseWorldY , additiveMode , ( x , y ) => circuit . GetComponentOver ( ) ) ) {
80+
81+ transformer . Clear ( ) ;
82+ transformer . AddItems ( selector . SelectedItems ) ;
83+ transformer . StartDrag ( canvas . MouseWorldX , canvas . MouseWorldY ) ;
84+
85+ canvas . Refresh ( ) ;
86+ }
7687
77- canvas . Refresh ( ) ;
7888 }
7989
90+ } else if ( e . Button == MouseButtons . Right ) {
91+
8092 }
93+
8194 }
8295
8396 private void Canvas_MouseMove ( object sender , MouseEventArgs e ) {
84- selector . Drag ( canvas . MouseWorldX , canvas . MouseWorldY ) ;
97+ bool needsRefresh = circuit . Update ( canvas . MouseWorldX , canvas . MouseWorldY ) ;
8598
99+ selector . Drag ( canvas . MouseWorldX , canvas . MouseWorldY ) ;
86100 transformer . Drag ( canvas . MouseWorldX , canvas . MouseWorldY ) ;
101+
102+ if ( needsRefresh || selectedPin != null )
103+ canvas . Refresh ( ) ;
87104 }
88105
89106 private void Canvas_MouseUp ( object sender , MouseEventArgs e ) {
@@ -94,8 +111,19 @@ private void Canvas_MouseUp(object sender, MouseEventArgs e) {
94111
95112 selector . EndDrag ( canvas . MouseWorldX , canvas . MouseWorldY , additiveMode , inclusiveMode ) ;
96113 transformer . EndDrag ( ) ;
114+
115+ if ( selectedPin != null ) {
116+
117+ Pin nextSelectedPin = circuit . GetPinOver ( ) ;
118+ if ( nextSelectedPin != null )
119+ circuit . AddWire ( selectedPin , nextSelectedPin ) ;
120+
121+ selectedPin = null ;
122+ canvas . Refresh ( ) ;
123+ }
124+
97125 }
98- canvas . Refresh ( ) ;
126+
99127 }
100128
101129 private void Selector_OnEndDrag ( object sender , List < DigitalComponent > selectedItems ) {
@@ -105,14 +133,7 @@ private void Selector_OnEndDrag(object sender, List<DigitalComponent> selectedIt
105133 }
106134
107135
108- // TODO: Improve method.
109- private DigitalComponent GetItemOver ( float x , float y ) {
110- foreach ( DigitalComponent component in digitalComponents ) {
111- if ( component . ContainsPoint ( x , y ) )
112- return component ;
113- }
114- return null ;
115- }
136+
116137
117138 #region Menu Strip
118139
0 commit comments