aboutsummaryrefslogtreecommitdiffstats
path: root/src/graphui
diff options
context:
space:
mode:
authorSven Göthel <sgothel@jausoft.com>2024-01-20 05:04:15 +0100
committerSven Göthel <sgothel@jausoft.com>2024-01-20 05:04:15 +0100
commitcd47baa406818f99c6d7e7711b7c1d16357456f6 (patch)
tree3e879cd8ab65cbfdef38b7482129912f8194401b /src/graphui
parentc1531c3d99b19032040018b9414263b0d3000147 (diff)
GraphUI Graph/Scene: Reuse TreeTool for contains(), getShapeByID() and getShapeByName(), also adding full traversion (instead of a flat lookup)
Diffstat (limited to 'src/graphui')
-rw-r--r--src/graphui/classes/com/jogamp/graph/ui/Group.java27
-rw-r--r--src/graphui/classes/com/jogamp/graph/ui/Scene.java16
-rw-r--r--src/graphui/classes/jogamp/graph/ui/TreeTool.java64
3 files changed, 54 insertions, 53 deletions
diff --git a/src/graphui/classes/com/jogamp/graph/ui/Group.java b/src/graphui/classes/com/jogamp/graph/ui/Group.java
index d1156120a..fc6f91980 100644
--- a/src/graphui/classes/com/jogamp/graph/ui/Group.java
+++ b/src/graphui/classes/com/jogamp/graph/ui/Group.java
@@ -564,19 +564,8 @@ public class Group extends Shape implements Container {
@Override
public boolean contains(final Shape s) {
- if( shapes.contains(s) ) {
- return true;
- }
- for(final Shape shape : shapes) {
- if( shape instanceof Container ) {
- if( ((Container)shape).contains(s) ) {
- return true;
- }
- }
- }
- return false;
+ return TreeTool.contains(shapes, s);
}
-
@Override
public Shape getShapeByIdx(final int id) {
if( 0 > id ) {
@@ -586,21 +575,11 @@ public class Group extends Shape implements Container {
}
@Override
public Shape getShapeByID(final int id) {
- for(final Shape b : shapes) {
- if(b.getID() == id ) {
- return b;
- }
- }
- return null;
+ return TreeTool.getShapeByID(shapes, id);
}
@Override
public Shape getShapeByName(final String name) {
- for(final Shape b : shapes) {
- if( b.getName().equals(name) ) {
- return b;
- }
- }
- return null;
+ return TreeTool.getShapeByName(shapes, name);
}
@Override
diff --git a/src/graphui/classes/com/jogamp/graph/ui/Scene.java b/src/graphui/classes/com/jogamp/graph/ui/Scene.java
index 03cc29097..fa11f2675 100644
--- a/src/graphui/classes/com/jogamp/graph/ui/Scene.java
+++ b/src/graphui/classes/com/jogamp/graph/ui/Scene.java
@@ -332,7 +332,7 @@ public final class Scene implements Container, GLEventListener {
@Override
public boolean contains(final Shape s) {
- return false;
+ return TreeTool.contains(shapes, s);
}
@Override
public Shape getShapeByIdx(final int id) {
@@ -343,21 +343,11 @@ public final class Scene implements Container, GLEventListener {
}
@Override
public Shape getShapeByID(final int id) {
- for(final Shape b : shapes) {
- if(b.getID() == id ) {
- return b;
- }
- }
- return null;
+ return TreeTool.getShapeByID(shapes, id);
}
@Override
public Shape getShapeByName(final String name) {
- for(final Shape b : shapes) {
- if( b.getName().equals(name) ) {
- return b;
- }
- }
- return null;
+ return TreeTool.getShapeByName(shapes, name);
}
/** Returns {@link RegionRenderer#getSampleCount()}. */
diff --git a/src/graphui/classes/jogamp/graph/ui/TreeTool.java b/src/graphui/classes/jogamp/graph/ui/TreeTool.java
index b26ec8417..c37cd53d8 100644
--- a/src/graphui/classes/jogamp/graph/ui/TreeTool.java
+++ b/src/graphui/classes/jogamp/graph/ui/TreeTool.java
@@ -79,18 +79,16 @@ public class TreeTool {
* @return true to signal operation complete and to stop traversal, i.e. {@link Visitor1#visit(Shape)} returned true, otherwise false
*/
public static boolean forAll(final List<Shape> shapes, final Visitor1 v) {
- for(int i=0; i<shapes.size(); ++i) {
+ boolean res = false;
+ for(int i=0; !res && i<shapes.size(); ++i) {
final Shape s = shapes.get(i);
- boolean res = v.visit(s);
+ res = v.visit(s);
if( !res && s instanceof Container ) {
final Container c = (Container)s;
res = c.forAll(v);
}
- if( res ) {
- return true;
- }
}
- return false;
+ return res;
}
/**
@@ -100,21 +98,19 @@ public class TreeTool {
* @return true to signal operation complete and to stop traversal, i.e. {@link Visitor2#visit(Shape, PMVMatrix4f)} returned true, otherwise false
*/
public static boolean forAll(final List<Shape> shapes, final PMVMatrix4f pmv, final Visitor2 v) {
- for(int i=0; i<shapes.size(); ++i) {
+ boolean res = false;
+ for(int i=0; !res && i<shapes.size(); ++i) {
final Shape s = shapes.get(i);
pmv.pushMv();
s.setTransformMv(pmv);
- boolean res = v.visit(s, pmv);
+ res = v.visit(s, pmv);
if( !res && s instanceof Container ) {
final Container c = (Container)s;
res = c.forAll(pmv, v);
}
pmv.popMv();
- if( res ) {
- return true;
- }
}
- return false;
+ return res;
}
/**
@@ -130,22 +126,58 @@ public class TreeTool {
public static boolean forSortedAll(final Comparator<Shape> sortComp, final List<Shape> shapes, final PMVMatrix4f pmv, final Visitor2 v) {
final Object[] shapesS = shapes.toArray();
Arrays.sort(shapesS, (Comparator)sortComp);
+ boolean res = false;
- for(int i=0; i<shapesS.length; ++i) {
+ for(int i=0; !res && i<shapesS.length; ++i) {
final Shape s = (Shape)shapesS[i];
pmv.pushMv();
s.setTransformMv(pmv);
- boolean res = v.visit(s, pmv);
+ res = v.visit(s, pmv);
if( !res && s instanceof Container ) {
final Container c = (Container)s;
res = c.forSortedAll(sortComp, pmv, v);
}
pmv.popMv();
- if( res ) {
- return true;
+ }
+ return res;
+ }
+
+ public static boolean contains(final List<Shape> shapes, final Shape s) {
+ if( shapes.contains(s) ) {
+ return true;
+ }
+ for(final Shape shape : shapes) {
+ if( shape instanceof Container ) {
+ if( ((Container)shape).contains(s) ) {
+ return true;
+ }
}
}
return false;
}
+ public static Shape getShapeByID(final List<Shape> shapes, final int id) {
+ final Shape[] res = { null };
+ forAll(shapes, (final Shape s) -> {
+ if( s.getID() == id ) {
+ res[0] = s;
+ return true;
+ } else {
+ return false;
+ }
+ });
+ return res[0];
+ }
+ public static Shape getShapeByName(final List<Shape> shapes, final String name) {
+ final Shape[] res = { null };
+ forAll(shapes, (final Shape s) -> {
+ if( s.getName().equals(name) ) {
+ res[0] = s;
+ return true;
+ } else {
+ return false;
+ }
+ });
+ return res[0];
+ }
}