Hi, so in my nifty menu I have created a new window control, which i want to pop up as soon as a button is pressed, so i create it in XML and set visible to false right off the bat. Then I have another method, button1Pressed, and which sets the window visible. Then in my button control I add
[xml]<interact onClick=“button1Pressed()” />[/xml]
but it has no effect. Add this to the fact that I cant drag around the window, it seems like it should be in some kind of update loop somewhere, although I thought the nifty would run in a separate thread. Searched Nifty’s and jmonkey’s documentation, cant seem to find out whats going on
Is your XML defining the controller class? Actually… can you post the XML & the control? It would save a bunch of questions.
Nifty is updated in the main render loop.
Most likely your controller isn’t registered properly, have you checked that the method is being called and being called in the right object?
[xml]
<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“Interface/nifty-charcoal/nifty-charcoal.xml” />
<useControls filename=“nifty-default-controls.xml” />
<screen id=“start” controller=“TheHunt.GUI.Menu”>
<layer id=“background” backgroundColor="#1000" childLayout=“vertical” visibleToMouse=“true”>
<panel id=“Title” height=“25%” width=“30%” align=“center” childLayout=“center” backgroundColor="#f60f" style=“nifty-panel”>
<text text=“Start Menu” width=“100%” height=“100%” font=“Interface/Fonts/Default.fnt”/>
</panel>
<panel id=“Button1” height=“20%” width=“30%” align=“left” childLayout=“center” valign=“center” visibleToMouse=“true”>
<control id=“submitButton1” type=“button” label=“Connect to a server” height=“30%” width=“70%” visibleToMouse=“true”>
<interact onClick=“startPressed()” />
</control>
</panel>
<panel id=“Button3” height=“20%” width=“30%” align=“left” childLayout=“center” valign=“center”>
<control id=“submitButton2” type=“button” label=“Quit” height=“31%” width=“71%”>
</control>
</panel>
</layer>
<layer id=“windows” childLayout=“absolute” visibleToMouse=“true”>
<control id=“window-3” name=“window” title=“Please Drag Me Too!” width=“500px” height=“400px” x=“400px” y=“200px” visibleToMouse=“true”>
<text text=“I’m Another Window!” style=“base-font” color="#eeef" valign=“center” width=“100%” />
</control>
</layer>
</screen>
</nifty>[/xml]
[java]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package TheHunt.GUI;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.window.WindowControl;
import de.lessvoid.nifty.elements.Element;
/**
*
-
@author Dan
*/
public class Menu extends AbstractAppState {
private SimpleApplication app;
private AssetManager assetManager;
private InputManager inputManager;
private AudioRenderer audioRenderer;
private ViewPort guiViewPort;
private NiftyJmeDisplay startMenu;
private Nifty nifty;
private FlyByCamera flyCam;
private Element z;
private WindowControl w;
public Menu(FlyByCamera flyCam, AssetManager assetManager, AudioRenderer ausioRenderer, ViewPort guiViewPort) {
this.flyCam = flyCam;
this.assetManager = assetManager;
this.guiViewPort = guiViewPort;
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app;
startMenu = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
nifty = startMenu.getNifty();
nifty.fromXml("Interface/Menu.xml", "start");
}
@Override
public void cleanup() {
super.cleanup();
}
@Override
public void setEnabled(boolean enabled) {
// Pause and unpause
super.setEnabled(enabled);
if (enabled) {
guiViewPort.addProcessor(startMenu);
flyCam.setEnabled(false);
flyCam.setDragToRotate(true);
// inputManager.setCursorVisible(true);
w = nifty.getScreen("start").findNiftyControl("window-3", WindowControl.class);
w.setTitle("Choose a server");
z = nifty.getScreen("start").findElementByName("window-3");
//z.setVisible(false);
} else {
guiViewPort.removeProcessor(startMenu);
}
}
@Override
public void update(float tpf) {
nifty.update();
}
public void startPressed() {
z.setVisible(true);
}
}
[/java]
The menu is in a separate App State
You haven’t registered your screen controller with nifty. (Do it either as a separate call before the fromXml or as an extra paramater to fromXml).
What do you mean I havent registered my screen controller? isnt that what you do here [xml]<screen id="start" controller="TheHunt.GUI.Menu">[/xml]?
If not, then how do I register it?
You class also doesn’t implement screencontroller. Do that and then in you fromxml, add “this” (reference to the object) at the end
Okay, added this to fromXML, implemented ScreenController, all abstract methods implemented, do I need to use these methods?
Not unless you need to do something with them.
I still cant use my button though
BTW I also get this wierd exception before the screen is loaded,
WARNING: An event service by the name NiftyEventBusalready exists. Perhaps multiple threads tried to create a service about the same time?
org.bushe.swing.event.EventServiceExistsException: An event service by the name NiftyEventBusalready exists. Perhaps multiple threads tried to create a service about the same time?
at org.bushe.swing.event.EventServiceLocator.setEventService(EventServiceLocator.java:123)
at de.lessvoid.nifty.Nifty.initalizeEventBus(Nifty.java:221)
at de.lessvoid.nifty.Nifty.initialize(Nifty.java:201)
at de.lessvoid.nifty.Nifty.(Nifty.java:142)
at com.jme3.niftygui.NiftyJmeDisplay.(NiftyJmeDisplay.java:107)
at TheHunt.GUI.Menu.initialize(Menu.java:56)
at com.jme3.app.state.AppStateManager.initializePending(AppStateManager.java:218)
at com.jme3.app.state.AppStateManager.update(AppStateManager.java:248)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:238)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:662)
Do you have another appstate that also does:
[java]new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
[/java]
?
No, I only have two classes in my entire project, and this is all my main class does: [java]
public void simpleInitApp() {
Menu menu = new Menu(flyCam, assetManager, audioRenderer, guiViewPort);
menu.initialize(stateManager, this);
stateManager.attach(menu);
menu.setEnabled(true);
}[/java]
[java] menu.initialize(stateManager, this);
stateManager.attach(menu);[/java]
Statemanager will call initialize for you, do not call it yourself. In this way you’ll get two instances of nifty.
Okay, thanks I took that out but I STILL can’t get my button to do anything when pressed, could anyone tell me why? I’ve followed everything everyone has said…
BUMP pleasee cold anyone help with this button problem, I’ll post my updated code here as I’ve made another controller class since I last posted
[xml]<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“Interface/nifty-charcoal/nifty-charcoal.xml” />
<useControls filename=“nifty-default-controls.xml” />
<screen id=“start” controller=“TheHunt.GUI.MenuControl”>
<layer id=“windows” childLayout=“absolute” visibleToMouse=“true” backgroundColor="#f60f" >
<control id=“window” name=“window” title=“Please Drag Me Too!” width=“500px” height=“400px” x=“400px” y=“200px” visibleToMouse=“true”>
<text text=“I’m Another Window!” style=“base-font” color="#eeef" valign=“center” width=“100%” />
</control>
</layer>
<layer id=“background” backgroundColor="#1000" childLayout=“vertical” visibleToMouse=“true”>
<panel id=“Title” height=“25%” width=“30%” align=“center” childLayout=“center” backgroundColor="#f60f" style=“nifty-panel”>
<text text=“Start Menu” width=“100%” height=“100%” font=“Interface/Fonts/Default.fnt”/>
</panel>
<panel id=“Button1” height=“20%” width=“30%” align=“left” childLayout=“center” valign=“center” visibleToMouse=“true” backgroundColor="#f60f" >
<control id=“submitButton1” type=“button” label=“Connect to a server” height=“30%” width=“70%” visibleToMouse=“true”>
<interact onClick=“startPressed()” />
</control>
</panel>
<panel id=“Button3” height=“20%” width=“30%” align=“left” childLayout=“center” valign=“center” backgroundColor="#f60f" >
<control id=“submitButton2” type=“button” label=“Quit” height=“31%” width=“71%”>
</control>
</panel>
</layer>
</screen>
</nifty>
[/xml]
[java]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package TheHunt.GUI;
import TheHunt.Main;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.window.WindowControl;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
/**
*
-
@author Dan
/
public class Menu extends AbstractAppState {
private SimpleApplication app;
private AssetManager assetManager;
private InputManager inputManager;
private AudioRenderer audioRenderer;
private ViewPort guiViewPort;
private NiftyJmeDisplay startMenu;
private Nifty nifty;
private Screen screen;
private FlyByCamera flyCam;
private Element z;
private WindowControl w;
public Menu(FlyByCamera flyCam, AssetManager assetManager, AudioRenderer audioRenderer, ViewPort guiViewPort) {
this.flyCam = flyCam;
this.assetManager = assetManager;
this.audioRenderer = audioRenderer;
this.guiViewPort = guiViewPort;
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app;
startMenu = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
nifty = startMenu.getNifty();
nifty.fromXml("Interface/Menu.xml", "start", new MenuControl());
setEnabled(true);
}
@Override
public void cleanup() {
super.cleanup();
}
@Override
public void setEnabled(boolean enabled) {
// Pause and unpause
super.setEnabled(enabled);
if (enabled) {
this.guiViewPort.addProcessor(startMenu);
flyCam.setEnabled(false);
flyCam.setDragToRotate(true);
nifty.getScreen("start").findElementByName("window").setVisible(false);
} else {
}
}
@Override
public void update(float tpf) {
nifty.update();
}
}
[/java]
[java]/
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package TheHunt.GUI;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
/**
*
-
@author Dan
*/
public class MenuControl implements ScreenController{
private Screen screen;
private Nifty nifty;
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
public void onStartScreen() {
}
public void onEndScreen() {
}
public void startPressed(){
//this is just a test to show me whether the button is working
System.out.println(“selected”);
System.out.println(“selected”);
System.out.println(“selected”);
System.out.println(“selected”);
System.out.println(“selected”);
System.out.println(“selected”);
System.out.println(“selected”);
System.out.println(“selected”);
}
}
[/java]
I really cant figure this out done everything anyone said but my button still doesn’t do anything when I press it
I can’t see anything wrong with what you have. Have you checked your logs to make sure Nifty isn’t reporting any errors?
the only thing that looks weird to me is the [java]nifty.update();[/java] and possibly your setEnabled() method
whats strange about the setEnabled() method? hmm