From deb7aa29cdeda8742e47b56dac94d794f2ea7fc0 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 4 Jul 2018 13:24:16 +0200 Subject: [PATCH] OSX: Various tweaks to imgui_impl_osx courtesy of @warrenm + fix NewFrame position which has been moved recently master. (#1873) --- examples/example_osx_opengl2/main.mm | 1 + examples/imgui_impl_osx.h | 8 ++++---- examples/imgui_impl_osx.mm | 26 +++++++++++++------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/examples/example_osx_opengl2/main.mm b/examples/example_osx_opengl2/main.mm index a5b89bf75b26..710ffe243d74 100644 --- a/examples/example_osx_opengl2/main.mm +++ b/examples/example_osx_opengl2/main.mm @@ -42,6 +42,7 @@ -(void)updateAndDrawDemoView { ImGui_ImplOpenGL2_NewFrame(); ImGui_ImplOSX_NewFrame(self); + ImGui::NewFrame(); // Global data for the demo static bool show_demo_window = true; diff --git a/examples/imgui_impl_osx.h b/examples/imgui_impl_osx.h index 0626371dc3b2..bbe72fc97a43 100644 --- a/examples/imgui_impl_osx.h +++ b/examples/imgui_impl_osx.h @@ -1,11 +1,11 @@ // ImGui Platform Binding for: OSX / Cocoa -// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan..) +// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan, Metal..) @class NSEvent; -@class NSOpenGLView; +@class NSVew; // FIXME-OSX: Try replacing with NSView IMGUI_API bool ImGui_ImplOSX_Init(); IMGUI_API void ImGui_ImplOSX_Shutdown(); -IMGUI_API void ImGui_ImplOSX_NewFrame(NSOpenGLView* view); -IMGUI_API bool ImGui_ImplOSX_HandleEvent(NSEvent* event); +IMGUI_API void ImGui_ImplOSX_NewFrame(NSView *_Nonnull view); +IMGUI_API bool ImGui_ImplOSX_HandleEvent(NSEvent *_Nonnull event, NSView *_Nullable view); diff --git a/examples/imgui_impl_osx.mm b/examples/imgui_impl_osx.mm index eba2b10829cc..d0b7efd2a0dc 100644 --- a/examples/imgui_impl_osx.mm +++ b/examples/imgui_impl_osx.mm @@ -1,5 +1,5 @@ // ImGui Platform Binding for: OSX / Cocoa -// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan..) +// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan, Metal..) // Issues: // [ ] Platform: Keys are all generally very broken. Best using [event keycode] and not [event characters].. @@ -85,7 +85,7 @@ void ImGui_ImplOSX_Shutdown() { } -void ImGui_ImplOSX_NewFrame(NSOpenGLView* view) +void ImGui_ImplOSX_NewFrame(NSView* view) { // Setup display size ImGuiIO& io = ImGui::GetIO(); @@ -99,14 +99,6 @@ void ImGui_ImplOSX_NewFrame(NSOpenGLView* view) clock_t current_time = clock(); io.DeltaTime = (double)(current_time - g_Time) / CLOCKS_PER_SEC; g_Time = current_time; - - NSWindow* main_window = [view window]; - NSPoint mouse_pos = [main_window mouseLocationOutsideOfEventStream]; - mouse_pos = [view convertPoint:mouse_pos fromView:nil]; - io.MousePos = ImVec2(mouse_pos.x, mouse_pos.y - 1); - - // 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. - ImGui::NewFrame(); } static int mapCharacterToKey(int c) @@ -129,11 +121,11 @@ static void resetKeys() io.KeysDown[n] = false; } -bool ImGui_ImplOSX_HandleEvent(NSEvent* event) +bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view) { ImGuiIO& io = ImGui::GetIO(); - if (event.type == NSEventTypeLeftMouseDown) + if (event.type == NSEventTypeLeftMouseDown || event.type == NSEventTypeRightMouseDown || event.type == NSEventTypeOtherMouseDown) { int button = (int)[event buttonNumber]; if (button >= 0 && button < IM_ARRAYSIZE(io.MouseDown)) @@ -141,7 +133,7 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event) return io.WantCaptureMouse; } - if (event.type == NSEventTypeLeftMouseUp) + if (event.type == NSEventTypeLeftMouseUp || event.type == NSEventTypeRightMouseUp || event.type == NSEventTypeOtherMouseUp) { int button = (int)[event buttonNumber]; if (button >= 0 && button < IM_ARRAYSIZE(io.MouseDown)) @@ -149,6 +141,14 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event) return io.WantCaptureMouse; } + if (event.type == NSEventTypeMouseMoved || event.type == NSEventTypeLeftMouseDragged) + { + NSPoint mousePoint = event.locationInWindow; + mousePoint = [view convertPoint:mousePoint fromView:nil]; + mousePoint = NSMakePoint(mousePoint.x, view.bounds.size.height - mousePoint.y); + io.MousePos = ImVec2(mousePoint.x, mousePoint.y); + } + if (event.type == NSEventTypeScrollWheel) { double wheel_dx = 0.0;