/** * 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.gl; import java.util.ArrayList; 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.OutlineShape; import com.jogamp.graph.curve.Region; import com.jogamp.graph.curve.opengl.GLRegion; import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.graph.font.Font; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.geom.Vertex.Factory; import com.jogamp.graph.geom.plane.AffineTransform; 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.MouseEvent.PointerClass; import com.jogamp.newt.event.MouseListener; import com.jogamp.opengl.math.FloatUtil; import com.jogamp.opengl.math.Quaternion; import com.jogamp.opengl.math.VectorUtil; import com.jogamp.opengl.math.geom.AABBox; import com.jogamp.opengl.util.PMVMatrix; /** * GraphUI Shape *
* 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)}. *
** GraphUI is GPU based and resolution independent. *
** GraphUI is intended to become an immediate- and retained-mode API. *
* @see Scene */ public abstract class Shape { public static final boolean DRAW_DEBUG_BOX = false; private static final boolean DEBUG = false; protected static final int DIRTY_SHAPE = 1 << 0 ; protected static final int DIRTY_STATE = 1 << 1 ; private final Factory extends Vertex> vertexFactory; private final int renderModes; protected final AABBox box; protected final AffineTransform tempT1 = new AffineTransform(); protected final AffineTransform tempT2 = new AffineTransform(); protected final AffineTransform tempT3 = new AffineTransform(); protected final AffineTransform tempT4 = new AffineTransform(); protected final float[] position = new float[] { 0f, 0f, 0f }; protected final Quaternion rotation = new Quaternion(); protected final float[] rotOrigin = new float[] { 0f, 0f, 0f }; protected final float[] scale = new float[] { 1f, 1f, 1f }; protected GLRegion region = null; protected int regionQuality = Region.MAX_QUALITY; protected int dirty = DIRTY_SHAPE | DIRTY_STATE; protected float shapesSharpness = OutlineShape.DEFAULT_SHARPNESS; /** 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 enabled = true; private ArrayList* No matrix operations (translate, scale, ..) are performed. *
* @param gl * @param renderer * @param sampleCount */ public void drawShape(final GL2ES2 gl, final RegionRenderer renderer, final int[] sampleCount) { final float r, g, b, a; final boolean isPressed = isPressed(), isToggleOn = isToggleOn(); final boolean modBaseColor = !Region.hasColorChannel( renderModes ) && !Region.hasColorTexture( renderModes ); if( modBaseColor ) { if( isPressed ) { r = rgbaColor[0]*pressedRGBAModulate[0]; g = rgbaColor[1]*pressedRGBAModulate[1]; b = rgbaColor[2]*pressedRGBAModulate[2]; a = rgbaColor[3]*pressedRGBAModulate[3]; } else if( isToggleable() ) { if( isToggleOn ) { r = rgbaColor[0]*toggleOnRGBAModulate[0]; g = rgbaColor[1]*toggleOnRGBAModulate[1]; b = rgbaColor[2]*toggleOnRGBAModulate[2]; a = rgbaColor[3]*toggleOnRGBAModulate[3]; } else { r = rgbaColor[0]*toggleOffRGBAModulate[0]; g = rgbaColor[1]*toggleOffRGBAModulate[1]; b = rgbaColor[2]*toggleOffRGBAModulate[2]; a = rgbaColor[3]*toggleOffRGBAModulate[3]; } } else { r = rgbaColor[0]; g = rgbaColor[1]; b = rgbaColor[2]; a = rgbaColor[3]; } } else { if( isPressed ) { r = pressedRGBAModulate[0]; g = pressedRGBAModulate[1]; b = pressedRGBAModulate[2]; a = pressedRGBAModulate[3]; } else if( isToggleable() ) { if( isToggleOn ) { r = toggleOnRGBAModulate[0]; g = toggleOnRGBAModulate[1]; b = toggleOnRGBAModulate[2]; a = toggleOnRGBAModulate[3]; } else { r = toggleOffRGBAModulate[0]; g = toggleOffRGBAModulate[1]; b = toggleOffRGBAModulate[2]; a = toggleOffRGBAModulate[3]; } } else { r = rgbaColor[0]; g = rgbaColor[1]; b = rgbaColor[2]; a = rgbaColor[3]; } } renderer.getRenderState().setColorStatic(r, g, b, a); getRegion(gl, renderer).draw(gl, renderer, sampleCount); } protected GLRegion createGLRegion(final GLProfile glp) { return GLRegion.create(glp, renderModes, null); } /** * Validates the shape's underlying {@link GLRegion}. * * @param gl * @param renderer */ public final void validate(final GL2ES2 gl, final RegionRenderer renderer) { if( isShapeDirty() || null == region ) { box.reset(); if( null == region ) { region = createGLRegion(gl.getGLProfile()); } else { region.clear(gl); } addShapeToRegion(gl, renderer); if( DRAW_DEBUG_BOX ) { region.clear(gl); final OutlineShape shape = new OutlineShape(renderer.getRenderState().getVertexFactory()); shape.setSharpness(shapesSharpness); shape.setIsQuadraticNurbs(); region.addOutlineShape(shape, null, rgbaColor); } region.setQuality(regionQuality); dirty &= ~(DIRTY_SHAPE|DIRTY_STATE); } else if( isStateDirty() ) { region.markStateDirty(); dirty &= ~DIRTY_STATE; } } /** * Setup the pre-selected {@link GLMatrixFunc#GL_MODELVIEW} {@link PMVMatrix} for this object. * @param pmv the matrix */ public void setTransform(final PMVMatrix pmv) { final float[] uiTranslate = getPosition(); pmv.glTranslatef(uiTranslate[0], uiTranslate[1], uiTranslate[2]); final Quaternion quat = getRotation(); final boolean rotate = !quat.isIdentity(); final float[] uiScale = getScale(); final boolean scale = !VectorUtil.isVec3Equal(uiScale, 0, VectorUtil.VEC3_ONE, 0, FloatUtil.EPSILON); if( rotate || scale ) { final float[] rotOrigin = getRotationOrigin(); final boolean pivot = !VectorUtil.isVec3Zero(rotOrigin, 0, FloatUtil.EPSILON); if( pivot ) { pmv.glTranslatef(rotOrigin[0], rotOrigin[1], rotOrigin[2]); } if( scale ) { pmv.glScalef(uiScale[0], uiScale[1], uiScale[2]); } if( rotate ) { pmv.glRotate(quat); } if( pivot ) { pmv.glTranslatef(-rotOrigin[0], -rotOrigin[1], -rotOrigin[2]); } } } /** * Retrieve window surface size of this shape ** The {@link RegionRenderer#getMatrix()} has to be setup properly for this object, * i.e. reshape for {@link GLMatrixFunc#GL_PROJECTION} and {@link #setTransform(PMVMatrix)} for {@link GLMatrixFunc#GL_MODELVIEW}. *
* @param renderer source of viewport and {@link PMVMatrix} * @param surfaceSize int[2] target surface size * @return true for successful gluProject(..) operation, otherwise false */ public boolean getSurfaceSize(final RegionRenderer renderer, final int[/*2*/] surfaceSize) { boolean res = false; final int[/*4*/] viewport = renderer.getViewport(new int[4]); // System.err.println("UIShape::getSurfaceSize.VP "+viewport[0]+"/"+viewport[1]+" "+viewport[2]+"x"+viewport[3]); final float[] winCoordHigh = new float[3]; final float[] winCoordLow = new float[3]; final float[] high = getBounds().getHigh(); final float[] low = getBounds().getLow(); final PMVMatrix pmv = renderer.getMatrix(); if( pmv.gluProject(high[0], high[1], high[2], viewport, 0, winCoordHigh, 0) ) { // System.err.printf("UIShape::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[0], low[1], low[2], viewport, 0, winCoordLow, 0) ) { // System.err.printf("UIShape::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("UIShape::surfaceSize.S: shape %d: %f x %f -> %d x %d%n", getName(), winCoordHigh[0] - winCoordLow[0], winCoordHigh[1] - winCoordLow[1], surfaceSize[0], surfaceSize[1]); res = true; } } return res; } /** * Map given object coordinate relative to this shape to window coordinates ** The {@link RegionRenderer#getMatrix()} has to be setup properly for this object, * i.e. reshape for {@link GLMatrixFunc#GL_PROJECTION} and {@link #setTransform(PMVMatrix)} for {@link GLMatrixFunc#GL_MODELVIEW}. *
* @param renderer source of viewport and {@link PMVMatrix} * @param objPos float[3] object position relative to this shape's center * @param glWinPos int[2] target window position of objPos relative to this shape * @return true for successful gluProject(..) operation, otherwise false */ public boolean objToWinCoord(final RegionRenderer renderer, final float[/*3*/] objPos, final int[/*2*/] glWinPos) { boolean res = false; final int[/*4*/] viewport = renderer.getViewport(new int[4]); // System.err.println("UIShape::objToWinCoordgetSurfaceSize.VP "+viewport[0]+"/"+viewport[1]+" "+viewport[2]+"x"+viewport[3]); final float[] winCoord = new float[3]; final PMVMatrix pmv = renderer.getMatrix(); if( pmv.gluProject(objPos[0], objPos[1], objPos[2], viewport, 0, winCoord, 0) ) { // System.err.printf("UIShape::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("UIShape::objToWinCoord.X: shape %d: %f / %f -> %d / %d%n", getName(), winCoord[0], winCoord[1], glWinPos[0], glWinPos[1]); res = true; } return res; } /** * Map given gl-window-coordinates to object coordinates relative to this shape and its z-coordinate. ** The {@link RegionRenderer#getMatrix()} has to be setup properly for this object, * i.e. reshape for {@link GLMatrixFunc#GL_PROJECTION} and {@link #setTransform(PMVMatrix)} for {@link GLMatrixFunc#GL_MODELVIEW}. *
* @param renderer source of viewport and {@link PMVMatrix} * @param glWinX in GL window coordinates, origin bottom-left * @param glWinY in GL window coordinates, origin bottom-left * @param objPos float[3] target object position of glWinX/glWinY relative to this shape * @return @return true for successful gluProject(..) and gluUnProject(..) operations, otherwise false */ public boolean winToObjCoord(final RegionRenderer renderer, final int glWinX, final int glWinY, final float[/*3*/] objPos) { boolean res = false; final float[] ctr = getBounds().getCenter(); final int[] viewport = renderer.getViewport(new int[4]); final float[] tmp = new float[3]; final PMVMatrix pmv = renderer.getMatrix(); if( pmv.gluProject(ctr[0], ctr[1], ctr[2], viewport, 0, tmp, 0) ) { // System.err.printf("UIShape::winToObjCoord.0: shape %d: obj [%f, %f, %f] -> win [%f, %f, %f]%n", getName(), ctr[0], ctr[1], ctr[2], tmp[0], tmp[1], tmp[2]); if( pmv.gluUnProject(glWinX, glWinY, tmp[2], viewport, 0, objPos, 0) ) { // System.err.printf("UIShape::winToObjCoord.1: shape %d: win [%d, %d, %f] -> obj [%f, %f, %f]%n", getName(), glWinX, glWinY, tmp[2], objPos[0], objPos[1], objPos[2]); res = true; } } return res; } public float[] getColor() { return rgbaColor; } public final int getQuality() { return regionQuality; } public final void setQuality(final int q) { this.regionQuality = q; if( null != region ) { region.setQuality(q); } } public final void setSharpness(final float sharpness) { this.shapesSharpness = sharpness; markShapeDirty(); } public final float getSharpness() { return shapesSharpness; } /** * 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() { return "enabled "+enabled+", toggle[able "+toggleable+", state "+toggle+"], pos "+position[0]+" / "+position[1]+", box "+box; } // // Input // public void setPressed(final boolean b) { this.down = b; markStateDirty(); } public boolean isPressed() { return this.down; } 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)}. */ 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 draggable, * i.e. translated by 1-pointer-click and drag. ** Default draggable is true. *
*/ public void setDraggable(final boolean draggable) { this.draggable = draggable; } 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. *
*/ public void setResizable(final boolean resizable) { this.resizable = resizable; } public boolean isResizable() { return resizable; } public final void addMouseListener(final MouseGestureListener l) { if(l == null) { return; } @SuppressWarnings("unchecked") final ArrayList