Game Engine Design  Input

Mouse Input

The Mouse class is similar to the Keyboard class. This time we will be implementing the MouseListener and MouseMotionListener interfaces. Our polling style functions will allow us to get the current position of the mouse, and check whether any of the three buttons are pressed. These functions are all rather strait forward, so they wont be described in any more detail.

We still have not handled input from the mouse wheel. This is a different type of event, so it was not included in the Mouse class. Instead, the MouseWheel class will be used. The polling interface here is getWheelRotation() which returns the total number of units the mouse has been rotated between the last two updates. Again, this is rather strait forward, with one exception. Because we want the difference in the mouse rotation, not a cumulative tally, the wheel rotation needs to be reset each update. To do this, update() is overridden. First the wheel count is reset, then we call EventQueues update as normal.

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * Handles mouse and mouse motion events.
 *
 * The last event is used to determine the mouse's location.
 *
 * @author Eric
 */
public class Mouse extends EventQueue<MouseEvent>
        implements MouseListener, MouseMotionListener
{
    private int x;
    private int y;
    private boolean leftButton;
    private boolean middleButton;
    private boolean rightButton;

    /**
     * Get the x position of the mouse.
     * @return the x position of the mouse.
     */
    public int getX()
    {
        return x;
    }

    /**
     * Get the y position of the mouse.
     * @return the y position of the mouse.
     */
    public int getY()
    {
        return y;
    }

    /**
     * Returns true if the left mouse button is currently pressed.
     * @return left mouse butting state.
     */
    public boolean isLeftButtonPressed()
    {
        return leftButton;
    }

    /**
     * Returns true if the middle mouse button is currently pressed.
     * @return middle mouse butting state.
     */
    public boolean isMiddleButtonPressed()
    {
        return middleButton;
    }

    /**
     * Returns true if the right mouse button is currently pressed.
     * @return right mouse butting state.
     */
    public boolean isRightButtonPressed()
    {
        return rightButton;
    }

    @Override
    protected void processEvent(MouseEvent event)
    {
        x = event.getX();
        y = event.getY();

        // if the mouse was pressed, update the correct button
        if(event.getID() == MouseEvent.MOUSE_PRESSED)
        {
            if(event.getButton() == MouseEvent.BUTTON1) leftButton = true;
            if(event.getButton() == MouseEvent.BUTTON2) middleButton = true;
            if(event.getButton() == MouseEvent.BUTTON3) rightButton = true;

        }

        // if the mouse was released, update the correct button
        if(event.getID() == MouseEvent.MOUSE_RELEASED)
        {
            if(event.getButton() == MouseEvent.BUTTON1) leftButton = false;
            if(event.getButton() == MouseEvent.BUTTON2) middleButton = false;
            if(event.getButton() == MouseEvent.BUTTON3) rightButton = false;
        }
    }

    public void mouseClicked(MouseEvent event)
    {
        addEvent(event);
    }

    public void mousePressed(MouseEvent event)
    {
        addEvent(event);
    }

    public void mouseReleased(MouseEvent event)
    {
        addEvent(event);
    }

    public void mouseEntered(MouseEvent event)
    {
        addEvent(event);
    }

    public void mouseExited(MouseEvent event)
    {
        addEvent(event);
    }

    public void mouseDragged(MouseEvent event)
    {
        addEvent(event);
    }

    public void mouseMoved(MouseEvent event)
    {
        addEvent(event);
    }
}
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

/**
 * Handles wheel rotation events.
 *
 * @author Eric
 */
public class MouseWheel extends EventQueue<MouseWheelEvent>
        implements MouseWheelListener
{
    private int wheelRotation = 0;

    /**
     * Returns the total wheel rotation between the last two updates.
     *
     * Negative rotation is away from the user, positive is towards
     * @return
     */
    public int getWheelRotation()
    {
        return wheelRotation;
    }

    @Override
    public void update()
    {
        // reset the wheel rotation
        wheelRotation = 0;
        super.update();
    }

    @Override
    public void processEvent(MouseWheelEvent event)
    {
        // increment the wheel rotation
        wheelRotation += event.getWheelRotation();
    }

    public void mouseWheelMoved(MouseWheelEvent event)
    {
        addEvent(event);
    }
}
Leave a comment ?

0 Comments.

Leave a Comment

* Copy this password:

* Type or paste password here:

976 Spam Comments Blocked so far by Spam Free Wordpress


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Performance Optimization WordPress Plugins by W3 EDGE