Skip to content

Commit b8e5c25

Browse files
committed
solve issue #1542 (crash in TestAppStates) (#1545)
1 parent 88effd4 commit b8e5c25

File tree

4 files changed

+105
-31
lines changed

4 files changed

+105
-31
lines changed

jme3-examples/src/main/java/jme3test/app/state/TestAppStates.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2012 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -37,6 +37,7 @@
3737
import com.jme3.scene.Spatial;
3838
import com.jme3.system.AppSettings;
3939
import com.jme3.system.JmeContext;
40+
import jme3test.niftygui.StartScreenController;
4041

4142
public class TestAppStates extends LegacyApplication {
4243

@@ -70,10 +71,12 @@ public void initialize(){
7071
state.getRootNode().attachChild(model);
7172

7273
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
73-
inputManager,
74-
audioRenderer,
75-
guiViewPort);
76-
niftyDisplay.getNifty().fromXml("Interface/Nifty/HelloJme.xml", "start");
74+
inputManager,
75+
audioRenderer,
76+
guiViewPort);
77+
StartScreenController startScreen = new StartScreenController(this);
78+
niftyDisplay.getNifty().fromXml("Interface/Nifty/HelloJme.xml", "start",
79+
startScreen);
7780
guiViewPort.addProcessor(niftyDisplay);
7881
}
7982

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2009-2021 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package jme3test.niftygui;
33+
34+
import com.jme3.app.Application;
35+
import de.lessvoid.nifty.Nifty;
36+
import de.lessvoid.nifty.screen.Screen;
37+
import de.lessvoid.nifty.screen.ScreenController;
38+
39+
/**
40+
* A ScreenController for the "start" screen defined in
41+
* "Interfaces/Nifty/HelloJme.xml", which is used in the TestAppStates and
42+
* TestNiftyGui applications.
43+
*/
44+
public class StartScreenController implements ScreenController {
45+
46+
final private Application application;
47+
48+
/**
49+
* Instantiate a ScreenController for the specified Application.
50+
*
51+
* @param app the Application
52+
*/
53+
public StartScreenController(Application app) {
54+
this.application = app;
55+
}
56+
57+
/**
58+
* Nifty invokes this method when the screen gets enabled for the first
59+
* time.
60+
*
61+
* @param nifty (not null)
62+
* @param screen (not null)
63+
*/
64+
@Override
65+
public void bind(Nifty nifty, Screen screen) {
66+
System.out.println("bind(" + screen.getScreenId() + ")");
67+
}
68+
69+
/**
70+
* Nifty invokes this method each time the screen starts up.
71+
*/
72+
@Override
73+
public void onStartScreen() {
74+
System.out.println("onStartScreen");
75+
}
76+
77+
/**
78+
* Nifty invokes this method each time the screen shuts down.
79+
*/
80+
@Override
81+
public void onEndScreen() {
82+
System.out.println("onEndScreen");
83+
}
84+
85+
/**
86+
* Stop the Application. Nifty invokes this method (via reflection) after
87+
* the user clicks on the flashing orange panel.
88+
*/
89+
public void quit() {
90+
application.stop();
91+
}
92+
}

jme3-examples/src/main/java/jme3test/niftygui/TestNiftyGui.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2012 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -38,10 +38,8 @@
3838
import com.jme3.scene.Geometry;
3939
import com.jme3.scene.shape.Box;
4040
import de.lessvoid.nifty.Nifty;
41-
import de.lessvoid.nifty.screen.Screen;
42-
import de.lessvoid.nifty.screen.ScreenController;
4341

44-
public class TestNiftyGui extends SimpleApplication implements ScreenController {
42+
public class TestNiftyGui extends SimpleApplication {
4543

4644
private Nifty nifty;
4745

@@ -66,7 +64,8 @@ public void simpleInitApp() {
6664
audioRenderer,
6765
guiViewPort);
6866
nifty = niftyDisplay.getNifty();
69-
nifty.fromXml("Interface/Nifty/HelloJme.xml", "start", this);
67+
StartScreenController startScreen = new StartScreenController(this);
68+
nifty.fromXml("Interface/Nifty/HelloJme.xml", "start", startScreen);
7069

7170
// attach the nifty display to the gui view port as a processor
7271
guiViewPort.addProcessor(niftyDisplay);
@@ -76,24 +75,4 @@ public void simpleInitApp() {
7675
// flyCam.setDragToRotate(true);
7776
inputManager.setCursorVisible(true);
7877
}
79-
80-
@Override
81-
public void bind(Nifty nifty, Screen screen) {
82-
System.out.println("bind( " + screen.getScreenId() + ")");
83-
}
84-
85-
@Override
86-
public void onStartScreen() {
87-
System.out.println("onStartScreen");
88-
}
89-
90-
@Override
91-
public void onEndScreen() {
92-
System.out.println("onEndScreen");
93-
}
94-
95-
public void quit(){
96-
nifty.gotoScreen("end");
97-
}
98-
9978
}

jme3-testdata/src/main/resources/Interface/Nifty/HelloJme.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<nifty>
3-
<screen id="start" controller="jme3test.niftygui.TestNiftyGui">
3+
<screen id="start" controller="jme3test.niftygui.StartScreenController">
44
<layer id="layer" backgroundColor="#0000" childLayout="center">
55
<panel id="panel" height="25%" width="35%" align="center" valign="center" backgroundColor="#f60f" childLayout="center" visibleToMouse="true">
66
<interact onClick="quit()"/>

0 commit comments

Comments
 (0)