jmonkeyengine: Nifty GUI doesn't display correctly after context restart
It seems JME’s context restart feature is flawed because with the sample code below, the Nifty GUI refuses to display at all:
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
public class NiftyJme3RestartTest extends SimpleApplication implements ScreenController {
public static void main(String[] args) {
new NiftyJme3RestartTest().start();
}
private NiftyJmeDisplay niftyDisplay;
@Override
public void simpleInitApp() {
// this box here always renders
Box b = new Box(1, 1, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("/com/jme3/app/Monkey.png"));
geom.setMaterial(mat);
rootNode.attachChild(geom);
niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
Nifty nifty = niftyDisplay.getNifty();
nifty.loadStyleFile("nifty-default-styles.xml");
nifty.loadControlFile("nifty-default-controls.xml");
ScreenController ctrl = this;
new ScreenBuilder("start") {
{
controller(ctrl);
layer(new LayerBuilder() {
{
childLayoutVertical();
panel(new PanelBuilder() {
{
childLayoutCenter();
width("100%");
height("50%");
backgroundColor("#ff0000");
}
});
control(new ButtonBuilder("RestartButton", "Restart Context") {
{
alignCenter();
valignCenter();
height("32px");
width("480px");
interactOnClick("restartContext()");
}
});
}
});
}
}.build(nifty);
guiViewPort.addProcessor(niftyDisplay);
nifty.gotoScreen("start");
flyCam.setDragToRotate(true);
}
@Override
public void bind(Nifty nifty, Screen screen) {
}
@Override
public void onStartScreen() {
}
@Override
public void onEndScreen() {
}
public void restartContext() {
// even without changing settings, stuff breaks!
restart();
// ...and re-adding the processor doesn't help at all
guiViewPort.addProcessor(niftyDisplay);
}
}
The code was tested with jMonkeyEngine 3.2.2-stable. When run, the output log is pretty standard - there aren’t any exceptions thrown or anything. Also, it seems the flyCam
‘drag to rotate’ functionality breaks after restart as well, unable to… well, drag at all.
On a slightly more complicated GUI setup that I’ve made (with nested panels and a few more buttons written in XML) the screen became green after restarting the context.
Perhaps the problem can be related to this post?
I haven’t explored the engine much, so I’m hoping somebody who knows what they’re doing can investigate this issue.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 28 (27 by maintainers)
Commits related to this issue
- Added test issue from issue #1013 — committed to tonihele/jmonkeyengine by tonihele 4 years ago
- Lwjgl3 restart input handle (#1268) * Reinit inputs on context restart * Added test issue from issue #1013 * Verify that the inputs are already initialized — committed to jMonkeyEngine/jmonkeyengine by tonihele 4 years ago
- Lwjgl3 restart input handle (#1268) * Reinit inputs on context restart * Added test issue from issue #1013 * Verify that the inputs are already initialized — committed to jMonkeyEngine/jmonkeyengine by tonihele 4 years ago
Well, the situation is far better than it was! With 3.3 I would be left with a totally black screen after restart, with 3.4: (the textures maybe just disappeared but at least some of the content stays intact) LWJGL3, Before restart:
LWJGL3, After restart:
With LWJGL 3 the keyboard and mouse listeners are also tied to the window. When the context is restarted, the window is re-created. This seems to break the keyboard and mouse listeners as they still refer the old window handle. That explains the problems you get with the flycam. This one is easy to fix.
For our game the restart results in the whole scene gone, both Nifty and the regular scene context. The Nifty is there still alive and kicking, but I just can’t see it. Any buttons etc. seem to work.