Backends: OSX: import the glfw workaround to avoid missing mouse clicks. (#3261)

This commit is contained in:
Nicolas Burrus 2020-05-25 08:27:17 +02:00 committed by ocornut
parent 39d17ca07f
commit 43f79aa210

View File

@ -14,6 +14,7 @@
// CHANGELOG // CHANGELOG
// (minor and older changes stripped away, please see git history for details) // (minor and older changes stripped away, please see git history for details)
// 2020-05-25: Inputs: Added a fix for missing trackpad clicks when done with "soft tap".
// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor. // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
// 2019-10-11: Inputs: Fix using Backspace key. // 2019-10-11: Inputs: Fix using Backspace key.
// 2019-07-21: Re-added clipboard handlers as they are not enabled by default in core imgui.cpp (reverted 2019-05-18 change). // 2019-07-21: Re-added clipboard handlers as they are not enabled by default in core imgui.cpp (reverted 2019-05-18 change).
@ -27,6 +28,8 @@
static CFAbsoluteTime g_Time = 0.0; static CFAbsoluteTime g_Time = 0.0;
static NSCursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {}; static NSCursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
static bool g_MouseCursorHidden = false; static bool g_MouseCursorHidden = false;
static bool g_MouseJustPressed[5] = { false, false, false, false, false };
static bool g_MouseDown[5] = { false, false, false, false, false };
// Undocumented methods for creating cursors. // Undocumented methods for creating cursors.
@interface NSCursor() @interface NSCursor()
@ -121,9 +124,17 @@ void ImGui_ImplOSX_Shutdown()
{ {
} }
static void ImGui_ImplOSX_UpdateMouseCursor() static void ImGui_ImplOSX_UpdateMouseCursorAndButtons()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown) && i < IM_ARRAYSIZE(g_MouseJustPressed); i++)
{
// 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[i] = g_MouseJustPressed[i] || g_MouseDown[i];
g_MouseJustPressed[i] = false;
}
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
return; return;
@ -167,7 +178,7 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
io.DeltaTime = current_time - g_Time; io.DeltaTime = current_time - g_Time;
g_Time = current_time; g_Time = current_time;
ImGui_ImplOSX_UpdateMouseCursor(); ImGui_ImplOSX_UpdateMouseCursorAndButtons();
} }
static int mapCharacterToKey(int c) static int mapCharacterToKey(int c)
@ -197,16 +208,19 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
if (event.type == NSEventTypeLeftMouseDown || event.type == NSEventTypeRightMouseDown || event.type == NSEventTypeOtherMouseDown) if (event.type == NSEventTypeLeftMouseDown || event.type == NSEventTypeRightMouseDown || event.type == NSEventTypeOtherMouseDown)
{ {
int button = (int)[event buttonNumber]; int button = (int)[event buttonNumber];
if (button >= 0 && button < IM_ARRAYSIZE(io.MouseDown)) if (button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed))
io.MouseDown[button] = true; {
g_MouseJustPressed[button] = true;
g_MouseDown[button] = true;
}
return io.WantCaptureMouse; return io.WantCaptureMouse;
} }
if (event.type == NSEventTypeLeftMouseUp || event.type == NSEventTypeRightMouseUp || event.type == NSEventTypeOtherMouseUp) if (event.type == NSEventTypeLeftMouseUp || event.type == NSEventTypeRightMouseUp || event.type == NSEventTypeOtherMouseUp)
{ {
int button = (int)[event buttonNumber]; int button = (int)[event buttonNumber];
if (button >= 0 && button < IM_ARRAYSIZE(io.MouseDown)) if (button >= 0 && button < IM_ARRAYSIZE(g_MouseDown))
io.MouseDown[button] = false; g_MouseDown[button] = false;
return io.WantCaptureMouse; return io.WantCaptureMouse;
} }