/** * Copyright 2010-2023 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ package com.jogamp.graph.ui; import java.util.ArrayList; import java.util.Comparator; import com.jogamp.nativewindow.NativeWindowException; import com.jogamp.opengl.GL2ES2; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.fixedfunc.GLMatrixFunc; import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.newt.event.GestureHandler.GestureEvent; import com.jogamp.newt.event.GestureHandler.GestureListener; import com.jogamp.newt.event.MouseAdapter; import com.jogamp.newt.event.NEWTEvent; import com.jogamp.newt.event.PinchToZoomGesture; import com.jogamp.newt.event.MouseEvent; import com.jogamp.newt.event.MouseListener; import com.jogamp.opengl.math.FloatUtil; import com.jogamp.opengl.math.Quaternion; import com.jogamp.opengl.math.Vec2f; import com.jogamp.opengl.math.Vec3f; import com.jogamp.opengl.math.geom.AABBox; import com.jogamp.opengl.util.PMVMatrix; /** * Generic UI Shape, potentially using a Graph via {@link GraphShape} or other means of representing content. *
* A shape includes the following build-in user-interactions * - drag shape w/ 1-pointer click, see {@link #setDraggable(boolean)} * - resize shape w/ 1-pointer click and drag in 1/4th bottom-left and bottom-right corner, see {@link #setResizable(boolean)}. *
** A shape is expected to have its 0/0 origin in its bottom-left corner, otherwise the drag-zoom sticky-edge will not work as expected. *
** GraphUI is GPU based and resolution independent. *
** GraphUI is intended to become an immediate- and retained-mode API. *
* @see Scene */ public abstract class Shape { /** * General {@link Shape} visitor */ public static interface Visitor1 { /** * Visitor method * @param s the {@link Shape} to process * @return true to signal operation complete and to stop traversal, otherwise false */ boolean visit(Shape s); } /** * General {@link Shape} visitor */ public static interface Visitor2 { /** * Visitor method * @param s the {@link Shape} to process * @param pmv the {@link PMVMatrix} setup from the {@link Scene} down to the {@link Shape} * @return true to signal operation complete and to stop traversal, otherwise false */ boolean visit(Shape s, final PMVMatrix pmv); } /** * General {@link Shape} listener action */ public static interface Listener { void run(final Shape shape); } protected static final boolean DEBUG_DRAW = false; private static final boolean DEBUG = false; private static final int DIRTY_SHAPE = 1 << 0 ; private static final int DIRTY_STATE = 1 << 1 ; protected final AABBox box; private final Vec3f position = new Vec3f(); private final Quaternion rotation = new Quaternion(); private final Vec3f rotPivot = new Vec3f(); private final Vec3f scale = new Vec3f(1f, 1f, 1f); private volatile int dirty = DIRTY_SHAPE | DIRTY_STATE; private final Object dirtySync = new Object(); /** Default base-color w/o color channel, will be modulated w/ pressed- and toggle color */ protected final float[] rgbaColor = {0.75f, 0.75f, 0.75f, 1.0f}; /** Default pressed color-factor w/o color channel, modulated base-color. 0.75 * 1.2 = 0.9 */ protected final float[] pressedRGBAModulate = {1.2f, 1.2f, 1.2f, 0.7f}; /** Default toggle color-factor w/o color channel, modulated base-color. 0.75 * 1.13 ~ 0.85 */ protected final float[] toggleOnRGBAModulate = {1.13f, 1.13f, 1.13f, 1.0f}; /** Default toggle color-factor w/o color channel, modulated base-color. 0.75 * 0.86 ~ 0.65 */ protected final float[] toggleOffRGBAModulate = {0.86f, 0.86f, 0.86f, 1.0f}; private int name = -1; private boolean down = false; private boolean toggle = false; private boolean toggleable = false; private boolean draggable = true; private boolean resizable = true; private boolean interactive = true; private boolean enabled = true; private float dbgbox_thickness = 0f; // fractional thickness of bounds, 0f for no debug box private ArrayList* {@link #setTransform(PMVMatrix)} is expected to be completed beforehand. *
* @param gl the current GL object * @param renderer {@link RegionRenderer} which might be used for Graph Curve Rendering, also source of {@link RegionRenderer#getMatrix()} and {@link RegionRenderer#getViewport()}. * @param sampleCount sample count if used by Graph renderModes */ public void draw(final GL2ES2 gl, final RegionRenderer renderer, final int[] sampleCount) { final boolean isPressed = isPressed(), isToggleOn = isToggleOn(); final float[] rgba; if( hasColorChannel() ) { if( isPressed ) { rgba = pressedRGBAModulate; } else if( isToggleable() ) { if( isToggleOn ) { rgba = toggleOnRGBAModulate; } else { rgba = toggleOffRGBAModulate; } } else { rgba = rgbaColor; } } else { rgba = rgba_tmp; if( isPressed ) { rgba[0] = rgbaColor[0]*pressedRGBAModulate[0]; rgba[1] = rgbaColor[1]*pressedRGBAModulate[1]; rgba[2] = rgbaColor[2]*pressedRGBAModulate[2]; rgba[3] = rgbaColor[3]*pressedRGBAModulate[3]; } else if( isToggleable() ) { if( isToggleOn ) { rgba[0] = rgbaColor[0]*toggleOnRGBAModulate[0]; rgba[1] = rgbaColor[1]*toggleOnRGBAModulate[1]; rgba[2] = rgbaColor[2]*toggleOnRGBAModulate[2]; rgba[3] = rgbaColor[3]*toggleOnRGBAModulate[3]; } else { rgba[0] = rgbaColor[0]*toggleOffRGBAModulate[0]; rgba[1] = rgbaColor[1]*toggleOffRGBAModulate[1]; rgba[2] = rgbaColor[2]*toggleOffRGBAModulate[2]; rgba[3] = rgbaColor[3]*toggleOffRGBAModulate[3]; } } else { rgba[0] = rgbaColor[0]; rgba[1] = rgbaColor[1]; rgba[2] = rgbaColor[2]; rgba[3] = rgbaColor[3]; } } synchronized ( dirtySync ) { validate(gl); drawImpl0(gl, renderer, sampleCount, rgba); } } /** * Validates the shape's underlying {@link GLRegion}. ** If the region is dirty, it gets {@link GLRegion#clear(GL2ES2) cleared} and is reused. *
* @param gl current {@link GL2ES2} object * @see #validate(GLProfile) */ public final void validate(final GL2ES2 gl) { synchronized ( dirtySync ) { if( isShapeDirty() ) { box.reset(); } validateImpl(gl.getGLProfile(), gl); dirty = 0; } } /** * Validates the shape's underlying {@link GLRegion} w/o a current {@link GL2ES2} object ** If the region is dirty a new region is created * and the old one gets pushed to a dirty-list to get disposed when a GL context is available. *
* @see #validate(GL2ES2) */ public final void validate(final GLProfile glp) { synchronized ( dirtySync ) { if( isShapeDirty() ) { box.reset(); } validateImpl(glp, null); dirty = 0; } } /** * Setup the pre-selected {@link GLMatrixFunc#GL_MODELVIEW} {@link PMVMatrix} for this object. * - Scale shape from its center position * - Rotate shape around optional scaled pivot, see {@link #setRotationPivot(float[])}), otherwise rotate around its scaled center (default) ** Shape's origin should be bottom-left @ 0/0 to have build-in drag-zoom work properly. *
* @param pmv the matrix * @see #setRotationPivot(float[]) * @see #getRotation() * @see #moveTo(float, float, float) * @see #setScale(float, float, float) */ public void setTransform(final PMVMatrix pmv) { final boolean hasScale = !scale.isEqual(Vec3f.ONE); final boolean hasRotate = !rotation.isIdentity(); final boolean hasRotPivot = !rotPivot.isZero(); final Vec3f ctr = box.getCenter(); final boolean sameScaleRotatePivot = hasScale && hasRotate && ( !hasRotPivot || rotPivot.isEqual(ctr) ); pmv.glTranslatef(position.x(), position.y(), position.z()); // translate, scaled if( sameScaleRotatePivot ) { // Scale shape from its center position and rotate around its center pmv.glTranslatef(ctr.x()*scale.x(), ctr.y()*scale.y(), ctr.z()*scale.z()); // add-back center, scaled pmv.glRotate(rotation); pmv.glScalef(scale.x(), scale.y(), scale.z()); pmv.glTranslatef(-ctr.x(), -ctr.y(), -ctr.z()); // move to center } else if( hasRotate || hasScale ) { if( hasRotate ) { if( hasRotPivot ) { // Rotate shape around its scaled pivot pmv.glTranslatef(rotPivot.x()*scale.x(), rotPivot.y()*scale.y(), rotPivot.z()*scale.z()); // pivot back from rot-pivot, scaled pmv.glRotate(rotation); pmv.glTranslatef(-rotPivot.x()*scale.x(), -rotPivot.y()*scale.y(), -rotPivot.z()*scale.z()); // pivot to rot-pivot, scaled } else { // Rotate shape around its scaled center pmv.glTranslatef(ctr.x()*scale.x(), ctr.y()*scale.y(), ctr.z()*scale.z()); // pivot back from center-pivot, scaled pmv.glRotate(rotation); pmv.glTranslatef(-ctr.x()*scale.x(), -ctr.y()*scale.y(), -ctr.z()*scale.z()); // pivot to center-pivot, scaled } } if( hasScale ) { // Scale shape from its center position pmv.glTranslatef(ctr.x()*scale.x(), ctr.y()*scale.y(), ctr.z()*scale.z()); // add-back center, scaled pmv.glScalef(scale.x(), scale.y(), scale.z()); pmv.glTranslatef(-ctr.x(), -ctr.y(), -ctr.z()); // move to center } } // TODO: Add alignment features. } /** * Retrieve surface (view) size of this shape. ** The given {@link PMVMatrix} has to be setup properly for this object, * i.e. its {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW} for the surrounding scene * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param pmv well formed {@link PMVMatrix}, e.g. could have been setup via {@link Scene#setupMatrix(PMVMatrix) setupMatrix(..)} and {@link #setTransform(PMVMatrix)}. * @param viewport the int[4] viewport * @param surfaceSize int[2] target surface size * @return given int[2] {@code surfaceSize} for successful gluProject(..) operation, otherwise {@code null} * @see #getSurfaceSize(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], PMVMatrix, int[]) * @see #getSurfaceSize(Scene, PMVMatrix, int[]) */ public int[/*2*/] getSurfaceSize(final PMVMatrix pmv, final int[/*4*/] viewport, final int[/*2*/] surfaceSize) { // System.err.println("Shape::getSurfaceSize.VP "+viewport[0]+"/"+viewport[1]+" "+viewport[2]+"x"+viewport[3]); final float[] winCoordHigh = new float[3]; final float[] winCoordLow = new float[3]; final Vec3f high = box.getHigh(); final Vec3f low = box.getLow(); if( pmv.gluProject(high.x(), high.y(), high.z(), viewport, 0, winCoordHigh, 0) ) { // System.err.printf("Shape::surfaceSize.H: shape %d: obj [%f, %f, %f] -> win [%f, %f, %f]%n", getName(), high[0], high[1], high[2], winCoordHigh[0], winCoordHigh[1], winCoordHigh[2]); if( pmv.gluProject(low.x(), low.y(), low.z(), viewport, 0, winCoordLow, 0) ) { // System.err.printf("Shape::surfaceSize.L: shape %d: obj [%f, %f, %f] -> win [%f, %f, %f]%n", getName(), low[0], low[1], low[2], winCoordLow[0], winCoordLow[1], winCoordLow[2]); surfaceSize[0] = (int)(winCoordHigh[0] - winCoordLow[0]); surfaceSize[1] = (int)(winCoordHigh[1] - winCoordLow[1]); // System.err.printf("Shape::surfaceSize.S: shape %d: %f x %f -> %d x %d%n", getName(), winCoordHigh[0] - winCoordLow[0], winCoordHigh[1] - winCoordLow[1], surfaceSize[0], surfaceSize[1]); return surfaceSize; } } return null; } /** * Retrieve surface (view) size of this shape. ** The given {@link PMVMatrix} will be {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} properly for this shape * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param pmvMatrixSetup {@link Scene.PMVMatrixSetup} to {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} given {@link PMVMatrix} {@code pmv}. * @param viewport used viewport for {@link PMVMatrix#gluProject(float, float, float, int[], int, float[], int)} * @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup}, * {@link #setTransform(PMVMatrix) shape-transformed} and can be reused by the caller. * @param surfaceSize int[2] target surface size * @return given int[2] {@code surfaceSize} for successful gluProject(..) operation, otherwise {@code null} * @see #getSurfaceSize(PMVMatrix, int[], int[]) * @see #getSurfaceSize(Scene, PMVMatrix, int[]) */ public int[/*2*/] getSurfaceSize(final Scene.PMVMatrixSetup pmvMatrixSetup, final int[/*4*/] viewport, final PMVMatrix pmv, final int[/*2*/] surfaceSize) { pmvMatrixSetup.set(pmv, viewport[0], viewport[1], viewport[2], viewport[3]); setTransform(pmv); return getSurfaceSize(pmv, viewport, surfaceSize); } /** * Retrieve surface (view) size of this shape. ** The given {@link PMVMatrix} will be {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} properly for this shape * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param scene {@link Scene} to retrieve {@link Scene.PMVMatrixSetup} and the viewport. * @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup}, * {@link #setTransform(PMVMatrix) shape-transformed} and can be reused by the caller. * @param surfaceSize int[2] target surface size * @return given int[2] {@code surfaceSize} for successful gluProject(..) operation, otherwise {@code null} * @see #getSurfaceSize(PMVMatrix, int[], int[]) * @see #getSurfaceSize(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], PMVMatrix, int[]) */ public int[/*2*/] getSurfaceSize(final Scene scene, final PMVMatrix pmv, final int[/*2*/] surfaceSize) { return getSurfaceSize(scene.getPMVMatrixSetup(), scene.getViewport(), pmv, surfaceSize); } /** * Retrieve pixel per scaled shape-coordinate unit, i.e. [px]/[obj]. ** The given {@link PMVMatrix} will be {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} properly for this shape * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param scene {@link Scene} to retrieve {@link Scene.PMVMatrixSetup} and the viewport. * @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup}, * {@link #setTransform(PMVMatrix) shape-transformed} and can be reused by the caller. * @param pixPerShape float[2] pixel per scaled shape-coordinate unit result storage * @return given float[2] {@code pixPerShape} for successful gluProject(..) operation, otherwise {@code null} * @see #getPixelPerShapeUnit(int[], float[]) * @see #getSurfaceSize(Scene, PMVMatrix, int[]) * @see #getScaledWidth() * @see #getScaledHeight() */ public float[] getPixelPerShapeUnit(final Scene scene, final PMVMatrix pmv, final float[] pixPerShape) { final int[] shapeSizePx = new int[2]; if( null != getSurfaceSize(scene, new PMVMatrix(), shapeSizePx) ) { return getPixelPerShapeUnit(shapeSizePx, pixPerShape); } else { return null; } } /** * Retrieve pixel per scaled shape-coordinate unit, i.e. [px]/[obj]. * @param shapeSizePx int[2] shape size in pixel as retrieved via e.g. {@link #getSurfaceSize(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], PMVMatrix, int[])} * @param pixPerShape float[2] pixel scaled per shape-coordinate unit result storage * @return given float[2] {@code pixPerShape} * @see #getPixelPerShapeUnit(Scene, PMVMatrix, float[]) * @see #getSurfaceSize(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], PMVMatrix, int[]) * @see #getScaledWidth() * @see #getScaledHeight() */ public float[] getPixelPerShapeUnit(final int[] shapeSizePx, final float[] pixPerShape) { pixPerShape[0] = shapeSizePx[0] / getScaledWidth(); pixPerShape[0] = shapeSizePx[1] / getScaledHeight(); return pixPerShape; } /** * Map given object coordinate relative to this shape to window coordinates. ** The given {@link PMVMatrix} has to be setup properly for this object, * i.e. its {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW} for the surrounding scene * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param pmv well formed {@link PMVMatrix}, e.g. could have been setup via {@link Scene#setupMatrix(PMVMatrix) setupMatrix(..)} and {@link #setTransform(PMVMatrix)}. * @param viewport the int[4] viewport * @param objPos object position relative to this shape's center * @param glWinPos int[2] target window position of objPos relative to this shape * @return given int[2] {@code glWinPos} for successful gluProject(..) operation, otherwise {@code null} * @see #shapeToWinCoord(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], float[], PMVMatrix, int[]) * @see #shapeToWinCoord(Scene, float[], PMVMatrix, int[]) */ public int[/*2*/] shapeToWinCoord(final PMVMatrix pmv, final int[/*4*/] viewport, final Vec3f objPos, final int[/*2*/] glWinPos) { // System.err.println("Shape::objToWinCoordgetSurfaceSize.VP "+viewport[0]+"/"+viewport[1]+" "+viewport[2]+"x"+viewport[3]); final float[] winCoord = new float[3]; if( pmv.gluProject(objPos.x(), objPos.y(), objPos.z(), viewport, 0, winCoord, 0) ) { // System.err.printf("Shape::objToWinCoord.0: shape %d: obj [%f, %f, %f] -> win [%f, %f, %f]%n", getName(), objPos[0], objPos[1], objPos[2], winCoord[0], winCoord[1], winCoord[2]); glWinPos[0] = (int)(winCoord[0]); glWinPos[1] = (int)(winCoord[1]); // System.err.printf("Shape::objToWinCoord.X: shape %d: %f / %f -> %d / %d%n", getName(), winCoord[0], winCoord[1], glWinPos[0], glWinPos[1]); return glWinPos; } return null; } /** * Map given object coordinate relative to this shape to window coordinates. ** The given {@link PMVMatrix} will be {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} properly for this shape * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param pmvMatrixSetup {@link Scene.PMVMatrixSetup} to {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} given {@link PMVMatrix} {@code pmv}. * @param viewport used viewport for {@link PMVMatrix#gluProject(float, float, float, int[], int, float[], int)} * @param objPos object position relative to this shape's center * @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup}, * {@link #setTransform(PMVMatrix) shape-transformed} and can be reused by the caller. * @param glWinPos int[2] target window position of objPos relative to this shape * @return given int[2] {@code glWinPos} for successful gluProject(..) operation, otherwise {@code null} * @see #shapeToWinCoord(PMVMatrix, int[], float[], int[]) * @see #shapeToWinCoord(Scene, float[], PMVMatrix, int[]) */ public int[/*2*/] shapeToWinCoord(final Scene.PMVMatrixSetup pmvMatrixSetup, final int[/*4*/] viewport, final Vec3f objPos, final PMVMatrix pmv, final int[/*2*/] glWinPos) { pmvMatrixSetup.set(pmv, viewport[0], viewport[1], viewport[2], viewport[3]); setTransform(pmv); return this.shapeToWinCoord(pmv, viewport, objPos, glWinPos); } /** * Map given object coordinate relative to this shape to window coordinates. ** The given {@link PMVMatrix} will be {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} properly for this shape * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param scene {@link Scene} to retrieve {@link Scene.PMVMatrixSetup} and the viewport. * @param objPos object position relative to this shape's center * @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup}, * {@link #setTransform(PMVMatrix) shape-transformed} and can be reused by the caller. * @param glWinPos int[2] target window position of objPos relative to this shape * @return given int[2] {@code glWinPos} for successful gluProject(..) operation, otherwise {@code null} * @see #shapeToWinCoord(PMVMatrix, int[], float[], int[]) * @see #shapeToWinCoord(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], float[], PMVMatrix, int[]) */ public int[/*2*/] shapeToWinCoord(final Scene scene, final Vec3f objPos, final PMVMatrix pmv, final int[/*2*/] glWinPos) { return this.shapeToWinCoord(scene.getPMVMatrixSetup(), scene.getViewport(), objPos, pmv, glWinPos); } /** * Map given gl-window-coordinates to object coordinates relative to this shape and its z-coordinate. ** The given {@link PMVMatrix} has to be setup properly for this object, * i.e. its {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW} for the surrounding scene * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param pmv well formed {@link PMVMatrix}, e.g. could have been setup via {@link Scene#setupMatrix(PMVMatrix) setupMatrix(..)} and {@link #setTransform(PMVMatrix)}. * @param viewport the int[4] viewport * @param glWinX in GL window coordinates, origin bottom-left * @param glWinY in GL window coordinates, origin bottom-left * @param objPos target object position of glWinX/glWinY relative to this shape * @return given {@code objPos} for successful gluProject(..) and gluUnProject(..) operation, otherwise {@code null} * @see #winToShapeCoord(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], int, int, PMVMatrix, float[]) * @see #winToShapeCoord(Scene, int, int, PMVMatrix, float[]) */ public Vec3f winToShapeCoord(final PMVMatrix pmv, final int[/*4*/] viewport, final int glWinX, final int glWinY, final Vec3f objPos) { final Vec3f ctr = box.getCenter(); final float[] tmp = new float[3]; if( pmv.gluProject(ctr.x(), ctr.y(), ctr.z(), viewport, 0, tmp, 0) ) { // System.err.printf("Shape::winToObjCoord.0: shape %d: obj [%15.10ff, %15.10ff, %15.10ff] -> win [%d / %d -> %7.2ff, %7.2ff, %7.2ff, diff %7.2ff x %7.2ff]%n", getName(), ctr[0], ctr[1], ctr[2], glWinX, glWinY, tmp[0], tmp[1], tmp[2], glWinX-tmp[0], glWinY-tmp[1]); final float winZ = tmp[2]; if( pmv.gluUnProject(glWinX, glWinY, winZ, viewport, 0, objPos.get(tmp), 0) ) { // System.err.printf("Shape::winToObjCoord.X: shape %d: win [%d, %d, %7.2ff] -> obj [%15.10ff, %15.10ff, %15.10ff]%n", getName(), glWinX, glWinY, tmp[2], objPos[0], objPos[1], objPos[2]); objPos.set(tmp); return objPos; } } return null; } /** * Map given gl-window-coordinates to object coordinates relative to this shape and its z-coordinate. ** The given {@link PMVMatrix} will be {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} properly for this shape * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param pmvMatrixSetup {@link Scene.PMVMatrixSetup} to {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} given {@link PMVMatrix} {@code pmv}. * @param viewport used viewport for {@link PMVMatrix#gluUnProject(float, float, float, int[], int, float[], int)} * @param glWinX in GL window coordinates, origin bottom-left * @param glWinY in GL window coordinates, origin bottom-left * @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup}, * {@link #setTransform(PMVMatrix) shape-transformed} and can be reused by the caller. * @param objPos target object position of glWinX/glWinY relative to this shape * @return given {@code objPos} for successful gluProject(..) and gluUnProject(..) operation, otherwise {@code null} * @see #winToShapeCoord(PMVMatrix, int[], int, int, float[]) * @see #winToShapeCoord(Scene, int, int, PMVMatrix, float[]) */ public Vec3f winToShapeCoord(final Scene.PMVMatrixSetup pmvMatrixSetup, final int[/*4*/] viewport, final int glWinX, final int glWinY, final PMVMatrix pmv, final Vec3f objPos) { pmvMatrixSetup.set(pmv, viewport[0], viewport[1], viewport[2], viewport[3]); setTransform(pmv); return this.winToShapeCoord(pmv, viewport, glWinX, glWinY, objPos); } /** * Map given gl-window-coordinates to object coordinates relative to this shape and its z-coordinate. ** The given {@link PMVMatrix} will be {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) setup} properly for this shape * including this shape's {@link #setTransform(PMVMatrix)}. *
* @param scene {@link Scene} to retrieve {@link Scene.PMVMatrixSetup} and the viewport. * @param glWinX in GL window coordinates, origin bottom-left * @param glWinY in GL window coordinates, origin bottom-left * @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup}, * {@link #setTransform(PMVMatrix) shape-transformed} and can be reused by the caller. * @param objPos target object position of glWinX/glWinY relative to this shape * @return given {@code objPos} for successful gluProject(..) and gluUnProject(..) operation, otherwise {@code null} * @see #winToShapeCoord(PMVMatrix, int[], int, int, float[]) * @see #winToShapeCoord(com.jogamp.graph.ui.Scene.PMVMatrixSetup, int[], int, int, PMVMatrix, float[]) */ public Vec3f winToShapeCoord(final Scene scene, final int glWinX, final int glWinY, final PMVMatrix pmv, final Vec3f objPos) { return this.winToShapeCoord(scene.getPMVMatrixSetup(), scene.getViewport(), glWinX, glWinY, pmv, objPos); } public float[] getColor() { return rgbaColor; } /** * Set base color. ** Default base-color w/o color channel, will be modulated w/ pressed- and toggle color *
*/ public final void setColor(final float r, final float g, final float b, final float a) { this.rgbaColor[0] = r; this.rgbaColor[1] = g; this.rgbaColor[2] = b; this.rgbaColor[3] = a; } /** * Set pressed color. ** Default pressed color-factor w/o color channel, modulated base-color. 0.75 * 1.2 = 0.9 *
*/ public final void setPressedColorMod(final float r, final float g, final float b, final float a) { this.pressedRGBAModulate[0] = r; this.pressedRGBAModulate[1] = g; this.pressedRGBAModulate[2] = b; this.pressedRGBAModulate[3] = a; } /** * Set toggle-on color. ** Default toggle-on color-factor w/o color channel, modulated base-color. 0.75 * 1.13 ~ 0.85 *
*/ public final void setToggleOnColorMod(final float r, final float g, final float b, final float a) { this.toggleOnRGBAModulate[0] = r; this.toggleOnRGBAModulate[1] = g; this.toggleOnRGBAModulate[2] = b; this.toggleOnRGBAModulate[3] = a; } /** * Set toggle-off color. ** Default toggle-off color-factor w/o color channel, modulated base-color. 0.75 * 0.86 ~ 0.65 *
*/ public final void setToggleOffColorMod(final float r, final float g, final float b, final float a) { this.toggleOffRGBAModulate[0] = r; this.toggleOffRGBAModulate[1] = g; this.toggleOffRGBAModulate[2] = b; this.toggleOffRGBAModulate[3] = a; } @Override public final String toString() { return getClass().getSimpleName()+"["+getSubString()+"]"; } public String getSubString() { final String pivotS; if( !rotPivot.isZero() ) { pivotS = "pivot["+rotPivot+"], "; } else { pivotS = ""; } final String scaleS; if( !scale.isEqual( Vec3f.ONE ) ) { scaleS = "scale["+scale+"], "; } else { scaleS = "scale 1, "; } final String rotateS; if( !rotation.isIdentity() ) { rotateS = "rot "+rotation+", "; } else { rotateS = ""; } return "enabled "+enabled+", toggle[able "+toggleable+", state "+toggle+"], pos["+position+ "], "+pivotS+scaleS+rotateS+ "box "+box; } // // Input // public void setPressed(final boolean b) { this.down = b; markStateDirty(); } public boolean isPressed() { return this.down; } /** * * @param toggleable * @see #isInteractive() */ public void setToggleable(final boolean toggleable) { this.toggleable = toggleable; } /** * Returns true if this shape is toggable, * i.e. rendered w/ {@link #setToggleOnColorMod(float, float, float, float)} or {@link #setToggleOffColorMod(float, float, float, float)}. * @see #isInteractive() */ public boolean isToggleable() { return toggleable; } public void setToggle(final boolean v) { toggle = v; markStateDirty(); } public void toggle() { if( isToggleable() ) { toggle = !toggle; } markStateDirty(); } public boolean isToggleOn() { return toggle; } /** * Set whether this shape is interactive, * i.e. any user interaction like * - {@link #isToggleable()} * - {@link #isDraggable()} * - {@link #isResizable()} * but excluding programmatic changes. * @param v new value for {@link #isInteractive()} */ public void setInteractive(final boolean v) { interactive = v; } /** * Returns if this shape allows user interaction, see {@link #setInteractive(boolean)} * @see #setInteractive(boolean) */ public boolean isInteractive() { return interactive; } /** * Set whether this shape is draggable, * i.e. translated by 1-pointer-click and drag. ** Default draggable is true. *
* @see #isInteractive() */ public void setDraggable(final boolean draggable) { this.draggable = draggable; } /** * Returns if this shape is draggable, a user interaction. * @see #isInteractive() */ public boolean isDraggable() { return draggable; } /** * Set whether this shape is resizable, * i.e. zoomed by 1-pointer-click and drag in 1/4th bottom-left and bottom-right corner. ** Default resizable is true. *
* @see #isInteractive() */ public void setResizable(final boolean resizable) { this.resizable = resizable; } /** * Returns if this shape is resiable, a user interaction. * @see #isInteractive() */ public boolean isResizable() { return resizable; } public final void addMouseListener(final MouseGestureListener l) { if(l == null) { return; } @SuppressWarnings("unchecked") final ArrayList