diff options
author | Sven Gothel <[email protected]> | 2023-12-24 16:09:38 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-12-24 16:09:38 +0100 |
commit | a782f343aeecbfdb640198bcc73794a90f63730d (patch) | |
tree | 259eed17c8b2806d00984795b760b84bc1cf55b8 | |
parent | feb3d34be097bcbef5ebc40342b405a832ac581f (diff) |
GraphUI Scene/Shape: Add KeyListener for activeShape
-rw-r--r-- | src/graphui/classes/com/jogamp/graph/ui/Scene.java | 50 | ||||
-rw-r--r-- | src/graphui/classes/com/jogamp/graph/ui/Shape.java | 54 |
2 files changed, 98 insertions, 6 deletions
diff --git a/src/graphui/classes/com/jogamp/graph/ui/Scene.java b/src/graphui/classes/com/jogamp/graph/ui/Scene.java index b850fe346..101bc70e2 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/Scene.java +++ b/src/graphui/classes/com/jogamp/graph/ui/Scene.java @@ -63,6 +63,8 @@ import com.jogamp.math.util.PMVMatrix4f; import com.jogamp.graph.ui.Shape.Visitor1; import com.jogamp.newt.event.GestureHandler; import com.jogamp.newt.event.InputEvent; +import com.jogamp.newt.event.KeyEvent; +import com.jogamp.newt.event.KeyListener; import com.jogamp.newt.event.MouseEvent; import com.jogamp.newt.event.MouseListener; import com.jogamp.newt.event.PinchToZoomGesture; @@ -149,6 +151,7 @@ public final class Scene implements Container, GLEventListener { private SBCMouseListener sbcMouseListener = null; private SBCGestureListener sbcGestureListener = null; private PinchToZoomGesture pinchToZoomGesture = null; + private SBCKeyListener sbcKeyListener = null; final GLReadBufferUtil screenshot; @@ -222,15 +225,15 @@ public final class Scene implements Container, GLEventListener { @Override public final boolean isFrustumCullingEnabled() { return doFrustumCulling; } - public void attachGLAutoDrawable(final GLAutoDrawable drawable) { + public synchronized void attachGLAutoDrawable(final GLAutoDrawable drawable) { cDrawable = drawable; } - public void detachGLAutoDrawable(final GLAutoDrawable drawable) { + public synchronized void detachGLAutoDrawable(final GLAutoDrawable drawable) { if( cDrawable == drawable ) { cDrawable = null; } } - public void attachInputListenerTo(final GLWindow window) { + public synchronized void attachInputListenerTo(final GLWindow window) { cDrawable = window; if(null == sbcMouseListener) { sbcMouseListener = new SBCMouseListener(); @@ -240,9 +243,13 @@ public final class Scene implements Container, GLEventListener { pinchToZoomGesture = new PinchToZoomGesture(window.getNativeSurface(), false); window.addGestureHandler(pinchToZoomGesture); } + if(null == sbcKeyListener) { + sbcKeyListener = new SBCKeyListener(); + window.addKeyListener(sbcKeyListener); + } } - public void detachInputListenerFrom(final GLWindow window) { + public synchronized void detachInputListenerFrom(final GLWindow window) { if(null != sbcMouseListener) { window.removeMouseListener(sbcMouseListener); sbcMouseListener = null; @@ -251,6 +258,10 @@ public final class Scene implements Container, GLEventListener { window.removeGestureHandler(pinchToZoomGesture); pinchToZoomGesture = null; } + if(null == sbcKeyListener) { + window.removeKeyListener(sbcKeyListener); + sbcKeyListener = null; + } } @Override @@ -826,7 +837,20 @@ public final class Scene implements Container, GLEventListener { */ @Override public boolean forSortedAll(final Comparator<Shape> sortComp, final PMVMatrix4f pmv, final Visitor2 v) { - return TreeTool.forSortedAll(sortComp, shapes, pmv, v); + try { + return TreeTool.forSortedAll(sortComp, shapes, pmv, v); + } catch (final java.lang.IllegalArgumentException iae) { + System.err.println("Caught: "+iae.getMessage()); + System.err.println("float[] descendingZValues = { "); + for(final Shape s : shapes) { + final int thisBits = Float.floatToIntBits(s.getAdjustedZ()); + System.err.println(" Float.intBitsToFloat(0x"+Integer.toHexString(thisBits)+"),"); + } + System.err.println(" };"); + iae.printStackTrace(); + throw iae; + // return true; + } } /** @@ -1182,6 +1206,21 @@ public final class Scene implements Container, GLEventListener { clear(); } } + private final class SBCKeyListener implements KeyListener { + @Override + public void keyPressed(final KeyEvent e) { + if( null != activeShape && activeShape.isInteractive() ) { + activeShape.dispatchKeyEvent(e); + } + } + + @Override + public void keyReleased(final KeyEvent e) { + if( null != activeShape && activeShape.isInteractive() ) { + activeShape.dispatchKeyEvent(e); + } + } + } /** * Return a formatted status string containing avg fps and avg frame duration. @@ -1414,5 +1453,4 @@ public final class Scene implements Container, GLEventListener { public float getZFar() { return zFar; } }; private PMVMatrixSetup pmvMatrixSetup = new DefaultPMVMatrixSetup(); - } diff --git a/src/graphui/classes/com/jogamp/graph/ui/Shape.java b/src/graphui/classes/com/jogamp/graph/ui/Shape.java index 99b9cd9b7..ee7f946c2 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/Shape.java +++ b/src/graphui/classes/com/jogamp/graph/ui/Shape.java @@ -47,6 +47,8 @@ import com.jogamp.math.geom.AABBox; import com.jogamp.math.util.PMVMatrix4f; import com.jogamp.newt.event.GestureHandler.GestureEvent; import com.jogamp.newt.event.GestureHandler.GestureListener; +import com.jogamp.newt.event.KeyEvent; +import com.jogamp.newt.event.KeyListener; import com.jogamp.newt.event.MouseAdapter; import com.jogamp.newt.event.NEWTEvent; import com.jogamp.newt.event.PinchToZoomGesture; @@ -188,6 +190,7 @@ public abstract class Shape { private Padding padding = null; private final Vec4f borderColor = new Vec4f(0.0f, 0.0f, 0.0f, 1.0f); private ArrayList<MouseGestureListener> mouseListeners = new ArrayList<MouseGestureListener>(); + private ArrayList<KeyListener> keyListeners = new ArrayList<KeyListener>(); private ListenerBool onInitListener = null; private MoveListener onMoveListener = null; @@ -312,6 +315,7 @@ public abstract class Shape { scale.set(1f, 1f, 1f); box.reset(); mouseListeners.clear(); + keyListeners.clear(); onInitListener = null; onMoveListener = null; onToggleListener = null; @@ -1412,6 +1416,28 @@ public abstract class Shape { return this; } + public final Shape addKeyListener(final KeyListener l) { + if(l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList<KeyListener> clonedListeners = (ArrayList<KeyListener>) keyListeners.clone(); + clonedListeners.add(l); + keyListeners = clonedListeners; + return this; + } + + public final Shape removeKeyListener(final KeyListener l) { + if (l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList<KeyListener> clonedListeners = (ArrayList<KeyListener>) keyListeners.clone(); + clonedListeners.remove(l); + keyListeners = clonedListeners; + return this; + } + /** * Combining {@link MouseListener} and {@link GestureListener} */ @@ -1690,6 +1716,34 @@ public abstract class Shape { } } + /** + * Dispatch given NEWT key event to this shape + * @param e original Newt {@link KeyEvent} + * @return true to signal operation complete and to stop traversal, otherwise false + */ + /* pp */ final boolean dispatchKeyEvent(final KeyEvent e) { + /** + * Checked at caller! + if( !isInteractive() ) { + return false; + } */ + final short eventType = e.getEventType(); + for(int i = 0; !e.isConsumed() && i < keyListeners.size(); i++ ) { + final KeyListener l = keyListeners.get(i); + switch( eventType ) { + case KeyEvent.EVENT_KEY_PRESSED: + l.keyPressed(e); + break; + case KeyEvent.EVENT_KEY_RELEASED: + l.keyReleased(e); + break; + default: + throw new NativeWindowException("Unexpected key event type " + e.getEventType()); + } + } + return e.isConsumed(); // end signal traversal if consumed + } + // // // |