diff options
author | Sven Göthel <[email protected]> | 2024-02-07 03:25:27 +0100 |
---|---|---|
committer | Sven Göthel <[email protected]> | 2024-02-07 03:25:27 +0100 |
commit | 29697eaac78bdc0c1a6cfb8547516c614dc1db8e (patch) | |
tree | c3fc6ca20df251aa150022c7b519d6a12f0053dc /src/graphui/classes/com/jogamp/graph/ui/widgets | |
parent | 5bacb8e6887b55ab2053dc5ed76bebcc0743a45e (diff) |
GraphUI Shape: Add MouseEvent to MoveListener; RangeSlider: Add rel obj position + NEWT MouseEvent to listener, renamed {Slider->Change}Listener and add PeekListener for mouse-over events
Diffstat (limited to 'src/graphui/classes/com/jogamp/graph/ui/widgets')
3 files changed, 105 insertions, 41 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 4495570e2..7a0fed0fb 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java +++ b/src/graphui/classes/com/jogamp/graph/ui/widgets/MediaPlayer.java @@ -304,7 +304,7 @@ public class MediaPlayer extends Widget { } } } ); - ctrlSlider.addSliderListener((final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct) -> { + ctrlSlider.addChangeListener((final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct, final Vec3f pos, final MouseEvent e) -> { if( DEBUG ) { System.err.println("Dragged "+w.getName()+": "+PTS.toTimeStr(Math.round(val), true)+"ms, "+(val_pct*100f)+"%"); System.err.println("Slider.D "+ctrlSlider.getDescription()); 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 3bd11c432..2a2242f9e 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/widgets/RangeSlider.java +++ b/src/graphui/classes/com/jogamp/graph/ui/widgets/RangeSlider.java @@ -59,9 +59,9 @@ import com.jogamp.opengl.util.texture.TextureSequence; */ public final class RangeSlider extends Widget { /** - * {@link RangeSlider} slider listener + * {@link RangeSlider} slider value changed listener */ - public static interface SliderListener { + public static interface ChangeListener { /** * Slide dragged by user (including clicked position) * @param w the {@link RangeSlider} widget owning the slider @@ -69,11 +69,30 @@ public final class RangeSlider extends Widget { * @param val the absolute value position of the slider * @param old_val_pct previous percentage value position of the slider * @param val_pct the percentage value position of the slider + * @param pos object position relative to the slider's bar + * @param e NEWT original event or {@code null} if sourced from non-mouse, e.g. key-event */ - void dragged(RangeSlider w, float old_val, float val, float old_val_pct, float val_pct); + void dragged(RangeSlider w, float old_val, float val, float old_val_pct, float val_pct, Vec3f pos, MouseEvent e); } - private static interface SliderAction { - public void run(SliderListener l); + private static interface ChangedAction { + public void run(ChangeListener l); + } + /** + * {@link RangeSlider} slider value peek listener + */ + public static interface PeekListener { + /** + * Slide position/value peeked by user (mouse over/hover) + * @param w the {@link RangeSlider} widget owning the slider + * @param val the absolute value peeked at the slider + * @param val_pct the percentage value position peeked at the slider + * @param pos object position relative to the slider's bar + * @param e NEWT original event + */ + void peeked(RangeSlider w, float val, float val_pct, Vec3f pos, MouseEvent e); + } + private static interface PeekAction { + public void run(PeekListener l); } private static final boolean DEBUG = false; @@ -89,7 +108,8 @@ public final class RangeSlider extends Widget { private final Group barAndKnob, marks; private final Rectangle bar; private final GraphShape knob; - private ArrayList<SliderListener> sliderListeners = new ArrayList<SliderListener>(); + private ArrayList<ChangeListener> changeListeners = new ArrayList<ChangeListener>(); + private ArrayList<PeekListener> peekListeners = new ArrayList<PeekListener>(); private final Vec2f minMax = new Vec2f(0, 100); private final float knobScale; private float pageSize; @@ -183,27 +203,37 @@ public final class RangeSlider extends Widget { reconfig(minMax, true, value, false, 0); - knob.onMove((final Shape s, final Vec3f origin, final Vec3f dest) -> { + knob.onMove((final Shape s, final Vec3f origin, final Vec3f dest, final MouseEvent e) -> { final float old_val = val; final float old_val_pct = val_pct; if( Float.isFinite(pageSize) ) { final float dy = inverted ? +knobLength: 0; // offset to knob start - setValuePct( getKnobValuePct( dest.x(), dest.y(), dy ) ); + setValue(dest.x(), dest.y(), dy); } else { - setValuePct( getKnobValuePct( dest.x(), dest.y(), knobLength/2f ) ); // centered + setValue(dest.x(), dest.y(), knobLength/2f); // centered } - dispatchToListener( (final SliderListener l) -> { - l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + dispatchToListener( (final ChangeListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct, dest, e); }); }); bar.onClicked((final Shape s, final Vec3f pos, final MouseEvent e) -> { final float old_val = val; final float old_val_pct = val_pct; - setValuePct( getKnobValuePct( pos.x(), pos.y(), 0 ) ); - dispatchToListener( (final SliderListener l) -> { - l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + setValue(pos.x(), pos.y(), 0); + dispatchToListener( (final ChangeListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct, pos, e); + }); + }); + bar.onHover((final Shape s, final Vec3f pos, final MouseEvent e) -> { + final float pval_pct = getKnobValuePct( pos.x(), pos.y(), 0 ); + final float pval = valuePctToValue( pval_pct ); + dispatchToListener( (final PeekListener l) -> { + l.peeked(this, pval, pval_pct, pos, e); }); }); + bar.addActivationListener((final Shape s) -> { + dispatchActivationEvent(s); + }); final Shape.MouseGestureListener mouseListener = new Shape.MouseGestureAdapter() { @Override public void mouseWheelMoved(final MouseEvent e) { @@ -240,8 +270,8 @@ public final class RangeSlider extends Widget { } } setValue( v ); - dispatchToListener( (final SliderListener l) -> { - l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + dispatchToListener( (final ChangeListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct, knob.getPosition().minus(bar.getPosition()), e); }); } }; @@ -305,8 +335,8 @@ public final class RangeSlider extends Widget { } if( action ) { setValue( v ); - dispatchToListener( (final SliderListener l) -> { - l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct); + dispatchToListener( (final ChangeListener l) -> { + l.dragged(RangeSlider.this, old_val, val, old_val_pct, val_pct, knob.getPosition().minus(bar.getPosition()), null); }); } } @@ -354,38 +384,67 @@ public final class RangeSlider extends Widget { @Override protected void clearImpl0(final GL2ES2 gl, final RegionRenderer renderer) { super.clearImpl0(gl, renderer); - sliderListeners.clear(); + changeListeners.clear(); + peekListeners.clear(); } @Override protected void destroyImpl0(final GL2ES2 gl, final RegionRenderer renderer) { super.destroyImpl0(gl, renderer); - sliderListeners.clear(); + changeListeners.clear(); + peekListeners.clear(); } - public final RangeSlider addSliderListener(final SliderListener l) { + public final RangeSlider addChangeListener(final ChangeListener l) { if(l == null) { return this; } @SuppressWarnings("unchecked") - final ArrayList<SliderListener> clonedListeners = (ArrayList<SliderListener>) sliderListeners.clone(); + final ArrayList<ChangeListener> clonedListeners = (ArrayList<ChangeListener>) changeListeners.clone(); clonedListeners.add(l); - sliderListeners = clonedListeners; + changeListeners = clonedListeners; return this; } - public final RangeSlider removeSliderListener(final SliderListener l) { + public final RangeSlider removeChangeListener(final ChangeListener l) { if (l == null) { return this; } @SuppressWarnings("unchecked") - final ArrayList<SliderListener> clonedListeners = (ArrayList<SliderListener>) sliderListeners.clone(); + final ArrayList<ChangeListener> clonedListeners = (ArrayList<ChangeListener>) changeListeners.clone(); clonedListeners.remove(l); - sliderListeners = clonedListeners; + changeListeners = clonedListeners; return this; } - private final void dispatchToListener(final SliderAction action) { - final int sz = sliderListeners.size(); + private final void dispatchToListener(final ChangedAction action) { + final int sz = changeListeners.size(); for(int i = 0; i < sz; i++ ) { - action.run( sliderListeners.get(i) ); + action.run( changeListeners.get(i) ); + } + } + + public final RangeSlider addPeekListener(final PeekListener l) { + if(l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList<PeekListener> clonedListeners = (ArrayList<PeekListener>) peekListeners.clone(); + clonedListeners.add(l); + peekListeners = clonedListeners; + return this; + } + public final RangeSlider removePeekListener(final PeekListener l) { + if (l == null) { + return this; + } + @SuppressWarnings("unchecked") + final ArrayList<PeekListener> clonedListeners = (ArrayList<PeekListener>) peekListeners.clone(); + clonedListeners.remove(l); + peekListeners = clonedListeners; + return this; + } + private final void dispatchToListener(final PeekAction action) { + final int sz = peekListeners.size(); + for(int i = 0; i < sz; i++ ) { + action.run( peekListeners.get(i) ); } } @@ -596,17 +655,11 @@ public final class RangeSlider extends Widget { return reconfig(minMax, false, 0, true, pageSz); } - public RangeSlider setValuePct(final float v) { - final float range = getRange(); - if( Float.isFinite(v) && Float.isFinite(range) && !FloatUtil.isZero(range) ) { - final float pgsz_pct = Float.isFinite(pageSize) ? pageSize / range : 0f; - final float pct = Math.max(0f, Math.min(1f - pgsz_pct, v)); - return setValue( minMax.x() + ( pct * range ) ); - } else { - return setValue( 0f ); - } + private RangeSlider setValue(final float pos_x, final float pos_y, final float adjustment) { + return setValue( valuePctToValue( getKnobValuePct(pos_x, pos_y, adjustment) ) ); } + // private float getKnobValuePct(final float pos_x, final float pos_y, final float adjustment) { /** * Sets slider value * @param v new value of slider, clipped against {@link #getMinMax()} @@ -673,6 +726,16 @@ public final class RangeSlider extends Widget { } return Math.max(0.0f, Math.min(1.0f, inverted ? 1f - v : v)); } + private float valuePctToValue(final float v) { + final float range = getRange(); + if( Float.isFinite(v) && Float.isFinite(range) && !FloatUtil.isZero(range) ) { + final float pgsz_pct = Float.isFinite(pageSize) ? pageSize / range : 0f; + final float pct = Math.max(0f, Math.min(1f - pgsz_pct, v)); + return minMax.x() + ( pct * range ); + } else { + return 0f; + } + } private void setKnob() { final Vec2f pos = getItemPctPos(new Vec2f(), val_pct, knobLength, knobThickn); diff --git a/src/graphui/classes/com/jogamp/graph/ui/widgets/RangedGroup.java b/src/graphui/classes/com/jogamp/graph/ui/widgets/RangedGroup.java index e22b67c7f..0ebe3a258 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/widgets/RangedGroup.java +++ b/src/graphui/classes/com/jogamp/graph/ui/widgets/RangedGroup.java @@ -41,6 +41,7 @@ import com.jogamp.math.geom.AABBox; import com.jogamp.math.geom.Cube; import com.jogamp.math.geom.Frustum; import com.jogamp.math.util.PMVMatrix4f; +import com.jogamp.newt.event.MouseEvent; import com.jogamp.opengl.GL2ES2; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.util.texture.TextureSequence; @@ -103,7 +104,7 @@ public class RangedGroup extends Widget { horizSlider = new RangeSlider(renderModes, horizSliderParam.size, new Vec2f(0, content.getBounds().getWidth()), horizSliderParam.unitSize, contentSize.x(), 0).setInverted(horizSliderParam.inverted); addShape(horizSlider); - horizSlider.addSliderListener((final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct) -> { + horizSlider.addChangeListener((final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct, Vec3f pos, MouseEvent e) -> { final Vec3f oldPos = content.getPosition(); if( horizSlider.isInverted() ) { content.moveTo(contentPosZero.x()-val, oldPos.y(), oldPos.z()); @@ -118,7 +119,7 @@ public class RangedGroup extends Widget { vertSlider = new RangeSlider(renderModes, vertSliderParam.size, new Vec2f(0, content.getBounds().getHeight()), vertSliderParam.unitSize, contentSize.y(), 0).setInverted(vertSliderParam.inverted); addShape(vertSlider); - vertSlider.addSliderListener((final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct) -> { + vertSlider.addChangeListener((final RangeSlider w, final float old_val, final float val, final float old_val_pct, final float val_pct, Vec3f pos, MouseEvent e) -> { final Vec3f oldPos = content.getPosition(); if( vertSlider.isInverted() ) { content.moveTo(oldPos.x(), contentPosZero.y()+val, oldPos.z()); |