diff options
author | Sven Göthel <[email protected]> | 2024-01-15 04:48:48 +0100 |
---|---|---|
committer | Sven Göthel <[email protected]> | 2024-01-15 04:48:48 +0100 |
commit | 7f91a60a0f5d7043d44fd22639d27aaefa1dc1a4 (patch) | |
tree | 0ec84e77e13122358decafdfcaa9d0dd32d937cf /src/graphui/classes/com/jogamp/graph | |
parent | d7cb4a77b71cb3703ff7ac0667c5a97f29a5bdb4 (diff) |
GraphUI RangeSlider: Add SliderAdapter (convenience) and use a listener-array (was 1 instance)
Diffstat (limited to 'src/graphui/classes/com/jogamp/graph')
-rw-r--r-- | src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java | 16 | ||||
-rw-r--r-- | src/graphui/classes/com/jogamp/graph/ui/widgets/RangeSlider.java | 92 |
2 files changed, 70 insertions, 38 deletions
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 63a04c0d5..fadb3e1b2 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java +++ b/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java @@ -51,7 +51,7 @@ import com.jogamp.graph.ui.shapes.Button; import com.jogamp.graph.ui.shapes.Label; import com.jogamp.graph.ui.shapes.MediaButton; import com.jogamp.graph.ui.shapes.Rectangle; -import com.jogamp.graph.ui.widgets.RangeSlider.SliderListener; +import com.jogamp.graph.ui.widgets.RangeSlider.SliderAdapter; import com.jogamp.math.Vec2f; import com.jogamp.math.Vec3f; import com.jogamp.math.Vec4f; @@ -255,7 +255,7 @@ public class MediaPlayer extends Widget { } } } ); - ctrlSlider.onSlider(new SliderListener() { + ctrlSlider.addListener(new SliderAdapter() { private void seekPlayer(final int ptsMS) { final int durationMS = mPlayer.getDuration(); timeLabel.setText(getMultilineTime(ptsMS, durationMS)); @@ -267,18 +267,6 @@ public class MediaPlayer extends Widget { seekPlayer( Math.round( w.getValue() ) ); } @Override - public void pressed(final RangeSlider w, final MouseEvent e) { - // mPlayer.pause(false); - // seekPlayer( Math.round( w.getValue() ) ); - } - - @Override - public void released(final RangeSlider w, final MouseEvent e) { - // seekPlayer( Math.round( w.getValue() ) ); - // mPlayer.resume(); - } - - @Override public void dragged(final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct) { System.err.println("Dragged "+w.getName()+": "+PTS.millisToTimeStr(Math.round(val), true)+"ms, "+(val_pct*100f)+"%"); seekPlayer( Math.round( val ) ); diff --git a/src/graphui/classes/com/jogamp/graph/ui/widgets/RangeSlider.java b/src/graphui/classes/com/jogamp/graph/ui/widgets/RangeSlider.java index e72551952..6699d65de 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/widgets/RangeSlider.java +++ b/src/graphui/classes/com/jogamp/graph/ui/widgets/RangeSlider.java @@ -27,6 +27,8 @@ */ package com.jogamp.graph.ui.widgets; +import java.util.ArrayList; + import com.jogamp.graph.curve.Region; import com.jogamp.graph.curve.opengl.GLRegion; import com.jogamp.graph.curve.opengl.RegionRenderer; @@ -75,6 +77,19 @@ public final class RangeSlider extends Widget { */ void dragged(RangeSlider w, float old_val, float val, float old_val_pct, float val_pct); } + public static abstract class SliderAdapter implements SliderListener { + @Override + public void clicked(final RangeSlider w, final MouseEvent e) { } + @Override + public void pressed(final RangeSlider w, final MouseEvent e) { } + @Override + public void released(final RangeSlider w, final MouseEvent e) { } + @Override + public void dragged(final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct) { } + }; + private static interface SliderAction { + public void run(SliderListener l); + } private static final float pageKnobScale = 0.6f; // 0.6 * barWidth private static final float pageBarLineScale = 0.25f; // 1/4 * ( barWidth - pageKnobWidth ) @@ -88,7 +103,7 @@ public final class RangeSlider extends Widget { private final Group barAndKnob, marks; private final Rectangle bar; private final GraphShape knob; - private SliderListener sliderListener = null; + private ArrayList<SliderListener> sliderListeners = new ArrayList<SliderListener>(); private final Vec2f minMax = new Vec2f(0, 100); private float pageSize; private float val=0, val_pct=0; @@ -200,9 +215,9 @@ public final class RangeSlider extends Widget { final float dy = inverted ? +knobLength: 0; // offset to knob start setValuePct( getKnobValuePct( dest.x(), dest.y(), dy ) ); } - if( null != sliderListener ) { - sliderListener.dragged(this, old_val, val, old_val_pct, val_pct); - } + dispatchToListener( (final SliderListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + }); }); barAndKnob.addMouseListener(new Shape.MouseGestureAdapter() { @Override @@ -211,10 +226,10 @@ public final class RangeSlider extends Widget { final float old_val_pct = val_pct; final Shape.EventInfo shapeEvent = (Shape.EventInfo) e.getAttachment(); setValuePct( getKnobValuePct( shapeEvent.objPos.x(), shapeEvent.objPos.y(), 0 ) ); - if( null != sliderListener ) { - sliderListener.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); - sliderListener.clicked(RangeSlider.this, e); - } + dispatchToListener( (final SliderListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + l.clicked(RangeSlider.this, e); + }); } @Override public void mouseWheelMoved(final MouseEvent e) { @@ -251,9 +266,9 @@ public final class RangeSlider extends Widget { } } setValue( v ); - if( null != sliderListener ) { - sliderListener.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); - } + dispatchToListener( (final SliderListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + }); } }); knob.addMouseListener(new Shape.MouseGestureAdapter() { @@ -265,15 +280,15 @@ public final class RangeSlider extends Widget { } @Override public void mousePressed(final MouseEvent e) { - if( null != sliderListener ) { - sliderListener.pressed(RangeSlider.this, e); - } + dispatchToListener( (final SliderListener l) -> { + l.pressed(RangeSlider.this, e); + }); } @Override public void mouseReleased(final MouseEvent e) { - if( null != sliderListener ) { - sliderListener.released(RangeSlider.this, e); - } + dispatchToListener( (final SliderListener l) -> { + l.released(RangeSlider.this, e); + }); } }); final KeyListener keyListener = new KeyAdapter() { @@ -336,9 +351,9 @@ public final class RangeSlider extends Widget { } if( action ) { setValue( v ); - if( null != sliderListener ) { - sliderListener.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); - } + dispatchToListener( (final SliderListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + }); } } }; @@ -349,18 +364,40 @@ public final class RangeSlider extends Widget { @Override protected void clearImpl0(final GL2ES2 gl, final RegionRenderer renderer) { super.clearImpl0(gl, renderer); - sliderListener = null; + sliderListeners.clear(); } @Override protected void destroyImpl0(final GL2ES2 gl, final RegionRenderer renderer) { super.destroyImpl0(gl, renderer); - sliderListener = null; + sliderListeners.clear(); } - public RangeSlider onSlider(final SliderListener l) { - sliderListener = l; + public final RangeSlider addListener(final SliderListener l) { + if(l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList<SliderListener> clonedListeners = (ArrayList<SliderListener>) sliderListeners.clone(); + clonedListeners.add(l); + sliderListeners = clonedListeners; + return this; + } + public final RangeSlider removeListener(final SliderListener l) { + if (l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList<SliderListener> clonedListeners = (ArrayList<SliderListener>) sliderListeners.clone(); + clonedListeners.remove(l); + sliderListeners = clonedListeners; return this; } + private final void dispatchToListener(final SliderAction action) { + final int sz = sliderListeners.size(); + for(int i = 0; i < sz; i++ ) { + action.run( sliderListeners.get(i) ); + } + } public Rectangle getBar() { return bar; } public GraphShape getKnob() { return knob; } @@ -427,7 +464,14 @@ public final class RangeSlider extends Widget { public void setUnitSize(final float v) { this.unitSize = v; } public float getUnitSize() { return this.unitSize; } + /** + * Sets whether this slider uses an inverted value range, + * e.g. top 0% and bottom 100% for an vertical inverted slider + * instead of bottom 0% and top 100% for a vertical non-inverted slider. + */ public RangeSlider setInverted(final boolean v) { inverted = v; return setValue(val); } + /** See {@link #setInverted(boolean)}. */ + public boolean isInverted() { return inverted; } /** * Sets slider value range and current value |