ScreenSelector.java~
10.6 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (C) 2008-2009, Uwe Schmidt
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* The Software uses a third-party library (WiiRemoteJ) which is not part of
* the Software and is subject to its own license.
*/
package org.uweschmidt.wiimote.whiteboard.gui;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.media.jai.PerspectiveTransform;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import org.uweschmidt.wiimote.whiteboard.WiimoteDataHandler;
import org.uweschmidt.wiimote.whiteboard.WiimoteDataHandler.WiimoteDataListener;
import org.uweschmidt.wiimote.whiteboard.calibration.WiimoteCalibration;
import org.uweschmidt.wiimote.whiteboard.ds.IRDot;
import org.uweschmidt.wiimote.whiteboard.ds.Wiimote;
import org.uweschmidt.wiimote.whiteboard.util.Util;
@SuppressWarnings("serial")
public class ScreenSelector extends JPanel implements WiimoteDataListener {
private static final GraphicsDevice DEFAULT_SCREEN = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
private static final int MAX_H = 130;
private static final int MAX_W = 185;
private static final long REPAINT_FREQ = 1000 / 20;
private WiimoteCalibration calibration;
// private WiimoteDataHandler dh;
private List<ScreenBox> screenBoxes = new LinkedList<ScreenBox>();
private Point lastCursor = null, cursor = null;
public ScreenSelector(WiimoteCalibration calibration, WiimoteDataHandler dh) {
super(null);
this.calibration = calibration;
// this.dh = dh;
dh.addWiimoteDataListener(this);
final GraphicsDevice[] screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
// calculate maximum screen bounds (virtual device)
Rectangle r = new Rectangle();
for (GraphicsDevice s : screens) {
r = r.union(s.getDefaultConfiguration().getBounds());
}
final int maxH = MAX_H - (screens.length == 1 ? 30 : 0);
// proportional bounds of panel
int w = MAX_W;
int h = (int) Math.round((w * r.getHeight()) / r.getWidth());
// if too high
if (h > maxH) {
h = maxH;
w = (int) Math.round((h * r.getWidth()) / r.getHeight());
}
setPreferredSize(new Dimension(w, h));
// map screen bounds to panel bounds
PerspectiveTransform t = PerspectiveTransform.getQuadToQuad(
r.getMinX(), r.getMinY(), r.getMaxX(), r.getMinY(),
r.getMaxX(), r.getMaxY(), r.getMinX(), r.getMaxY(),
0, 0, w, 0, w, h, 0, h);
// TODO temporary until multiple screens are allowed
ButtonGroup bg = new ButtonGroup();
for (int i = 0; i < screens.length; i++) {
Rectangle b = screens[i].getDefaultConfiguration().getBounds();
final ScreenBox sb = new ScreenBox(screens[i], i, transformBounds(t, b), screens[i].equals(DEFAULT_SCREEN));
bg.add(sb);
screenBoxes.add(sb);
add(sb);
}
new Timer(true).schedule(new UpdateTask(), 0, REPAINT_FREQ);
}
private class UpdateTask extends TimerTask {
@Override
public void run() {
if (cursor != lastCursor) {
for (ScreenBox sb : screenBoxes) {
if (sb.isSelected() && sb.isEnabled()) {
lastCursor = cursor;
sb.repaint();
}
}
}
}
}
private Rectangle transformBounds(PerspectiveTransform t, Rectangle b) {
Point2D ul = t.transform(new Point2D.Double(b.getMinX(), b.getMinY()), null);
Point2D ur = t.transform(new Point2D.Double(b.getMaxX(), b.getMinY()), null);
Point2D ll = t.transform(new Point2D.Double(b.getMinX(), b.getMaxY()), null);
return new Rectangle2D.Double(ul.getX(), ul.getY(), ul.distance(ur), ul.distance(ll)).getBounds();
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
enableScreenBoxes(enabled);
}
// TODO use JToggleButton instead of JRadioButton or JCheckBox
private class ScreenBox extends JRadioButton {
private final BasicStroke STROKE = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
private static final int DIAMETER = 4;
private static final int MAX_TRACE = 1000 / (int)REPAINT_FREQ;
private LinkedList<Point> trace = new LinkedList<Point>();
private GraphicsDevice screen;
private final Rectangle bounds;
// private final int screenWidth, screenHeight;
public ScreenBox(GraphicsDevice screen, int i, Rectangle b, boolean selected) {
super(Util.getResourceMap(ScreenSelector.class).getString("screenText", i+1, (int)screen.getDefaultConfiguration().getBounds().getWidth(), (int)screen.getDefaultConfiguration().getBounds().getHeight()));
this.screen = screen;
bounds = screen.getDefaultConfiguration().getBounds();
// screenWidth = (int)screen.getDefaultConfiguration().getBounds().getWidth();
// screenHeight = (int)screen.getDefaultConfiguration().getBounds().getHeight();
setBounds(b);
setSelected(selected);
setFocusable(false);
setHorizontalAlignment(SwingConstants.CENTER);
if (!Util.MAC_OS_X) setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(true);
addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
calibration.setScreen(ScreenBox.this.screen);
}
// enableScreenBoxes(true);
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isSelected() && cursor != null) {
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try {
// g.setColor(new Color(255,255,255,75));
// g.fillRect(0, 0, getWidth(), getHeight());
int x = Math.max(0, Math.min(getWidth(), (int)Math.round(((cursor.x - bounds.x) / bounds.getWidth()) * getWidth())));
int y = Math.max(0, Math.min(getHeight(), (int)Math.round(((cursor.y - bounds.y) / bounds.getHeight()) * getHeight())));
if (trace.size() > MAX_TRACE)
trace.removeFirst();
if (!trace.isEmpty()) {
g2d.setStroke(STROKE);
int inc = 255/MAX_TRACE, alpha = (MAX_TRACE - trace.size()) * inc;
Iterator<Point> it = trace.iterator();
Point lp = it.next();
while (it.hasNext()) {
Point p = it.next();
g2d.setColor(new Color(0,0,255,alpha));
g2d.drawLine(lp.x, lp.y, p.x, p.y);
lp = p;
alpha += inc;
}
g2d.setColor(Color.blue);
g2d.drawLine(lp.x, lp.y, x, y);
}
g2d.setColor(Color.blue);
g2d.fillOval(x-DIAMETER/2, y-DIAMETER/2, DIAMETER, DIAMETER);
trace.addLast(new Point(x,y));
} catch (NullPointerException e) {
// cursor can be null because of UpdateTask, no problem
trace.clear();
}
} else {
trace.clear();
}
}
}
private void enableScreenBoxes(boolean enabled) {
for (ScreenBox b : screenBoxes)
b.setEnabled(enabled);
// if (!enabled) {
// for (ScreenBox b : screenBoxes)
// b.setEnabled(false);
// } else {
// LinkedList<ScreenBox> list = new LinkedList<ScreenBox>();
//
// // enable all screens
// for (ScreenBox sb : screenBoxes) {
// sb.setEnabled(true);
// if (sb.isSelected()) list.add(sb);
// }
//
// // disallow deselection if only screen left
// if (list.size() == 1)
// list.getFirst().setEnabled(false);
// }
}
public void irWarped(Map<Wiimote, IRDot[]> data, Point[] warped) {
cursor = warped[0];
}
public void batteryLevel(Wiimote wiimote, double level) {}
public void irLights(Wiimote wiimote, IRDot[] lights) {}
public void wiimoteConnected(Wiimote wiimote) {}
public void wiimoteDisconnected(Wiimote wiimote) {}
// private class ScreenLabel extends JLabel {
//
// private boolean selected;
// private GraphicsDevice screen;
//
// public ScreenLabel(GraphicsDevice screen, int i, Rectangle b, boolean selected) {
// super(String.format("<html><center>Screen %d<br><small>%d x %d</small></center></html>", i+1, (int)screen.getDefaultConfiguration().getBounds().getWidth(), (int)screen.getDefaultConfiguration().getBounds().getHeight()));
// this.screen = screen;
// setSelected(selected);
// setBounds(b);
// setOpaque(true);
// setBorder(BorderFactory.createLineBorder(SystemColor.windowBorder));
// setHorizontalAlignment(SwingConstants.CENTER);
//
// final ScreenLabel thisLabel = this;
// addMouseListener(new MouseAdapter() {
// @Override
// public void mousePressed(MouseEvent e) {
// if (thisLabel.isEnabled() && !thisLabel.isSelected()) {
// calibration.setScreen(thisLabel.screen);
// thisLabel.setSelected(true);
// repaint();
// for (ScreenLabel l : screenLabels) {
// if (thisLabel != l) {
// l.setSelected(false);
// l.repaint();
// }
// }
// }
// }
// });
// }
//
// @Override
// public void setEnabled(boolean enabled) {
// super.setEnabled(enabled);
// setForeground(enabled ? SystemColor.textText : SystemColor.textInactiveText);
// }
//
// public void setSelected(boolean selected) {
// this.selected = selected;
// setBackground(selected ? SystemColor.textHighlight : SystemColor.window);
// }
//
// public boolean isSelected() {
// return selected;
// }
//
// }
}