3

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;
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
user2757842
  • 651
  • 1
  • 11
  • 24

1 Answers1

2

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;
}
BDL
  • 21,052
  • 22
  • 49
  • 55
  • Ah its so obvious when you spell it out like that (thanks for the variable reminder too :P) This works great, thank you – user2757842 Dec 03 '14 at 16:08