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:
All game states needs to be subclasses of BasicTWLGameState. This allows them to provide their own UI when desired:
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.
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 .... // }
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); } }
on 2012/02/06 14:03