Skip to content

Commit 29bc3cb

Browse files
committed
working on an implementation of of setIcon() for the OpenGL renderer
1 parent 36f4667 commit 29bc3cb

File tree

2 files changed

+156
-16
lines changed

2 files changed

+156
-16
lines changed

core/src/processing/opengl/PJOGL.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public class PJOGL extends PGL {
6565
// OpenGL profile to use (2, 3 or 4)
6666
public static int profile = 2;
6767

68+
// User-provided icons to override defaults
69+
protected static String[] icons = null;
70+
6871
// The two windowing toolkits available to use in JOGL:
6972
public static final int AWT = 0; // http://jogamp.org/wiki/index.php/Using_JOGL_in_AWT_SWT_and_Swing
7073
public static final int NEWT = 1; // http://jogamp.org/jogl/doc/NEWT-Overview.html
@@ -168,6 +171,12 @@ protected void reinitSurface() {}
168171
protected void registerListeners() {}
169172

170173

174+
static public void setIcon(String... icons) {
175+
PJOGL.icons = new String[icons.length];
176+
PApplet.arrayCopy(icons, PJOGL.icons);
177+
}
178+
179+
171180
///////////////////////////////////////////////////////////////
172181

173182
// Public methods to get/set renderer's properties

core/src/processing/opengl/PSurfaceJOGL.java

Lines changed: 147 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
import java.awt.Rectangle;
3232
import java.awt.image.BufferedImage;
3333
import java.awt.image.DataBufferInt;
34+
import java.io.File;
35+
import java.io.FileInputStream;
36+
import java.io.IOException;
37+
import java.io.InputStream;
3438
import java.nio.ByteBuffer;
3539
import java.util.ArrayList;
3640
import java.util.Collections;
@@ -104,6 +108,8 @@ public class PSurfaceJOGL implements PSurface {
104108

105109
protected float[] currentPixelScale = {0, 0};
106110

111+
protected boolean external = false;
112+
107113
public PSurfaceJOGL(PGraphics graphics) {
108114
this.graphics = graphics;
109115
this.pgl = (PJOGL) ((PGraphicsOpenGL)graphics).pgl;
@@ -483,7 +489,8 @@ public void run() {
483489

484490

485491
public void setIcon(PImage icon) {
486-
// TODO Auto-generated method stub
492+
PGraphics.showWarning("Window icons for OpenGL sketches can only be set in settings()\n" +
493+
"using PJOGL.setIcon(filename).");
487494
}
488495

489496

@@ -499,28 +506,150 @@ public void run() {
499506

500507

501508
protected void initIcons() {
502-
final int[] sizes = { 16, 32, 48, 64, 128, 256, 512 };
503-
String[] iconImages = new String[sizes.length];
504-
for (int i = 0; i < sizes.length; i++) {
505-
iconImages[i] = "/icon/icon-" + sizes[i] + ".png";
509+
IOUtil.ClassResources res = null;
510+
if (PJOGL.icons == null || PJOGL.icons.length == 0) {
511+
// Default Processing icons
512+
final int[] sizes = { 16, 32, 48, 64, 128, 256, 512 };
513+
String[] iconImages = new String[sizes.length];
514+
for (int i = 0; i < sizes.length; i++) {
515+
iconImages[i] = "/icon/icon-" + sizes[i] + ".png";
516+
}
517+
res = new ClassResources(iconImages,
518+
PApplet.class.getClassLoader(),
519+
PApplet.class);
520+
} else {
521+
// Loading custom icons from user-provided files.
522+
String[] iconImages = new String[PJOGL.icons.length];
523+
for (int i = 0; i < PJOGL.icons.length; i++) {
524+
iconImages[i] = resourceFilename(PJOGL.icons[i]);
525+
}
526+
527+
res = new ClassResources(iconImages,
528+
sketch.getClass().getClassLoader(),
529+
sketch.getClass());
506530
}
507-
IOUtil.ClassResources res = new ClassResources(iconImages,
508-
PApplet.class.getClassLoader(),
509-
PApplet.class);
510531
NewtFactory.setWindowIcons(res);
511532
}
512533

513534

514-
// private void setFrameCentered() {
515-
// }
535+
@SuppressWarnings("resource")
536+
private String resourceFilename(String filename) {
537+
// The code below comes from PApplet.createInputRaw() with a few adaptations
538+
InputStream stream = null;
539+
try {
540+
// First see if it's in a data folder. This may fail by throwing
541+
// a SecurityException. If so, this whole block will be skipped.
542+
File file = new File(sketch.dataPath(filename));
543+
if (!file.exists()) {
544+
// next see if it's just in the sketch folder
545+
file = sketch.sketchFile(filename);
546+
}
547+
548+
if (file.exists() && !file.isDirectory()) {
549+
try {
550+
// handle case sensitivity check
551+
String filePath = file.getCanonicalPath();
552+
String filenameActual = new File(filePath).getName();
553+
// make sure there isn't a subfolder prepended to the name
554+
String filenameShort = new File(filename).getName();
555+
// if the actual filename is the same, but capitalized
556+
// differently, warn the user.
557+
//if (filenameActual.equalsIgnoreCase(filenameShort) &&
558+
//!filenameActual.equals(filenameShort)) {
559+
if (!filenameActual.equals(filenameShort)) {
560+
throw new RuntimeException("This file is named " +
561+
filenameActual + " not " +
562+
filename + ". Rename the file " +
563+
"or change your code.");
564+
}
565+
} catch (IOException e) { }
566+
}
567+
568+
stream = new FileInputStream(file);
569+
if (stream != null) {
570+
stream.close();
571+
return file.getCanonicalPath();
572+
}
573+
574+
// have to break these out because a general Exception might
575+
// catch the RuntimeException being thrown above
576+
} catch (IOException ioe) {
577+
} catch (SecurityException se) { }
578+
579+
ClassLoader cl = sketch.getClass().getClassLoader();
580+
581+
try {
582+
// by default, data files are exported to the root path of the jar.
583+
// (not the data folder) so check there first.
584+
stream = cl.getResourceAsStream("data/" + filename);
585+
if (stream != null) {
586+
String cn = stream.getClass().getName();
587+
// this is an irritation of sun's java plug-in, which will return
588+
// a non-null stream for an object that doesn't exist. like all good
589+
// things, this is probably introduced in java 1.5. awesome!
590+
// http://dev.processing.org/bugs/show_bug.cgi?id=359
591+
if (!cn.equals("sun.plugin.cache.EmptyInputStream")) {
592+
stream.close();
593+
return "data/" + filename;
594+
}
595+
}
596+
597+
// When used with an online script, also need to check without the
598+
// data folder, in case it's not in a subfolder called 'data'.
599+
// http://dev.processing.org/bugs/show_bug.cgi?id=389
600+
stream = cl.getResourceAsStream(filename);
601+
if (stream != null) {
602+
String cn = stream.getClass().getName();
603+
if (!cn.equals("sun.plugin.cache.EmptyInputStream")) {
604+
stream.close();
605+
return filename;
606+
}
607+
}
608+
} catch (IOException e) { }
609+
610+
try {
611+
// attempt to load from a local file, used when running as
612+
// an application, or as a signed applet
613+
try { // first try to catch any security exceptions
614+
try {
615+
String path = sketch.dataPath(filename);
616+
stream = new FileInputStream(path);
617+
if (stream != null) {
618+
stream.close();
619+
return path;
620+
}
621+
} catch (IOException e2) { }
622+
623+
try {
624+
String path = sketch.sketchPath(filename);
625+
stream = new FileInputStream(path);
626+
if (stream != null) {
627+
stream.close();
628+
return path;
629+
}
630+
} catch (Exception e) { } // ignored
631+
632+
try {
633+
stream = new FileInputStream(filename);
634+
if (stream != null) {
635+
stream.close();
636+
return filename;
637+
}
638+
} catch (IOException e1) { }
639+
640+
} catch (SecurityException se) { } // online, whups
641+
642+
} catch (Exception e) {
643+
//die(e.getMessage(), e);
644+
e.printStackTrace();
645+
}
646+
647+
return "";
648+
}
516649

517650

518651
@Override
519652
public void placeWindow(int[] location, int[] editorLocation) {
520-
// Dimension dim = new Dimension(sketchWidth, sketchHeight);
521-
// int contentW = Math.max(sketchWidth, MIN_WINDOW_WIDTH);
522-
// int contentH = Math.max(sketchHeight, MIN_WINDOW_HEIGHT);
523-
524653
int x = window.getX() - window.getInsets().getLeftWidth();
525654
int y = window.getY() - window.getInsets().getTopHeight();
526655
int w = window.getWidth() + window.getInsets().getTotalWidth();
@@ -588,8 +717,7 @@ public void placePresent(int stopColor) {
588717

589718

590719
public void setupExternalMessages() {
591-
// TODO Auto-generated method stub
592-
720+
external = true;
593721
}
594722

595723

@@ -820,6 +948,9 @@ public void windowDestroyed(com.jogamp.newt.event.WindowEvent arg0) {
820948

821949
@Override
822950
public void windowMoved(com.jogamp.newt.event.WindowEvent arg0) {
951+
if (external) {
952+
sketch.frameMoved(window.getX(), window.getY());
953+
}
823954
}
824955

825956
@Override

0 commit comments

Comments
 (0)