GLUT鼠标按下 [英] GLUT mouse button down

查看:122
本文介绍了GLUT鼠标按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要按住鼠标右键,我就可以放大对象.现在的问题是,每次我要缩放时都必须单击它.有什么方法可以修改代码,以便在按住按钮而不是单击的同时缩放代码?

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;
}

推荐答案

函数的状态变量告诉您​​发生了哪种类型的鼠标按钮事件:可以是GLUT_DOWN或GLUT_UP.

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.

知道这一点后,您可以将该状态存储在鼠标功能之外的其他变量中,并进行缩放,只要状态设置为true(必须在每一帧的某处进行此操作)即可.该代码可能如下所示:

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;
}

这篇关于GLUT鼠标按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆