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