Slick2D, Nifty GUI listeners problem

Posted by Patokun on Game Development See other posts from Game Development or by Patokun
Published on 2012-02-09T17:15:38Z Indexed on 2012/04/04 17:43 UTC
Read the original article Hit count: 381

Filed under:
|

I'm trying to get Nifty GUI to work with Slick2D. So far everything is going great, except that I can't seem to figure out how to properly interact with the GUI. I'm trying the example in the nifty manual http://sourceforge.n....0.pdf/download but it doesn't seem to entirely work.

The Element controller is being called for bind(...), init(...) and onStartScreen() as it should, as I can see their println output, but the next() method isn't being called when I click on the GUI element that I assigned the controller to, nor the screen controller as no output from println is shown.

What's weird is, that the player is moving, so the mouse input is working. It's supposed to be called when I click the mouse button on it from the in the XML.

Here is my code:

My Element controller:

public class ElementController implements Controller {

private Element element;

@Override
public void bind(Nifty nifty, Screen screen, Element element, Properties parameter, Attributes controlDefinitionAttributes) {
  this.element = element;
  System.out.println("bind() called for element: " + element);
}

@Override
public void init(Properties parameter, Attributes controlDefinitionAttributes) {
  System.out.println("init()  called for element: " + element);
}

@Override
public void onStartScreen() {
  System.out.println("onStartScreen()  alled for element: " + element);
}

@Override
public void onFocus(boolean getFocus) {
  System.out.println("onFocus()  called for element: " + element + ", with: " +
    getFocus);
}

@Override
public boolean inputEvent(NiftyInputEvent inputEvent) {
  return false;
}

public void next() {
  System.out.println("next() clicked for element: " + element);
}
}

MyScreenController:

class MyScreenController implements ScreenController {
    public void bind(Nifty nifty, Screen screen) {}
    public void onEndScreen() {}
    public void onStartScreen() {}
    public void next() {
     System.out.println("next() called from MyScreenController");
}
}

And my XML file:

<?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://niftygui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">
<screen id="start" controller="predaN00b.theThing.V0004.MyScreenController">
  <layer childLayout="center" controller="predaN00b.theThing.V0004.ElementController">
   <panel width="100px" height="100px" childLayout="vertical" backgroundColor="#ff0f">
     <text font="aurulent-sans-16.fnt" color="#ffff" text="Hello World!">
      <interact onClick="next()" />
     </text>
   </panel>
  </layer>
</screen>
</nifty>

My main class, in case it's needed:

public class MainGameState extends BasicGame {

public Nifty nifty;

public MainGame() {
  super("Test");  
}

public void init(GameContainer container, StateBasedGame game) throws SlickException {

  nifty = new Nifty(new SlickRenderDevice(container), new NullSoundDevice(), new PlainSlickInputSystem(), new AccurateTimeProvider());

  nifty.addXml("/xml/MainState.xml");
     nifty.gotoScreen("start");

}

public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
  nifty.update();
}

public void render(GameContainer container, StateBasedGame game, Graphics graphics) throws SlickException {
  nifty.render(false);
}

  public static void main(String[] args) throws SlickException {
     AppGameContainer app = new AppGameContainer(new MainGame());
     app.setAlwaysRender(true);
     app.setDisplayMode( 1260 , 720, false); //window size
     app.start();    
}
}

© Game Development or respective owner

Related posts about java

Related posts about slick