Scene -> Field Resolver

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.

2 Likes

This seems like something that could be useful to quite a few folks. On the one hand, when using an ECS it’s best practice to store references into the scenegraph rather than resolving them later. On the other hand, lots of folks don’t use an ECS, and even when using one there are some nodes that don’t necessarily fit into an ECS very well (like things involved in atmospheric effects).

I could see this being pretty handy.