|
23 | 23 |
|
24 | 24 | #include "imgui.h" |
25 | 25 | #include "ImGuiImplMacOS.hpp" |
26 | | -#include "backends/imgui_impl_osx.h" |
| 26 | +#include "../../ThirdParty/imgui_v1.85/imgui_impl_osx_v1.85.h" |
27 | 27 | #import <Cocoa/Cocoa.h> |
28 | 28 |
|
29 | | -ImGuiKey ImGui_ImplOSX_KeyCodeToImGuiKey(int key_code); |
30 | | - |
31 | 29 | namespace Diligent |
32 | 30 | { |
33 | 31 |
|
|
39 | 37 | ImGuiImplMacOS::ImGuiImplMacOS(const ImGuiDiligentCreateInfo& CI, void* _Nullable view) : |
40 | 38 | ImGuiImplDiligent{CI} |
41 | 39 | { |
42 | | - ImGui_ImplOSX_Init((NSView*)view); |
| 40 | + ImGui_ImplOSX_Init(); |
43 | 41 | ImGuiIO& io = ImGui::GetIO(); |
44 | 42 | io.BackendPlatformName = "Diligent-ImGuiImplMacOS"; |
45 | 43 |
|
|
63 | 61 | ImGuiImplDiligent::NewFrame(RenderSurfaceWidth, RenderSurfaceHeight, SurfacePreTransform); |
64 | 62 | } |
65 | 63 |
|
66 | | - |
67 | | -// Must only be called for a mouse event, otherwise an exception occurs |
68 | | -// (Note that NSEventTypeScrollWheel is considered "other input". Oddly enough an exception does not occur with it, but the value will sometimes be wrong!) |
69 | | -static ImGuiMouseSource GetMouseSource(NSEvent* event) |
70 | | -{ |
71 | | - switch (event.subtype) |
72 | | - { |
73 | | - case NSEventSubtypeTabletPoint: |
74 | | - return ImGuiMouseSource_Pen; |
75 | | - // macOS considers input from relative touch devices (like the trackpad or Apple Magic Mouse) to be touch input. |
76 | | - // This doesn't really make sense for Dear ImGui, which expects absolute touch devices only. |
77 | | - // There does not seem to be a simple way to disambiguate things here so we consider NSEventSubtypeTouch events to always come from mice. |
78 | | - // See https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW24 |
79 | | - //case NSEventSubtypeTouch: |
80 | | - // return ImGuiMouseSource_TouchScreen; |
81 | | - case NSEventSubtypeMouseEvent: |
82 | | - default: |
83 | | - return ImGuiMouseSource_Mouse; |
84 | | - } |
85 | | -} |
86 | | - |
87 | | -static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view) |
88 | | -{ |
89 | | - // Only process events from the window containing ImGui view |
90 | | - if (event.window != view.window) |
91 | | - return false; |
92 | | - ImGuiIO& io = ImGui::GetIO(); |
93 | | - |
94 | | - if (event.type == NSEventTypeLeftMouseDown || event.type == NSEventTypeRightMouseDown || event.type == NSEventTypeOtherMouseDown) |
95 | | - { |
96 | | - int button = (int)[event buttonNumber]; |
97 | | - if (button >= 0 && button < ImGuiMouseButton_COUNT) |
98 | | - { |
99 | | - io.AddMouseSourceEvent(GetMouseSource(event)); |
100 | | - io.AddMouseButtonEvent(button, true); |
101 | | - } |
102 | | - return io.WantCaptureMouse; |
103 | | - } |
104 | | - |
105 | | - if (event.type == NSEventTypeLeftMouseUp || event.type == NSEventTypeRightMouseUp || event.type == NSEventTypeOtherMouseUp) |
106 | | - { |
107 | | - int button = (int)[event buttonNumber]; |
108 | | - if (button >= 0 && button < ImGuiMouseButton_COUNT) |
109 | | - { |
110 | | - io.AddMouseSourceEvent(GetMouseSource(event)); |
111 | | - io.AddMouseButtonEvent(button, false); |
112 | | - } |
113 | | - return io.WantCaptureMouse; |
114 | | - } |
115 | | - |
116 | | - if (event.type == NSEventTypeMouseMoved || event.type == NSEventTypeLeftMouseDragged || event.type == NSEventTypeRightMouseDragged || event.type == NSEventTypeOtherMouseDragged) |
117 | | - { |
118 | | - NSPoint mousePoint = event.locationInWindow; |
119 | | - if (event.window == nil) |
120 | | - mousePoint = [[view window] convertPointFromScreen:mousePoint]; |
121 | | - mousePoint = [view convertPoint:mousePoint fromView:nil]; |
122 | | - if ([view isFlipped]) |
123 | | - mousePoint = NSMakePoint(mousePoint.x, mousePoint.y); |
124 | | - else |
125 | | - mousePoint = NSMakePoint(mousePoint.x, view.bounds.size.height - mousePoint.y); |
126 | | - io.AddMouseSourceEvent(GetMouseSource(event)); |
127 | | - io.AddMousePosEvent((float)mousePoint.x, (float)mousePoint.y); |
128 | | - return io.WantCaptureMouse; |
129 | | - } |
130 | | - |
131 | | - if (event.type == NSEventTypeScrollWheel) |
132 | | - { |
133 | | - // Ignore canceled events. |
134 | | - // |
135 | | - // From macOS 12.1, scrolling with two fingers and then decelerating |
136 | | - // by tapping two fingers results in two events appearing: |
137 | | - // |
138 | | - // 1. A scroll wheel NSEvent, with a phase == NSEventPhaseMayBegin, when the user taps |
139 | | - // two fingers to decelerate or stop the scroll events. |
140 | | - // |
141 | | - // 2. A scroll wheel NSEvent, with a phase == NSEventPhaseCancelled, when the user releases the |
142 | | - // two-finger tap. It is this event that sometimes contains large values for scrollingDeltaX and |
143 | | - // scrollingDeltaY. When these are added to the current x and y positions of the scrolling view, |
144 | | - // it appears to jump up or down. It can be observed in Preview, various JetBrains IDEs and here. |
145 | | - if (event.phase == NSEventPhaseCancelled) |
146 | | - return false; |
147 | | - |
148 | | - double wheel_dx = 0.0; |
149 | | - double wheel_dy = 0.0; |
150 | | - |
151 | | - #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 |
152 | | - if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) |
153 | | - { |
154 | | - wheel_dx = [event scrollingDeltaX]; |
155 | | - wheel_dy = [event scrollingDeltaY]; |
156 | | - if ([event hasPreciseScrollingDeltas]) |
157 | | - { |
158 | | - wheel_dx *= 0.01; |
159 | | - wheel_dy *= 0.01; |
160 | | - } |
161 | | - } |
162 | | - else |
163 | | - #endif // MAC_OS_X_VERSION_MAX_ALLOWED |
164 | | - { |
165 | | - wheel_dx = [event deltaX] * 0.1; |
166 | | - wheel_dy = [event deltaY] * 0.1; |
167 | | - } |
168 | | - if (wheel_dx != 0.0 || wheel_dy != 0.0) |
169 | | - io.AddMouseWheelEvent((float)wheel_dx, (float)wheel_dy); |
170 | | - |
171 | | - return io.WantCaptureMouse; |
172 | | - } |
173 | | - |
174 | | - if (event.type == NSEventTypeKeyDown || event.type == NSEventTypeKeyUp) |
175 | | - { |
176 | | - if ([event isARepeat]) |
177 | | - return io.WantCaptureKeyboard; |
178 | | - |
179 | | - int key_code = (int)[event keyCode]; |
180 | | - ImGuiKey key = ImGui_ImplOSX_KeyCodeToImGuiKey(key_code); |
181 | | - io.AddKeyEvent(key, event.type == NSEventTypeKeyDown); |
182 | | - io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code) |
183 | | - |
184 | | - return io.WantCaptureKeyboard; |
185 | | - } |
186 | | - |
187 | | - if (event.type == NSEventTypeFlagsChanged) |
188 | | - { |
189 | | - unsigned short key_code = [event keyCode]; |
190 | | - NSEventModifierFlags modifier_flags = [event modifierFlags]; |
191 | | - |
192 | | - io.AddKeyEvent(ImGuiMod_Shift, (modifier_flags & NSEventModifierFlagShift) != 0); |
193 | | - io.AddKeyEvent(ImGuiMod_Ctrl, (modifier_flags & NSEventModifierFlagControl) != 0); |
194 | | - io.AddKeyEvent(ImGuiMod_Alt, (modifier_flags & NSEventModifierFlagOption) != 0); |
195 | | - io.AddKeyEvent(ImGuiMod_Super, (modifier_flags & NSEventModifierFlagCommand) != 0); |
196 | | - |
197 | | - ImGuiKey key = ImGui_ImplOSX_KeyCodeToImGuiKey(key_code); |
198 | | - if (key != ImGuiKey_None) |
199 | | - { |
200 | | - // macOS does not generate down/up event for modifiers. We're trying |
201 | | - // to use hardware dependent masks to extract that information. |
202 | | - // 'imgui_mask' is left as a fallback. |
203 | | - NSEventModifierFlags mask = 0; |
204 | | - switch (key) |
205 | | - { |
206 | | - case ImGuiKey_LeftCtrl: mask = 0x0001; break; |
207 | | - case ImGuiKey_RightCtrl: mask = 0x2000; break; |
208 | | - case ImGuiKey_LeftShift: mask = 0x0002; break; |
209 | | - case ImGuiKey_RightShift: mask = 0x0004; break; |
210 | | - case ImGuiKey_LeftSuper: mask = 0x0008; break; |
211 | | - case ImGuiKey_RightSuper: mask = 0x0010; break; |
212 | | - case ImGuiKey_LeftAlt: mask = 0x0020; break; |
213 | | - case ImGuiKey_RightAlt: mask = 0x0040; break; |
214 | | - default: |
215 | | - return io.WantCaptureKeyboard; |
216 | | - } |
217 | | - io.AddKeyEvent(key, (modifier_flags & mask) != 0); |
218 | | - io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code) |
219 | | - } |
220 | | - |
221 | | - return io.WantCaptureKeyboard; |
222 | | - } |
223 | | - |
224 | | - return false; |
225 | | -} |
226 | | - |
227 | 64 | bool ImGuiImplMacOS::HandleOSXEvent(NSEvent *_Nonnull event, NSView *_Nonnull view) |
228 | 65 | { |
229 | 66 | std::lock_guard<std::mutex> Lock(m_Mtx); |
|
0 commit comments