Skip to content

Commit

Permalink
Examples: Mouse cursor handling comments/tweaks to homogenize. (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Feb 20, 2018
1 parent d534207 commit daddd09
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions examples/opengl2_example/imgui_impl_glfw_gl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,16 @@ void ImGui_ImplGlfwGL2_NewFrame()
g_MouseJustPressed[i] = false;
}

// Update mouse cursor
// Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
else
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetCursor(g_Window, g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}

// Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
Expand Down
4 changes: 2 additions & 2 deletions examples/opengl3_example/imgui_impl_glfw_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,16 @@ void ImGui_ImplGlfwGL3_NewFrame()
g_MouseJustPressed[i] = false;
}

// Update mouse cursor
// Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
else
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetCursor(g_Window, g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}

// Gamepad navigation mapping [BETA]
Expand Down
29 changes: 16 additions & 13 deletions examples/sdl_opengl2_example/imgui_impl_sdl_gl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
static Uint64 g_Time = 0;
static bool g_MousePressed[3] = { false, false, false };
static GLuint g_FontTexture = 0;
static SDL_Cursor* g_SdlCursors[ImGuiMouseCursor_Count_] = { 0 };
static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_Count_] = { 0 };

// OpenGL2 Render function.
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
Expand Down Expand Up @@ -252,13 +252,13 @@ bool ImGui_ImplSdlGL2_Init(SDL_Window* window)
io.GetClipboardTextFn = ImGui_ImplSdlGL2_GetClipboardText;
io.ClipboardUserData = NULL;

g_SdlCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
g_SdlCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
g_SdlCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
g_SdlCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
g_SdlCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
g_SdlCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
g_SdlCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);

#ifdef _WIN32
SDL_SysWMinfo wmInfo;
Expand All @@ -274,10 +274,13 @@ bool ImGui_ImplSdlGL2_Init(SDL_Window* window)

void ImGui_ImplSdlGL2_Shutdown()
{
ImGui_ImplSdlGL2_InvalidateDeviceObjects();

// Destroy SDL mouse cursors
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_Count_; cursor_n++)
SDL_FreeCursor(g_SdlCursors[cursor_n]);
SDL_FreeCursor(g_MouseCursors[cursor_n]);
memset(g_MouseCursors, 0, sizeof(g_MouseCursors));

// Destroy OpenGL objects
ImGui_ImplSdlGL2_InvalidateDeviceObjects();
}

void ImGui_ImplSdlGL2_NewFrame(SDL_Window *window)
Expand Down Expand Up @@ -326,15 +329,15 @@ void ImGui_ImplSdlGL2_NewFrame(SDL_Window *window)
io.MousePos = ImVec2((float)mx, (float)my);
#endif

// Hide OS mouse cursor if ImGui is drawing it
// Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
{
SDL_ShowCursor(0);
}
else
{
SDL_SetCursor(g_SdlCursors[cursor]);
SDL_SetCursor(g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
SDL_ShowCursor(1);
}

Expand Down
29 changes: 16 additions & 13 deletions examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
static int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0;
static unsigned int g_VboHandle = 0, g_VaoHandle = 0, g_ElementsHandle = 0;
static SDL_Cursor* g_SdlCursors[ImGuiMouseCursor_Count_] = { 0 };
static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_Count_] = { 0 };

// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
Expand Down Expand Up @@ -366,13 +366,13 @@ bool ImGui_ImplSdlGL3_Init(SDL_Window* window)
io.GetClipboardTextFn = ImGui_ImplSdlGL3_GetClipboardText;
io.ClipboardUserData = NULL;

g_SdlCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
g_SdlCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
g_SdlCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
g_SdlCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
g_SdlCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
g_SdlCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
g_SdlCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);

#ifdef _WIN32
SDL_SysWMinfo wmInfo;
Expand All @@ -388,10 +388,13 @@ bool ImGui_ImplSdlGL3_Init(SDL_Window* window)

void ImGui_ImplSdlGL3_Shutdown()
{
ImGui_ImplSdlGL3_InvalidateDeviceObjects();

// Destroy SDL mouse cursors
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_Count_; cursor_n++)
SDL_FreeCursor(g_SdlCursors[cursor_n]);
SDL_FreeCursor(g_MouseCursors[cursor_n]);
memset(g_MouseCursors, 0, sizeof(g_MouseCursors));

// Destroy OpenGL objects
ImGui_ImplSdlGL3_InvalidateDeviceObjects();
}

void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window)
Expand Down Expand Up @@ -440,15 +443,15 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window)
io.MousePos = ImVec2((float)mx, (float)my);
#endif

// Hide OS mouse cursor if ImGui is drawing it
// Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
{
SDL_ShowCursor(0);
}
else
{
SDL_SetCursor(g_SdlCursors[cursor]);
SDL_SetCursor(g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
SDL_ShowCursor(1);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/vulkan_example/imgui_impl_glfw_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,16 +864,16 @@ void ImGui_ImplGlfwVulkan_NewFrame()
g_MouseJustPressed[i] = false;
}

// Update mouse cursor
// Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
else
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetCursor(g_Window, g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}

// Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
Expand Down

0 comments on commit daddd09

Please sign in to comment.