-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
214 lines (181 loc) · 7.11 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LogicSim
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
mCircuit = new Circuit();
mCircuit.Add(ShowComponent(new Components.On(1000)));
}
private Circuit mCircuit;
private void timer1_Tick(object sender, EventArgs e)
{
mCircuit.Run();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog d = new System.Windows.Forms.SaveFileDialog();
d.Filter = "Component files (*.xml)|*.xml|Circuit files (*.xml)|*.xml";
if (d.ShowDialog() == DialogResult.OK)
{
System.IO.Stream s;
if ((s = d.OpenFile()) != null)
{
mCircuit.Serialize(s);
s.Close();
}
}
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog d = new OpenFileDialog();
d.Filter = "Component files (*.xml)|*.xml|Circuit files (*.xml)|*.xml";
// Set the default path to open files to the directory where the executable is running
//d.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
if (d.ShowDialog() == DialogResult.OK)
{
System.IO.Stream s;
if ((s = d.OpenFile()) != null)
{
mCircuit.Deserialize(s);
s.Close();
// Show each component in the circuit.
foreach (Components.Component c in mCircuit.Components)
{
c.Show(panel1, contextMenuStrip1);
}
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Clear();
}
private Components.Component ShowComponent(Components.Component c)
{
c.Show(panel1, contextMenuStrip1);
return c;
}
private void bulbButton_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.Bulb()));
}
private void switchButton_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.Switch()));
}
private void onToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.On(0)));
}
private void strobeToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.On(1000)));
}
private void horizontalWireToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.HorizontalWire()));
}
private void verticalWireToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.VerticalWire()));
}
private void shortVericalWireToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.ShortVerticalWire()));
}
private void andToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.And()));
}
private void orToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.Or()));
}
private void nandToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.Nand()));
}
private void xorToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.Xor()));
}
private void shortHorizontalWireToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.ShortHorizontalWire()));
}
private void notToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.Not()));
}
private void bitDisplayToolStripMenuItem_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.DigitalDisplay(4)));
}
private void bitDisplayToolStripMenuItem1_Click(object sender, EventArgs e)
{
mCircuit.Add(ShowComponent(new Components.DigitalDisplay(8)));
}
private string GetNameWithoutExtension(string filename)
{
System.IO.FileInfo info = new System.IO.FileInfo(filename);
return info.Name.Remove(info.Name.Length - info.Extension.Length);
}
private void userCreatedToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == DialogResult.OK)
{
System.IO.Stream s;
if ((s = d.OpenFile()) != null)
{
Components.IC ic = new Components.IC(GetNameWithoutExtension(d.FileName));
ic.LoadCircuit(s);
s.Close();
mCircuit.Add(ShowComponent(ic));
}
}
}
class MyToolStripButton : ToolStripButton
{
public MyToolStripButton(string name, Components.ComponentControl component)
: base(name)
{
mComponent = component;
}
public Components.ComponentControl Component
{
get { return mComponent; }
set { mComponent = value; }
}
Components.ComponentControl mComponent;
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
Components.ComponentControl c = contextMenuStrip1.SourceControl as Components.ComponentControl;
contextMenuStrip1.Items.Clear();
ToolStripItem delete = new MyToolStripButton("Delete", c);
delete.Click +=new EventHandler(delete_Click);
contextMenuStrip1.Items.Add(delete);
c.ShowContextMenu(contextMenuStrip1,e);
e.Cancel = false;
}
void delete_Click(object sender, EventArgs e)
{
MyToolStripButton b = sender as MyToolStripButton;
b.Component.DeleteComponent();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox ab = new AboutBox();
ab.ShowDialog();
}
}
}