Skip to content

Commit

Permalink
Examples: Simplified mouse wheel handling. (#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Jan 20, 2018
1 parent 7dea158 commit 7e7c017
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 47 deletions.
8 changes: 2 additions & 6 deletions examples/marmalade_example/imgui_impl_marmalade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// Data
static double g_Time = 0.0f;
static bool g_MousePressed[3] = { false, false, false };
static float g_MouseWheel = 0.0f;
static CIwTexture* g_FontTexture = NULL;
static char* g_ClipboardText = NULL;
static bool g_osdKeyboardEnabled = false;
Expand Down Expand Up @@ -128,9 +127,9 @@ int32 ImGui_Marmalade_PointerButtonEventCallback(void* SystemData, void* pUserDa
if (pEvent->m_Button == S3E_POINTER_BUTTON_MIDDLEMOUSE)
g_MousePressed[2] = true;
if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELUP)
g_MouseWheel += pEvent->m_y;
io.MouseWheel += pEvent->m_y;
if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELDOWN)
g_MouseWheel += pEvent->m_y;
io.MouseWheel += pEvent->m_y;
}

return 0;
Expand Down Expand Up @@ -282,9 +281,6 @@ void ImGui_Marmalade_NewFrame()
g_MousePressed[i] = false;
}

io.MouseWheel = g_MouseWheel;
g_MouseWheel = 0.0f;

// TODO: Hide OS mouse cursor if ImGui is drawing it
// s3ePointerSetInt(S3E_POINTER_HIDE_CURSOR,(io.MouseDrawCursor ? 0 : 1));

Expand Down
10 changes: 3 additions & 7 deletions examples/opengl2_example/imgui_impl_glfw_gl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
static GLFWwindow* g_Window = NULL;
static double g_Time = 0.0f;
static bool g_MouseJustPressed[3] = { false, false, false };
static ImVec2 g_MouseWheel = ImVec2(0.0f, 0.0f);
static GLuint g_FontTexture = 0;

// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
Expand Down Expand Up @@ -136,8 +135,9 @@ void ImGui_ImplGlfwGL2_MouseButtonCallback(GLFWwindow*, int button, int action,

void ImGui_ImplGlfwGL2_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
{
g_MouseWheel.x += (float)xoffset; // Use fractional mouse wheel.
g_MouseWheel.y += (float)yoffset;
ImGuiIO& io = ImGui::GetIO();
io.MouseWheelH += (float)xoffset;
io.MouseWheel += (float)yoffset;
}

void ImGui_ImplGlfwGL2_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
Expand Down Expand Up @@ -296,10 +296,6 @@ void ImGui_ImplGlfwGL2_NewFrame()
g_MouseJustPressed[i] = false;
}

io.MouseWheel = g_MouseWheel.y;
io.MouseWheelH = g_MouseWheel.x;
g_MouseWheel.x = g_MouseWheel.x = 0.0f;

// Hide OS mouse cursor if ImGui is drawing it
glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);

Expand Down
10 changes: 3 additions & 7 deletions examples/opengl3_example/imgui_impl_glfw_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
static GLFWwindow* g_Window = NULL;
static double g_Time = 0.0f;
static bool g_MouseJustPressed[3] = { false, false, false };
static ImVec2 g_MouseWheel = ImVec2(0.0f, 0.0f);
static GLuint g_FontTexture = 0;
static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
Expand Down Expand Up @@ -157,8 +156,9 @@ void ImGui_ImplGlfwGL3_MouseButtonCallback(GLFWwindow*, int button, int action,

void ImGui_ImplGlfwGL3_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
{
g_MouseWheel.x += (float)xoffset; // Use fractional mouse wheel.
g_MouseWheel.y += (float)yoffset;
ImGuiIO& io = ImGui::GetIO();
io.MouseWheelH += (float)xoffset;
io.MouseWheel += (float)yoffset;
}

void ImGui_ImplGlfwGL3_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
Expand Down Expand Up @@ -408,10 +408,6 @@ void ImGui_ImplGlfwGL3_NewFrame()
g_MouseJustPressed[i] = false;
}

io.MouseWheel = g_MouseWheel.y;
io.MouseWheelH = g_MouseWheel.x;
g_MouseWheel.x = g_MouseWheel.x = 0.0f;

// Hide OS mouse cursor if ImGui is drawing it
glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);

Expand Down
12 changes: 4 additions & 8 deletions examples/sdl_opengl2_example/imgui_impl_sdl_gl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
// Data
static double g_Time = 0.0f;
static bool g_MousePressed[3] = { false, false, false };
static ImVec2 g_MouseWheel = ImVec2(0.0f, 0.0f);
static GLuint g_FontTexture = 0;

// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
Expand Down Expand Up @@ -132,10 +131,10 @@ bool ImGui_ImplSdlGL2_ProcessEvent(SDL_Event* event)
{
case SDL_MOUSEWHEEL:
{
if (event->wheel.x > 0) g_MouseWheel.x = +1;
if (event->wheel.x < 0) g_MouseWheel.x = -1;
if (event->wheel.y > 0) g_MouseWheel.y = +1;
if (event->wheel.y < 0) g_MouseWheel.y = -1;
if (event->wheel.x > 0) io.MouseWheelH += 1;
if (event->wheel.x < 0) io.MouseWheelH -= 1;
if (event->wheel.y > 0) io.MouseWheel += 1;
if (event->wheel.y < 0) io.MouseWheel -= 1;
return true;
}
case SDL_MOUSEBUTTONDOWN:
Expand Down Expand Up @@ -274,13 +273,10 @@ void ImGui_ImplSdlGL2_NewFrame(SDL_Window *window)
int mx, my;
Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
io.MouseWheel = g_MouseWheel.y;
io.MouseWheelH = g_MouseWheel.x;
io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
g_MouseWheel.x = g_MouseWheel.x = 0.0f;

// We need to use SDL_CaptureMouse() to easily retrieve mouse coordinates outside of the client area. This is only supported from SDL 2.0.4 (released Jan 2016)
#if (SDL_MAJOR_VERSION >= 2) && (SDL_MINOR_VERSION >= 0) && (SDL_PATCHLEVEL >= 4)
Expand Down
12 changes: 4 additions & 8 deletions examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// Data
static double g_Time = 0.0f;
static bool g_MousePressed[3] = { false, false, false };
static ImVec2 g_MouseWheel = ImVec2(0.0f, 0.0f);
static GLuint g_FontTexture = 0;
static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
Expand Down Expand Up @@ -154,10 +153,10 @@ bool ImGui_ImplSdlGL3_ProcessEvent(SDL_Event* event)
{
case SDL_MOUSEWHEEL:
{
if (event->wheel.x > 0) g_MouseWheel.x = +1;
if (event->wheel.x < 0) g_MouseWheel.x = -1;
if (event->wheel.y > 0) g_MouseWheel.y = +1;
if (event->wheel.y < 0) g_MouseWheel.y = -1;
if (event->wheel.x > 0) io.MouseWheelH += 1;
if (event->wheel.x < 0) io.MouseWheelH -= 1;
if (event->wheel.y > 0) io.MouseWheel += 1;
if (event->wheel.y < 0) io.MouseWheel -= 1;
return true;
}
case SDL_MOUSEBUTTONDOWN:
Expand Down Expand Up @@ -385,13 +384,10 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window)
int mx, my;
Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
io.MouseWheel = g_MouseWheel.y;
io.MouseWheelH = g_MouseWheel.x;
io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
g_MouseWheel.x = g_MouseWheel.x = 0.0f;

// We need to use SDL_CaptureMouse() to easily retrieve mouse coordinates outside of the client area. This is only supported from SDL 2.0.4 (released Jan 2016)
#if (SDL_MAJOR_VERSION >= 2) && (SDL_MINOR_VERSION >= 0) && (SDL_PATCHLEVEL >= 4)
Expand Down
18 changes: 7 additions & 11 deletions examples/vulkan_example/imgui_impl_glfw_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
// GLFW Data
static GLFWwindow* g_Window = NULL;
static double g_Time = 0.0f;
static bool g_MousePressed[3] = { false, false, false };
static ImVec2 g_MouseWheel = ImVec2(0.0f, 0.0f);
static bool g_MouseJustPressed[3] = { false, false, false };

// Vulkan Data
static VkAllocationCallbacks* g_Allocator = NULL;
Expand Down Expand Up @@ -327,13 +326,14 @@ static void ImGui_ImplGlfwVulkan_SetClipboardText(void* user_data, const char* t
void ImGui_ImplGlfwVulkan_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
{
if (action == GLFW_PRESS && button >= 0 && button < 3)
g_MousePressed[button] = true;
g_MouseJustPressed[button] = true;
}

void ImGui_ImplGlfwVulkan_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
{
g_MouseWheel.x += (float)xoffset; // Use fractional mouse wheel.
g_MouseWheel.y += (float)yoffset;
ImGuiIO& io = ImGui::GetIO();
io.MouseWheelH += (float)xoffset;
io.MouseWheel += (float)yoffset;
}

void ImGui_ImplGlfwVulkan_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
Expand Down Expand Up @@ -822,14 +822,10 @@ void ImGui_ImplGlfwVulkan_NewFrame()

for (int i = 0; i < 3; i++)
{
io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
g_MousePressed[i] = false;
io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
g_MouseJustPressed[i] = false;
}

io.MouseWheel = g_MouseWheel.y;
io.MouseWheelH = g_MouseWheel.x;
g_MouseWheel.x = g_MouseWheel.x = 0.0f;

// Hide OS mouse cursor if ImGui is drawing it
glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);

Expand Down

0 comments on commit 7e7c017

Please sign in to comment.