In the previous Lemur Gem #1, I talked about setting up camera movement using Lemur’s cool InputMapper class.
In this gem, I provide a simpler more self-contained example to show how simple button to method mapping can be accomplished. The example I use is a state to toggle the camera movement state on and off. This will be useful for future gems where mouse input will be necessary.
InputMapper has an addDelegate() method that can be used to directly wire an input function to a method of an object.
For this example, I’ve made the whole state self-contained to avoid confusion. This is about as simple as it gets. It may not always be the best practice but it’s convenient.
I think the code probably speaks for itself in this case:
http://code.google.com/p/jmonkeyplatform-contributions/source/browse/trunk/Lemur/examples/LemurGems/src/gems/CameraToggleState.java
[java]
public class CameraToggleState extends BaseAppState {
public static final FunctionId F_CAMERA_TOGGLE = new FunctionId("Camera Toggle");
private InputMapper inputMapper;
public CameraToggleState() {
}
public void toggleCamera() {
CameraMovementState state = getState(CameraMovementState.class);
state.setEnabled(!state.isEnabled());
}
@Override
protected void initialize(Application app) {
// Setup the input mappings configured in the constructor
inputMapper = GuiGlobals.getInstance().getInputMapper();
// Map the space bar to our function
inputMapper.map(F_CAMERA_TOGGLE, KeyInput.KEY_SPACE);
}
@Override
protected void cleanup(Application app) {
}
@Override
protected void enable() {
// Register ourselves to be called when the function is triggered
inputMapper.addDelegate(F_CAMERA_TOGGLE, this, "toggleCamera");
}
@Override
protected void disable() {
inputMapper.removeDelegate(F_CAMERA_TOGGLE, this, "toggleCamera");
}
}
[/java]
Just attach this state to the app and it will toggle the enabled state of the CameraMovementState whenever the player presses the space bar.
The main demo has been adjusted to include this: http://code.google.com/p/jmonkeyplatform-contributions/source/browse/trunk/Lemur/examples/LemurGems/src/gems/Main.java