WarpedMonitor.java
4.43 KB
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
package org.mote.wiimote.whiteboard.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JPanel;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.mote.wiimote.whiteboard.WiimoteDataHandler;
import org.mote.wiimote.whiteboard.WiimoteWhiteboard;
import org.mote.wiimote.whiteboard.WiimoteDataHandler.WiimoteDataListener;
import org.mote.wiimote.whiteboard.calibration.WiimoteCalibration;
import org.mote.wiimote.whiteboard.ds.IRDot;
import org.mote.wiimote.whiteboard.ds.Wiimote;
import org.mote.wiimote.whiteboard.preferences.WWPreferences;
import org.mote.wiimote.whiteboard.util.Util;
@SuppressWarnings("serial")
public class WarpedMonitor extends JDialog implements WiimoteDataListener {
private static final int REPAINT_FREQ = 1000 / 50;
static final int RADIUS = 10;
private JPanel canvas;
private Point2D[][] lights = new Point2D[WWPreferences.WIIMOTES+1][4];
private LightLabel[][] labels = new LightLabel[WWPreferences.WIIMOTES+1][4];
private WiimoteCalibration calibration;
private WiimoteDataHandler dh;
public WarpedMonitor(WiimoteDataHandler dh, final WiimoteCalibration calibration) {
super(Application.getInstance(WiimoteWhiteboard.class).getMainFrame(), "Warped Point Monitor");
setLayout(new BorderLayout());
dh.addWiimoteDataListener(this);
this.calibration = calibration;
this.dh = dh;
((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
canvas = new JPanel(null, true);
canvas.setBorder(BorderFactory.createLineBorder(Color.black));
add(canvas, BorderLayout.CENTER);
setUndecorated(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_F || e.getKeyCode() == KeyEvent.VK_ESCAPE) {
GraphicsDevice screen = calibration.getScreen();
if (screen != null) {
if (screen.getFullScreenWindow() == WarpedMonitor.this) {
((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
screen.setFullScreenWindow(null);
Util.placeDialogWindow(WarpedMonitor.this, 640, 480);
} else {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
WarpedMonitor.this.setVisible(false);
else {
((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder());
screen.setFullScreenWindow(WarpedMonitor.this);
}
}
}
}
}
});
Util.placeDialogWindow(this, 640, 480);
new Timer(true).schedule(new UpdateTask(), 0, REPAINT_FREQ);
}
@Action
public void monitor() {
setVisible(true);
}
private class UpdateTask extends TimerTask {
@Override
public void run() {
if (isVisible()) {
for (int i = WWPreferences.WIIMOTES; i >= 0; i--)
for (int j = 0; j < 4; j++) {
if (labels[i][j] != null)
labels[i][j].update();
}
}
}
}
public void irLights(Wiimote wiimote, IRDot[] lights) {
}
public void irWarped(Map<Wiimote, IRDot[]> data, Point[] warped) {
if (isVisible()) {
Rectangle bounds = calibration.getScreen().getDefaultConfiguration().getBounds();
for (int i = 0; i < 4; i++) {
final Point w = warped[i];
this.lights[0][i] = w == null ? null : new Point2D.Double(w.getX() / bounds.getWidth(), 1 - w.getY() / bounds.getHeight());
}
for (Wiimote wiimote : dh.getConnectedWiimotes()) {
for (int i = 0; i < 4; i++) {
final Point2D w = calibration.warp(i, wiimote, data);
this.lights[wiimote.getId()][i] = w == null ? null : new Point2D.Double(w.getX() / bounds.getWidth(), 1 - w.getY() / bounds.getHeight());
}
}
}
}
public void batteryLevel(Wiimote wiimote, double level) {
}
public void wiimoteConnected(Wiimote wiimote) {
if (wiimote.getId() == 1)
for (int i = 0; i < 4; i++)
canvas.add(labels[0][i] = new LightLabel(canvas, lights, 0, i + 1, Color.red));
for (int i = 0; i < 4; i++)
canvas.add(labels[wiimote.getId()][i] = new LightLabel(canvas, lights, wiimote.getId(), i + 1));
}
public void wiimoteDisconnected(Wiimote wiimote) {
for (int i = 0; i < 4; i++) {
canvas.remove(labels[wiimote.getId()][i]);
labels[wiimote.getId()][i] = null;
}
}
}