Cleanups.
This commit is contained in:
parent
3798d268dc
commit
ed87814a7f
2 changed files with 119 additions and 110 deletions
|
@ -7,12 +7,12 @@ namespace OpenTK.Platform.MacOS
|
||||||
static class NSApplication
|
static class NSApplication
|
||||||
{
|
{
|
||||||
internal static IntPtr Handle;
|
internal static IntPtr Handle;
|
||||||
|
internal static IntPtr AutoreleasePool;
|
||||||
|
|
||||||
internal static void Initialize()
|
internal static void Initialize()
|
||||||
{
|
{
|
||||||
// Create the NSAutoreleasePool
|
// Create the NSAutoreleasePool
|
||||||
Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSAutoreleasePool"), Selector.Alloc),
|
AutoreleasePool = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSAutoreleasePool"), Selector.Alloc), Selector.Init);
|
||||||
Selector.Init);
|
|
||||||
|
|
||||||
// Fetch the application handle
|
// Fetch the application handle
|
||||||
Handle = Cocoa.SendIntPtr(Class.Get("NSApplication"), Selector.Get("sharedApplication"));
|
Handle = Cocoa.SendIntPtr(Class.Get("NSApplication"), Selector.Get("sharedApplication"));
|
||||||
|
@ -20,7 +20,6 @@ namespace OpenTK.Platform.MacOS
|
||||||
// Setup the application
|
// Setup the application
|
||||||
Cocoa.SendBool(Handle, Selector.Get("setActivationPolicy:"), (int)NSApplicationActivationPolicy.Regular);
|
Cocoa.SendBool(Handle, Selector.Get("setActivationPolicy:"), (int)NSApplicationActivationPolicy.Regular);
|
||||||
Cocoa.SendVoid(Handle, Selector.Get("activateIgnoringOtherApps:"), true);
|
Cocoa.SendVoid(Handle, Selector.Get("activateIgnoringOtherApps:"), true);
|
||||||
Cocoa.SendVoid(Handle, Selector.Get("finishLaunching"));
|
|
||||||
|
|
||||||
// Create the menu bar
|
// Create the menu bar
|
||||||
var menubar = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSMenu"), Selector.Alloc),
|
var menubar = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSMenu"), Selector.Alloc),
|
||||||
|
@ -32,6 +31,9 @@ namespace OpenTK.Platform.MacOS
|
||||||
// Add menu item to bar, and bar to application
|
// Add menu item to bar, and bar to application
|
||||||
Cocoa.SendIntPtr(menubar, Selector.Get("addItem:"), menuItem);
|
Cocoa.SendIntPtr(menubar, Selector.Get("addItem:"), menuItem);
|
||||||
Cocoa.SendIntPtr(Handle, Selector.Get("setMainMenu:"), menubar);
|
Cocoa.SendIntPtr(Handle, Selector.Get("setMainMenu:"), menubar);
|
||||||
|
|
||||||
|
// Tell cocoa we're ready to run the application (usually called by [NSApp run]).
|
||||||
|
Cocoa.SendVoid(Handle, Selector.Get("finishLaunching"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -266,6 +266,7 @@ namespace OpenTK.Platform.MacOS
|
||||||
if (trackingArea != IntPtr.Zero)
|
if (trackingArea != IntPtr.Zero)
|
||||||
{
|
{
|
||||||
Cocoa.SendVoid(owner, selRemoveTrackingArea, trackingArea);
|
Cocoa.SendVoid(owner, selRemoveTrackingArea, trackingArea);
|
||||||
|
Cocoa.SendVoid(trackingArea, Selector.Release);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ownerBounds = Cocoa.SendRect(owner, selBounds);
|
var ownerBounds = Cocoa.SendRect(owner, selBounds);
|
||||||
|
@ -329,132 +330,138 @@ namespace OpenTK.Platform.MacOS
|
||||||
|
|
||||||
public void ProcessEvents()
|
public void ProcessEvents()
|
||||||
{
|
{
|
||||||
var e = Cocoa.SendIntPtr(NSApplication.Handle, selNextEventMatchingMask, uint.MaxValue, IntPtr.Zero, NSDefaultRunLoopMode, true);
|
while (true)
|
||||||
|
|
||||||
if (e == IntPtr.Zero)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var type = (NSEventType)Cocoa.SendInt(e, selType);
|
|
||||||
switch (type)
|
|
||||||
{
|
{
|
||||||
case NSEventType.KeyDown:
|
var e = Cocoa.SendIntPtr(NSApplication.Handle, selNextEventMatchingMask, uint.MaxValue, IntPtr.Zero, NSDefaultRunLoopMode, true);
|
||||||
{
|
|
||||||
var keyCode = Cocoa.SendUshort(e, selKeyCode);
|
|
||||||
var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
|
|
||||||
var isARepeat = Cocoa.SendBool(e, selIsARepeat);
|
|
||||||
GetKey(keyCode, modifierFlags, keyArgs);
|
|
||||||
InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, true);
|
|
||||||
|
|
||||||
if (!isARepeat || InputDriver.Keyboard[0].KeyRepeat)
|
if (e == IntPtr.Zero)
|
||||||
{
|
|
||||||
KeyDown(this, keyArgs);
|
|
||||||
}
|
|
||||||
|
|
||||||
var s = Cocoa.FromNSString(Cocoa.SendIntPtr(e, selCharactersIgnoringModifiers));
|
|
||||||
foreach (var c in s)
|
|
||||||
{
|
|
||||||
int intVal = (int)c;
|
|
||||||
if (!Char.IsControl(c) && (intVal < 63232 || intVal > 63235))
|
|
||||||
{
|
|
||||||
// For some reason, arrow keys (mapped 63232-63235) are seen as non-control characters, so get rid of those.
|
|
||||||
|
|
||||||
keyPressArgs.KeyChar = c;
|
|
||||||
KeyPress(this, keyPressArgs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Steal all keydown events to avoid the annoying "bleep" sound.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
case NSEventType.KeyUp:
|
|
||||||
{
|
|
||||||
var keyCode = Cocoa.SendUshort(e, selKeyCode);
|
|
||||||
var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
|
|
||||||
|
|
||||||
GetKey(keyCode, modifierFlags, keyArgs);
|
|
||||||
InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, false);
|
|
||||||
|
|
||||||
KeyUp(this, keyArgs);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NSEventType.MouseEntered:
|
var type = (NSEventType)Cocoa.SendInt(e, selType);
|
||||||
{
|
switch (type)
|
||||||
var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea);
|
{
|
||||||
var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner);
|
case NSEventType.KeyDown:
|
||||||
if (trackingAreaOwner == windowInfo.ViewHandle)
|
|
||||||
{
|
{
|
||||||
if (!cursorVisible)
|
var keyCode = Cocoa.SendUshort(e, selKeyCode);
|
||||||
|
var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
|
||||||
|
var isARepeat = Cocoa.SendBool(e, selIsARepeat);
|
||||||
|
GetKey(keyCode, modifierFlags, keyArgs);
|
||||||
|
InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, true);
|
||||||
|
|
||||||
|
if (!isARepeat || InputDriver.Keyboard[0].KeyRepeat)
|
||||||
{
|
{
|
||||||
SetCursorVisible(false);
|
KeyDown(this, keyArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseEnter(this, EventArgs.Empty);
|
var s = Cocoa.FromNSString(Cocoa.SendIntPtr(e, selCharactersIgnoringModifiers));
|
||||||
}
|
foreach (var c in s)
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NSEventType.MouseExited:
|
|
||||||
{
|
|
||||||
var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea);
|
|
||||||
var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner);
|
|
||||||
if (trackingAreaOwner == windowInfo.ViewHandle)
|
|
||||||
{
|
|
||||||
if (!cursorVisible)
|
|
||||||
{
|
{
|
||||||
SetCursorVisible(true);
|
int intVal = (int)c;
|
||||||
|
if (!Char.IsControl(c) && (intVal < 63232 || intVal > 63235))
|
||||||
|
{
|
||||||
|
// For some reason, arrow keys (mapped 63232-63235) are seen as non-control characters, so get rid of those.
|
||||||
|
|
||||||
|
keyPressArgs.KeyChar = c;
|
||||||
|
KeyPress(this, keyPressArgs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseLeave(this, EventArgs.Empty);
|
// Steal all keydown events to avoid the annoying "bleep" sound.
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NSEventType.MouseMoved:
|
case NSEventType.KeyUp:
|
||||||
{
|
{
|
||||||
var pf = Cocoa.SendPoint(e, selLocationInWindowOwner);
|
var keyCode = Cocoa.SendUshort(e, selKeyCode);
|
||||||
var p = new Point((int)pf.X, (int)pf.Y);
|
var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
|
||||||
|
|
||||||
var s = ClientSize;
|
GetKey(keyCode, modifierFlags, keyArgs);
|
||||||
if (p.X < 0) p.X = 0;
|
InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, false);
|
||||||
if (p.Y < 0) p.Y = 0;
|
|
||||||
if (p.X > s.Width) p.X = s.Width;
|
|
||||||
if (p.Y > s.Height) p.Y = s.Height;
|
|
||||||
p.Y = s.Height - p.Y;
|
|
||||||
|
|
||||||
InputDriver.Mouse[0].Position = p;
|
KeyUp(this, keyArgs);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NSEventType.ScrollWheel:
|
case NSEventType.MouseEntered:
|
||||||
{
|
{
|
||||||
var scrollingDelta = Cocoa.SendFloat(e, selScrollingDeltaY);
|
var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea);
|
||||||
InputDriver.Mouse[0].WheelPrecise += scrollingDelta;
|
var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner);
|
||||||
}
|
if (trackingAreaOwner == windowInfo.ViewHandle)
|
||||||
break;
|
{
|
||||||
|
if (!cursorVisible)
|
||||||
|
{
|
||||||
|
SetCursorVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
case NSEventType.LeftMouseDown:
|
MouseEnter(this, EventArgs.Empty);
|
||||||
case NSEventType.RightMouseDown:
|
}
|
||||||
case NSEventType.OtherMouseDown:
|
}
|
||||||
{
|
break;
|
||||||
var buttonNumber = Cocoa.SendInt(e, selButtonNumber);
|
|
||||||
InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NSEventType.LeftMouseUp:
|
case NSEventType.MouseExited:
|
||||||
case NSEventType.RightMouseUp:
|
{
|
||||||
case NSEventType.OtherMouseUp:
|
var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea);
|
||||||
{
|
var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner);
|
||||||
var buttonNumber = Cocoa.SendInt(e, selButtonNumber);
|
if (trackingAreaOwner == windowInfo.ViewHandle)
|
||||||
InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = false;
|
{
|
||||||
}
|
if (!cursorVisible)
|
||||||
break;
|
{
|
||||||
|
SetCursorVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseLeave(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NSEventType.MouseMoved:
|
||||||
|
{
|
||||||
|
var pf = Cocoa.SendPoint(e, selLocationInWindowOwner);
|
||||||
|
var p = new Point((int)pf.X, (int)pf.Y);
|
||||||
|
|
||||||
|
var s = ClientSize;
|
||||||
|
if (p.X < 0)
|
||||||
|
p.X = 0;
|
||||||
|
if (p.Y < 0)
|
||||||
|
p.Y = 0;
|
||||||
|
if (p.X > s.Width)
|
||||||
|
p.X = s.Width;
|
||||||
|
if (p.Y > s.Height)
|
||||||
|
p.Y = s.Height;
|
||||||
|
p.Y = s.Height - p.Y;
|
||||||
|
|
||||||
|
InputDriver.Mouse[0].Position = p;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NSEventType.ScrollWheel:
|
||||||
|
{
|
||||||
|
var scrollingDelta = Cocoa.SendFloat(e, selScrollingDeltaY);
|
||||||
|
InputDriver.Mouse[0].WheelPrecise += scrollingDelta;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NSEventType.LeftMouseDown:
|
||||||
|
case NSEventType.RightMouseDown:
|
||||||
|
case NSEventType.OtherMouseDown:
|
||||||
|
{
|
||||||
|
var buttonNumber = Cocoa.SendInt(e, selButtonNumber);
|
||||||
|
InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NSEventType.LeftMouseUp:
|
||||||
|
case NSEventType.RightMouseUp:
|
||||||
|
case NSEventType.OtherMouseUp:
|
||||||
|
{
|
||||||
|
var buttonNumber = Cocoa.SendInt(e, selButtonNumber);
|
||||||
|
InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cocoa.SendVoid(NSApplication.Handle, selSendEvent, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
Cocoa.SendVoid(NSApplication.Handle, selSendEvent, e);
|
|
||||||
Cocoa.SendVoid(NSApplication.Handle, selUpdateWindows);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public System.Drawing.Point PointToClient(System.Drawing.Point point)
|
public System.Drawing.Point PointToClient(System.Drawing.Point point)
|
||||||
|
|
Loading…
Reference in a new issue