-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathViewer.java
More file actions
465 lines (405 loc) · 16.1 KB
/
Viewer.java
File metadata and controls
465 lines (405 loc) · 16.1 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package nodebox.client;
import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.event.*;
import edu.umd.cs.piccolo.util.PAffineTransform;
import edu.umd.cs.piccolo.util.PPaintContext;
import nodebox.graphics.Canvas;
import nodebox.graphics.*;
import nodebox.handle.Handle;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
public class Viewer extends PCanvas implements PaneView, MouseListener, MouseMotionListener, KeyListener {
public static final float POINT_SIZE = 4f;
public static final float MIN_ZOOM = 0.01f;
public static final float MAX_ZOOM = 64.0f;
private static final String HANDLE_UNDO_TEXT = "Handle Changes";
private static final String HANDLE_UNDO_TYPE = "handle";
private static Cursor defaultCursor, panCursor;
private final NodeBoxDocument document;
private Object outputValue;
private Handle handle;
private boolean showHandle = true;
private boolean handleEnabled = true;
private boolean showPoints = false;
private boolean showPointNumbers = false;
private boolean showOrigin = false;
private boolean panEnabled = false;
private PLayer viewerLayer;
private JPopupMenu viewerMenu;
private Class outputClass;
private ViewerEventListener listener;
static {
Image panCursorImage;
try {
if (PlatformUtils.onWindows())
panCursorImage = ImageIO.read(new File("res/view-cursor-pan-32.png"));
else
panCursorImage = ImageIO.read(new File("res/view-cursor-pan.png"));
Toolkit toolkit = Toolkit.getDefaultToolkit();
panCursor = toolkit.createCustomCursor(panCursorImage, new Point(0, 0), "PanCursor");
defaultCursor = Cursor.getDefaultCursor();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public Viewer(final NodeBoxDocument document) {
this.document = document;
addMouseListener(this);
addMouseMotionListener(this);
setFocusable(true);
addKeyListener(this);
// Setup Piccolo canvas
setBackground(Theme.VIEWER_BACKGROUND_COLOR);
setAnimatingRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);
setInteractingRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);
// Remove default panning and zooming behaviour
removeInputEventListener(getPanEventHandler());
removeInputEventListener(getZoomEventHandler());
// Install custom panning and zooming
PInputEventFilter panFilter = new PInputEventFilter();
panFilter.setNotMask(InputEvent.CTRL_MASK);
PPanEventHandler panHandler = new PPanEventHandler() {
public void processEvent(final PInputEvent evt, final int i) {
if (evt.isMouseEvent() && evt.isLeftMouseButton() && panEnabled)
super.processEvent(evt, i);
}
};
panHandler.setAutopan(false);
panHandler.setEventFilter(panFilter);
addInputEventListener(panHandler);
setZoomEventHandler(new PZoomEventHandler() {
public void processEvent(final PInputEvent evt, final int i) {
if (evt.isMouseWheelEvent()) {
double currentScale = evt.getCamera().getViewScale();
double scaleDelta = 1D - 0.05 * evt.getWheelRotation();
double newScale = currentScale * scaleDelta;
if (newScale < MIN_ZOOM) {
scaleDelta = MIN_ZOOM / currentScale;
} else if (newScale > MAX_ZOOM) {
scaleDelta = MAX_ZOOM / currentScale;
}
final Point2D p = evt.getPosition();
evt.getCamera().scaleViewAboutPoint(scaleDelta, p.getX(), p.getY());
}
}
});
// Add the zoomable view layer
viewerLayer = new ViewerLayer();
getCamera().addLayer(0, viewerLayer);
initMenus();
}
public void setEventListener(ViewerEventListener e) {
listener = e;
}
private void initMenus() {
viewerMenu = new JPopupMenu();
viewerMenu.add(new ResetViewAction());
PopupHandler popupHandler = new PopupHandler();
addInputEventListener(popupHandler);
}
public boolean isShowHandle() {
return showHandle;
}
public void setShowHandle(boolean showHandle) {
this.showHandle = showHandle;
repaint();
}
public boolean isShowPoints() {
return showPoints;
}
public void setShowPoints(boolean showPoints) {
this.showPoints = showPoints;
repaint();
}
public boolean isShowPointNumbers() {
return showPointNumbers;
}
public void setShowPointNumbers(boolean showPointNumbers) {
this.showPointNumbers = showPointNumbers;
repaint();
}
public boolean isShowOrigin() {
return showOrigin;
}
public void setShowOrigin(boolean showOrigin) {
this.showOrigin = showOrigin;
repaint();
}
//// Handle support ////
public Handle getHandle() {
return handle;
}
public void setHandle(Handle handle) {
this.handle = handle;
repaint();
}
public boolean hasVisibleHandle() {
if (handle == null) return false;
if (!showHandle) return false;
if (!handleEnabled) return false;
return handle.isVisible();
}
//// Network data events ////
public Object getOutputValue() {
return outputValue;
}
public void setOutputValue(Object outputValue) {
this.outputValue = outputValue;
if (outputValue != null && outputClass != outputValue.getClass()) {
if (outputValue instanceof Canvas) {
// The canvas is placed in the top-left corner, as in NodeBox 1.
resetView();
Canvas canvas = (Canvas) outputValue;
viewerLayer.setBounds(canvas.getBounds().getRectangle2D());
viewerLayer.setOffset(getWidth() / 2, getHeight() / 2);
} else if (outputValue instanceof Grob) {
// Other graphic objects are displayed in the center.
resetView();
viewerLayer.setBounds(-Integer.MAX_VALUE / 2, -Integer.MAX_VALUE / 2, Integer.MAX_VALUE, Integer.MAX_VALUE);
viewerLayer.setOffset(getWidth() / 2, getHeight() / 2);
} else {
// Other output will be converted to a string, and placed just off the top-left corner.
resetView();
viewerLayer.setBounds(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
viewerLayer.setOffset(5, 5);
}
outputClass = outputValue.getClass();
}
repaint();
}
//// Node attribute listener ////
public boolean isHandleEnabled() {
return handleEnabled;
}
public void setHandleEnabled(boolean handleEnabled) {
if (this.handleEnabled != handleEnabled) {
this.handleEnabled = handleEnabled;
// We could just repaint the handle.
repaint();
}
}
public void reloadHandle() {
// TODO Implement
}
public void resetView() {
getCamera().setViewTransform(new AffineTransform());
}
//// Mouse events ////
private nodebox.graphics.Point pointForEvent(MouseEvent e) {
Point2D originalPoint = new Point2D.Float(e.getX(), e.getY());
PAffineTransform transform = getCamera().getViewTransform();
Point2D transformedPoint;
try {
transformedPoint = transform.inverseTransform(originalPoint, null);
} catch (NoninvertibleTransformException ex) {
return new nodebox.graphics.Point(0, 0);
}
Point2D offset = viewerLayer.getOffset();
double cx = -offset.getX() + transformedPoint.getX();
double cy = -offset.getY() + transformedPoint.getY();
// double cx = -getWidth() / 2.0 + transformedPoint.getX();
// double cy = -getHeight() / 2.0 + transformedPoint.getY();
return new nodebox.graphics.Point((float) cx, (float) cy);
}
public void mouseClicked(MouseEvent e) {
// We register the mouse click as an edit since it can trigger a change to the node.
if (e.isPopupTrigger()) return;
if (hasVisibleHandle()) {
//getDocument().addEdit(HANDLE_UNDO_TEXT, HANDLE_UNDO_TYPE, activeNode);
handle.mouseClicked(pointForEvent(e));
}
}
public void mousePressed(MouseEvent e) {
// We register the mouse press as an edit since it can trigger a change to the node.
if (e.isPopupTrigger()) return;
if (hasVisibleHandle()) {
//getDocument().addEdit(HANDLE_UNDO_TEXT, HANDLE_UNDO_TYPE, activeNode);
handle.mousePressed(pointForEvent(e));
}
}
public void mouseReleased(MouseEvent e) {
// We register the mouse release as an edit since it can trigger a change to the node.
if (e.isPopupTrigger()) return;
if (hasVisibleHandle()) {
//getDocument().addEdit(HANDLE_UNDO_TEXT, HANDLE_UNDO_TYPE, activeNode);
handle.mouseReleased(pointForEvent(e));
}
}
public void mouseEntered(MouseEvent e) {
// Entering the viewer with your mouse should not change the node, so we do not register an edit.
if (e.isPopupTrigger()) return;
if (hasVisibleHandle()) {
handle.mouseEntered(pointForEvent(e));
}
}
public void mouseExited(MouseEvent e) {
// Exiting the viewer with your mouse should not change the node, so we do not register an edit.
if (e.isPopupTrigger()) return;
if (hasVisibleHandle()) {
handle.mouseExited(pointForEvent(e));
}
}
public void mouseDragged(MouseEvent e) {
// We register the mouse drag as an edit since it can trigger a change to the node.
if (e.isPopupTrigger()) return;
if (hasVisibleHandle()) {
//getDocument().addEdit(HANDLE_UNDO_TEXT, HANDLE_UNDO_TYPE, activeNode);
handle.mouseDragged(pointForEvent(e));
}
}
public void mouseMoved(MouseEvent e) {
// Moving the mouse in the viewer area should not change the node, so we do not register an edit.
if (e.isPopupTrigger()) return;
if (hasVisibleHandle()) {
handle.mouseMoved(pointForEvent(e));
}
}
public void keyTyped(KeyEvent e) {
if (hasVisibleHandle())
handle.keyTyped(e.getKeyCode(), e.getModifiersEx());
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
panEnabled = true;
if (!getCursor().equals(panCursor))
setCursor(panCursor);
}
if (hasVisibleHandle())
handle.keyPressed(e.getKeyCode(), e.getModifiersEx());
}
public void keyReleased(KeyEvent e) {
panEnabled = false;
if (!getCursor().equals(defaultCursor))
setCursor(defaultCursor);
if (hasVisibleHandle())
handle.keyReleased(e.getKeyCode(), e.getModifiersEx());
}
@Override
public boolean isFocusable() {
return true;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the origin.
Point2D origin = getCamera().getViewTransform().transform(viewerLayer.getOffset(), null);
int x = (int) Math.round(origin.getX());
int y = (int) Math.round(origin.getY());
if (showOrigin) {
g.setColor(Color.DARK_GRAY);
g.drawLine(x, 0, x, getHeight());
g.drawLine(0, y, getWidth(), y);
}
}
public class ViewerLayer extends PLayer {
@Override
protected void paint(PPaintContext paintContext) {
Graphics2D g2 = paintContext.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// If the output value is geometry, store it in a geometry object.
// This is used to draw the points / point numbers.
IGeometry geo = null;
// Draw the canvas bounds
if (outputValue instanceof Canvas) {
Canvas c = (Canvas) outputValue;
geo = c.asGeometry(false);
Rectangle2D canvasBounds = c.getBounds().getRectangle2D();
g2.setColor(Color.DARK_GRAY);
g2.setStroke(new BasicStroke(1f));
g2.draw(canvasBounds);
}
if (outputValue instanceof Grob) {
Grob g = (Grob) outputValue;
if (g instanceof IGeometry) {
geo = (IGeometry) outputValue;
}
Shape oldClip = g2.getClip();
g.draw(g2);
g2.setClip(oldClip);
} else if (outputValue != null) {
g2.setColor(Theme.TEXT_NORMAL_COLOR);
g2.setFont(Theme.EDITOR_FONT);
String s = outputValue.toString();
int y = 20;
for (String line : s.split("\n")) {
g2.drawString(line, 5, y);
y += 14;
}
}
// Draw the handle.
if (hasVisibleHandle()) {
// Create a canvas with a transparent background
nodebox.graphics.Canvas canvas = new nodebox.graphics.Canvas();
canvas.setBackground(new nodebox.graphics.Color(0, 0, 0, 0));
CanvasContext ctx = new CanvasContext(canvas);
try {
handle.draw(ctx);
} catch (Exception e) {
e.printStackTrace();
}
ctx.getCanvas().draw(g2);
}
// Draw the points.
if (showPoints && geo != null) {
// Create a canvas with a transparent background
Path onCurves = new Path();
Path offCurves = new Path();
onCurves.setFill(new nodebox.graphics.Color(0f, 0f, 1f));
offCurves.setFill(new nodebox.graphics.Color(1f, 0f, 0f));
for (nodebox.graphics.Point pt : geo.getPoints()) {
if (pt.isOnCurve()) {
onCurves.ellipse(pt.x, pt.y, POINT_SIZE, POINT_SIZE);
} else {
offCurves.ellipse(pt.x, pt.y, POINT_SIZE, POINT_SIZE);
}
}
onCurves.draw(g2);
offCurves.draw(g2);
}
// Draw the point numbers.
if (showPointNumbers && geo != null) {
g2.setFont(Theme.SMALL_MONO_FONT);
g2.setColor(Color.BLUE);
// Create a canvas with a transparent background
int index = 0;
for (nodebox.graphics.Point pt : geo.getPoints()) {
if (pt.isOnCurve()) {
g2.setColor(Color.BLUE);
} else {
g2.setColor(Color.RED);
}
g2.drawString(index + "", pt.x + 3, pt.y - 2);
index++;
}
}
}
}
private class PopupHandler extends PBasicInputEventHandler {
public void processEvent(PInputEvent e, int i) {
if (!e.isPopupTrigger()) return;
if (e.isHandled()) return;
Point2D p = e.getCanvasPosition();
viewerMenu.show(Viewer.this, (int) p.getX(), (int) p.getY());
}
}
private class ResetViewAction extends AbstractAction {
private ResetViewAction() {
super("Reset View");
}
public void actionPerformed(ActionEvent e) {
resetView();
}
}
}