【问题标题】:opengl mouse interaction checkboxopengl 鼠标交互复选框
【发布时间】:2014-10-03 22:01:25
【问题描述】:

我正在尝试从头开始创建一个复选框,但遇到了一些问题。

在我的 control.h 文件中我初始化了

public : int checked = 0;

所以每当鼠标按下正确的区域时,checked 就会变成一个。 而drawCheckBox 方法会检查它是1 还是0,并在方框上打勾。 程序运行,但是当我按下方框区域并检查 checked 的值时,它一直显示 0。我不知道为什么。

绘制复选框/检查用户是否选中

// Function to generate food selection box in left selection area.
void Control::drawCheckBox(string food, double x1, double y1, double x2, double y2)
{
    glColor3f(0, 1, 1);
    //placement of the check box
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glRectf(x1, y1, x2,y2);

    // Draw black boundary.
    glColor3f(0.0, 0.0, 0.0);
    glLineWidth(5);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glRectf(x1, y1, x2, y2);


    if (checked == 1)checker(x1, y1, x2, y2); // checks the check box.
    else glColor3f(1, 1, 1);                        
    cout << checked;


}

鼠标回调例程

// The mouse callback routine.
void mouseControl(int button, int state, int x, int y)
{
    Control check;
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)

        // Store the clicked point in the points array after correcting
        // from event to OpenGL co-ordinates.
        //points.push_back(Point(x, height - y));

        if ((x >= 5 && x <= 10) && (y <= 85 && y >= 80))
        {
        if (check.checked == 1)
        {
            check.checked = check.checked - 1;
        }
        else check.checked++;
        }

    if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit(0);

        glutPostRedisplay();

}

【问题讨论】:

  • mouseControl() 是否只在鼠标状态改变时被调用,还是每帧都会被调用(即在用户按住鼠标时不断调用)。您可以考虑为所有 if 语句使用大括号,否则很容易运行您不期望的代码。
  • 从你的代码来看,Control check; 永远不会被实例化,只存在于你的 mouseControl 函数的范围内。

标签: c++ oop opengl graphics logic


【解决方案1】:

首先,您在 mouseControl() 函数中本地声明了一个
Control check;
的对象。
我假设您的Control 构造函数正在将Control::checked 状态初始化为0
因此,您总是会看到 Control::checked 的值是 0

您需要确定Control check 对象的范围和生命周期。 希望这将有助于解决问题。
否则,如果您发布更多代码,尤其是完整的 Control 类实现以及调用 mouseControl 的位置和方式,将会有所帮助。
然后我们可以弄清楚应该怎么做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2015-03-04
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多