It appears this is possible and I’m finding traces of how it’s done it in the Nifty source, but I’m having a hard time figuring it out.
Basically what I’m trying to do is create custom controls at runtime, without having to define them all in the layout/xml definitions.
We need to populate a map at runtime with interactive icons (our custom control, not just a panel or textfield), and don’t know the exact amount of icons to generate until runtime (game play).
Does anyone know how this is done? take a look at the quick example code below of a technical description.
[java]import com.jme3.app.SimpleApplication;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.*;
public class HelloTestScript extends SimpleApplication {
public static final String SCREEN_RUNTIME = “screenRuntime”;
private Nifty nifty;
public static void main(String[] args){
HelloTestScript app = new HelloTestScript();
app.setPauseOnLostFocus(false);
app.setShowSettings(false);
app.start();
}
public void simpleInitApp() {
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
nifty = niftyDisplay.getNifty();
guiViewPort.addProcessor(niftyDisplay);
inputManager.setCursorVisible(true);
// Define Screen Layout which contains parent element
nifty.addScreen(SCREEN_RUNTIME, new ScreenBuilder(SCREEN_RUNTIME) {{
layer(new LayerBuilder("#mapLayer") {{
childLayoutCenter();
panel(new PanelBuilder("#map") {{
childLayoutCenter();
width(“70%”);
height(“40%”);
backgroundColor("#0000ff");
}});
}});
}}.build(nifty));
// Define the Custom control that is our child element
new ControlDefinitionBuilder("#icon") {{
panel(new PanelBuilder("#icon_panel") {{
childLayoutAbsolute();
backgroundColor("#33ff00");
// … would include several custom interactive child elements …
}});
}}.registerControlDefintion(nifty);
nifty.gotoScreen(SCREEN_RUNTIME); // start the screen
}
@Override
public void simpleUpdate(float tpf) {
/**
- During runtime we need to be able to add our custom controls ‘icons’ to the ‘map’
- How is this possible ?!?
- After digging through the source I thought I found how this works internally (shown below)
- But it’s not working this way and the below code will through a NullPointerException
/
/
// !! How come this doesn’t work? Is this possible ?!?
Screen screen = nifty.getScreen(SCREEN_RUNTIME);
Element parentElement = screen.findElementByName("#map");
Element childElement = screen.findElementByName(“panel_main”);
nifty.createElementFromType(screen, parentElement, childElement.getElementType());
*/
}
}
[/java]