所以我试图在我的 OpenGL 应用程序中捕获鼠标拖动。到目前为止我已经完成了以下操作:
glfwSetMouseButtonCallback(window, mouse_callback);
static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT) {
double x;
double y;
glfwGetCursorPos(window, &x, &y);
if (previous_y_position - y > 0)
{
camera_translation.y -= 1.0f;
previous_y_position = y;
}
else
{
camera_translation.y += 1.0f;
previous_y_position = y;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是,如果我想放大,我需要向上移动鼠标,然后重复单击。由于某种原因,如果我按下鼠标左键并向上拖动,它不会执行任何操作。
小智 6
在cursor_pos_callback中,只需确认按钮是否被按下,就可以了。
void mouse_cursor_callback( GLFWwindow * window, double xpos, double ypos)
{
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE)
{
return;
}
// `write your drag code here`
}
Run Code Online (Sandbox Code Playgroud)
mouse_callback是无国籍的。它接收事件、瞬时“动作”。您需要使您的程序“记住”鼠标按钮被按下。这样,当在第 1 帧中按下按钮时,您可以在该帧之后以及释放鼠标按钮之前的所有帧中引用此信息。
简单的方法是在按下/释放时翻转布尔标志:
static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if(GLFW_PRESS == action)
lbutton_down = true;
else if(GLFW_RELEASE == action)
lbutton_down = false;
}
if(lbutton_down) {
// do your drag here
}
}
Run Code Online (Sandbox Code Playgroud)
示意图:
state released pressed released
timeline -------------|------------------------------|---------------
^ ^
mouse_callback calls GLFW_PRESS GLFW_RELEASE
Run Code Online (Sandbox Code Playgroud)
困难的方法是使用状态机(特别是如果您需要更复杂的输入控制器状态组合)。
| 归档时间: |
|
| 查看次数: |
15947 次 |
| 最近记录: |