Skip to content

Commit

Permalink
OSX: Various tweaks to imgui_impl_osx courtesy of @warrenm + fix NewF…
Browse files Browse the repository at this point in the history
…rame position which has been moved recently master. (#1873)
  • Loading branch information
ocornut committed Jul 4, 2018
1 parent 0d7e779 commit deb7aa2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/example_osx_opengl2/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions examples/imgui_impl_osx.h
Original file line number Diff line number Diff line change
@@ -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);
26 changes: 13 additions & 13 deletions examples/imgui_impl_osx.mm
Original file line number Diff line number Diff line change
@@ -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]..
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -129,26 +121,34 @@ 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))
io.MouseDown[button] = true;
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))
io.MouseDown[button] = false;
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;
Expand Down

0 comments on commit deb7aa2

Please sign in to comment.