From efef632ec2355dbe5ada8a62ee8604d9d8701afd Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Thu, 18 Jun 2015 16:05:15 -0400 Subject: [PATCH] more surface updates based on discussion in #3388 --- core/src/processing/core/PApplet.java | 21 ++++++---- core/src/processing/core/PGraphics.java | 2 +- core/src/processing/core/PSurface.java | 14 +++++++ core/src/processing/core/PSurfaceAWT.java | 19 +++++++-- core/src/processing/core/PSurfaceFX.java | 20 +++++++++ core/src/processing/core/PSurfaceNone.java | 35 ++++++++++++---- core/src/processing/opengl/PSurfaceJOGL.java | 43 +++++++++++++++----- core/todo.txt | 9 ++-- 8 files changed, 125 insertions(+), 38 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 86bdbdbb5..e7a85ae2d 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -772,18 +772,21 @@ public class PApplet implements PConstants { protected PSurface surface; - /** The frame containing this sketch (if any) */ + /** + * A dummy frame to keep compatibility with 2.x code + * and encourage users to update. + */ public Frame frame; - public Frame getFrame() { - return frame; - } - - - public void setFrame(Frame frame) { - this.frame = frame; - } +// public Frame getFrame() { +// return frame; +// } +// +// +// public void setFrame(Frame frame) { +// this.frame = frame; +// } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . diff --git a/core/src/processing/core/PGraphics.java b/core/src/processing/core/PGraphics.java index ab3ab0ca9..1b4cb22c9 100644 --- a/core/src/processing/core/PGraphics.java +++ b/core/src/processing/core/PGraphics.java @@ -4934,7 +4934,7 @@ public class PGraphics extends PImage implements PConstants { */ @SuppressWarnings("deprecation") public FontMetrics getFontMetrics(Font font) { // ignore - Frame frame = parent.getFrame(); + Frame frame = parent.frame; if (frame != null) { return frame.getToolkit().getFontMetrics(font); } diff --git a/core/src/processing/core/PSurface.java b/core/src/processing/core/PSurface.java index 10359db3d..45dcf1f53 100644 --- a/core/src/processing/core/PSurface.java +++ b/core/src/processing/core/PSurface.java @@ -46,6 +46,16 @@ public interface PSurface { // int deviceIndex, boolean fullScreen, boolean spanDisplays); public void initFrame(PApplet sketch); + /** + * Get the native window object associated with this drawing surface. + * For Java2D, this will be an AWT Frame object. For OpenGL, the window. + * The data returned here is subject to the whims of the renderer, + * and using this method means you're willing to deal with underlying + * implementation changes and that you won't throw a fit like a toddler + * if your code breaks sometime in the future. + */ + public Object getNative(); + // // Just call these on an AWT Frame object stored in PApplet. @@ -61,6 +71,8 @@ public interface PSurface { /** Set true if we want to resize things (default is not resizable) */ public void setResizable(boolean resizable); + public void setIcon(PImage icon); + // // public void placeWindow(int[] location); @@ -78,6 +90,8 @@ public interface PSurface { // sets displayWidth/Height inside PApplet //public void checkDisplaySize(); + public void setLocation(int x, int y); + public void setSize(int width, int height); /** diff --git a/core/src/processing/core/PSurfaceAWT.java b/core/src/processing/core/PSurfaceAWT.java index 3bb5f19f5..6b849c598 100644 --- a/core/src/processing/core/PSurfaceAWT.java +++ b/core/src/processing/core/PSurfaceAWT.java @@ -149,6 +149,11 @@ public class PSurfaceAWT extends PSurfaceNone { private Dimension newSize = new Dimension(0, 0); + public Frame getFrame() { + return frame; + } + + @Override public Dimension getPreferredSize() { return new Dimension(sketchWidth, sketchHeight); @@ -531,7 +536,13 @@ public class PSurfaceAWT extends PSurfaceNone { // http://code.google.com/p/processing/issues/detail?id=467 frame.setResizable(false); - sketch.setFrame(frame); +// sketch.setFrame(frame); + } + + + @Override + public Object getNative() { + return canvas; } @@ -833,9 +844,9 @@ public class PSurfaceAWT extends PSurfaceNone { // } - @Override - public void setSmooth(int level) { - } +// @Override +// public void setSmooth(int level) { +// } /* diff --git a/core/src/processing/core/PSurfaceFX.java b/core/src/processing/core/PSurfaceFX.java index e7a61fc9e..df4501611 100644 --- a/core/src/processing/core/PSurfaceFX.java +++ b/core/src/processing/core/PSurfaceFX.java @@ -68,6 +68,11 @@ public class PSurfaceFX implements PSurface { } + public Object getNative() { + return canvas; + } + + class ResizableCanvas extends Canvas { public ResizableCanvas() { @@ -141,6 +146,10 @@ public class PSurfaceFX implements PSurface { }); } + public Stage getStage() { + return stage; + } + @Override public boolean isResizable() { return true; @@ -248,6 +257,11 @@ public class PSurfaceFX implements PSurface { } + public void setIcon(PImage icon) { + // TODO implement this in JavaFX + } + + /* @Override public void placeWindow(int[] location) { @@ -386,6 +400,12 @@ public class PSurfaceFX implements PSurface { } + public void setLocation(int x, int y) { + stage.setX(x); + stage.setY(y); + } + + public void setSize(int width, int height) { //System.out.format("%s.setSize(%d, %d)%n", getClass().getSimpleName(), width, height); stage.setWidth(width); diff --git a/core/src/processing/core/PSurfaceNone.java b/core/src/processing/core/PSurfaceNone.java index b8220d734..ec83cb951 100644 --- a/core/src/processing/core/PSurfaceNone.java +++ b/core/src/processing/core/PSurfaceNone.java @@ -67,26 +67,38 @@ public class PSurfaceNone implements PSurface { } + public Object getNative() { + return null; + } + + /** Set the window (and dock, or whatever necessary) title. */ + @Override public void setTitle(String title) { - // TODO ignored? + // You're in a utopian PSurface implementation where titles don't exist. + } + + + @Override + public void setIcon(PImage image) { + // I ain't visible, man. } /** Show or hide the window. */ + @Override public void setVisible(boolean visible) { - // TODO ignored? + // I'm always invisible. You can't catch me. } /** Set true if we want to resize things (default is not resizable) */ + @Override public void setResizable(boolean resizable) { - // TODO ignored? + // I don't need size to know my worth. } -// public void placeWindow(int[] location) { } - @Override public void placeWindow(int[] location, int[] editorLocation) { } @@ -102,6 +114,12 @@ public class PSurfaceNone implements PSurface { // + @Override + public void setLocation(int x, int y) { + // I'm everywhere, because I'm nowhere. + } + + @Override public void setSize(int wide, int high) { if (PApplet.DEBUG) { @@ -136,10 +154,9 @@ public class PSurfaceNone implements PSurface { // } - public void setSmooth(int level) { - // TODO Auto-generated method stub - - } +// public void setSmooth(int level) { +// // TODO Auto-generated method stub +// } // void requestFocus() { // } diff --git a/core/src/processing/opengl/PSurfaceJOGL.java b/core/src/processing/opengl/PSurfaceJOGL.java index 4ce45990a..bea3e521a 100644 --- a/core/src/processing/opengl/PSurfaceJOGL.java +++ b/core/src/processing/opengl/PSurfaceJOGL.java @@ -111,6 +111,12 @@ public class PSurfaceJOGL implements PSurface { } + public Object getNative() { + System.err.println("PSurfaceJOGL.getNative() not implemented"); + return null; + } + + protected void initScreen() { display = NewtFactory.createDisplay(null); display.addReference(); @@ -336,22 +342,40 @@ public class PSurfaceJOGL implements PSurface { }).start(); } + @Override public void setTitle(String title) { window.setTitle(title); } + @Override public void setVisible(boolean visible) { window.setVisible(visible); } + @Override public void setResizable(boolean resizable) { // TODO Auto-generated method stub - } + + public void setIcon(PImage icon) { + // TODO Auto-generated method stub + } + + + protected void initIcons() { + final int[] sizes = { 16, 32, 48, 64, 128, 256, 512 }; + String[] iconImages = new String[sizes.length]; + for (int i = 0; i < sizes.length; i++) { + iconImages[i] = "/icon/icon-" + sizes[i] + ".png"; + } + NewtFactory.setWindowIcons(new ClassResources(PApplet.class, iconImages)); + } + + // private void setFrameCentered() { // } @@ -460,16 +484,6 @@ public class PSurfaceJOGL implements PSurface { } - protected void initIcons() { - final int[] sizes = { 16, 32, 48, 64, 128, 256, 512 }; - String[] iconImages = new String[sizes.length]; - for (int i = 0; i < sizes.length; i++) { - iconImages[i] = "/icon/icon-" + sizes[i] + ".png"; - } - NewtFactory.setWindowIcons(new ClassResources(PApplet.class, iconImages)); - } - - public void setupExternalMessages() { // TODO Auto-generated method stub @@ -523,6 +537,13 @@ public class PSurfaceJOGL implements PSurface { } } + + public void setLocation(int x, int y) { + // TODO implement me! + System.err.println("PSurfaceJOGL.setLocation() not yet implemented."); + } + + public void setSize(int width, int height) { if (width == sketch.width && height == sketch.height) { return; diff --git a/core/todo.txt b/core/todo.txt index cd1bbcaf2..f18716ef1 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -81,11 +81,11 @@ beta _ pixelDensity(BEST)? (messes with pixels, but for most sketches, helpful) _ surface.setXxx() handling _ https://github.com/processing/processing/issues/3388 -_ setResizable, setVisible, setTitle -_ setIconImage(java.awt.Image) -> take a PImage instead? +X setResizable, setVisible, setTitle +X setIconImage(java.awt.Image) -> take a PImage instead? _ removeNotify(), addNotify(), setUndecorated(boolean) -_ setting menubar will be surface-specific -_ setLocation(int, int) and setSize(int, int) +X setting menubar will be surface-specific +X setLocation(int, int) and setSize(int, int) _ add the "don't use this" warning to the JFrame in PSurfaceAWT _ draw() executes twice when noLoop() called in setup() _ https://github.com/processing/processing/issues/3310 @@ -126,6 +126,7 @@ _ move loadImage() into PGraphics, with AWT version the default? _ or pass createImage() through to renderer? _ implement frameRate() _ implement external messages (moving the window) +_ implement PSurfaceFX.setIcon() opengl