user2757842
user2757842

Reputation: 639

GLUT mouse button down

I would like to zoom in on an object for as long as I hold the right mouse button down. The issue right now is that I have to click it every time I want to zoom. Is there a way I can modify my code so that it will zoom while I hold the button, rather than clicking it?

void mouse(int button, int state, int x, int y)
{
    // Save the left button state
    if (button == GLUT_LEFT_BUTTON)
    {
        leftMouseButtonDown = (state == GLUT_DOWN);
        zMovement += 0.1f;
    }

    else if (button == GLUT_RIGHT_BUTTON)
    {
        leftMouseButtonDown = (state == GLUT_DOWN);
        zMovement -= 0.1f;
    }

    // Save the mouse position
    mouseXPos = x;
    mouseYPos = y;
}

Upvotes: 3

Views: 10744

Answers (1)

BDL
BDL

Reputation: 22166

The state variable of your function tells you what type of mouse-button event had happened: It can either be GLUT_DOWN or GLUT_UP.

Knowing this, you can store this state in an extra variable outside of the mouse function and zoom as long as the state is set to true (this has to be done somewhere in every frame). The code could look like the following:

void mouse(int button, int state, int x, int y)
{
    // Save the left button state
    if (button == GLUT_LEFT_BUTTON)
    {
        leftMouseButtonDown = (state == GLUT_DOWN);
    }

    else if (button == GLUT_RIGHT_BUTTON)
    {
        // \/ right MouseButton
        rightMouseButtonDown = (state == GLUT_DOWN);
    }

    // Save the mouse position
    mouseXPos = x;
    mouseYPos = y;
}

void callThisInEveryFrame()
{
    if (leftMouseButtonDown)
        zMovement += 0.1f;
    if (rightMouseButtonDown)
        zMovement -= 0.1f;
}

Upvotes: 2

Related Questions