diff options
author | Sven Gothel <[email protected]> | 2014-01-04 17:18:47 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-01-04 17:18:47 +0100 |
commit | bec29cf970e6a55eb8f720afdae5a3bdc97c1ba2 (patch) | |
tree | b94db19188923dc674b5a594de9cbed4c298fd9c | |
parent | fcc0e7397bb6f3ceb1fe143667f8c59b5bf63874 (diff) |
Bug 935: NEWT OSX PointerIcon/Pointer-Visibility: Impl. OffscreenLayerSurface (OSX CALayer) w/ JAWTWindow Path
Add setCursor(..) and hideCursor() to OffscreenLayerSurface interface,
impl. in JAWTWindow w/ AWT.
This allows an OSX NEWT Window using CALayer (i.e. NewtCanvasAWT)
to have setPointerIcon(..) and setPointerVisible(..) functionality!
4 files changed, 119 insertions, 2 deletions
diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java b/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java index 3c660b41b..46bdc4d1f 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/awt/JAWTWindow.java @@ -38,18 +38,21 @@ package com.jogamp.nativewindow.awt; import com.jogamp.common.os.Platform; +import com.jogamp.common.util.IOUtil; import com.jogamp.common.util.locks.LockFactory; import com.jogamp.common.util.locks.RecursiveLock; import com.jogamp.nativewindow.MutableGraphicsConfiguration; import java.awt.Component; import java.awt.Container; +import java.awt.Cursor; import java.awt.Window; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.HierarchyEvent; import java.awt.event.HierarchyListener; import java.applet.Applet; +import java.io.IOException; import javax.media.nativewindow.AbstractGraphicsConfiguration; import javax.media.nativewindow.AbstractGraphicsDevice; @@ -63,6 +66,7 @@ import javax.media.nativewindow.SurfaceUpdatedListener; import javax.media.nativewindow.util.Insets; import javax.media.nativewindow.util.InsetsImmutable; import javax.media.nativewindow.util.Point; +import javax.media.nativewindow.util.PointImmutable; import javax.media.nativewindow.util.Rectangle; import javax.media.nativewindow.util.RectangleImmutable; @@ -427,6 +431,30 @@ public abstract class JAWTWindow implements NativeWindow, OffscreenLayerSurface, return surfaceLock; } + @Override + public final boolean setCursor(IOUtil.ClassResources resources, PointImmutable hotSpot) throws IOException { + final Cursor c; + if( null == resources || null == hotSpot ) { + c = Cursor.getDefaultCursor(); + } else { + final java.awt.Point awtHotspot = new java.awt.Point(hotSpot.getX(), hotSpot.getY()); + c = AWTMisc.getCursor(resources, awtHotspot); + } + if( null != c ) { + component.setCursor(c); + return true; + } else { + return false; + } + } + + @Override + public boolean hideCursor() { + final Cursor c = AWTMisc.getNullCursor(); + component.setCursor(c); + return true; + } + // // SurfaceUpdateListener // diff --git a/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java b/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java index 8681422ef..81983a7c2 100644 --- a/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java +++ b/src/nativewindow/classes/javax/media/nativewindow/OffscreenLayerSurface.java @@ -27,6 +27,11 @@ */ package javax.media.nativewindow; +import java.io.IOException; + +import javax.media.nativewindow.util.PointImmutable; + +import com.jogamp.common.util.IOUtil; import com.jogamp.common.util.locks.RecursiveLock; /** @@ -65,4 +70,19 @@ public interface OffscreenLayerSurface { /** Returns the recursive lock object of this surface, which synchronizes multithreaded access. */ public RecursiveLock getLock(); + /** + * Optional method setting cursor in the corresponding on-screen surface/window, if exists. + * + * @param resources maybe null for default cursor + * @param hotSpot maybe null for default cursor + * @return true if successful, i.e. on-screen surface/window w/ cursor capabilities exists. Otherwise false. + * @throws IOException + */ + public boolean setCursor(IOUtil.ClassResources resources, PointImmutable hotSpot) throws IOException; + /** + * Optional method hiding the cursor in the corresponding on-screen surface/window, if exists. + * + * @return true if successful, i.e. on-screen surface/window w/ cursor capabilities exists. Otherwise false. + */ + public boolean hideCursor(); } diff --git a/src/nativewindow/classes/jogamp/nativewindow/awt/AWTMisc.java b/src/nativewindow/classes/jogamp/nativewindow/awt/AWTMisc.java index 0fa5006cc..31de84137 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/awt/AWTMisc.java +++ b/src/nativewindow/classes/jogamp/nativewindow/awt/AWTMisc.java @@ -27,21 +27,31 @@ */ package jogamp.nativewindow.awt; +import java.awt.Cursor; import java.awt.FocusTraversalPolicy; import java.awt.Insets; +import java.awt.Point; +import java.awt.Toolkit; import java.awt.Window; import java.awt.Component; import java.awt.Container; import java.awt.Frame; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.net.URLConnection; +import java.util.HashMap; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JRootPane; import javax.swing.WindowConstants; +import javax.imageio.ImageIO; import javax.media.nativewindow.NativeWindowException; import javax.media.nativewindow.WindowClosingProtocol; import javax.swing.MenuSelectionManager; +import com.jogamp.common.util.IOUtil; + public class AWTMisc { public static JFrame getJFrame(Component c) { @@ -163,6 +173,33 @@ public class AWTMisc { MenuSelectionManager.defaultManager().clearSelectedPath(); } + static final HashMap<String, Cursor> cursorMap = new HashMap<String, Cursor>(); + static final Cursor nulCursor; + static { + final Toolkit toolkit = Toolkit.getDefaultToolkit(); + BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR); + nulCursor = toolkit.createCustomCursor(img, new Point(0,0), "nullCursor"); + } + + public static synchronized Cursor getNullCursor() { return nulCursor; } + + public static synchronized Cursor getCursor(IOUtil.ClassResources resources, Point hotSpot) throws IOException { + final String key = resources.getClass().getName()+":"+resources.resourcePaths[0]; + Cursor cursor = cursorMap.get(key); + if( null == cursor ) { + cursor = createAWTCursor(resources, hotSpot); + cursorMap.put(key, cursor); + } + return cursor; + } + private static synchronized Cursor createAWTCursor(IOUtil.ClassResources resources, Point hotSpot) throws IOException { + final URLConnection urlConn = resources.resolve(0); + BufferedImage img = ImageIO.read(urlConn.getInputStream()); + + final Toolkit toolkit = Toolkit.getDefaultToolkit(); + return toolkit.createCustomCursor(img, hotSpot, resources.resourcePaths[0]); + } + public static WindowClosingProtocol.WindowClosingMode AWT2NWClosingOperation(int awtClosingOperation) { switch (awtClosingOperation) { case WindowConstants.DISPOSE_ON_CLOSE: diff --git a/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java b/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java index 6f3c95570..748604994 100644 --- a/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java +++ b/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java @@ -39,6 +39,8 @@ import javax.media.nativewindow.GraphicsConfigurationFactory; import javax.media.nativewindow.NativeWindow; import javax.media.nativewindow.NativeWindowException; import javax.media.nativewindow.MutableSurface; +import javax.media.nativewindow.NativeWindowFactory; +import javax.media.nativewindow.OffscreenLayerSurface; import javax.media.nativewindow.VisualIDHolder; import javax.media.nativewindow.util.Insets; import javax.media.nativewindow.util.Point; @@ -403,7 +405,23 @@ public class WindowDriver extends WindowImpl implements MutableSurface, DriverCl throw new RuntimeException("Failed: "+pi+", "+WindowDriver.this); } } } ); - } // else may need offscreen solution ? FIXME + } else { + final OffscreenLayerSurface ols = NativeWindowFactory.getOffscreenLayerSurface(this, true); + if( null != ols ) { + try { + setOLSPointer(ols, pi); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + private static void setOLSPointer(final OffscreenLayerSurface ols, final PointerIconImpl pi) throws Exception { + if( null != pi ) { + ols.setCursor(pi.getResource(), pi.getHotspot()); + } else { + ols.setCursor(null, null); // default + } } @Override @@ -417,7 +435,21 @@ public class WindowDriver extends WindowImpl implements MutableSurface, DriverCl } } } ); return true; // setPointerVisible0 always returns true .. - } // else may need offscreen solution ? FIXME + } else { + final OffscreenLayerSurface ols = NativeWindowFactory.getOffscreenLayerSurface(this, true); + if( null != ols ) { + try { + if( pointerVisible ) { + setOLSPointer(ols, (PointerIconImpl)getPointerIcon()); + } else { + ols.hideCursor(); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + } + } + } return false; } |