From 79eba1f0e450a75f01505ca5ed5817ec481491a8 Mon Sep 17 00:00:00 2001 From: Sven Göthel Date: Sun, 7 Jan 2024 04:48:16 +0100 Subject: GraphUI Shape: Maintain multiple Activation Listener (ArrayList instead of single instance) This allows listenting to activation of Group members, while Group is set to widget-mode. For the latter, Group adds a forward listener to itself. --- src/graphui/classes/com/jogamp/graph/ui/Group.java | 4 +- src/graphui/classes/com/jogamp/graph/ui/Shape.java | 47 +++++++++++++++++----- .../com/jogamp/graph/ui/widgets/MediaPlayer.java | 2 +- 3 files changed, 40 insertions(+), 13 deletions(-) (limited to 'src/graphui/classes/com') diff --git a/src/graphui/classes/com/jogamp/graph/ui/Group.java b/src/graphui/classes/com/jogamp/graph/ui/Group.java index 9dca781b1..025213f8d 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/Group.java +++ b/src/graphui/classes/com/jogamp/graph/ui/Group.java @@ -288,7 +288,7 @@ public class Group extends Shape implements Container { * Enabled widget behavior for a group causes * *

@@ -313,7 +313,7 @@ public class Group extends Shape implements Container { final Group sg = (Group)s; sg.setWidgetMode(v); } - s.onActivation(activationListener); + s.addActivationListener(activationListener); } } diff --git a/src/graphui/classes/com/jogamp/graph/ui/Shape.java b/src/graphui/classes/com/jogamp/graph/ui/Shape.java index e3a1f0e74..65a989ebd 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/Shape.java +++ b/src/graphui/classes/com/jogamp/graph/ui/Shape.java @@ -195,7 +195,7 @@ public abstract class Shape { private ListenerBool onInitListener = null; private MoveListener onMoveListener = null; private Listener onToggleListener = null; - private Listener onActivationListener = null; + private ArrayList activationListeners = new ArrayList(); private Listener onClickedListener = null; private final Vec2f objDraggedFirst = new Vec2f(); // b/c its relative to Shape and we stick to it @@ -319,7 +319,7 @@ public abstract class Shape { onInitListener = null; onMoveListener = null; onToggleListener = null; - onActivationListener = null; + activationListeners.clear(); onClickedListener = null; markShapeDirty(); } @@ -358,13 +358,44 @@ public abstract class Shape { *

*/ public final void onToggle(final Listener l) { onToggleListener = l; } + /** - * Set user callback to be notified when shape is activated (pointer-over and/or click) or de-activated (pointer left). + * Add user callback to be notified when shape is activated (pointer-over and/or click) or de-activated (pointer left). *

* Use {@link #isActive()} to retrieve the state. *

*/ - public final void onActivation(final Listener l) { onActivationListener = l; } + public final Shape addActivationListener(final Listener l) { + if(l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList clonedListeners = (ArrayList) activationListeners.clone(); + clonedListeners.add(l); + activationListeners = clonedListeners; + return this; + } + public final Shape removeActivationListener(final Listener l) { + if (l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList clonedListeners = (ArrayList) activationListeners.clone(); + clonedListeners.remove(l); + activationListeners = clonedListeners; + return this; + } + /** + * Dispatch activation event event to this shape + * @return true to signal operation complete and to stop traversal, otherwise false + */ + private final void dispatchActivationEvent(final Shape s) { + final int sz = activationListeners.size(); + for(int i = 0; i < sz; i++ ) { + activationListeners.get(i).run(s); + } + } + /** * Set user callback to be notified when shape is clicked. *

@@ -1268,9 +1299,7 @@ public abstract class Shape { if( DEBUG ) { System.err.println("XXX "+(v?" Active":"DeActive")+" "+this); } - if( null != onActivationListener ) { - onActivationListener.run(this); - } + dispatchActivationEvent(this); return true; } else { return false; @@ -1282,9 +1311,7 @@ public abstract class Shape { protected final Listener forwardActivation = new Listener() { @Override public void run(final Shape shape) { - if( null != onActivationListener ) { - onActivationListener.run(shape); - } + dispatchActivationEvent(shape); } }; diff --git a/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java b/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java index 4ea05912b..da912fa8a 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java +++ b/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java @@ -464,7 +464,7 @@ public class MediaPlayer extends Widget { } this.setWidgetMode(true); - this.onActivation( (final Shape s) -> { + this.addActivationListener( (final Shape s) -> { if( this.isActive() ) { this.setBorderColor(borderColorA); } else { -- cgit v1.2.3