Lemur InputMapper >> Mapping a mouse click

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.

I have managed to get it working. It always seems to be the way … you post on the forum and then find the answer yourself. But I must admit … I WAS stuck.

The changes were these:

InputNames.java

inputMapper.map(F_MOUSE_CLICK, Button.MOUSE_BUTTON1);

implementPicking() method:

inputMapper.addStateListener(inputActions, InputNames.F_MOUSE_CLICK);

Cheers.

Glad you got it working. I’m just now getting back on the forum so I should be a bit more active now. I gave up trying to read the old one… still getting used to the new one.

Just some advice in case your code wasn’t just for testing… in general, I think you should try to use more descriptive functions that are logical rather than specific to some input. So, for example is the mouse button should be shooting then the function would be F_SHOOT and you might then map that to any number of inputs.

+1, just stumbled upon this, the thing was rather confusing - why not using the jMEs MouseButton class constants?

I think you mean MouseInput… and because an int carries no information. I can’t distinguish an int from a mouse button or a key input, etc… There are lots of KeyInputs so I used their int and everything else needs to be a real object.