/** * Copyright 2023-2024 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.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import com.jogamp.graph.curve.Region; import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.graph.ui.layout.Padding; import com.jogamp.graph.ui.shapes.Rectangle; import com.jogamp.math.FloatUtil; import com.jogamp.math.Matrix4f; import com.jogamp.math.Vec2f; import com.jogamp.math.Vec3f; import com.jogamp.math.Vec4f; 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.opengl.GL2ES2; import com.jogamp.opengl.GLProfile; import jogamp.graph.ui.TreeTool; /** * Group of {@link Shape}s, optionally utilizing a {@link Group.Layout}. * @see Scene * @see Shape * @see Group.Layout */ public class Group extends Shape implements Container { /** Layout for the GraphUI {@link Group}, called @ {@link Shape#validate(GL2ES2)} or {@link Shape#validate(GLProfile)}. */ public static interface Layout { /** Prepare given {@link Shape} before {@link Shape#validate(GL2ES2) validation}, e.g. {@link Shape#setPaddding(Padding)}. */ void preValidate(final Shape s); /** * Performing the layout of {@link Group#getShapes()}, called @ {@link Shape#validate(GL2ES2)} or {@link Shape#validate(GLProfile)}. *
* According to the implemented layout, method * - may scale the {@Link Shape}s * - may move the {@Link Shape}s * - may reuse the given {@link PMVMatrix4f} `pmv` * - must update the given {@link AABBox} `box` *
* @param g the {@link Group} to layout * @param box the bounding box of {@link Group} to be updated by this method. * @param pmv a {@link PMVMatrix4f} which can be reused. */ void layout(final Group g, final AABBox box, final PMVMatrix4f pmv); } private final List* Default is {@link #setInteractive(boolean) non-interactive}. *
*/ public Group() { this(null, null, null, null); } /** * Create a group of {@link Shape}s w/ given {@link Group.Layout}. ** Default is {@link #setInteractive(boolean) non-interactive}. *
* @param l optional {@link Layout}, maybe {@code null} */ public Group(final Layout l) { this(null, l, null, null); } /** * Create a group of {@link Shape}s w/ given {@link Group.Layout} and {@link Shape}. ** Default is {@link #setInteractive(boolean) non-interactive}. *
* @param name optional name for {@link #setName(String)} * @param l optional {@link Layout}, maybe {@code null} * @param fixedSize optional fixed size for {@link #setFixedSize(Vec2f)} * @param s optional {@link Shape} for {@link #addShape(Shape)} */ public Group(final String name, final Layout l, final Vec2f fixedSize, final Shape s) { super(); if( null != name ) { this.setName(name); } this.layouter = l; this.setInteractive(false); if( null != fixedSize ) { this.setFixedSize(fixedSize); } if( null != s ) { addShape(s); } } @Override public final boolean isGroup() { return true; } /** Return current {@link Group.Layout}. */ public Layout getLayout() { return layouter; } /** Set {@link Group.Layout}. */ public Group setLayout(final Layout l) { layouter = l; return this; } /** Enforce size of this group for all given 3 dimensions {@link #getBounds()} without adjusting 3D z-axis like {@link #setFixedSize(Vec2f)}. */ public Group setFixedSize(final Vec3f v) { fixedSize.set(v); return this; } /** * Enforce size of this group to given 2 dimensions, * adjusting the 3D z-axis {@link #getBounds()} giving room for potential clipping via {@link #setClipOnBounds(boolean)} or {@link #setClipMvFrustum(Frustum)}. * @see #setFixedSize(Vec3f) */ public Group setFixedSize(final Vec2f v) { fixedSize.set(v.x(), v.y(), Float.NaN); return this; } /** Returns borrowed fixed size instance, see {@link #setFixedSize(Vec3f)} and {@link #setFixedSize(Vec2f)}. */ public Vec3f getFixedSize() { return fixedSize; } /** Returns given {@link Vec2f} instance set with 2 dimensions, see {@link #setFixedSize(Vec2f)}. */ public Vec2f getFixedSize(final Vec2f out) { out.set(fixedSize.x(), fixedSize.y()); return out; } /** * Enable Modelview (Mv) {@link Frustum} clipping on {@link #getBounds()} for this group and its shapes as follows ** {@link #setClipMvFrustum(Frustum)} takes precedence over {@link #setClipOnBounds(boolean)}. *
** With clipping enabled, the 3D z-axis {@link #getBounds()} depth * will be slightly increased for functional {@link Frustum} operation. *
* @param v boolean to toggle clipping * @return this instance for chaining * @see #setClipMvFrustum(Frustum) * @see #setFixedSize(Vec2f) * @see #setFixedSize(Vec3f) */ public Group setClipOnBounds(final boolean v) { clipOnBounds = v; return this; } /** Returns {@link #setClipOnBounds(boolean)} value */ public boolean getClipOnBounds() { return clipOnBounds; } /** * Enable Modelview (Mv) {@link Frustum} clipping on explicit given pre-multiplied w/ Mv-matrix {@code clip-box} * for this group and its shapes as follows ** {@link #setClipMvFrustum(Frustum)} takes precedence over {@link #setClipOnBounds(boolean)}. *
** With clipping enabled, the 3D z-axis {@link #getBounds()} depth * will be slightly increased for functional {@link Frustum} operation. *
* @param v {@link Frustum} pre-multiplied w/ Mv-matrix * @return this instance for chaining * @see #setClipOnBounds(boolean) * @see #setFixedSize(Vec2f) * @see #setFixedSize(Vec3f) */ public Group setClipMvFrustum(final Frustum v) { clipFrustum = v; return this; } /** Returns {@link #setClipMvFrustum(Frustum)} value */ public Frustum getClipMvFrustum() { return clipFrustum; } @Override public int getShapeCount() { return shapes.size(); } @Override public List* Enabled top-level widget behavior for a group causes *
* This method modifies all elements of this group for enabled or disabled top-level widget behavior. *
** Disable this behavior via {@link #disableTopLevelWidget()}, otherwise done per-default * at {@link #clear(GL2ES2, RegionRenderer)} or {@link #destroy(GL2ES2, RegionRenderer)}. *
* @param scene the top-level widget holder where this {@link Group} gets registered * @return this group for chaining * @see #disableTopLevelWidget() */ public final Group enableTopLevelWidget(final Scene scene) { topLevelHolder = scene; setWidgetChilds(true, forwardActivation); scene.addTopLevel(this); return this; } /** Disables top-level widget behavior as potentially set via {@link #enableTopLevelWidget(Scene)}. NOP if not enabled. */ public final Group disableTopLevelWidget() { final Scene tlh = topLevelHolder; topLevelHolder = null; if( null != tlh ) { tlh.removeTopLevel(this); setWidgetChilds(false, forwardActivation); } return this; } private final void setWidgetChilds(final boolean enable, final Listener fwdActivationListener) { TreeTool.forAll(this, (final Shape s) -> { if( enable ) { s.addActivationListener(fwdActivationListener); } else { s.removeActivationListener(fwdActivationListener); } return false; }); } /** Returns whether {@link #setTopLevelWidget(boolean)} is enabled or disabled. */ public final boolean isTopLevelWidget() { return null != topLevelHolder; } @Override public boolean isActive() { return super.isActive() || ( isTopLevelWidget() && TreeTool.forAll(this, (final Shape gs) -> { return gs.isActive(); } ) ); } /** * {@inheritDoc} ** If re-layouting on dirty shape mode is enabled (default), see {@link #setRelayoutOnDirtyShapes(boolean)}, * this method traverses through all shapes updating all dirty states of all its groups * provoking a re-layout if required. *
*/ @Override protected boolean isShapeDirty() { if( relayoutOnDirtyShapes ) { // Deep dirty state update: // - Ensure all group member's dirty state is updated // - Allowing all group member's validate to function for(final Shape s : shapes) { if( s.isShapeDirty() ) { markShapeDirty(); } } } return super.isShapeDirty(); } @Override protected void validateImpl(final GL2ES2 gl, final GLProfile glp) { if( isShapeDirty() ) { final boolean needsRMs = hasBorder() && null == border; GraphShape firstGS = null; // box has been reset final PMVMatrix4f pmv = new PMVMatrix4f(); if( null != layouter ) { if( 0 == shapes.size() ) { box.resize(0, 0, 0); } else { for(final Shape s : shapes) { if( needsRMs && null == firstGS && s instanceof GraphShape ) { firstGS = (GraphShape)s; } layouter.preValidate(s); s.validate(gl, glp); } layouter.layout(this, box, pmv); } } else if( 0 == shapes.size() ) { box.resize(0, 0, 0); } else { final AABBox tsbox = new AABBox(); for(final Shape s : shapes) { if( needsRMs && null == firstGS && s instanceof GraphShape ) { firstGS = (GraphShape)s; } s.validate(gl, glp); pmv.pushMv(); s.applyMatToMv(pmv); s.getBounds().transform(pmv.getMv(), tsbox); pmv.popMv(); box.resize(tsbox); } } if( hasPadding() ) { final Padding p = getPadding(); final Vec3f l = box.getLow(); final Vec3f h = box.getHigh(); box.resize(l.x() - p.left, l.y() - p.bottom, l.z()); box.resize(h.x() + p.right, h.y() + p.top, l.z()); setRotationPivot( box.getCenter() ); } final boolean useFixedSize = !FloatUtil.isZero(fixedSize.x()) && !FloatUtil.isZero(fixedSize.y()); final boolean useClipping = null != clipFrustum || clipOnBounds; if( useFixedSize || useClipping ) { // final AABBox old = new AABBox(box); final boolean adjustZ = useClipping || ( useFixedSize && Float.isNaN(fixedSize.z()) ); final Vec3f lo = box.getLow(); if( adjustZ ) { final float oldDepth = box.getDepth(); final Vec3f hi; final float zAdjustment = 10f*Scene.DEFAULT_ACTIVE_ZOFFSET_SCALE*Scene.DEFAULT_Z16_EPSILON; lo.add( 0, 0, -(1f*zAdjustment)); if( useFixedSize ) { hi = new Vec3f(lo); hi.add(fixedSize.x(), fixedSize.y(), oldDepth+(2f*zAdjustment)); } else { hi = box.getHigh(); hi.add( 0, 0, oldDepth+(1f*zAdjustment)); } box.setSize(lo, hi); } else if( useFixedSize ) { final Vec3f hi = useFixedSize ? new Vec3f(lo) : box.getHigh(); hi.add(fixedSize.x(), fixedSize.y(), fixedSize.z()); box.setSize(lo, hi); } // System.err.println("- was "+old); // System.err.println("- has "+box); } if( hasBorder() ) { if( null == border ) { final int firstRMs = null != firstGS ? firstGS.getRenderModes() : 0; final int myRMs = Region.isVBAA(firstRMs) ? Region.VBAA_RENDERING_BIT : 0; border = new Rectangle(myRMs, box, getBorderThickness()); } else { border.setVisible(true); border.setBounds(box, getBorderThickness()); } border.setColor(getBorderColor()); } else if( null != border ) { border.setVisible(false); } } } @Override public boolean contains(final Shape s) { return TreeTool.contains(this, s); } @Override public Shape getShapeByIdx(final int id) { if( 0 > id ) { return null; } return shapes.get(id); } @Override public Shape getShapeByID(final int id) { return TreeTool.getShapeByID(this, id); } @Override public Shape getShapeByName(final String name) { return TreeTool.getShapeByName(this, name); } @Override public AABBox getBounds(final PMVMatrix4f pmv, final Shape shape) { pmv.reset(); applyMatToMv(pmv); final AABBox res = new AABBox(); if( null == shape ) { return res; } TreeTool.forOne(this, pmv, shape, () -> { shape.getBounds().transform(pmv.getMv(), res); }); return res; } @Override public String getSubString() { return super.getSubString()+", shapes "+shapes.size(); } }