1+ package rprocessing .mode .run ;
2+
3+ import java .awt .Point ;
4+ import java .io .File ;
5+ import java .io .Serializable ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+
9+ import processing .app .Base ;
10+ import processing .app .Platform ;
11+ import processing .app .Sketch ;
12+ import processing .core .PApplet ;
13+ import rprocessing .RunnableSketch ;
14+ import rprocessing .mode .DisplayType ;
15+
16+ /**
17+ * A sketch run from the PDE.
18+ *
19+ * This is created in the PDE process, serialized, and then used in the sketch process.
20+ *
21+ * @author github.com/gaocegege
22+ */
23+ @ SuppressWarnings ("serial" )
24+ public class PdeSketch implements RunnableSketch , Serializable {
25+
26+ private final List <File > libraryDirs ;
27+ private final File mainFile ;
28+ private final String mainCode ;
29+ private final File sketchHome ;
30+ private final File realSketchPath ;
31+ private final Point location ;
32+ private final LocationType locationType ;
33+ private final DisplayType displayType ;
34+
35+ public final String [] codeFileNames ; // unique to PdeSketch - leave as public field?
36+
37+ public PdeSketch (final Sketch sketch , final File sketchPath , final DisplayType displayType ,
38+ final Point location , final LocationType locationType ) {
39+
40+ this .displayType = displayType ;
41+ this .location = location ;
42+ this .locationType = locationType ;
43+
44+ this .mainFile = sketchPath .getAbsoluteFile ();
45+ this .mainCode = sketch .getMainProgram ();
46+ this .sketchHome = sketch .getFolder ().getAbsoluteFile ();
47+ this .realSketchPath = sketchPath ;
48+
49+ final List <File > libraryDirs = new ArrayList <>();
50+ libraryDirs .add (Platform .getContentFile ("modes/java/libraries" ));
51+ libraryDirs .add (Base .getSketchbookLibrariesFolder ());
52+ libraryDirs .add (sketchPath );
53+ this .libraryDirs = libraryDirs ;
54+
55+ final String [] codeFileNames = new String [sketch .getCodeCount ()];
56+ for (int i = 0 ; i < codeFileNames .length ; i ++) {
57+ codeFileNames [i ] = sketch .getCode (i ).getFile ().getName ();
58+ }
59+ this .codeFileNames = codeFileNames ;
60+ }
61+
62+ public static enum LocationType {
63+ EDITOR_LOCATION , SKETCH_LOCATION ;
64+ }
65+
66+ @ Override
67+ public File getMainFile () {
68+ return mainFile ;
69+ }
70+
71+ @ Override
72+ public String getMainCode () {
73+ return mainCode ;
74+ }
75+
76+ @ Override
77+ public File getHomeDirectory () {
78+ return mainFile .getParentFile ();
79+ }
80+
81+ @ Override
82+ public String [] getPAppletArguments () {
83+ final List <String > args = new ArrayList <>();
84+
85+ args .add (PApplet .ARGS_EXTERNAL );
86+ args .add (PApplet .ARGS_SKETCH_FOLDER + "=" + sketchHome );
87+
88+ switch (displayType ) {
89+ case WINDOWED :
90+ if (locationType == LocationType .EDITOR_LOCATION ) {
91+ args .add (String .format ("%s=%d,%d" , PApplet .ARGS_EDITOR_LOCATION , location .x ,
92+ location .y ));
93+ } else if (locationType == LocationType .SKETCH_LOCATION ) {
94+ args .add (String .format ("%s=%d,%d" , PApplet .ARGS_LOCATION , location .x ,
95+ location .y ));
96+ }
97+ break ;
98+ case PRESENTATION :
99+ args .add ("fullScreen" );
100+ break ;
101+ }
102+
103+ args .add (mainFile .getName ()); // sketch name; has to be last argument
104+
105+ return args .toArray (new String [0 ]);
106+ }
107+
108+ @ Override
109+ public boolean shouldRun () {
110+ return true ;
111+ }
112+ }
0 commit comments