This approach should be easier to use together with Slick's StateBasedGame.

As the source code is a larger for this approach it is provided as zip file

Instead of directly using StateBasedGame you subclass TWLStateBasedGame and override getThemeURL() like this:

public class MyGame extends TWLStateBasedGame {
    /**
     * Implement this method and return the URL for the TWL theme.
     * 
     * @return the URL for the TWL theme. Must not be null.
     */
    @Override
    protected URL getThemeURL() {
        return MyGame.class.getResource("ui/theme.xml");
    }

    //
    // init game states etc ....
    //
}

All game states needs to be subclasses of BasicTWLGameState. This allows them to provide their own UI when desired:

public class MainMenuState extends BasicTWLGameState {
    private Button btn;

    @Override
    protected RootPane createRootPane() {
        RootPane rp = super.createRootPane();
        rp.setTheme("mainMenu");

        btn = new Button("Hello World");
        btn.addCallback(new Runnable() {
            public void run() {
                System.out.println("It works!");
            }
        });

        rp.add(btn);
        return rp;
    }

    @Override
    protected void layoutRootPane() {
        btn.adjustSize();
        btn.setPosition(100, 100);
    }
}

These classes will replace the normal Slick input handling and use TWL's event routing instead. All input events which are not consumed by an UI widget (like the Button above) are forwarded to the game state's input handling methods. But no other InputListeners are called.


Creator: Matthias Mann on 2010/06/17 14:25
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 1.8.17790 - Documentation