Lemur Gems #2 : InputMapper delegates


#1

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


Dynamic Style/NiftyInputEvent Problem
Toon shader effect for one geometry
#2

The next gem will be about general scene picking support.


#3

Hi, since not too many people gave their feedback: this is very useful! I also like the other gems. This sure will come in handy! Thanks!


#4
@shvn said: Hi, since not too many people gave their feedback: this is very useful! I also like the other gems. This sure will come in handy! Thanks!

Thanks for the kind words and you’re welcome.


#5

I know this is an old topic, but I think wroth it.

This is amazing! I tough Lemur was just UI, but it’s really a toolkit. Also this Delegate strategy is very flexible, the trade-off between usability and performance, by using Reflections, really worth it.

:+1:


#6

For cached Method objects, there is basically no performance penalty for reflection. It’s the equivalent of making two method calls instead of one but that is slightly offset by the fact that it doesn’t have to runtime resolve the method anymore.

…but the time taken just to call a method in Java is impossibly small to time accurately. Sub-nanosecond or something. So doubling that is nothing.

And to be clear, I’m literally talking about the difference between:
public void foo() {
// do the stuff
}

and…
public void foo() {
bar();
}
public void bar() {
// do the stuff
}