forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSurfaceFX.java
More file actions
958 lines (779 loc) · 27.3 KB
/
Copy pathPSurfaceFX.java
File metadata and controls
958 lines (779 loc) · 27.3 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2015 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.javafx;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.canvas.Canvas;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
import processing.core.*;
public class PSurfaceFX implements PSurface {
PApplet sketch;
PGraphicsFX2D fx;
Stage stage;
Canvas canvas;
final Animation animation;
float frameRate = 60;
public PSurfaceFX(PGraphicsFX2D graphics) {
fx = graphics;
canvas = new ResizableCanvas();
// set up main drawing loop
KeyFrame keyFrame = new KeyFrame(Duration.millis(1000),
new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
long startNanoTime = System.nanoTime();
sketch.handleDraw();
long drawNanos = System.nanoTime() - startNanoTime;
if (sketch.exitCalled()) {
// using Platform.runLater() didn't work
// Platform.runLater(new Runnable() {
// public void run() {
// instead of System.exit(), safely shut down JavaFX this way
Platform.exit();
// }
// });
}
if (sketch.frameCount > 5) {
animation.setRate(-PApplet.min(1e9f / drawNanos, frameRate));
}
}
});
animation = new Timeline(keyFrame);
animation.setCycleCount(Animation.INDEFINITE);
// key frame has duration of 1 second, so the rate of the animation
// should be set to frames per second
// setting rate to negative so that event fires at the start of
// the key frame and first frame is drawn immediately
animation.setRate(-frameRate);
}
public Object getNative() {
return canvas;
}
class ResizableCanvas extends Canvas {
public ResizableCanvas() {
widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> value,
Number oldWidth, Number newWidth) {
// sketch.width = newWidth.intValue();
sketch.setSize(newWidth.intValue(), sketch.height);
// draw();
fx.setSize(sketch.width, sketch.height);
}
});
heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> value,
Number oldHeight, Number newHeight) {
// sketch.height = newHeight.intValue();
sketch.setSize(sketch.width, newHeight.intValue());
// draw();
fx.setSize(sketch.width, sketch.height);
}
});
//addEventHandler(eventType, eventHandler);
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
fxMouseEvent(e);
}
};
setOnMousePressed(mouseHandler);
setOnMouseReleased(mouseHandler);
setOnMouseClicked(mouseHandler);
setOnMouseEntered(mouseHandler);
setOnMouseExited(mouseHandler);
setOnMouseDragged(mouseHandler);
setOnMouseMoved(mouseHandler);
setOnScroll(new EventHandler<ScrollEvent>() {
public void handle(ScrollEvent e) {
fxScrollEvent(e);
}
});
EventHandler<KeyEvent> keyHandler = new EventHandler<KeyEvent>() {
public void handle(KeyEvent e) {
fxKeyEvent(e);
}
};
setOnKeyPressed(keyHandler);
setOnKeyReleased(keyHandler);
setOnKeyTyped(keyHandler);
setFocusTraversable(false); // prevent tab from de-focusing
focusedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> value,
Boolean oldValue, Boolean newValue) {
if (newValue.booleanValue()) {
sketch.focused = true;
sketch.focusGained();
} else {
sketch.focused = false;
sketch.focusLost();
}
}
});
}
public Stage getStage() {
return stage;
}
@Override
public boolean isResizable() {
return true;
}
@Override
public double prefWidth(double height) {
return getWidth();
}
@Override
public double prefHeight(double width) {
return getHeight();
}
}
public void initOffscreen(PApplet sketch) {
}
// public Component initComponent(PApplet sketch) {
// return null;
// }
static public class PApplicationFX extends Application {
static public PSurfaceFX surface;
// static String title; // title set at launch
// static boolean resizable; // set at launch
public PApplicationFX() { }
@Override
public void start(final Stage stage) {
// if (title != null) {
// stage.setTitle(title);
// }
PApplet sketch = surface.sketch;
// Use AWT display code, because FX orders screens in different way
GraphicsDevice displayDevice = null;
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
int displayNum = sketch.sketchDisplay();
if (displayNum > 0) { // if -1, use the default device
GraphicsDevice[] devices = environment.getScreenDevices();
if (displayNum <= devices.length) {
displayDevice = devices[displayNum - 1];
} else {
System.err.format("Display %d does not exist, " +
"using the default display instead.%n", displayNum);
for (int i = 0; i < devices.length; i++) {
System.err.format("Display %d is %s%n", (i+1), devices[i]);
}
}
}
if (displayDevice == null) {
displayDevice = environment.getDefaultScreenDevice();
}
boolean fullScreen = sketch.sketchFullScreen();
boolean spanDisplays = sketch.sketchDisplay() == PConstants.SPAN;
Rectangle primaryScreenRect = displayDevice.getDefaultConfiguration().getBounds();
Rectangle screenRect = primaryScreenRect;
if (fullScreen || spanDisplays) {
double minX = screenRect.getMinX();
double maxX = screenRect.getMaxX();
double minY = screenRect.getMinY();
double maxY = screenRect.getMaxY();
if (spanDisplays) {
for (GraphicsDevice s : environment.getScreenDevices()) {
Rectangle bounds = s.getDefaultConfiguration().getBounds();
minX = Math.min(minX, bounds.getMinX());
maxX = Math.max(maxX, bounds.getMaxX());
minY = Math.min(minY, bounds.getMinY());
maxY = Math.max(maxY, bounds.getMaxY());
}
}
if (minY < 0) {
// FX can't handle this
System.err.format("FX can't place window at negative Y coordinate " +
"[x=%d, y=%d]. Please make sure that your secondary " +
"display does not extend above the main display.",
(int) minX, (int) minY);
screenRect = primaryScreenRect;
} else {
screenRect = new Rectangle((int) minX, (int) minY,
(int) (maxX - minX), (int) (maxY - minY));
}
}
// Set the displayWidth/Height variables inside PApplet, so that they're
// usable and can even be returned by the sketchWidth()/Height() methods.
sketch.displayWidth = (int) screenRect.getWidth();
sketch.displayHeight = (int) screenRect.getHeight();
int sketchWidth = sketch.sketchWidth();
int sketchHeight = sketch.sketchHeight();
if (fullScreen || spanDisplays) {
sketchWidth = (int) screenRect.getWidth();
sketchHeight = (int) screenRect.getHeight();
stage.initStyle(StageStyle.UNDECORATED);
stage.setX(screenRect.getMinX());
stage.setY(screenRect.getMinY());
stage.setWidth(screenRect.getWidth());
stage.setHeight(screenRect.getHeight());
}
Canvas canvas = surface.canvas;
surface.fx.context = canvas.getGraphicsContext2D();
StackPane stackPane = new StackPane();
stackPane.getChildren().add(canvas);
canvas.widthProperty().bind(stackPane.widthProperty());
canvas.heightProperty().bind(stackPane.heightProperty());
int width = sketchWidth;
int height = sketchHeight;
int smooth = sketch.sketchSmooth();
// Workaround for https://bugs.openjdk.java.net/browse/JDK-8136495
// https://github.com/processing/processing/issues/3823
if ((PApplet.platform == PConstants.MACOSX ||
PApplet.platform == PConstants.LINUX) &&
PApplet.javaVersionName.compareTo("1.8.0_60") >= 0) {
System.err.println("smooth() disabled for JavaFX with this Java version due to Oracle bug");
System.err.println("https://github.com/processing/processing/issues/3795");
smooth = 0;
}
SceneAntialiasing sceneAntialiasing = (smooth == 0) ?
SceneAntialiasing.DISABLED : SceneAntialiasing.BALANCED;
stage.setScene(new Scene(stackPane, width, height, false, sceneAntialiasing));
// initFrame in different thread is waiting for
// the stage, assign it only when it is all set up
surface.stage = stage;
}
}
//public Frame initFrame(PApplet sketch, java.awt.Color backgroundColor,
public void initFrame(PApplet sketch) {/*, int backgroundColor,
int deviceIndex, boolean fullScreen,
boolean spanDisplays) {*/
this.sketch = sketch;
PApplicationFX.surface = this;
//Frame frame = new DummyFrame();
new Thread(new Runnable() {
public void run() {
Application.launch(PApplicationFX.class);
}
}).start();
// wait for stage to be initialized on its own thread before continuing
while (stage == null) {
try {
//System.out.println("waiting for launch");
Thread.sleep(5);
} catch (InterruptedException e) { }
}
}
/** Set the window (and dock, or whatever necessary) title. */
public void setTitle(String title) {
// PApplicationFX.title = title; // store this in case the stage still null
// if (stage != null) {
stage.setTitle(title);
// }
}
/** Show or hide the window. */
@Override
public void setVisible(boolean visible) {
Platform.runLater(new Runnable() {
public void run() {
stage.show();
canvas.requestFocus();
}
});
}
/** Set true if we want to resize things (default is not resizable) */
public void setResizable(boolean resizable) {
// PApplicationFX.resizable = resizable;
// if (stage != null) {
stage.setResizable(resizable);
// }
}
public void setIcon(PImage icon) {
// TODO implement this in JavaFX
}
@Override
public void setAlwaysOnTop(boolean always) {
stage.setAlwaysOnTop(always);
}
/*
@Override
public void placeWindow(int[] location) {
//setFrameSize();
if (location != null) {
// a specific location was received from the Runner
// (applet has been run more than once, user placed window)
stage.setX(location[0]);
stage.setY(location[1]);
} else { // just center on screen
// Can't use frame.setLocationRelativeTo(null) because it sends the
// frame to the main display, which undermines the --display setting.
// frame.setLocation(screenRect.x + (screenRect.width - sketchWidth) / 2,
// screenRect.y + (screenRect.height - sketchHeight) / 2);
}
if (stage.getY() < 0) {
// Windows actually allows you to place frames where they can't be
// closed. Awesome. http://dev.processing.org/bugs/show_bug.cgi?id=1508
//frame.setLocation(frameLoc.x, 30);
stage.setY(30);
}
//setCanvasSize();
// TODO add window closing behavior
// frame.addWindowListener(new WindowAdapter() {
// @Override
// public void windowClosing(WindowEvent e) {
// System.exit(0);
// }
// });
// TODO handle frame resizing events
// setupFrameResizeListener();
if (sketch.getGraphics().displayable()) {
setVisible(true);
}
}
*/
@Override
public void placeWindow(int[] location, int[] editorLocation) {
if (sketch.sketchFullScreen()) {
PApplet.hideMenuBar();
}
//Dimension window = setFrameSize();
// int contentW = Math.max(sketchWidth, MIN_WINDOW_WIDTH);
// int contentH = Math.max(sketchHeight, MIN_WINDOW_HEIGHT);
// System.out.println("stage size is " + stage.getWidth() + " " + stage.getHeight());
int wide = sketch.width; // stage.getWidth() is NaN here
int high = sketch.height; // stage.getHeight()
if (location != null) {
// a specific location was received from the Runner
// (applet has been run more than once, user placed window)
stage.setX(location[0]);
stage.setY(location[1]);
} else if (editorLocation != null) {
int locationX = editorLocation[0] - 20;
int locationY = editorLocation[1];
if (locationX - wide > 10) {
// if it fits to the left of the window
stage.setX(locationX - wide);
stage.setY(locationY);
} else { // doesn't fit
// // if it fits inside the editor window,
// // offset slightly from upper lefthand corner
// // so that it's plunked inside the text area
// locationX = editorLocation[0] + 66;
// locationY = editorLocation[1] + 66;
//
// if ((locationX + stage.getWidth() > sketch.displayWidth - 33) ||
// (locationY + stage.getHeight() > sketch.displayHeight - 33)) {
// // otherwise center on screen
// locationX = (int) ((sketch.displayWidth - wide) / 2);
// locationY = (int) ((sketch.displayHeight - high) / 2);
// }
locationX = (sketch.displayWidth - wide) / 2;
locationY = (sketch.displayHeight - high) / 2;
stage.setX(locationX);
stage.setY(locationY);
}
} else { // just center on screen
//setFrameCentered();
}
if (stage.getY() < 0) {
// Windows actually allows you to place frames where they can't be
// closed. Awesome. http://dev.processing.org/bugs/show_bug.cgi?id=1508
//frame.setLocation(frameLoc.x, 30);
stage.setY(30);
}
//canvas.setBounds((contentW - sketchWidth)/2,
// (contentH - sketchHeight)/2,
// sketchWidth, sketchHeight);
// handle frame resizing events
//setupFrameResizeListener();
if (sketch.getGraphics().displayable()) {
setVisible(true);
}
}
// http://download.java.net/jdk8/jfxdocs/javafx/stage/Stage.html#setFullScreenExitHint-java.lang.String-
// http://download.java.net/jdk8/jfxdocs/javafx/stage/Stage.html#setFullScreenExitKeyCombination-javafx.scene.input.KeyCombination-
public void placePresent(int stopColor) {
// TODO Auto-generated method stub
PApplet.hideMenuBar();
}
@Override
public void setupExternalMessages() {
stage.xProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> value,
Number oldX, Number newX) {
sketch.frameMoved(newX.intValue(), stage.yProperty().intValue());
}
});
stage.yProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> value,
Number oldY, Number newY) {
sketch.frameMoved(stage.xProperty().intValue(), newY.intValue());
}
});
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
sketch.exit();
}
});
}
public void setLocation(int x, int y) {
stage.setX(x);
stage.setY(y);
}
public void setSize(int width, int height) {
//System.out.format("%s.setSize(%d, %d)%n", getClass().getSimpleName(), width, height);
Scene scene = stage.getScene();
double decorH = stage.getWidth() - scene.getWidth();
double decorV = stage.getHeight() - scene.getHeight();
stage.setWidth(width + decorH);
stage.setHeight(height + decorV);
fx.setSize(width, height);
}
// public Component getComponent() {
// return null;
// }
public void setSmooth(int level) {
// TODO Auto-generated method stub
}
public void setFrameRate(float fps) {
// setting rate to negative so that event fires at the start of
// the key frame and first frame is drawn immediately
if (fps > 0) {
frameRate = fps;
animation.setRate(-frameRate);
}
}
// @Override
// public void requestFocus() {
// canvas.requestFocus();
// }
public void setCursor(int kind) {
// TODO Auto-generated method stub
}
public void setCursor(PImage image, int hotspotX, int hotspotY) {
// TODO Auto-generated method stub
}
public void showCursor() {
// TODO Auto-generated method stub
}
public void hideCursor() {
// TODO Auto-generated method stub
}
public void startThread() {
animation.play();
}
public void pauseThread() {
animation.pause();
}
public void resumeThread() {
animation.play();
}
public boolean stopThread() {
animation.stop();
return true;
}
public boolean isStopped() {
return animation.getStatus() == Animation.Status.STOPPED;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/*
protected void addListeners() {
canvas.addMouseListener(new MouseListener() {
public void mousePressed(java.awt.event.MouseEvent e) {
nativeMouseEvent(e);
}
public void mouseReleased(java.awt.event.MouseEvent e) {
nativeMouseEvent(e);
}
public void mouseClicked(java.awt.event.MouseEvent e) {
nativeMouseEvent(e);
}
public void mouseEntered(java.awt.event.MouseEvent e) {
nativeMouseEvent(e);
}
public void mouseExited(java.awt.event.MouseEvent e) {
nativeMouseEvent(e);
}
});
canvas.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(java.awt.event.MouseEvent e) {
nativeMouseEvent(e);
}
public void mouseMoved(java.awt.event.MouseEvent e) {
nativeMouseEvent(e);
}
});
canvas.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
nativeMouseEvent(e);
}
});
canvas.addKeyListener(new KeyListener() {
public void keyPressed(java.awt.event.KeyEvent e) {
nativeKeyEvent(e);
}
public void keyReleased(java.awt.event.KeyEvent e) {
nativeKeyEvent(e);
}
public void keyTyped(java.awt.event.KeyEvent e) {
nativeKeyEvent(e);
}
});
canvas.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
sketch.focused = true;
sketch.focusGained();
}
public void focusLost(FocusEvent e) {
sketch.focused = false;
sketch.focusLost();
}
});
}
*/
static Map<EventType<? extends MouseEvent>, Integer> mouseMap =
new HashMap<EventType<? extends MouseEvent>, Integer>();
static {
mouseMap.put(MouseEvent.MOUSE_PRESSED, processing.event.MouseEvent.PRESS);
mouseMap.put(MouseEvent.MOUSE_RELEASED, processing.event.MouseEvent.RELEASE);
mouseMap.put(MouseEvent.MOUSE_CLICKED, processing.event.MouseEvent.CLICK);
mouseMap.put(MouseEvent.MOUSE_DRAGGED, processing.event.MouseEvent.DRAG);
mouseMap.put(MouseEvent.MOUSE_MOVED, processing.event.MouseEvent.MOVE);
mouseMap.put(MouseEvent.MOUSE_ENTERED, processing.event.MouseEvent.ENTER);
mouseMap.put(MouseEvent.MOUSE_EXITED, processing.event.MouseEvent.EXIT);
}
protected void fxMouseEvent(MouseEvent fxEvent) {
// the 'amount' is the number of button clicks for a click event,
// or the number of steps/clicks on the wheel for a mouse wheel event.
int count = fxEvent.getClickCount();
int action = mouseMap.get(fxEvent.getEventType());
//EventType<? extends MouseEvent> et = nativeEvent.getEventType();
// if (et == MouseEvent.MOUSE_PRESSED) {
// peAction = processing.event.MouseEvent.PRESS;
// } else if (et == MouseEvent.MOUSE_RELEASED) {
// peAction = processing.event.MouseEvent.RELEASE;
int modifiers = 0;
if (fxEvent.isShiftDown()) {
modifiers |= processing.event.Event.SHIFT;
}
if (fxEvent.isControlDown()) {
modifiers |= processing.event.Event.CTRL;
}
if (fxEvent.isMetaDown()) {
modifiers |= processing.event.Event.META;
}
if (fxEvent.isAltDown()) {
modifiers |= processing.event.Event.ALT;
}
int button = 0;
if (fxEvent.isPrimaryButtonDown()) {
button = PConstants.LEFT;
} else if (fxEvent.isSecondaryButtonDown()) {
button = PConstants.RIGHT;
} else if (fxEvent.isMiddleButtonDown()) {
button = PConstants.CENTER;
}
// If running on Mac OS, allow ctrl-click as right mouse.
// Verified to be necessary with Java 8u45.
if (PApplet.platform == PConstants.MACOSX &&
fxEvent.isControlDown() &&
button == PConstants.LEFT) {
button = PConstants.RIGHT;
}
//long when = nativeEvent.getWhen(); // from AWT
long when = System.currentTimeMillis();
int x = (int) fxEvent.getX(); // getSceneX()?
int y = (int) fxEvent.getY();
sketch.postEvent(new processing.event.MouseEvent(fxEvent, when,
action, modifiers,
x, y, button, count));
}
// https://docs.oracle.com/javafx/2/api/javafx/scene/input/ScrollEvent.html
protected void fxScrollEvent(ScrollEvent event) {
// //case java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL:
// case java.awt.event.MouseEvent.MOUSE_WHEEL:
// peAction = MouseEvent.WHEEL;
// /*
// if (preciseWheelMethod != null) {
// try {
// peAmount = ((Double) preciseWheelMethod.invoke(nativeEvent, (Object[]) null)).floatValue();
// } catch (Exception e) {
// preciseWheelMethod = null;
// }
// }
// */
// peCount = ((MouseWheelEvent) nativeEvent).getWheelRotation();
// break;
}
protected void fxKeyEvent(javafx.scene.input.KeyEvent fxEvent) {
int action = 0;
EventType<? extends KeyEvent> et = fxEvent.getEventType();
if (et == KeyEvent.KEY_PRESSED) {
action = processing.event.KeyEvent.PRESS;
} else if (et == KeyEvent.KEY_RELEASED) {
action = processing.event.KeyEvent.RELEASE;
} else if (et == KeyEvent.KEY_TYPED) {
action = processing.event.KeyEvent.TYPE;
}
int modifiers = 0;
if (fxEvent.isShiftDown()) {
modifiers |= processing.event.Event.SHIFT;
}
if (fxEvent.isControlDown()) {
modifiers |= processing.event.Event.CTRL;
}
if (fxEvent.isMetaDown()) {
modifiers |= processing.event.Event.META;
}
if (fxEvent.isAltDown()) {
modifiers |= processing.event.Event.ALT;
}
long when = System.currentTimeMillis();
char keyChar = getKeyChar(fxEvent);
int keyCode = getKeyCode(fxEvent);
sketch.postEvent(new processing.event.KeyEvent(fxEvent, when,
action, modifiers,
keyChar, keyCode));
}
@SuppressWarnings("deprecation")
private int getKeyCode(KeyEvent fxEvent) {
if (fxEvent.getEventType() == KeyEvent.KEY_TYPED) {
return 0;
}
KeyCode kc = fxEvent.getCode();
switch (kc) {
case ALT_GRAPH:
return PConstants.ALT;
default:
break;
}
return kc.impl_getCode();
}
@SuppressWarnings("deprecation")
private char getKeyChar(KeyEvent fxEvent) {
KeyCode kc = fxEvent.getCode();
// Overriding chars for some
// KEY_PRESSED and KEY_RELEASED events
switch (kc) {
case UP:
case KP_UP:
case DOWN:
case KP_DOWN:
case LEFT:
case KP_LEFT:
case RIGHT:
case KP_RIGHT:
case ALT:
case ALT_GRAPH:
case CONTROL:
case SHIFT:
case CAPS:
case META:
case WINDOWS:
case CONTEXT_MENU:
case HOME:
case PAGE_UP:
case PAGE_DOWN:
case END:
case PAUSE:
case PRINTSCREEN:
case INSERT:
case NUM_LOCK:
case SCROLL_LOCK:
case F1:
case F2:
case F3:
case F4:
case F5:
case F6:
case F7:
case F8:
case F9:
case F10:
case F11:
case F12:
return PConstants.CODED;
case ENTER:
return '\n';
case DIVIDE:
return '/';
case MULTIPLY:
return '*';
case SUBTRACT:
return '-';
case ADD:
return '+';
case NUMPAD0:
return '0';
case NUMPAD1:
return '1';
case NUMPAD2:
return '2';
case NUMPAD3:
return '3';
case NUMPAD4:
return '4';
case NUMPAD5:
return '5';
case NUMPAD6:
return '6';
case NUMPAD7:
return '7';
case NUMPAD8:
return '8';
case NUMPAD9:
return '9';
case DECIMAL:
// KEY_TYPED does not go through here and will produce
// dot or comma based on the keyboard layout.
// For KEY_PRESSED and KEY_RELEASED, let's just go with
// the dot. Users can detect the key by its keyCode.
return '.';
case UNDEFINED:
// KEY_TYPED has KeyCode: UNDEFINED
// and falls through here
break;
default:
break;
}
// Just go with what FX gives us for the rest of
// KEY_PRESSED and KEY_RELEASED and all of KEY_TYPED
String ch;
if (fxEvent.getEventType() == KeyEvent.KEY_TYPED) {
ch = fxEvent.getCharacter();
} else {
ch = kc.impl_getChar();
}
if (ch.length() < 1) return PConstants.CODED;
if (ch.startsWith("\r")) return '\n'; // normalize enter key
return ch.charAt(0);
}
}