Imgui处理glfw的鼠标键盘的方法

在Imgui初始化时,会重新接手glfw的键盘鼠标事件。也就是遇到glfw的键盘鼠标事件时,imgui先会运行自己的处理过程,然后再去处理用户自己注册的glfw的键盘鼠标事件。

看imgui_impl_glfw.cpp源码的安装回调函数部分代码

cpp 复制代码
void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
{
    ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
    IM_ASSERT(bd->InstalledCallbacks == false && "Callbacks already installed!");
    IM_ASSERT(bd->Window == window);

    bd->PrevUserCallbackWindowFocus = glfwSetWindowFocusCallback(window, ImGui_ImplGlfw_WindowFocusCallback);
    bd->PrevUserCallbackCursorEnter = glfwSetCursorEnterCallback(window, ImGui_ImplGlfw_CursorEnterCallback);
    bd->PrevUserCallbackCursorPos = glfwSetCursorPosCallback(window, ImGui_ImplGlfw_CursorPosCallback);
    bd->PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
    bd->PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
    bd->PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
    bd->PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
    bd->PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback);
    bd->InstalledCallbacks = true;
}

看到了吗,Imgui会用一个类似PrevUserCallbackMousebutton这样的函数记录glfwSetMouseButtonCallback之前绑定的函数

glfwSetMouseButtonCallback会返回之前已经绑定的函数。

(从这个意义上说,在Imgui初始化之前,就需要先绑定用户自己的鼠标键盘事件函数)

然后再看Imgui接手的函数

cpp 复制代码
void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
    ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
    if (bd->PrevUserCallbackMousebutton != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
        bd->PrevUserCallbackMousebutton(window, button, action, mods);

    ImGui_ImplGlfw_UpdateKeyModifiers(window);

    ImGuiIO& io = ImGui::GetIO();
    if (button >= 0 && button < ImGuiMouseButton_COUNT)
        io.AddMouseButtonEvent(button, action == GLFW_PRESS);
}

看到了吗,当发生鼠标键盘事件时,会先调用用户之前注册的事件处理方法,然后再是Imgui自己的处理方法。

因此在写自己的键盘鼠标事件函数时,不需要考虑去调用Imgui的事件处理函数, 只需要专心写自己的函数即可。

当用户在于Imgui交互时,因为也会触发自己的键盘鼠标事件,这个时候可以在自己的事件处理函数中忽略这个交互

cpp 复制代码
    ImGuiIO& io = ImGui::GetIO();
    
    if (io.WantCaptureMouse) return; 

	/* 
	*  后面是自己的事件处理过程
	*/
相关推荐
郝学胜-神的一滴5 天前
[简化版 GAMES 101] 计算机图形学 13:从光栅化到着色——赋予三维像素光影灵魂
c++·计算机视觉·unity·godot·图形渲染·opengl·unreal
Strugglingler6 天前
【Qt,OpenGL, RHI,Wayland 等概念梳理】
qt·opengl·wayland·rhi·x11·egl·glx
郝学胜-神的一滴9 天前
中级OpenGL教程 009:用环境光告别模型死黑
前端·c++·unity·godot·图形渲染·opengl·unreal
郝学胜-神的一滴12 天前
[简化版 GAMES 101] 计算机图形学 12:可见性与 Z‑Buffer 深度缓存
unity·godot·图形渲染·three.js·opengl·unreal
郝学胜-神的一滴16 天前
中级OpenGL教程 008:精准控制高光光斑大小与强度
c++·unity·godot·three.js·图形学·opengl·unreal
cjzcjl16 天前
在Android中使用opengl 片元shader实现在录像过程对人脸进行识别并自动打马赛
opengl·马赛克·人脸·人脸追踪·执法仪
tangchao340勤奋的老年?17 天前
C++ OpenGL显示地图
c++·opengl
郝学胜-神的一滴19 天前
[简化版 GAMES 101] 计算机图形学 11:频域·卷积·抗锯齿
c++·unity·图形渲染·opengl·three·unreal
肥or胖21 天前
Qt中OpenGL快速入门
qt·音视频·opengl
郝学胜-神的一滴23 天前
中级OpenGL教程 007:解决背面光照异常高光问题
c++·unity·游戏引擎·three.js·opengl·unreal