Hi.
I have been using InputMapper quite successfully for a little while now, but I would like to map mouse clicks with it and I can’t work out how to do it. I have read Gems #1 and the javadoc regarding InputMapper.
Here are some code snippets:
InputNames.java
public class InputNames {
public static final String GROUP = "Player Controls";
public static final FunctionId F_PRINT_MESSAGE = new FunctionId(GROUP, "Print Message");
public static final FunctionId F_MOUSE_CLICK = new FunctionId(GROUP, "Left Mouse Click");
/**
* Initializes a default set of input mappings for game functions.
* These can be changed later without impact... or multiple input
* controls can be mapped to the same function.
*/
public static void initializeDefaultMappings( InputMapper inputMapper ) {
// Default key mappings
inputMapper.map(F_PRINT_MESSAGE, KeyInput.KEY_D);
inputMapper.map(F_MOUSE_CLICK, MouseInput.BUTTON_LEFT);
}
}
InputActions.java
public class InputActions implements StateFunctionListener, AnalogFunctionListener {
// if (state == InputState.Positive) {} // REGISTERS KEY_DOWN
// if (state == InputState.Off) {} // REGISTERS KEY_RELEASED
// if (state == InputState.Negative) {} // REGISTERS AN OPPOSITE "REGISTERED" ACTION.
public void valueChanged(FunctionId func, InputState state, double tpf) {
System.out.println("valueChanged() is being called.");
if(func == InputNames.F_PRINT_MESSAGE && state == InputState.Positive){
System.out.println("Message printed to console. tpf was: " + tpf);
}
}
public void valueActive(FunctionId func, double speed, double tpf) {
System.out.println("valueActive() is being called.");
if(func == InputNames.F_MOUSE_CLICK){ // || state == InputState.Positive){
System.out.println("Message printed to console. tpf was: " + tpf);
}
}
}
A private method called from simpleInitApp() … which sets up the keys:
private void implementPicking() {
GuiGlobals.initialize(this);
InputMapper inputMapper = GuiGlobals.getInstance().getInputMapper();
InputActions inputActions = new InputActions();
InputNames.initializeDefaultMappings(inputMapper);
inputMapper.addStateListener(inputActions InputNames.F_PRINT_MESSAGE);
inputMapper.addAnalogListener(inputActions, InputNames.F_MOUSE_CLICK);
inputMapper.activateGroup(InputNames.GROUP);
}
So … that is how I’m trying to register the mouse click. Looking forward to comments.
Thanks in advance.