Browse Source

OSX: Various tweaks to imgui_impl_osx courtesy of @warrenm + fix NewFrame position which has been moved recently master. (#1873)

omar 7 years ago
parent
commit
deb7aa29cd
3 changed files with 18 additions and 17 deletions
  1. 1 0
      examples/example_osx_opengl2/main.mm
  2. 4 4
      examples/imgui_impl_osx.h
  3. 13 13
      examples/imgui_impl_osx.mm

+ 1 - 0
examples/example_osx_opengl2/main.mm

@@ -42,6 +42,7 @@
 {
 {
 	ImGui_ImplOpenGL2_NewFrame();
 	ImGui_ImplOpenGL2_NewFrame();
 	ImGui_ImplOSX_NewFrame(self);
 	ImGui_ImplOSX_NewFrame(self);
+    ImGui::NewFrame();
 
 
     // Global data for the demo
     // Global data for the demo
     static bool show_demo_window = true;
     static bool show_demo_window = true;

+ 4 - 4
examples/imgui_impl_osx.h

@@ -1,11 +1,11 @@
 // ImGui Platform Binding for: OSX / Cocoa
 // 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 NSEvent;
-@class NSOpenGLView;
+@class NSVew;
 
 
 // FIXME-OSX: Try replacing with NSView
 // FIXME-OSX: Try replacing with NSView
 IMGUI_API bool        ImGui_ImplOSX_Init();
 IMGUI_API bool        ImGui_ImplOSX_Init();
 IMGUI_API void        ImGui_ImplOSX_Shutdown();
 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);

+ 13 - 13
examples/imgui_impl_osx.mm

@@ -1,5 +1,5 @@
 // ImGui Platform Binding for: OSX / Cocoa
 // 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:
 // Issues:
 // [ ] Platform: Keys are all generally very broken. Best using [event keycode] and not [event characters]..
 // [ ] 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
     // Setup display size
     ImGuiIO& io = ImGui::GetIO();
     ImGuiIO& io = ImGui::GetIO();
@@ -99,14 +99,6 @@ void ImGui_ImplOSX_NewFrame(NSOpenGLView* view)
     clock_t current_time = clock();
     clock_t current_time = clock();
     io.DeltaTime = (double)(current_time - g_Time) / CLOCKS_PER_SEC;
     io.DeltaTime = (double)(current_time - g_Time) / CLOCKS_PER_SEC;
     g_Time = current_time;
     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)
 static int mapCharacterToKey(int c)
@@ -129,11 +121,11 @@ static void resetKeys()
         io.KeysDown[n] = false;
         io.KeysDown[n] = false;
 }
 }
 
 
-bool ImGui_ImplOSX_HandleEvent(NSEvent* event)
+bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
 {
 {
     ImGuiIO& io = ImGui::GetIO();
     ImGuiIO& io = ImGui::GetIO();
 
 
-    if (event.type == NSEventTypeLeftMouseDown)
+    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(io.MouseDown))
@@ -141,7 +133,7 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event)
         return io.WantCaptureMouse;
         return io.WantCaptureMouse;
     }
     }
 
 
-    if (event.type == NSEventTypeLeftMouseUp)
+    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(io.MouseDown))
@@ -149,6 +141,14 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event)
         return io.WantCaptureMouse;
         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)
     if (event.type == NSEventTypeScrollWheel)
     {
     {
         double wheel_dx = 0.0;
         double wheel_dx = 0.0;