From 603233b19373bfa157dd033132bff809af6a123f Mon Sep 17 00:00:00 2001
From: Sven Gothel
Date: Fri, 31 Mar 2023 11:09:26 +0200
Subject: GraphUI: Use Shape.ZAscendingComparator in Scene, to be reused across
typs
---
src/graphui/classes/com/jogamp/graph/ui/Scene.java | 47 +++++++++-------------
src/graphui/classes/com/jogamp/graph/ui/Shape.java | 15 +++++++
2 files changed, 33 insertions(+), 29 deletions(-)
(limited to 'src/graphui/classes/com')
diff --git a/src/graphui/classes/com/jogamp/graph/ui/Scene.java b/src/graphui/classes/com/jogamp/graph/ui/Scene.java
index cf0f96b28..831a9d334 100644
--- a/src/graphui/classes/com/jogamp/graph/ui/Scene.java
+++ b/src/graphui/classes/com/jogamp/graph/ui/Scene.java
@@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
+import java.util.List;
import java.util.Locale;
import com.jogamp.opengl.FPSCounter;
@@ -98,7 +99,7 @@ public final class Scene implements GLEventListener {
@SuppressWarnings("unused")
private static final boolean DEBUG = false;
- private final ArrayList shapes = new ArrayList();
+ private final List shapes = new ArrayList();
private float dbgbox_thickness = 0f;
private boolean doFrustumCulling = false;
@@ -200,7 +201,7 @@ public final class Scene implements GLEventListener {
}
}
- public ArrayList getShapes() {
+ public List getShapes() {
return shapes;
}
public void addShape(final Shape s) {
@@ -315,25 +316,11 @@ public final class Scene implements GLEventListener {
pmvMatrixSetup.setPlaneBox(planeBox, renderer.getMatrix(), x, y, width, height);
}
- private static Comparator shapeZAscComparator = new Comparator() {
- @Override
- public int compare(final Shape s1, final Shape s2) {
- final float s1Z = s1.getBounds().getMinZ()+s1.getPosition()[2];
- final float s2Z = s2.getBounds().getMinZ()+s2.getPosition()[2];
- if( FloatUtil.isEqual(s1Z, s2Z, FloatUtil.EPSILON) ) {
- return 0;
- } else if( s1Z < s2Z ){
- return -1;
- } else {
- return 1;
- }
- } };
-
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void display(final GLAutoDrawable drawable) {
final Object[] shapesS = shapes.toArray();
- Arrays.sort(shapesS, (Comparator)shapeZAscComparator);
+ Arrays.sort(shapesS, (Comparator)Shape.ZAscendingComparator);
display(drawable, shapesS, false);
}
@@ -368,7 +355,7 @@ public final class Scene implements GLEventListener {
//final int shapeCount = shapes.size();
final int shapeCount = shapes.length;
for(int i=0; i
* Method performs on current thread and returns after probing every {@link Shape}.
*
- * @param glWinX window X coordinate, bottom-left origin
- * @param glWinY window Y coordinate, bottom-left origin
* @param pmv a new {@link PMVMatrix} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix, int, int, int, int) be setup},
* {@link Shape#setTransform(PMVMatrix) shape-transformed} and can be reused by the caller and runnable.
+ * @param glWinX window X coordinate, bottom-left origin
+ * @param glWinY window Y coordinate, bottom-left origin
* @param objPos storage for found object position in model-space of found {@link Shape}
* @param shape storage for found {@link Shape} or null
* @param runnable the action to perform if {@link Shape} was found
+ * @return picked Shape if any or null as stored in {@code shape}
*/
- public Shape pickShape(final int glWinX, final int glWinY, final PMVMatrix pmv, final float[] objPos, final Shape[] shape, final Runnable runnable) {
- shape[0] = pickShapeImpl(glWinX, glWinY, pmv, objPos);
- if( null != shape[0] ) {
+ public Shape pickShape(final PMVMatrix pmv, final int glWinX, final int glWinY, final float[] objPos, final Shape[] shape, final Runnable runnable) {
+ setupMatrix(pmv);
+ final Shape pick = pickShapeImpl(pmv, glWinX, glWinY, objPos);
+ shape[0] = pick;
+ if( null != pick ) {
runnable.run();
}
- return shape[0];
+ return pick;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- private Shape pickShapeImpl(final int glWinX, final int glWinY, final PMVMatrix pmv, final float[] objPos) {
+ private Shape pickShapeImpl(final PMVMatrix pmv, final int glWinX, final int glWinY, final float[] objPos) {
final float winZ0 = 0f;
final float winZ1 = 0.3f;
/**
@@ -477,12 +467,11 @@ public final class Scene implements GLEventListener {
gl.glReadPixels( x, y, 1, 1, GL2ES2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, winZRB);
winZ1 = winZRB.get(0); // dir
*/
- setupMatrix(pmv);
final Ray ray = new Ray();
final Object[] shapesS = shapes.toArray();
- Arrays.sort(shapesS, (Comparator)shapeZAscComparator);
+ Arrays.sort(shapesS, (Comparator)Shape.ZAscendingComparator);
for(int i=shapesS.length-1; i>=0; i--) {
final Shape uiShape = (Shape)shapesS[i];
@@ -550,7 +539,7 @@ public final class Scene implements GLEventListener {
@SuppressWarnings({ "unchecked", "rawtypes" })
private Shape pickShapeGLImpl(final GLAutoDrawable drawable, final int glWinX, final int glWinY) {
final Object[] shapesS = shapes.toArray();
- Arrays.sort(shapesS, (Comparator)shapeZAscComparator);
+ Arrays.sort(shapesS, (Comparator)Shape.ZAscendingComparator);
final GLPixelStorageModes psm = new GLPixelStorageModes();
final ByteBuffer pixel = Buffers.newDirectByteBuffer(4);
@@ -837,7 +826,7 @@ public final class Scene implements GLEventListener {
final PMVMatrix pmv = new PMVMatrix();
final float[] objPos = new float[3];
final Shape[] shape = { null };
- if( null == pickShape(glWinX, glWinY, pmv, objPos, shape, () -> {
+ if( null == pickShape(pmv, glWinX, glWinY, objPos, shape, () -> {
setActiveShape(shape[0]);
shape[0].dispatchMouseEvent(e, glWinX, glWinY, objPos);
} ) )
diff --git a/src/graphui/classes/com/jogamp/graph/ui/Shape.java b/src/graphui/classes/com/jogamp/graph/ui/Shape.java
index e777dacff..dcac39504 100644
--- a/src/graphui/classes/com/jogamp/graph/ui/Shape.java
+++ b/src/graphui/classes/com/jogamp/graph/ui/Shape.java
@@ -28,6 +28,7 @@
package com.jogamp.graph.ui;
import java.util.ArrayList;
+import java.util.Comparator;
import com.jogamp.nativewindow.NativeWindowException;
import com.jogamp.opengl.GL2ES2;
@@ -1185,6 +1186,20 @@ public abstract class Shape {
*/
public abstract boolean hasColorChannel();
+ public static Comparator ZAscendingComparator = new Comparator() {
+ @Override
+ public int compare(final Shape s1, final Shape s2) {
+ final float s1Z = s1.getBounds().getMinZ()+s1.getPosition()[2];
+ final float s2Z = s2.getBounds().getMinZ()+s2.getPosition()[2];
+ if( FloatUtil.isEqual(s1Z, s2Z, FloatUtil.EPSILON) ) {
+ return 0;
+ } else if( s1Z < s2Z ){
+ return -1;
+ } else {
+ return 1;
+ }
+ } };
+
//
//
//
--
cgit v1.2.3