0
我想从头开始创建一个复选框,我遇到了一些问题。opengl鼠标交互复选框
在我control.h文件I初始化
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语句使用大括号,否则很容易运行您不期望的代码。 – megadan 2014-10-03 22:22:12
从你的代码中,'Control check;'永远不会被实例化,只存在于你的mouseControl函数的作用域中。 – TheBlindSpring 2014-10-16 16:43:47