Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();    
}
}
share|improve this question
1  
+1 for what looks like a nicely asked question on your first time :) –  James Feb 9 '12 at 17:47
    
Why thank you! I hope I get answers x3 –  Patokun Feb 10 '12 at 7:56
add comment

1 Answer

Sorry, I do not keep track on the quests asked here. For support on the Nifty-GUI and on Slick best use the boards of Slick2D and Nifty-GUI to ask your questions.

How ever in case your question is still relevant, you shall get your answer.

To enable the Nifty-GUI to receive mouse and keyboard you have to forward all events arriving in Slick2D to the Nifty-GUI. This is a pretty major task as Slick2D and Nifty-GUI have separated polling times for this events. So you basically have to buffer the events from Slick2D and forward them to Nifty-GUI upon request.

How ever you are using the PlainSlickInputSystem that basically has this functionality already included. In order to hook it into Slick properly you need to do the following:

public void init(GameContainer container, StateBasedGame game) throws SlickException {
 SlickInputSystem inputSys = new PlainSlickInputSystem();
 inputSys.setInput(input);

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

 input.removeListener(this);
 input.removeListener(inputSystem);
 input.addListener(inputSystem);

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

Once you did so the PlainSlickInputSystem will take care for relaying the input events from Slick to Nifty.

How ever I propse that you make use of the Game, BasicGame, GameState and BasicGameState implementations that are delivered along with the Slick2D-render library for Nifty. Those classes implement the default Slick classes with there respective names and add all functionality to display and use the Nifty-GUI. The entire initialization is done more or less automatically. Updating and rendering is done automatically as well.

Please also take a look at the Nifty-Slick2D-Renderer reference page for further details on how use use this renderer in the best way.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.