23
23
package processing .mode .android ;
24
24
25
25
import org .gradle .tooling .*;
26
+
26
27
import processing .app .Base ;
27
28
import processing .app .Library ;
28
29
import processing .app .Messages ;
39
40
import java .util .HashMap ;
40
41
import java .util .Properties ;
41
42
43
+ import java .util .*;
44
+ import processing .app .SketchCode ;
45
+ import processing .mode .java .preproc .PreprocessorResult ;
46
+
47
+
42
48
/**
43
49
* Class with all the infrastructure needed to build a sketch in the Android
44
50
* mode and run it either on the device or in the emulator, using Gradle as the
@@ -245,7 +251,7 @@ protected File createProject(boolean external, String password)
245
251
PdePreprocessor preprocessor = PdePreprocessor .builderFor (sketch .getName ()).setDestinationPackage (pckgName ).build ();
246
252
sketchClassName = preprocess (srcFolder , pckgName , preprocessor , false );
247
253
if (sketchClassName != null ) {
248
- renderer = "P2D" ;
254
+ renderer = getRenderer ( srcFolder , pckgName , preprocessor ) ;
249
255
writeMainClass (srcFolder , external );
250
256
createTopModule ("':" + module + "'" , password );
251
257
createAppModule (module );
@@ -254,6 +260,38 @@ protected File createProject(boolean external, String password)
254
260
return tmpFolder ;
255
261
}
256
262
263
+ protected String getRenderer (File srcFolder ,
264
+ String packageName ,
265
+ PdePreprocessor preprocessor ) throws SketchException {
266
+ StringBuilder bigCode = new StringBuilder ();
267
+ int bigCount = 0 ;
268
+ List <Integer > linesPerTab = new ArrayList <>();
269
+ for (SketchCode sc : sketch .getCode ()) {
270
+ if (sc .isExtension ("pde" )) {
271
+ sc .setPreprocOffset (bigCount );
272
+ bigCode .append (sc .getProgram ());
273
+ bigCode .append ('\n' );
274
+ linesPerTab .add (bigCount );
275
+ bigCount += sc .getLineCount ();
276
+ }
277
+ }
278
+ linesPerTab .add (bigCount );
279
+
280
+ PreprocessorResult result ;
281
+ try {
282
+ File outputFolder = (packageName == null ) ?
283
+ srcFolder : new File (srcFolder , packageName .replace ('.' , '/' ));
284
+ outputFolder .mkdirs ();
285
+ final File tmp = new File (outputFolder , sketch .getMainName () + "_tmp.java" );
286
+ try (PrintWriter stream = PApplet .createWriter (tmp )) {
287
+ result = preprocessor .write (stream , bigCode .toString (), null );
288
+ }
289
+ tmp .delete ();
290
+ return result .getSketchRenderer ();
291
+ } catch (Exception ex ) {
292
+ return "P2D" ;
293
+ }
294
+ }
257
295
258
296
protected boolean gradleBuildBundle () throws SketchException {
259
297
ProjectConnection connection = GradleConnector .newConnector ()
@@ -822,7 +860,7 @@ private void copyImportedLibs(final File libsFolder,
822
860
final File mainFolder ,
823
861
final File assetsFolder ) throws IOException {
824
862
for (Library library : getImportedLibraries ()) {
825
- // add each item from the library folder / export list to the output
863
+ // Add each item from the library folder / export list to the output
826
864
for (File exportFile : library .getAndroidExports ()) {
827
865
String exportName = exportFile .getName ();
828
866
0 commit comments