2323package processing .mode .android ;
2424
2525import org .gradle .tooling .*;
26+
2627import processing .app .Base ;
2728import processing .app .Library ;
2829import processing .app .Messages ;
3940import java .util .HashMap ;
4041import java .util .Properties ;
4142
43+ import java .util .*;
44+ import processing .app .SketchCode ;
45+ import processing .mode .java .preproc .PreprocessorResult ;
46+
47+
4248/**
4349 * Class with all the infrastructure needed to build a sketch in the Android
4450 * 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)
245251 PdePreprocessor preprocessor = PdePreprocessor .builderFor (sketch .getName ()).setDestinationPackage (pckgName ).build ();
246252 sketchClassName = preprocess (srcFolder , pckgName , preprocessor , false );
247253 if (sketchClassName != null ) {
248- renderer = "P2D" ;
254+ renderer = getRenderer ( srcFolder , pckgName , preprocessor ) ;
249255 writeMainClass (srcFolder , external );
250256 createTopModule ("':" + module + "'" , password );
251257 createAppModule (module );
@@ -254,6 +260,38 @@ protected File createProject(boolean external, String password)
254260 return tmpFolder ;
255261 }
256262
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+ }
257295
258296 protected boolean gradleBuildBundle () throws SketchException {
259297 ProjectConnection connection = GradleConnector .newConnector ()
@@ -822,7 +860,7 @@ private void copyImportedLibs(final File libsFolder,
822860 final File mainFolder ,
823861 final File assetsFolder ) throws IOException {
824862 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
826864 for (File exportFile : library .getAndroidExports ()) {
827865 String exportName = exportFile .getName ();
828866
0 commit comments