I’ve outlined a way to resolve fields to scene objects - much like swing/javafx controllers, and would appreciate feedback/advice.
So the basic premise is that you have a scene and you want to reference an object in that scene to manipulate it. For example you have a Lemur Button or a AnimControl or whatever it may be. You would normally traverse the scene and find it and cast it to your type.
This class resolves field names with spatials/controls automatically. So if you have a spatial with name “customNode” - you would create a field in your class called customNode
as shown below.
If you have a Control, you can’t name them, so instead it takes the name of its owner + class.getSimpleName()
. So in this instance it would be customNodeRotateControl
.
And so we get the same functionality as a swing/javafx controller, but for scenes/spatials.
public class TestClass {
private int testIntField;
private String testFieldString;
private Node myNode;
private CustomNode customNode;
private Geometry myGeometry;
private CustomGeometry customGeometry;
private RotateControl customNodeRotateControl;
private Control customGeometryRotateControl;
private DirectionalLight sunLight;
public TestClass(Spatial scene) {
SceneFieldResolver resolver = new SceneFieldResolver();
resolver.resolve(this, scene);
}
}
would return the following result:
Before
---
Field: testIntField -> 0
Field: testFieldString -> null
Field: myNode -> null
Field: customNode -> null
Field: myGeometry -> null
Field: customGeometry -> null
Field: customNodeRotateControl -> null
Field: customGeometryRotateControl -> null
Field: sunLight -> null
After
---
Field: testIntField -> 0
Field: testFieldString -> null
Field: myNode -> myNode (Node)
Field: customNode -> customNode (CustomNode)
Field: myGeometry -> myGeometry (Geometry)
Field: customGeometry -> customGeometry (CustomGeometry)
Field: customNodeRotateControl -> com.jayfella.sdk.resolver.SceneFieldResolver$RotateControl@7494e528
Field: customGeometryRotateControl -> com.jayfella.sdk.resolver.SceneFieldResolver$RotateControl@4bbfb90a
Field: sunLight -> DirectionalLight[name=sunLight, direction=(0.0, -1.0, 0.0), color=Color[1.0, 1.0, 1.0, 1.0], enabled=true]
Below is a github gist of the SceneFieldResolver class with a working example.