added support for smooth trackpad scrolling on macos

This commit is contained in:
David Jeske 2013-08-09 23:20:49 -07:00
parent 5037836c08
commit 2c29df6296
3 changed files with 83 additions and 7 deletions

View file

@ -85,10 +85,26 @@ namespace OpenTK.Platform.MacOS.Carbon
static void ConnectEvents()
{
EventTypeSpec[] eventTypes = new EventTypeSpec[] { new EventTypeSpec(EventClass.Application, AppEventKind.AppActivated), new EventTypeSpec(EventClass.Application, AppEventKind.AppDeactivated), new EventTypeSpec(EventClass.Application, AppEventKind.AppQuit), new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDown), new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseUp), new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseMoved), new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDragged), new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseEntered), new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseExited), new EventTypeSpec(EventClass.Mouse, MouseEventKind.WheelMoved),
EventTypeSpec[] eventTypes = new EventTypeSpec[] {
new EventTypeSpec(EventClass.Application, AppEventKind.AppActivated),
new EventTypeSpec(EventClass.Application, AppEventKind.AppDeactivated),
new EventTypeSpec(EventClass.Application, AppEventKind.AppQuit),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDown),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseUp),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseMoved),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDragged),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseEntered),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseExited),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.WheelMoved),
new EventTypeSpec(EventClass.Mouse, MouseEventKind.WheelScroll),
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyDown), new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyRepeat), new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyUp), new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyModifiersChanged), new EventTypeSpec(EventClass.AppleEvent, AppleEventKind.AppleEvent) };
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyDown),
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyRepeat),
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyUp),
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyModifiersChanged),
new EventTypeSpec(EventClass.AppleEvent, AppleEventKind.AppleEvent),
};
MacOSEventHandler handler = EventHandler;
uppHandler = API.NewEventHandlerUPP(handler);

View file

@ -246,6 +246,7 @@ namespace OpenTK.Platform.MacOS.Carbon
MouseEntered = 8,
MouseExited = 9,
WheelMoved = 10,
WheelScroll = 11,
}
internal enum MouseButton : short
{
@ -286,8 +287,11 @@ namespace OpenTK.Platform.MacOS.Carbon
WindowMouseLocation = 0x776d6f75, // typeHIPoint
MouseButton = 0x6d62746e, // typeMouseButton
ClickCount = 0x63636e74, // typeUInt32
MouseWheelAxis = 0x6d776178, // typeMouseWheelAxis
MouseWheelDelta = 0x6d77646c, // typeSInt32
MouseWheelAxis = 0x6d776178, // typeMouseWheelAxis 'mwax'
MouseWheelDelta = 0x6d77646c, // typeSInt32 'mwdl'
MouseWheelSmoothVerticalDelta = 0x73617879, // typeSInt32 'saxy'
MouseWheelSmoothHorizontalDelta = 0x73617878, // typeSInt32 'saxx'
MouseDelta = 0x6d647461, // typeHIPoint
// Keyboard events
@ -712,6 +716,52 @@ namespace OpenTK.Platform.MacOS.Carbon
return (MouseButton)button;
}
internal struct ScrollDelta {
internal float deltaX;
internal float deltaY;
}
static internal ScrollDelta GetEventWheelScroll(IntPtr inEvent)
{
ScrollDelta scrolldelta = new ScrollDelta();
Int32 delta;
unsafe
{
Int32* d = δ
OSStatus result;
// vertical scroll Delta in pixels
result = API.GetEventParameter(inEvent,
EventParamName.MouseWheelSmoothVerticalDelta, EventParamType.typeSInt32,
IntPtr.Zero, (uint)sizeof(int), IntPtr.Zero, (IntPtr)d);
if (result == OSStatus.EventParameterNotFound) {
// it's okay for it to be simply missing...
} else if (result != OSStatus.NoError) {
throw new MacOSException(result);
} else {
scrolldelta.deltaY = delta / 20.0f;
}
// horizontal scroll Delta in pixels
result = API.GetEventParameter(inEvent,
EventParamName.MouseWheelSmoothHorizontalDelta, EventParamType.typeSInt32,
IntPtr.Zero, (uint)sizeof(int), IntPtr.Zero, (IntPtr)d);
if (result == OSStatus.EventParameterNotFound) {
// it's okay for it to be simply missing...
} else if (result != OSStatus.NoError) {
throw new MacOSException(result);
} else {
scrolldelta.deltaY = delta / 20.0f;
}
}
return scrolldelta;
}
static internal int GetEventMouseWheelDelta(IntPtr inEvent)
{
int delta;

View file

@ -510,9 +510,19 @@ namespace OpenTK.Platform.MacOS
}
return OSStatus.NoError;
case MouseEventKind.WheelMoved:
float delta = API.GetEventMouseWheelDelta(inEvent);
InputDriver.Mouse[0].WheelPrecise += delta;
case MouseEventKind.WheelMoved: // older, integer resolution only
{
// this is really an int, we use a float to avoid clipping the wheel value
float delta = API.GetEventMouseWheelDelta (inEvent);
InputDriver.Mouse[0].WheelPrecise += delta;
}
return OSStatus.NoError;
case MouseEventKind.WheelScroll: // newer, more precise X and Y scroll
{
API.ScrollDelta delta = API.GetEventWheelScroll(inEvent);
InputDriver.Mouse[0].WheelPrecise += delta.deltaY;
}
return OSStatus.NoError;
case MouseEventKind.MouseMoved: