Reformatted everything. Eclipse built-in Linewrapping/Comments 140 chars
[Mograsim.git] / era.mi / src / era / mi / logic / tests / GUITest.java
1 package era.mi.logic.tests;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.Rectangle;
7 import java.awt.RenderingHints;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import javax.swing.JFrame;
15 import javax.swing.JPanel;
16 import javax.swing.WindowConstants;
17
18 import era.mi.logic.Simulation;
19 import era.mi.logic.components.ManualSwitch;
20 import era.mi.logic.components.gates.NotGate;
21 import era.mi.logic.components.gates.OrGate;
22 import era.mi.logic.timeline.Timeline.ExecutionResult;
23 import era.mi.logic.wires.WireArray;
24
25 public class GUITest extends JPanel {
26
27         private static final long serialVersionUID = 1L;
28
29         private static final int WIRE_DELAY = 40;
30         private static final int OR_DELAY = 100;
31         private static final int NOT_DELAY = 100;
32
33         WireArray r = new WireArray(1, WIRE_DELAY);
34         WireArray s = new WireArray(1, WIRE_DELAY);
35         WireArray t1 = new WireArray(1, WIRE_DELAY);
36         WireArray t2 = new WireArray(1, WIRE_DELAY);
37         WireArray q = new WireArray(1, WIRE_DELAY);
38         WireArray nq = new WireArray(1, WIRE_DELAY);
39
40         ManualSwitch rIn = new ManualSwitch(r);
41         ManualSwitch sIn = new ManualSwitch(s);
42
43         OrGate or1 = new OrGate(OR_DELAY, t2, r, nq);
44         OrGate or2 = new OrGate(OR_DELAY, t1, s, q);
45         NotGate not1 = new NotGate(NOT_DELAY, t2, q);
46         NotGate not2 = new NotGate(NOT_DELAY, t1, nq);
47
48         Map<ManualSwitch, Rectangle> switchMap = new HashMap<>();
49
50         int height;
51         int width;
52         boolean sizeChanged;
53
54         public GUITest() {
55                 addMouseListener(new MouseListener() {
56
57                         @Override
58                         public void mouseReleased(MouseEvent e) {
59                                 for (Entry<ManualSwitch, Rectangle> dim : switchMap.entrySet()) {
60                                         if (dim.getValue().contains(e.getPoint())) {
61                                                 dim.getKey().switchOff();
62                                                 repaint();
63                                         }
64                                 }
65                         }
66
67                         @Override
68                         public void mousePressed(MouseEvent e) {
69                                 for (Entry<ManualSwitch, Rectangle> dim : switchMap.entrySet()) {
70                                         if (dim.getValue().contains(e.getPoint())) {
71                                                 dim.getKey().switchOn();
72                                                 repaint();
73                                         }
74                                 }
75                         }
76
77                         @Override
78                         public void mouseExited(MouseEvent e) {
79                                 // none
80                         }
81
82                         @Override
83                         public void mouseEntered(MouseEvent e) {
84                                 // none
85                         }
86
87                         @Override
88                         public void mouseClicked(MouseEvent e) {
89                                 // If you want toggle buttons, use this code instead
90 //                              for (Entry<ManualSwitch, Rectangle> dim : switchMap.entrySet()) {
91 //                                      if (dim.getValue().contains(e.getPoint())) {
92 //                                              dim.getKey().toggle();
93 //                                              repaint();
94 //                                      }
95 //                              }
96                         }
97                 });
98         }
99
100         @Override
101         public void paint(Graphics some_g) {
102                 super.paint(some_g);
103                 Graphics2D g = ((Graphics2D) some_g);
104                 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
105                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
106                 g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
107
108                 checkSizeChange();
109                 adaptFont(g);
110
111                 drawWire(g, r, "r", 2, 9, 4, 9);
112
113                 drawWire(g, s, "s", 2, 3, 4, 3);
114
115                 drawWire(g, t2, "t2", 5, 8.5, 6, 8.5);
116
117                 drawWire(g, t1, "t1", 5, 3.5, 6, 3.5);
118
119                 drawWire(g, q, "q", 7, 8.5, 9, 8.5);
120
121                 drawWire(g, nq, "nq", 7, 3.5, 9, 3.5);
122
123                 drawWire(g, q, "", 7.5, 8.5, 7.5, 7.5);
124                 drawWire(g, q, "", 7.5, 7.5, 3, 4.5);
125                 drawWire(g, q, "", 3, 4.5, 3, 4);
126                 drawWire(g, q, "q", 3, 4, 4, 4);
127
128                 drawWire(g, nq, "", 7.5, 3.5, 7.5, 4.5);
129                 drawWire(g, nq, "", 7.5, 4.5, 3, 7.5);
130                 drawWire(g, nq, "", 3, 7.5, 3, 8);
131                 drawWire(g, nq, "nq", 3, 8, 4, 8);
132
133                 drawSquare(g, 4, 8, "OR");
134                 drawSquare(g, 4, 3, "OR");
135
136                 drawSquare(g, 6, 8, "NOT");
137                 drawSquare(g, 6, 3, "NOT");
138
139                 drawSwitch(g, rIn, "Switch R", 0.5, 8.25, 2, 9.75);
140                 drawSwitch(g, sIn, "Switch S", 0.5, 2.25, 2, 3.75);
141
142                 drawString(g, "Hint: drag the cursor out of the pressed switch to keep it's state", 5, 0, 0.0, 1.0);
143         }
144
145         private void checkSizeChange() {
146                 sizeChanged = height != getHeight() || width != getWidth();
147                 if (sizeChanged) {
148                         height = getHeight();
149                         width = getWidth();
150                 }
151         }
152
153         private void adaptFont(Graphics g) {
154                 g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f));
155         }
156
157         private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY) {
158                 int h = g.getFontMetrics().getAscent();
159                 int w = g.getFontMetrics().stringWidth(s);
160                 g.drawString(s, x - (int) (w * anchorX), y + (int) (h * anchorY));
161         }
162
163         private void drawWire(Graphics g, WireArray wa, String name, double x1, double y1, double x2, double y2) {
164                 setTo(g, wa);
165                 g.drawLine(gX(x1), gY(y1), gX(x2), gY(y2));
166                 drawString(g, name, (gX(x1) + gX(x2)) / 2, (gY(y1) + gY(y2)) / 2 - 5, 0, 0);
167         }
168
169         private void drawSquare(Graphics g, int posX, int posY, String text) {
170                 int x1 = gX(posX) - 5;
171                 int x2 = gX(posX + 1) + 5;
172                 int y1 = gY(posY) - 5;
173                 int y2 = gY(posY + 1) + 5;
174
175                 g.setColor(Color.WHITE);
176                 g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
177                 setBlack(g);
178                 g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
179                 drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5);
180
181         }
182
183         private void drawSwitch(Graphics g, ManualSwitch ms, String text, double posX1, double posY1, double posX2, double posY2) {
184                 int x1 = gX(posX1) - 5;
185                 int x2 = gX(posX2) + 5;
186                 int y1 = gY(posY1) - 5;
187                 int y2 = gY(posY2) + 5;
188
189                 if (sizeChanged) {
190                         Rectangle r = new Rectangle(x1, y1, x2 - x1, y2 - y1);
191                         switchMap.put(ms, r);
192                 }
193
194                 g.setColor(ms.isOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE);
195                 g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
196                 setBlack(g);
197                 g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
198                 drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5);
199         }
200
201         private static void setBlack(Graphics g) {
202                 g.setColor(Color.BLACK);
203         }
204
205         private static void setTo(Graphics g, WireArray wa) {
206                 switch (wa.getValue()) {
207                 case ONE:
208                         g.setColor(Color.GREEN);
209                         break;
210                 case X:
211                         g.setColor(Color.RED);
212                         break;
213                 case Z:
214                         g.setColor(Color.DARK_GRAY);
215                         break;
216                 case ZERO:
217                         g.setColor(Color.BLACK);
218                         break;
219                 default:
220                         throw new IllegalArgumentException();
221                 }
222         }
223
224         private int gY(double pos) {
225                 return (int) (pos * height / 11);
226         }
227
228         private int gX(double pos) {
229                 return (int) (pos * width / 11) + 50;
230         }
231
232         public static void main(String[] args) {
233                 JFrame f = new JFrame("Test circuit 1.0.0");
234                 GUITest gt = new GUITest();
235                 f.add(gt);
236                 f.setSize(800, 600);
237                 f.setLocation(500, 400);
238                 f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
239                 f.setVisible(true);
240
241                 long begin = System.currentTimeMillis();
242
243                 long lastFrame = begin;
244                 long updateT = 16;
245
246                 while (f.isVisible()) {
247                         ExecutionResult er = Simulation.TIMELINE.executeUpTo((lastFrame - begin) * 3, lastFrame + 14);
248 //                              if (Simulation.TIMELINE.hasNext()) 
249 //                              Simulation.TIMELINE.executeNext();
250                         if (er != ExecutionResult.NOTHING_DONE)
251                                 gt.repaint(12);
252                         try {
253                                 Thread.sleep(Math.max(16 - System.currentTimeMillis() + lastFrame, 0));
254                         } catch (Exception e) {
255                                 e.printStackTrace();
256                         }
257                         lastFrame = System.currentTimeMillis();
258                 }
259         }
260 }