Remove rogue diagnostic.
[sgt/puzzles] / PuzzleApplet.java
1 /*
2 * PuzzleApplet.java: NestedVM applet for the puzzle collection
3 */
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.awt.image.BufferedImage;
7 import java.util.*;
8 import javax.swing.*;
9 import javax.swing.border.BevelBorder;
10 import javax.swing.Timer;
11 import java.util.List;
12
13 import org.ibex.nestedvm.Runtime;
14
15 public class PuzzleApplet extends JApplet implements Runtime.CallJavaCB {
16
17 private static final long serialVersionUID = 1L;
18
19 private static final int CFG_SETTINGS = 0, CFG_SEED = 1, CFG_DESC = 2,
20 LEFT_BUTTON = 0x0200, MIDDLE_BUTTON = 0x201, RIGHT_BUTTON = 0x202,
21 LEFT_DRAG = 0x203, MIDDLE_DRAG = 0x204, RIGHT_DRAG = 0x205,
22 LEFT_RELEASE = 0x206, CURSOR_UP = 0x209, CURSOR_DOWN = 0x20a,
23 CURSOR_LEFT = 0x20b, CURSOR_RIGHT = 0x20c, MOD_CTRL = 0x1000,
24 MOD_SHFT = 0x2000, MOD_NUM_KEYPAD = 0x4000, ALIGN_VCENTRE = 0x100,
25 ALIGN_HCENTRE = 0x001, ALIGN_HRIGHT = 0x002, C_STRING = 0,
26 C_CHOICES = 1, C_BOOLEAN = 2;
27
28 private JFrame mainWindow;
29
30 private JMenu typeMenu;
31 private JMenuItem solveCommand;
32 private Color[] colors;
33 private JLabel statusBar;
34 private PuzzlePanel pp;
35 private Runtime runtime;
36 private String[] puzzle_args;
37 private Graphics2D gg;
38 private Timer timer;
39 private int xarg1, xarg2, xarg3;
40 private int[] xPoints, yPoints;
41 private BufferedImage[] blitters = new BufferedImage[512];
42 private ConfigDialog dlg;
43
44 static {
45 try {
46 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
47 } catch (Exception ex) {
48 ex.printStackTrace();
49 }
50 }
51
52 public void init() {
53 try {
54 Container cp = getContentPane();
55 cp.setLayout(new BorderLayout());
56 runtime = (Runtime) Class.forName("PuzzleEngine").newInstance();
57 runtime.setCallJavaCB(this);
58 JMenuBar menubar = new JMenuBar();
59 JMenu jm;
60 menubar.add(jm = new JMenu("Game"));
61 addMenuItemWithKey(jm, "New", 'n');
62 addMenuItemCallback(jm, "Restart", "jcallback_restart_event");
63 addMenuItemCallback(jm, "Specific...", "jcallback_config_event", CFG_DESC);
64 addMenuItemCallback(jm, "Random Seed...", "jcallback_config_event", CFG_SEED);
65 jm.addSeparator();
66 addMenuItemWithKey(jm, "Undo", 'u');
67 addMenuItemWithKey(jm, "Redo", 'r');
68 jm.addSeparator();
69 solveCommand = addMenuItemCallback(jm, "Solve", "jcallback_solve_event");
70 solveCommand.setEnabled(false);
71 if (mainWindow != null) {
72 jm.addSeparator();
73 addMenuItemWithKey(jm, "Exit", 'q');
74 }
75 menubar.add(typeMenu = new JMenu("Type"));
76 typeMenu.setVisible(false);
77 menubar.add(jm = new JMenu("Help"));
78 addMenuItemCallback(jm, "About", "jcallback_about_event");
79 setJMenuBar(menubar);
80 cp.add(pp = new PuzzlePanel(), BorderLayout.CENTER);
81 pp.addKeyListener(new KeyAdapter() {
82 public void keyPressed(KeyEvent e) {
83 int key = -1;
84 int shift = e.isShiftDown() ? MOD_SHFT : 0;
85 int ctrl = e.isControlDown() ? MOD_CTRL : 0;
86 switch (e.getKeyCode()) {
87 case KeyEvent.VK_LEFT:
88 case KeyEvent.VK_KP_LEFT:
89 key = shift | ctrl | CURSOR_LEFT;
90 break;
91 case KeyEvent.VK_RIGHT:
92 case KeyEvent.VK_KP_RIGHT:
93 key = shift | ctrl | CURSOR_RIGHT;
94 break;
95 case KeyEvent.VK_UP:
96 case KeyEvent.VK_KP_UP:
97 key = shift | ctrl | CURSOR_UP;
98 break;
99 case KeyEvent.VK_DOWN:
100 case KeyEvent.VK_KP_DOWN:
101 key = shift | ctrl | CURSOR_DOWN;
102 break;
103 case KeyEvent.VK_PAGE_UP:
104 key = shift | ctrl | MOD_NUM_KEYPAD | '9';
105 break;
106 case KeyEvent.VK_PAGE_DOWN:
107 key = shift | ctrl | MOD_NUM_KEYPAD | '3';
108 break;
109 case KeyEvent.VK_HOME:
110 key = shift | ctrl | MOD_NUM_KEYPAD | '7';
111 break;
112 case KeyEvent.VK_END:
113 key = shift | ctrl | MOD_NUM_KEYPAD | '1';
114 break;
115 default:
116 if (e.getKeyCode() >= KeyEvent.VK_NUMPAD0 && e.getKeyCode() <=KeyEvent.VK_NUMPAD9) {
117 key = MOD_NUM_KEYPAD | (e.getKeyCode() - KeyEvent.VK_NUMPAD0+'0');
118 }
119 break;
120 }
121 if (key != -1) {
122 runtimeCall("jcallback_key_event", new int[] {0, 0, key});
123 }
124 }
125 public void keyTyped(KeyEvent e) {
126 runtimeCall("jcallback_key_event", new int[] {0, 0, e.getKeyChar()});
127 }
128 });
129 pp.addMouseListener(new MouseAdapter() {
130 public void mouseReleased(MouseEvent e) {
131 mousePressedReleased(e, true);
132 }
133 public void mousePressed(MouseEvent e) {
134 pp.requestFocus();
135 mousePressedReleased(e, false);
136 }
137 private void mousePressedReleased(MouseEvent e, boolean released) {
138 int button;
139 if ((e.getModifiers() & (InputEvent.BUTTON2_MASK | InputEvent.SHIFT_MASK)) != 0)
140 button = MIDDLE_BUTTON;
141 else if ((e.getModifiers() & (InputEvent.BUTTON3_MASK | InputEvent.ALT_MASK)) != 0)
142 button = RIGHT_BUTTON;
143 else if ((e.getModifiers() & (InputEvent.BUTTON1_MASK)) != 0)
144 button = LEFT_BUTTON;
145 else
146 return;
147 if (released)
148 button += LEFT_RELEASE - LEFT_BUTTON;
149 runtimeCall("jcallback_key_event", new int[] {e.getX(), e.getY(), button});
150 }
151 });
152 pp.addMouseMotionListener(new MouseMotionAdapter() {
153 public void mouseDragged(MouseEvent e) {
154 int button;
155 if ((e.getModifiers() & (InputEvent.BUTTON2_MASK | InputEvent.SHIFT_MASK)) != 0)
156 button = MIDDLE_DRAG;
157 else if ((e.getModifiers() & (InputEvent.BUTTON3_MASK | InputEvent.ALT_MASK)) != 0)
158 button = RIGHT_DRAG;
159 else
160 button = LEFT_DRAG;
161 runtimeCall("jcallback_key_event", new int[] {e.getX(), e.getY(), button});
162 }
163 });
164 pp.addComponentListener(new ComponentAdapter() {
165 public void componentResized(ComponentEvent e) {
166 handleResized();
167 }
168 });
169 pp.setFocusable(true);
170 pp.requestFocus();
171 timer = new Timer(20, new ActionListener() {
172 public void actionPerformed(ActionEvent e) {
173 runtimeCall("jcallback_timer_func", new int[0]);
174 }
175 });
176 String gameid;
177 try {
178 gameid = getParameter("game_id");
179 } catch (java.lang.NullPointerException ex) {
180 gameid = null;
181 }
182 if (gameid == null) {
183 puzzle_args = null;
184 } else {
185 puzzle_args = new String[2];
186 puzzle_args[0] = "puzzle";
187 puzzle_args[1] = gameid;
188 }
189 SwingUtilities.invokeLater(new Runnable() {
190 public void run() {
191 runtime.start(puzzle_args);
192 runtime.execute();
193 }
194 });
195 } catch (Exception ex) {
196 ex.printStackTrace();
197 }
198 }
199
200 public void destroy() {
201 SwingUtilities.invokeLater(new Runnable() {
202 public void run() {
203 runtime.execute();
204 if (mainWindow != null) {
205 mainWindow.dispose();
206 System.exit(0);
207 }
208 }
209 });
210 }
211
212 protected void handleResized() {
213 pp.createBackBuffer(pp.getWidth(), pp.getHeight(), colors[0]);
214 runtimeCall("jcallback_resize", new int[] {pp.getWidth(), pp.getHeight()});
215 }
216
217 private void addMenuItemWithKey(JMenu jm, String name, int key) {
218 addMenuItemCallback(jm, name, "jcallback_menu_key_event", key);
219 }
220
221 private JMenuItem addMenuItemCallback(JMenu jm, String name, final String callback, final int arg) {
222 return addMenuItemCallback(jm, name, callback, new int[] {arg});
223 }
224
225 private JMenuItem addMenuItemCallback(JMenu jm, String name, final String callback) {
226 return addMenuItemCallback(jm, name, callback, new int[0]);
227 }
228
229 private JMenuItem addMenuItemCallback(JMenu jm, String name, final String callback, final int[] args) {
230 JMenuItem jmi;
231 if (jm == typeMenu)
232 typeMenu.add(jmi = new JCheckBoxMenuItem(name));
233 else
234 jm.add(jmi = new JMenuItem(name));
235 jmi.addActionListener(new ActionListener() {
236 public void actionPerformed(ActionEvent e) {
237 runtimeCall(callback, args);
238 }
239 });
240 return jmi;
241 }
242
243 protected void runtimeCall(String func, int[] args) {
244 if (runtimeCallWithResult(func, args) == 42 && mainWindow != null) {
245 destroy();
246 }
247 }
248
249 protected int runtimeCallWithResult(String func, int[] args) {
250 try {
251 return runtime.call(func, args);
252 } catch (Runtime.CallException ex) {
253 ex.printStackTrace();
254 return 42;
255 }
256 }
257
258 private void buildConfigureMenuItem() {
259 if (typeMenu.isVisible()) {
260 typeMenu.addSeparator();
261 } else {
262 typeMenu.setVisible(true);
263 }
264 addMenuItemCallback(typeMenu, "Custom...", "jcallback_config_event", CFG_SETTINGS);
265 }
266
267 private void addTypeItem(String name, final int ptrGameParams) {
268 typeMenu.setVisible(true);
269 addMenuItemCallback(typeMenu, name, "jcallback_preset_event", ptrGameParams);
270 }
271
272 public int call(int cmd, int arg1, int arg2, int arg3) {
273 try {
274 switch(cmd) {
275 case 0: // initialize
276 if (mainWindow != null) mainWindow.setTitle(runtime.cstring(arg1));
277 if ((arg2 & 1) != 0) buildConfigureMenuItem();
278 if ((arg2 & 2) != 0) addStatusBar();
279 if ((arg2 & 4) != 0) solveCommand.setEnabled(true);
280 colors = new Color[arg3];
281 return 0;
282 case 1: // Type menu item
283 addTypeItem(runtime.cstring(arg1), arg2);
284 return 0;
285 case 2: // MessageBox
286 JOptionPane.showMessageDialog(this, runtime.cstring(arg2), runtime.cstring(arg1), arg3 == 0 ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE);
287 return 0;
288 case 3: // Resize
289 pp.setPreferredSize(new Dimension(arg1, arg2));
290 if (mainWindow != null) mainWindow.pack();
291 handleResized();
292 if (mainWindow != null) mainWindow.setVisible(true);
293 return 0;
294 case 4: // drawing tasks
295 switch(arg1) {
296 case 0:
297 String text = runtime.cstring(arg2);
298 if (text.equals("")) text = " ";
299 System.out.println("status '" + text + "'");
300 statusBar.setText(text); break;
301 case 1:
302 gg = pp.backBuffer.createGraphics();
303 if (arg2 != 0 || arg3 != 0) {
304 gg.setColor(Color.black);
305 gg.fillRect(0, 0, arg2, getHeight());
306 gg.fillRect(0, 0, getWidth(), arg3);
307 gg.fillRect(getWidth() - arg2, 0, arg2, getHeight());
308 gg.fillRect(0, getHeight() - arg3, getWidth(), arg3);
309 gg.setClip(arg2, arg3, getWidth()-2*arg2, getHeight()-2*arg3);
310 }
311 break;
312 case 2: gg.dispose(); pp.repaint(); break;
313 case 3: gg.setClip(arg2, arg3, xarg1, xarg2); break;
314 case 4:
315 if (arg2 == 0 && arg3 == 0) {
316 gg.fillRect(0, 0, getWidth(), getHeight());
317 } else {
318 gg.setClip(arg2, arg3, getWidth()-2*arg2, getHeight()-2*arg3);
319 }
320 break;
321 case 5:
322 gg.setColor(colors[xarg3]);
323 gg.fillRect(arg2, arg3, xarg1, xarg2);
324 break;
325 case 6:
326 gg.setColor(colors[xarg3]);
327 gg.drawLine(arg2, arg3, xarg1, xarg2);
328 break;
329 case 7:
330 xPoints = new int[arg2];
331 yPoints = new int[arg2];
332 break;
333 case 8:
334 if (arg3 != -1) {
335 gg.setColor(colors[arg3]);
336 gg.fillPolygon(xPoints, yPoints, xPoints.length);
337 }
338 gg.setColor(colors[arg2]);
339 gg.drawPolygon(xPoints, yPoints, xPoints.length);
340 break;
341 case 9:
342 if (arg3 != -1) {
343 gg.setColor(colors[arg3]);
344 gg.fillOval(xarg1-xarg3, xarg2-xarg3, xarg3*2, xarg3*2);
345 }
346 gg.setColor(colors[arg2]);
347 gg.drawOval(xarg1-xarg3, xarg2-xarg3, xarg3*2, xarg3*2);
348 break;
349 case 10:
350 for(int i=0; i<blitters.length; i++) {
351 if (blitters[i] == null) {
352 blitters[i] = new BufferedImage(arg2, arg3, BufferedImage.TYPE_3BYTE_BGR);
353 return i;
354 }
355 }
356 throw new RuntimeException("No free blitter found!");
357 case 11: blitters[arg2] = null; break;
358 case 12:
359 timer.start(); break;
360 case 13:
361 timer.stop(); break;
362 }
363 return 0;
364 case 5: // more arguments
365 xarg1 = arg1;
366 xarg2 = arg2;
367 xarg3 = arg3;
368 return 0;
369 case 6: // polygon vertex
370 xPoints[arg1]=arg2;
371 yPoints[arg1]=arg3;
372 return 0;
373 case 7: // string
374 gg.setColor(colors[arg2]);
375 {
376 String text = runtime.cstring(arg3);
377 Font ft = new Font((xarg3 & 0x10) != 0 ? "Monospaced" : "Dialog",
378 Font.PLAIN, 100);
379 int height100 = this.getFontMetrics(ft).getHeight();
380 ft = ft.deriveFont(arg1 * 100 / (float)height100);
381 FontMetrics fm = this.getFontMetrics(ft);
382 int asc = fm.getAscent(), desc = fm.getDescent();
383 if ((xarg3 & ALIGN_VCENTRE) != 0)
384 xarg2 += asc - (asc+desc)/2;
385 else
386 xarg2 += asc;
387 int wid = fm.stringWidth(text);
388 if ((xarg3 & ALIGN_HCENTRE) != 0)
389 xarg1 -= wid / 2;
390 else if ((xarg3 & ALIGN_HRIGHT) != 0)
391 xarg1 -= wid;
392 gg.setFont(ft);
393 gg.drawString(text, xarg1, xarg2);
394 }
395 return 0;
396 case 8: // blitter_save
397 Graphics g2 = blitters[arg1].createGraphics();
398 g2.drawImage(pp.backBuffer, 0, 0, blitters[arg1].getWidth(), blitters[arg1].getHeight(),
399 arg2, arg3, arg2 + blitters[arg1].getWidth(), arg3 + blitters[arg1].getHeight(), this);
400 g2.dispose();
401 return 0;
402 case 9: // blitter_load
403 gg.drawImage(blitters[arg1], arg2, arg3, this);
404 return 0;
405 case 10: // dialog_init
406 dlg= new ConfigDialog(this, runtime.cstring(arg1));
407 return 0;
408 case 11: // dialog_add_control
409 {
410 int sval_ptr = arg1;
411 int ival = arg2;
412 int ptr = xarg1;
413 int type=xarg2;
414 String name = runtime.cstring(xarg3);
415 switch(type) {
416 case C_STRING:
417 dlg.addTextBox(ptr, name, runtime.cstring(sval_ptr));
418 break;
419 case C_BOOLEAN:
420 dlg.addCheckBox(ptr, name, ival != 0);
421 break;
422 case C_CHOICES:
423 dlg.addComboBox(ptr, name, runtime.cstring(sval_ptr), ival);
424 }
425 }
426 return 0;
427 case 12:
428 dlg.finish();
429 dlg = null;
430 return 0;
431 case 13: // tick a menu item
432 if (arg1 < 0) arg1 = typeMenu.getItemCount() - 1;
433 for (int i = 0; i < typeMenu.getItemCount(); i++) {
434 if (typeMenu.getMenuComponent(i) instanceof JCheckBoxMenuItem) {
435 ((JCheckBoxMenuItem)typeMenu.getMenuComponent(i)).setSelected(arg1 == i);
436 }
437 }
438 return 0;
439 default:
440 if (cmd >= 1024 && cmd < 2048) { // palette
441 colors[cmd-1024] = new Color(arg1, arg2, arg3);
442 }
443 if (cmd == 1024) {
444 pp.setBackground(colors[0]);
445 if (statusBar != null) statusBar.setBackground(colors[0]);
446 this.setBackground(colors[0]);
447 }
448 return 0;
449 }
450 } catch (Throwable ex) {
451 ex.printStackTrace();
452 System.exit(-1);
453 return 0;
454 }
455 }
456
457 private void addStatusBar() {
458 statusBar = new JLabel("test");
459 statusBar.setBorder(new BevelBorder(BevelBorder.LOWERED));
460 getContentPane().add(BorderLayout.SOUTH,statusBar);
461 }
462
463 // Standalone runner
464 public static void main(String[] args) {
465 final PuzzleApplet a = new PuzzleApplet();
466 JFrame jf = new JFrame("Loading...");
467 jf.getContentPane().setLayout(new BorderLayout());
468 jf.getContentPane().add(a, BorderLayout.CENTER);
469 a.mainWindow=jf;
470 a.init();
471 a.start();
472 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
473 jf.addWindowListener(new WindowAdapter() {
474 public void windowClosing(WindowEvent e) {
475 a.stop();
476 a.destroy();
477 }
478 });
479 jf.setVisible(true);
480 }
481
482 public static class PuzzlePanel extends JPanel {
483
484 private static final long serialVersionUID = 1L;
485 protected BufferedImage backBuffer;
486
487 public PuzzlePanel() {
488 setPreferredSize(new Dimension(100,100));
489 createBackBuffer(100,100, Color.black);
490 }
491
492 public void createBackBuffer(int w, int h, Color bg) {
493 if (w > 0 && h > 0) {
494 backBuffer = new BufferedImage(w,h, BufferedImage.TYPE_3BYTE_BGR);
495 Graphics g = backBuffer.createGraphics();
496 g.setColor(bg);
497 g.fillRect(0, 0, w, h);
498 g.dispose();
499 }
500 }
501
502 protected void paintComponent(Graphics g) {
503 g.drawImage(backBuffer, 0, 0, this);
504 }
505 }
506
507 public static class ConfigComponent {
508 public int type;
509 public int configItemPointer;
510 public JComponent component;
511
512 public ConfigComponent(int type, int configItemPointer, JComponent component) {
513 this.type = type;
514 this.configItemPointer = configItemPointer;
515 this.component = component;
516 }
517 }
518
519 public class ConfigDialog extends JDialog {
520
521 private GridBagConstraints gbcLeft = new GridBagConstraints(
522 GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, 1, 1,
523 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
524 new Insets(0, 0, 0, 0), 0, 0);
525 private GridBagConstraints gbcRight = new GridBagConstraints(
526 GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE,
527 GridBagConstraints.REMAINDER, 1, 1.0, 0,
528 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
529 new Insets(5, 5, 5, 5), 0, 0);
530 private GridBagConstraints gbcBottom = new GridBagConstraints(
531 GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE,
532 GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER,
533 1.0, 1.0, GridBagConstraints.CENTER,
534 GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
535
536 private static final long serialVersionUID = 1L;
537 private List components = new ArrayList();
538
539 public ConfigDialog(JApplet parent, String title) {
540 super(JOptionPane.getFrameForComponent(parent), title, true);
541 getContentPane().setLayout(new GridBagLayout());
542 }
543
544 public void addTextBox(int ptr, String name, String value) {
545 getContentPane().add(new JLabel(name), gbcLeft);
546 JComponent c = new JTextField(value, 25);
547 getContentPane().add(c, gbcRight);
548 components.add(new ConfigComponent(C_STRING, ptr, c));
549 }
550
551
552 public void addCheckBox(int ptr, String name, boolean selected) {
553 JComponent c = new JCheckBox(name, selected);
554 getContentPane().add(c, gbcRight);
555 components.add(new ConfigComponent(C_BOOLEAN, ptr, c));
556 }
557
558 public void addComboBox(int ptr, String name, String values, int selected) {
559 getContentPane().add(new JLabel(name), gbcLeft);
560 StringTokenizer st = new StringTokenizer(values.substring(1), values.substring(0,1));
561 JComboBox c = new JComboBox();
562 c.setEditable(false);
563 while(st.hasMoreTokens())
564 c.addItem(st.nextToken());
565 c.setSelectedIndex(selected);
566 getContentPane().add(c, gbcRight);
567 components.add(new ConfigComponent(C_CHOICES, ptr, c));
568 }
569
570 public void finish() {
571 JPanel buttons = new JPanel(new GridLayout(1, 2, 5, 5));
572 getContentPane().add(buttons, gbcBottom);
573 JButton b;
574 buttons.add(b=new JButton("OK"));
575 b.addActionListener(new ActionListener() {
576 public void actionPerformed(ActionEvent e) {
577 save();
578 dispose();
579 }
580 });
581 getRootPane().setDefaultButton(b);
582 buttons.add(b=new JButton("Cancel"));
583 b.addActionListener(new ActionListener() {
584 public void actionPerformed(ActionEvent e) {
585 dispose();
586 }
587 });
588 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
589 pack();
590 setLocationRelativeTo(null);
591 setVisible(true);
592 }
593 private void save() {
594 for (int i = 0; i < components.size(); i++) {
595 ConfigComponent cc = (ConfigComponent) components.get(i);
596 switch(cc.type) {
597 case C_STRING:
598 JTextField jtf = (JTextField)cc.component;
599 runtimeCall("jcallback_config_set_string", new int[] {cc.configItemPointer, runtime.strdup(jtf.getText())});
600 break;
601 case C_BOOLEAN:
602 JCheckBox jcb = (JCheckBox)cc.component;
603 runtimeCall("jcallback_config_set_boolean", new int[] {cc.configItemPointer, jcb.isSelected()?1:0});
604 break;
605 case C_CHOICES:
606 JComboBox jcm = (JComboBox)cc.component;
607 runtimeCall("jcallback_config_set_boolean", new int[] {cc.configItemPointer, jcm.getSelectedIndex()});
608 break;
609 }
610 }
611 runtimeCall("jcallback_config_ok", new int[0]);
612 }
613 }
614 }