MouseClickInput

Hi, I needed a method to check the mouse for clicks. When using isMousePressed(btn) on the update method it usually gets invoked several times before you get the chance to actually release the mouse. So, using a swing like behaviour I wrote a class that detects both button pressed and released.



Now, being a newbie, this class probably is bad written and better implemented somewhere else. So help is welcome!



The class:



import com.jme.input.MouseInput;

public class MouseClickInput extends MouseInput {

    private MouseInput mi;

    private boolean[] buttonDown;

    private int count;

    public MouseClickInput(MouseInput mi) {
        this.mi = mi;

        count = getButtonCount();

        buttonDown = new boolean[count];
    }

    protected void destroy() {
        // something here?
    }

    public int getButtonIndex(String buttonName) {
        return mi.getButtonIndex(buttonName);
    }

    public boolean isButtonDown(int btn) {
        return mi.isButtonDown(btn);
    }

    public boolean isButtonClick(int btn) {
        if (mi.isButtonDown(btn)) {
            buttonDown[btn] = true;
            return false;
        }
        else {
            if (buttonDown[btn]) {
                buttonDown[btn] = false;
                return true;
            }
            else {
                return false;
            }
        }
    }

    public String getButtonName(int buttonIndex) {
        return mi.getButtonName(buttonIndex);
    }

    public boolean isCreated() {
        return mi.isCreated();
    }

    public int getWheelDelta() {
        return mi.getWheelDelta();
    }

    public int getXDelta() {
        return mi.getXDelta();
    }

    public int getYDelta() {
        return mi.getYDelta();
    }

    public int getXAbsolute() {
        return mi.getXAbsolute();
    }

    public int getYAbsolute() {
        return mi.getYAbsolute();
    }

    public void update() {
        mi.update();
    }

    public void setCursorVisible(boolean v) {
        mi.setCursorVisible(v);
    }

    public boolean isCursorVisible() {
        return mi.isCursorVisible();
    }

    public int getWheelRotation() {
        return mi.getWheelRotation();
    }

    public int getButtonCount() {
        return mi.getButtonCount();
    }
}



The use:


        // Create MouseClickInput
        mouseInput = new MouseClickInput(MouseInput.get());
        ...
        // use
        if (mouseInput.isButtonClick(0)) {
            ...
        }



jME already has the InputHandlers for these things. Have a look into the Wiki page about the input system on how to use it.

Oooops…



I used a MouseInputListener and solved it on three lines :oops:



Thanks!  :smiley: