-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathPApplet.java
More file actions
15225 lines (13375 loc) · 525 KB
/
PApplet.java
File metadata and controls
15225 lines (13375 loc) · 525 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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-22 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
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.core;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.zip.*;
// loadXML() error handling
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
// TODO have this removed by 4.0 final
import processing.awt.ShimAWT;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
/**
* Base class for all sketches that use processing.core.
* <p/>
* The <A HREF="https://github.com/processing/processing/wiki/Window-Size-and-Full-Screen">
* Window Size and Full Screen</A> page on the Wiki has useful information
* about sizing, multiple displays, full screen, etc.
* <p/>
* Processing uses active mode rendering. All animation tasks happen on the
* "Processing Animation Thread". The setup() and draw() methods are handled
* by that thread, and events (like mouse movement and key presses, which are
* fired by the event dispatch thread or EDT) are queued to be safely handled
* at the end of draw().
* <p/>
* Starting with 3.0a6, blit operations are on the EDT, so as not to cause
* GUI problems with Swing and AWT. In the case of the default renderer, the
* sketch renders to an offscreen image, then the EDT is asked to bring that
* image to the screen.
* <p/>
* For code that needs to run on the EDT, use EventQueue.invokeLater(). When
* doing so, be careful to synchronize between that code and the Processing
* animation thread. That is, you can't call Processing methods from the EDT
* or at any random time from another thread. Use of a callback function or
* the registerXxx() methods in PApplet can help ensure that your code doesn't
* do something naughty.
* <p/>
* As of Processing 3.0, we have removed Applet as the base class for PApplet.
* This means that we can remove lots of legacy code, however one downside is
* that it's no longer possible (without extra code) to embed a PApplet into
* another Java application.
* <p/>
* As of Processing 3.0, we have discontinued support for versions of Java
* prior to 1.8. We don't have enough people to support it, and for a
* project of our (tiny) size, we should be focusing on the future, rather
* than working around legacy Java code.
*/
@SuppressWarnings({"unused", "FinalStaticMethod", "ManualMinMaxCalculation"})
public class PApplet implements PConstants {
//public class PApplet extends PSketch { // possible in the next alpha
/** Full name of the Java version (i.e. 1.5.0_11). */
static public final String javaVersionName =
System.getProperty("java.version");
static public final int javaPlatform;
static {
String version = javaVersionName;
if (javaVersionName.startsWith("1.")) {
version = version.substring(2);
javaPlatform = parseInt(version.substring(0, version.indexOf('.')));
} else {
// Remove -xxx and .yyy from java.version (@see JEP-223)
javaPlatform = parseInt(version.replaceAll("-.*","").replaceAll("\\..*",""));
}
}
/**
* Do not use; javaPlatform or javaVersionName are better options.
* For instance, javaPlatform is useful when you need a number for
* comparison, i.e. "if (javaPlatform >= 9)".
*/
@Deprecated
public static final float javaVersion = 1 + javaPlatform / 10f;
/**
* Current platform in use, one of the PConstants WINDOWS, MACOS, LINUX or OTHER.
*/
static public int platform;
static {
final String name = System.getProperty("os.name");
if (name.contains("Mac")) {
platform = MACOS;
} else if (name.contains("Windows")) {
platform = WINDOWS;
} else if (name.equals("Linux")) { // true for the ibm vm
platform = LINUX;
} else {
platform = OTHER;
}
}
/**
* Whether to use native (AWT) dialogs for selectInput and selectOutput.
* The native dialogs on some platforms can be ugly, buggy, or missing
* features. For 3.3.5, this defaults to true on all platforms.
*/
static public boolean useNativeSelect = true;
/** The PGraphics renderer associated with this PApplet */
public PGraphics g;
/**
* System variable that stores the width of the computer screen.
* For example, if the current screen resolution is 1920x1080,
* <b>displayWidth</b> is 1920 and <b>displayHeight</b> is 1080.
*
* @webref environment
* @webBrief Variable that stores the width of the computer screen
* @see PApplet#displayHeight
* @see PApplet#size(int, int)
*/
public int displayWidth;
/**
* System variable that stores the height of the computer screen.
* For example, if the current screen resolution is 1920x1080,
* <b>displayWidth</b> is 1920 and <b>displayHeight</b> is 1080.
*
* @webref environment
* @webBrief Variable that stores the height of the computer screen
* @see PApplet#displayWidth
* @see PApplet#size(int, int)
*/
public int displayHeight;
public int windowX;
public int windowY;
/** A leech graphics object that is echoing all events. */
public PGraphics recorder;
/**
* Command line options passed in from main().
* This does not include the arguments passed in to PApplet itself.
* @see PApplet#main
*/
public String[] args;
/**
* Path to sketch folder. Previously undocumented, and made private
* in 3.0 alpha 5 so that people use the sketchPath() method which
* will initialize it properly. Call sketchPath() once to set it.
*/
private String sketchPath;
static final boolean DEBUG = false;
// static final boolean DEBUG = true;
/** Default width and height for sketch when not specified */
static public final int DEFAULT_WIDTH = 100;
static public final int DEFAULT_HEIGHT = 100;
/**
* The <b>pixels[]</b> array contains the values for all the pixels in the
* display window. These values are of the color datatype. This array is
* defined by the size of the display window. For example, if the window is
* 100 x 100 pixels, there will be 10,000 values and if the window is
* 200 x 300 pixels, there will be 60,000 values. When the pixel density is
* set to higher than 1 with the <b>pixelDensity()</b> function, these values
* will change. See the reference for <b>pixelWidth</b> or <b>pixelHeight</b>
* for more information.
* <br /><br />
* Before accessing this array, the data must be loaded with the <b>loadPixels()</b>
* function. Failure to do so may result in a NullPointerException. Subsequent
* changes to the display window will not be reflected in <b>pixels</b> until
* <b>loadPixels()</b> is called again. After <b>pixels</b> has been modified,
* the <b>updatePixels()</b> function must be run to update the content of the
* display window.
*
* @webref image:pixels
* @webBrief Array containing the values for all the pixels in the display window
* @see PApplet#loadPixels()
* @see PApplet#updatePixels()
* @see PApplet#get(int, int, int, int)
* @see PApplet#set(int, int, int)
* @see PImage
* @see PApplet#pixelDensity(int)
* @see PApplet#pixelWidth
* @see PApplet#pixelHeight
*/
public int[] pixels;
/**
*
* System variable which stores the width of the display window. This value
* is set by the first parameter of the <b>size()</b> function. For
* example, the function call <b>size(320, 240)</b> sets the <b>width</b>
* variable to the value 320. The value of <b>width</b> defaults to 100 if
* <b>size()</b> is not used in a program.
*
* @webref environment
* @webBrief System variable which stores the width of the display window
* @see PApplet#height
* @see PApplet#size(int, int)
*/
public int width = DEFAULT_WIDTH;
/**
*
* System variable which stores the height of the display window. This
* value is set by the second parameter of the <b>size()</b> function. For
* example, the function call <b>size(320, 240)</b> sets the <b>height</b>
* variable to the value 240. The value of <b>height</b> defaults to 100 if
* <b>size()</b> is not used in a program.
*
* @webref environment
* @webBrief System variable which stores the height of the display window
* @see PApplet#width
* @see PApplet#size(int, int)
*/
public int height = DEFAULT_HEIGHT;
/**
*
* When <b>pixelDensity(2)</b> is used to make use of a high resolution
* display (called a Retina display on OS X or high-dpi on Windows and
* Linux), the width and height of the sketch do not change, but the
* number of pixels is doubled. As a result, all operations that use pixels
* (like <b>loadPixels()</b>, <b>get()</b>, <b>set()</b>, etc.) happen
* in this doubled space. As a convenience, the variables <b>pixelWidth</b>
* and <b>pixelHeight</b> hold the actual width and height of the sketch
* in pixels. This is useful for any sketch that uses the <b>pixels[]</b>
* array, for instance, because the number of elements in the array will
* be <b>pixelWidth*pixelHeight</b>, not <b>width*height</b>.
*
* @webref environment
* @webBrief The actual pixel width when using high resolution display
* @see PApplet#pixelHeight
* @see #pixelDensity(int)
* @see #displayDensity()
*/
public int pixelWidth;
/**
* When <b>pixelDensity(2)</b> is used to make use of a high resolution
* display (called a Retina display on OS X or high-dpi on Windows and
* Linux), the width and height of the sketch do not change, but the
* number of pixels is doubled. As a result, all operations that use pixels
* (like <b>loadPixels()</b>, <b>get()</b>, <b>set()</b>, etc.) happen
* in this doubled space. As a convenience, the variables <b>pixelWidth</b>
* and <b>pixelHeight</b> hold the actual width and height of the sketch
* in pixels. This is useful for any sketch that uses the <b>pixels[]</b>
* array, for instance, because the number of elements in the array will
* be <b>pixelWidth*pixelHeight</b>, not <b>width*height</b>.
*
* @webref environment
* @webBrief The actual pixel height when using high resolution display
* @see PApplet#pixelWidth
* @see #pixelDensity(int)
* @see #displayDensity()
*/
public int pixelHeight;
// Making this private until we have a compelling reason to make it public.
// Seems problematic/weird for it to be possible to set windowRatio = false
// relative to how other API works. And not sure what the use case would be.
private boolean windowRatio;
/**
* Version of mouseX/mouseY to use with windowRatio().
*/
public int rmouseX;
public int rmouseY;
/**
* Version of width/height to use with windowRatio().
*/
public int rwidth;
public int rheight;
/** Offset from left when windowRatio is in use. */
public float ratioLeft;
/** Offset from the top when windowRatio is in use. */
public float ratioTop;
/** Amount of scaling to be applied for the window ratio. */
public float ratioScale;
/**
* Keeps track of ENABLE_KEY_REPEAT hint
*/
protected boolean keyRepeatEnabled = false;
/**
* The system variable <b>mouseX</b> always contains the current horizontal
* coordinate of the mouse.
* <br /><br />
* Note that Processing can only track the mouse position when the pointer
* is over the current window. The default value of <b>mouseX</b> is <b>0</b>,
* so <b>0</b> will be returned until the mouse moves in front of the sketch
* window. (This typically happens when a sketch is first run.) Once the
* mouse moves away from the window, <b>mouseX</b> will continue to report
* its most recent position.
*
* @webref input:mouse
* @webBrief The system variable that always contains the current horizontal coordinate of the mouse
* @see PApplet#mouseY
* @see PApplet#pmouseX
* @see PApplet#pmouseY
* @see PApplet#mousePressed
* @see PApplet#mousePressed()
* @see PApplet#mouseReleased()
* @see PApplet#mouseClicked()
* @see PApplet#mouseMoved()
* @see PApplet#mouseDragged()
* @see PApplet#mouseButton
* @see PApplet#mouseWheel(MouseEvent)
*/
public int mouseX;
/**
* The system variable <b>mouseY</b> always contains the current
* vertical coordinate of the mouse.
* <br /><br />
* Note that Processing can only track the mouse position when the pointer
* is over the current window. The default value of <b>mouseY</b> is <b>0</b>,
* so <b>0</b> will be returned until the mouse moves in front of the sketch
* window. (This typically happens when a sketch is first run.) Once the
* mouse moves away from the window, <b>mouseY</b> will continue to report
* its most recent position.
*
* @webref input:mouse
* @webBrief The system variable that always contains the current vertical coordinate of the mouse
* @see PApplet#mouseX
* @see PApplet#pmouseX
* @see PApplet#pmouseY
* @see PApplet#mousePressed
* @see PApplet#mousePressed()
* @see PApplet#mouseReleased()
* @see PApplet#mouseClicked()
* @see PApplet#mouseMoved()
* @see PApplet#mouseDragged()
* @see PApplet#mouseButton
* @see PApplet#mouseWheel(MouseEvent)
*
*/
public int mouseY;
/**
* The system variable <b>pmouseX</b> always contains the horizontal
* position of the mouse in the frame previous to the current frame.<br />
* <br />
* You may find that <b>pmouseX</b> and <b>pmouseY</b> have different values
* when referenced inside of <b>draw()</b> and inside of mouse events like
* <b>mousePressed()</b> and <b>mouseMoved()</b>. Inside <b>draw()</b>,
* <b>pmouseX</b> and <b>pmouseY</b> update only once per frame (once per trip
* through the <b>draw()</b> loop). But inside mouse events, they update each
* time the event is called. If these values weren't updated immediately during
* events, then the mouse position would be read only once per frame, resulting
* in slight delays and choppy interaction. If the mouse variables were always
* updated multiple times per frame, then something like <b>line(pmouseX, pmouseY,
* mouseX, mouseY)</b> inside <b>draw()</b> would have lots of gaps, because
* <b>pmouseX</b> may have changed several times in between the calls to
* <b>line()</b>.<br /><br />
* If you want values relative to the previous frame, use <b>pmouseX</b> and
* <b>pmouseY</b> inside <b>draw()</b>. If you want continuous response, use
* <b>pmouseX</b> and <b>pmouseY</b> inside the mouse event functions.
*
* @webref input:mouse
* @webBrief The system variable that always contains the horizontal
* position of the mouse in the frame previous to the current frame
* @see PApplet#mouseX
* @see PApplet#mouseY
* @see PApplet#pmouseY
* @see PApplet#mousePressed
* @see PApplet#mousePressed()
* @see PApplet#mouseReleased()
* @see PApplet#mouseClicked()
* @see PApplet#mouseMoved()
* @see PApplet#mouseDragged()
* @see PApplet#mouseButton
* @see PApplet#mouseWheel(MouseEvent)
*/
public int pmouseX;
/**
* The system variable <b>pmouseY</b> always contains the vertical position
* of the mouse in the frame previous to the current frame. More detailed
* information about how <b>pmouseY</b> is updated inside of <b>draw()</b>
* and mouse events is explained in the reference for <b>pmouseX</b>.
*
* @webref input:mouse
* @webBrief The system variable that always contains the vertical position
* of the mouse in the frame previous to the current frame
* @see PApplet#mouseX
* @see PApplet#mouseY
* @see PApplet#pmouseX
* @see PApplet#mousePressed
* @see PApplet#mousePressed()
* @see PApplet#mouseReleased()
* @see PApplet#mouseClicked()
* @see PApplet#mouseMoved()
* @see PApplet#mouseDragged()
* @see PApplet#mouseButton
* @see PApplet#mouseWheel(MouseEvent)
*/
public int pmouseY;
/**
* Previous mouseX/Y for the draw loop, separated out because this is
* separate from the pmouseX/Y when inside the mouse event handlers.
* See emouseX/Y for an explanation.
*/
protected int dmouseX, dmouseY;
/**
* The pmouseX/Y for the event handlers (mousePressed(), mouseDragged() etc)
* these are different because mouse events are queued to the end of
* draw, so the previous position has to be updated on each event,
* as opposed to the pmouseX/Y that's used inside draw, which is expected
* to be updated once per trip through draw().
*/
protected int emouseX, emouseY;
/**
* Used to set pmouseX/Y to mouseX/Y the first time mouseX/Y are used,
* otherwise pmouseX/Y are always zero, causing a nasty jump.
* <p>
* Just using (frameCount == 0) won't work since mouseXxxxx()
* may not be called until a couple frames into things.
* <p>
* @deprecated Please refrain from using this variable, it will be removed
* from future releases of Processing because it cannot be used consistently
* across platforms and input methods.
*/
@Deprecated
public boolean firstMouse = true;
/**
* When a mouse button is pressed, the value of the system variable
* <b>mouseButton</b> is set to either <b>LEFT</b>, <b>RIGHT</b>, or
* <b>CENTER</b>, depending on which button is pressed. (If no button is
* pressed, <b>mouseButton</b> may be reset to <b>0</b>. For that reason,
* it's best to use <b>mousePressed</b> first to test if any button is being
* pressed, and only then test the value of <b>mouseButton</b>, as shown in
* the examples above.)
*
* <h3>Advanced:</h3>
*
* If running on macOS, a ctrl-click will be interpreted as the right-hand
* mouse button (unlike Java, which reports it as the left mouse).
* @webref input:mouse
* @webBrief Shows which mouse button is pressed
* @see PApplet#mouseX
* @see PApplet#mouseY
* @see PApplet#pmouseX
* @see PApplet#pmouseY
* @see PApplet#mousePressed
* @see PApplet#mousePressed()
* @see PApplet#mouseReleased()
* @see PApplet#mouseClicked()
* @see PApplet#mouseMoved()
* @see PApplet#mouseDragged()
* @see PApplet#mouseWheel(MouseEvent)
*/
public int mouseButton;
/**
* The <b>mousePressed</b> variable stores whether a mouse button has been pressed.
* The <b>mouseButton</b> variable (see the related reference entry) can be used to
* determine which button has been pressed.
* <br /><br />
* Mouse and keyboard events only work when a program has <b>draw()</b>.
* Without <b>draw()</b>, the code is only run once and then stops
* listening for events.
*
* @webref input:mouse
* @webBrief Variable storing if a mouse button is pressed
* @see PApplet#mouseX
* @see PApplet#mouseY
* @see PApplet#pmouseX
* @see PApplet#pmouseY
* @see PApplet#mousePressed()
* @see PApplet#mouseReleased()
* @see PApplet#mouseClicked()
* @see PApplet#mouseMoved()
* @see PApplet#mouseDragged()
* @see PApplet#mouseButton
* @see PApplet#mouseWheel(MouseEvent)
*/
public boolean mousePressed;
// macOS: Ctrl + Left Mouse is converted to Right Mouse.
// This boolean tracks whether the conversion happened on PRESS,
// to report the same button during DRAG and on RELEASE,
// even though CTRL might have been released already.
// Otherwise, the events are inconsistent.
// https://github.com/processing/processing/issues/5672
private boolean macosCtrlClick;
/** @deprecated Use a mouse event handler that passes an event instead. */
@Deprecated
public MouseEvent mouseEvent;
/**
* The system variable <b>key</b> always contains the value of the most
* recent key on the keyboard that was used (either pressed or released).
* <br/> <br/>
* For non-ASCII keys, use the <b>keyCode</b> variable. The keys included
* in the ASCII specification (BACKSPACE, TAB, ENTER, RETURN, ESC, and
* DELETE) do not require checking to see if they key is coded, and you
* should simply use the <b>key</b> variable instead of <b>keyCode</b> If
* you're making cross-platform projects, note that the ENTER key is
* commonly used on PCs and Unix and the RETURN key is used instead on
* Macintosh. Check for both ENTER and RETURN to make sure your program
* will work for all platforms.
* <br /><br />
* There are issues with how <b>keyCode</b> behaves across different
* renderers and operating systems. Watch out for unexpected behavior as
* you switch renderers and operating systems.
*
* <h3>Advanced</h3>
*
* Last key pressed.
* <p>
* If it's a coded key, i.e. UP/DOWN/CTRL/SHIFT/ALT,
* this will be set to CODED (0xffff or 65535).
*
* @webref input:keyboard
* @webBrief The system variable that always contains the value of the most
* recent key on the keyboard that was used (either pressed or released)
* @see PApplet#keyCode
* @see PApplet#keyPressed
* @see PApplet#keyPressed()
* @see PApplet#keyReleased()
*/
public char key;
/**
* The variable <b>keyCode</b> is used to detect special keys such as the
* UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT.
* <br /><br />
* When checking for these keys, it can be useful to first check if the key
* is coded. This is done with the conditional <b>if (key == CODED)</b>, as
* shown in the example above.
* <br/> <br/>
* The keys included in the ASCII specification (BACKSPACE, TAB, ENTER,
* RETURN, ESC, and DELETE) do not require checking to see if the key is
* coded; for those keys, you should simply use the <b>key</b> variable
* directly (and not <b>keyCode</b>). If you're making cross-platform
* projects, note that the ENTER key is commonly used on PCs and Unix,
* while the RETURN key is used on Macs. Make sure your program will work
* on all platforms by checking for both ENTER and RETURN.
* <br/> <br/>
* For those familiar with Java, the values for UP and DOWN are simply
* shorter versions of Java's <b>KeyEvent.VK_UP</b> and <b>KeyEvent.VK_DOWN</b>.
* Other <b>keyCode</b> values can be found in the Java
* <a href="https://docs.oracle.com/javase/8/docs/api/java/awt/event/KeyEvent.html">KeyEvent</a>
* reference.
* <br /><br />
* There are issues with how <b>keyCode</b> behaves across different
* renderers and operating systems. Watch out for unexpected behavior
* as you switch renderers and operating systems, and also whenever
* you are using keys not mentioned in this reference entry.
* <br /><br />
* If you are using P2D or P3D as your renderer, use the
* <a href="https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/event/KeyEvent.html">NEWT KeyEvent constants</a>.
*
* <h3>Advanced</h3>
* When "key" is set to CODED, this will contain a Java key code.
* <p>
* For the arrow keys, keyCode will be one of UP, DOWN, LEFT and RIGHT.
* ALT, CONTROL and SHIFT are also available. A full set of constants
* can be obtained from java.awt.event.KeyEvent, from the VK_XXXX variables.
*
* @webref input:keyboard
* @webBrief Used to detect special keys such as the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT
* @see PApplet#key
* @see PApplet#keyPressed
* @see PApplet#keyPressed()
* @see PApplet#keyReleased()
*/
public int keyCode;
/**
* The boolean system variable <b>keyPressed</b> is <b>true</b>
* if any key is pressed and <b>false</b> if no keys are pressed.
* <br /><br />
* Note that there is a similarly named function called <b>keyPressed()</b>.
* See its reference page for more information.
*
* @webref input:keyboard
* @webBrief The boolean system variable that is <b>true</b> if any key
* is pressed and <b>false</b> if no keys are pressed
* @see PApplet#key
* @see PApplet#keyCode
* @see PApplet#keyPressed()
* @see PApplet#keyReleased()
*/
public boolean keyPressed;
List<Long> pressedKeys = new ArrayList<>(6);
/**
* The last KeyEvent object passed into a mouse function.
* @deprecated Use a key event handler that passes an event instead.
*/
@Deprecated
public KeyEvent keyEvent;
/**
*
* Confirms if a Processing program is "focused", meaning that it is active
* and will accept input from mouse or keyboard. This variable is <b>true</b> if
* it is focused and <b>false</b> if not.
*
* @webref environment
* @webBrief Confirms if a Processing program is "focused"
*/
public boolean focused = false;
/**
* Time in milliseconds when the sketch was started.
* <p>
* Used by the millis() function.
*/
long millisOffset = System.currentTimeMillis();
/**
*
* The system variable <b>frameRate</b> contains the approximate frame rate
* of the software as it executes. The initial value is 10 fps and is
* updated with each frame. The value is averaged (integrated) over several
* frames. As such, this value won't be valid until after 5-10 frames.
*
* @webref environment
* @webBrief The system variable that contains the approximate frame rate
* of the software as it executes
* @see PApplet#frameRate(float)
* @see PApplet#frameCount
*/
public float frameRate = 60;
protected boolean looping = true;
/** flag set to true when redraw() is called by the user */
protected boolean redraw = true;
/**
* The system variable <b>frameCount</b> contains the number o
* frames displayed since the program started. Inside <b>setup()</b>
* the value is 0 and during the first iteration of draw it is 1, etc.
*
* @webref environment
* @webBrief The system variable that contains the number of frames
* displayed since the program started
* @see PApplet#frameRate(float)
* @see PApplet#frameRate
*/
public int frameCount;
/** true if the sketch has stopped permanently. */
public volatile boolean finished;
/** used by the UncaughtExceptionHandler, so has to be static */
static Throwable uncaughtThrowable;
/**
* true if exit() has been called so that things shut down
* once the main thread kicks off.
*/
protected boolean exitCalled;
// ok to be static because it's not possible to mix enabled/disabled
static protected boolean disableAWT = System.getProperty("processing.awt.disable", "false").equals("true");;
// messages to send if attached as an external vm
/**
* Position of the upper left-hand corner of the editor window
* that launched this sketch.
*/
static public final String ARGS_EDITOR_LOCATION = "--editor-location";
static public final String ARGS_EXTERNAL = "--external";
/**
* Location for where to position the sketch window on screen.
* <p>
* This is used by the editor to when saving the previous sketch
* location, or could be used by other classes to launch at a
* specific position on-screen.
*/
static public final String ARGS_LOCATION = "--location";
/** Used by the PDE to suggest a display (set in prefs, passed on Run) */
static public final String ARGS_DISPLAY = "--display";
/** Disable AWT so that LWJGL and others can run */
static public final String ARGS_DISABLE_AWT = "--disable-awt";
// static public final String ARGS_SPAN_DISPLAYS = "--span";
static public final String ARGS_BGCOLOR = "--bgcolor";
static public final String ARGS_FULL_SCREEN = "--full-screen";
static public final String ARGS_WINDOW_COLOR = "--window-color";
static public final String ARGS_PRESENT = "--present";
static public final String ARGS_STOP_COLOR = "--stop-color";
static public final String ARGS_HIDE_STOP = "--hide-stop";
/**
* Allows the user or PdeEditor to set a specific sketch folder path.
* <p>
* Used by PdeEditor to pass in the location where saveFrame()
* and all that stuff should write things.
*/
static public final String ARGS_SKETCH_FOLDER = "--sketch-path";
static public final String ARGS_UI_SCALE = "--ui-scale";
/**
* When run externally to a PdeEditor,
* this is sent by the sketch when it quits.
*/
static public final String EXTERNAL_STOP = "__STOP__";
/**
* When run externally to a PDE Editor, this is sent by the sketch
* whenever the window is moved.
* <p>
* This is used so that the editor can re-open the sketch window
* in the same position as the user last left it.
*/
static public final String EXTERNAL_MOVE = "__MOVE__";
/** true if this sketch is being run by the PDE */
boolean external = false;
static final String ERROR_MIN_MAX =
"Cannot use min() or max() on an empty array.";
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
protected PSurface surface;
public PSurface getSurface() {
return surface;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
boolean insideSettings;
String renderer = JAVA2D;
int smooth = 1; // default smoothing (whatever that means for the renderer)
boolean fullScreen;
int display = -1; // use default
// Unlike the others above, needs to be public to support
// the pixelWidth and pixelHeight fields.
public int pixelDensity = 1;
boolean pixelDensityWarning = false;
boolean present;
String outputPath;
OutputStream outputStream;
// Background default needs to be different from the default value in
// PGraphics.backgroundColor, otherwise sketches that have size(100, 100)
// appear to be larger than they are, because the bg color matches.
// https://github.com/processing/processing/issues/2297
int windowColor = 0xffDDDDDD;
/**
* @param method "size" or "fullScreen"
* @param args parameters passed to the function to show the user
* @return true if safely inside the settings() method
*/
boolean insideSettings(String method, Object... args) {
if (insideSettings) {
return true;
}
final String url = "https://processing.org/reference/" + method + "_.html";
if (!external) { // post a warning for users of Eclipse and other IDEs
StringList argList = new StringList(args);
System.err.println("When not using the PDE, " + method + "() can only be used inside settings().");
System.err.println("Remove the " + method + "() method from setup(), and add the following:");
System.err.println("public void settings() {");
System.err.println(" " + method + "(" + argList.join(", ") + ");");
System.err.println("}");
}
throw new IllegalStateException(method + "() cannot be used here, see " + url);
}
void handleSettings() {
insideSettings = true;
if (!disableAWT) {
displayWidth = ShimAWT.getDisplayWidth();
displayHeight = ShimAWT.getDisplayHeight();
} else {
// https://github.com/processing/processing4/issues/57
System.err.println("AWT disabled, displayWidth/displayHeight will be 0");
}
// Here's where size(), fullScreen(), smooth(N) and noSmooth() might
// be called, conjuring up the demons of various rendering configurations.
settings();
if (display == SPAN && platform == MACOS) {
// Make sure "Displays have separate Spaces" is unchecked
// in System Preferences > Mission Control
Process p = exec("defaults", "read", "com.apple.spaces", "spans-displays");
BufferedReader outReader = createReader(p.getInputStream());
BufferedReader errReader = createReader(p.getErrorStream());
StringBuilder stdout = new StringBuilder();
StringBuilder stderr = new StringBuilder();
String line;
try {
while ((line = outReader.readLine()) != null) {
stdout.append(line);
}
while ((line = errReader.readLine()) != null) {
stderr.append(line);
}
} catch (IOException e) {
printStackTrace(e);
}
int resultCode = -1;
try {
resultCode = p.waitFor();
} catch (InterruptedException ignored) { }
if (resultCode == 1) {
String msg = trim(stderr.toString());
// This message is confusing, so don't print if it's something typical
if (!(msg.contains("The domain/default pair") && msg.contains("does not exist"))) {
System.err.println("Could not check the status of “Displays have separate spaces.”");
System.err.println("Result for 'defaults read' was " + resultCode);
System.err.println(msg);
}
}
String processOutput = trim(stdout.toString());
// On Catalina, the option may not be set, so resultCode
// will be 1 (an error, since the param doesn't exist.)
// But "Displays have separate spaces" is on by default.
// For Monterey, it appears to not be set until the user
// has visited the Mission Control preference pane once.
if (resultCode == 1 || "0".equals(processOutput)) {
System.err.println("To use fullScreen(SPAN), visit System Preferences → Mission Control");
System.err.println("and make sure that “Displays have separate spaces” is turned off.");
System.err.println("Then log out and log back in.");
}
}
insideSettings = false;
}
/**
* The <b>settings()</b> function is new with Processing 3.0.
* It's not needed in most sketches. It's only useful when it's
* absolutely necessary to define the parameters to <b>size()</b>
* with a variable. Alternately, the <b>settings()</b> function
* is necessary when using Processing code outside the
* Processing Development Environment (PDE). For example, when
* using the Eclipse code editor, it's necessary to use
* <b>settings()</b> to define the <b>size()</b> and
* <b>smooth()</b> values for a sketch.
* <br /> <br />
* The <b>settings()</b> method runs before the sketch has been
* set up, so other Processing functions cannot be used at that
* point. For instance, do not use loadImage() inside settings().
* The settings() method runs "passively" to set a few variables,
* compared to the <b>setup()</b> command that call commands in
* the Processing API.
*
* @webref environment
* @webBrief Used when absolutely necessary to define the parameters to <b>size()</b>
* with a variable
* @see PApplet#fullScreen()
* @see PApplet#setup()
* @see PApplet#size(int,int)
* @see PApplet#smooth()
*/
public void settings() {
// is this necessary? (doesn't appear to be, so removing)
//size(DEFAULT_WIDTH, DEFAULT_HEIGHT, JAVA2D);
}
final public int sketchWidth() {
return width;
}
final public int sketchHeight() {
return height;
}
final public String sketchRenderer() {
return renderer;
}
// smoothing 1 is default.. 0 is none.. 2,4,8 depend on renderer
final public int sketchSmooth() {
return smooth;
}
final public boolean sketchFullScreen() {
return fullScreen;
}
// Numbered from 1, SPAN (0) means all displays, -1 means the default display
final public int sketchDisplay() {
return display;
}
final public String sketchOutputPath() {
return outputPath;
}
final public OutputStream sketchOutputStream() {
return outputStream;
}
final public int sketchWindowColor() {
return windowColor;
}
final public int sketchPixelDensity() {
return pixelDensity;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/**
*
* This function returns the number "2" if the screen is a high-density
* screen (called a Retina display on OS X or high-dpi on Windows and Linux)
* and a "1" if not. This information is useful for a program to adapt to
* run at double the pixel density on a screen that supports it.
*