From f2eca2924f583186fa84d2cb3e14904b7a8c20b6 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Sun, 27 Apr 2014 09:21:33 +0200 Subject: [PATCH] [Mac] Inherit from NativeWindowBase This reduces code duplication significantly. --- .../Platform/MacOS/CocoaNativeWindow.cs | 198 ++++-------------- 1 file changed, 46 insertions(+), 152 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs index 33d53daf..dfdbbf92 100644 --- a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs +++ b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs @@ -1,4 +1,4 @@ -#region License +#region License // // CocoaNativeWindow.cs // @@ -38,27 +38,10 @@ using OpenTK.Input; namespace OpenTK.Platform.MacOS { - class CocoaNativeWindow : INativeWindow + class CocoaNativeWindow : NativeWindowBase { static int UniqueId; - public event EventHandler Move = delegate { }; - public event EventHandler Resize = delegate { }; - public event EventHandler Closing = delegate { }; - public event EventHandler Closed = delegate { }; - public event EventHandler Disposed = delegate { }; - public event EventHandler IconChanged = delegate { }; - public event EventHandler TitleChanged = delegate { }; - public event EventHandler VisibleChanged = delegate { }; - public event EventHandler FocusedChanged = delegate { }; - public event EventHandler WindowBorderChanged = delegate { }; - public event EventHandler WindowStateChanged = delegate { }; - public event EventHandler KeyDown = delegate { }; - public event EventHandler KeyPress = delegate { }; - public event EventHandler KeyUp = delegate { }; - public event EventHandler MouseLeave = delegate { }; - public event EventHandler MouseEnter = delegate { }; - static readonly IntPtr selNextEventMatchingMask = Selector.Get("nextEventMatchingMask:untilDate:inMode:dequeue:"); static readonly IntPtr selSendEvent = Selector.Get("sendEvent:"); //static readonly IntPtr selUpdateWindows = Selector.Get("updateWindows"); @@ -95,6 +78,8 @@ namespace OpenTK.Platform.MacOS static readonly IntPtr selHide = Selector.Get("hide"); static readonly IntPtr selUnhide = Selector.Get("unhide"); static readonly IntPtr selScrollingDeltaY = Selector.Get("scrollingDeltaY"); + static readonly IntPtr selDeltaX = Selector.Get("deltaX"); + static readonly IntPtr selDeltaY = Selector.Get("deltaY"); static readonly IntPtr selButtonNumber = Selector.Get("buttonNumber"); static readonly IntPtr selSetStyleMask = Selector.Get("setStyleMask:"); static readonly IntPtr selStyleMask = Selector.Get("styleMask"); @@ -145,7 +130,6 @@ namespace OpenTK.Platform.MacOS private bool exists; private bool cursorVisible = true; private System.Drawing.Icon icon; - private LegacyInputDriver inputDriver = new LegacyInputDriver(); private WindowBorder windowBorder = WindowBorder.Resizable; private Nullable deferredWindowBorder; private Nullable previousWindowBorder; @@ -377,7 +361,7 @@ namespace OpenTK.Platform.MacOS Cocoa.SendVoid(owner, selAddTrackingArea, trackingArea); } - public void Close() + public override void Close() { shouldClose = true; } @@ -409,7 +393,7 @@ namespace OpenTK.Platform.MacOS return (MouseButton)cocoaButtonIndex; } - public void ProcessEvents() + public override void ProcessEvents() { while (true) { @@ -500,16 +484,32 @@ namespace OpenTK.Platform.MacOS case NSEventType.OtherMouseDragged: case NSEventType.MouseMoved: { - var pf = Cocoa.SendPoint(e, selLocationInWindowOwner); + Point p = InputDriver.Mouse[0].Position; + if (CursorVisible) + { + // Use absolute coordinates + var pf = Cocoa.SendPoint(e, selLocationInWindowOwner); - // Convert from points to pixel coordinates - var rf = Cocoa.SendRect(windowInfo.Handle, selConvertRectToBacking, - new RectangleF(pf.X, pf.Y, 0, 0)); + // Convert from points to pixel coordinates + var rf = Cocoa.SendRect(windowInfo.Handle, selConvertRectToBacking, + new RectangleF(pf.X, pf.Y, 0, 0)); - // See CocoaDrawingGuide under "Converting from Window to View Coordinates" - var p = new Point( - MathHelper.Clamp((int)Math.Round(rf.X), 0, Width), - MathHelper.Clamp((int)Math.Round(Height - rf.Y), 0, Height)); + // See CocoaDrawingGuide under "Converting from Window to View Coordinates" + p = new Point( + MathHelper.Clamp((int)Math.Round(rf.X), 0, Width), + MathHelper.Clamp((int)Math.Round(Height - rf.Y), 0, Height)); + } + else + { + // Mouse has been disassociated, + // use relative coordinates + var dx = Cocoa.SendFloat(e, selDeltaX); + var dy = Cocoa.SendFloat(e, selDeltaY); + + p = new Point( + MathHelper.Clamp((int)Math.Round(p.X + dx)), + MathHelper.Clamp((int)Math.Round(p.Y - dy))); + } InputDriver.Mouse[0].Position = p; } @@ -575,19 +575,19 @@ namespace OpenTK.Platform.MacOS } } - public System.Drawing.Point PointToClient(System.Drawing.Point point) + public override System.Drawing.Point PointToClient(System.Drawing.Point point) { var r = Cocoa.SendRect(windowInfo.Handle, selConvertRectFromScreen, new RectangleF(point.X, point.Y, 0, 0)); return new Point((int)r.X, (int)(GetContentViewFrame().Height - GetCurrentScreenFrame().Height - r.Y)); } - public System.Drawing.Point PointToScreen(System.Drawing.Point point) + public override System.Drawing.Point PointToScreen(System.Drawing.Point point) { var r = Cocoa.SendRect(windowInfo.Handle, selConvertRectToScreen, new RectangleF(point.X, point.Y, 0, 0)); return new Point((int)r.X, (int)(-GetContentViewFrame().Height + GetCurrentScreenFrame().Height - r.Y)); } - public System.Drawing.Icon Icon + public override System.Drawing.Icon Icon { get { return icon; } set @@ -602,7 +602,7 @@ namespace OpenTK.Platform.MacOS } } - public string Title + public override string Title { get { @@ -614,7 +614,7 @@ namespace OpenTK.Platform.MacOS } } - public bool Focused + public override bool Focused { get { @@ -622,7 +622,7 @@ namespace OpenTK.Platform.MacOS } } - public bool Visible + public override bool Visible { get { @@ -635,7 +635,7 @@ namespace OpenTK.Platform.MacOS } } - public bool Exists + public override bool Exists { get { @@ -643,7 +643,7 @@ namespace OpenTK.Platform.MacOS } } - public IWindowInfo WindowInfo + public override IWindowInfo WindowInfo { get { @@ -702,7 +702,7 @@ namespace OpenTK.Platform.MacOS previousWindowBorder = null; } - public WindowState WindowState + public override WindowState WindowState { get { @@ -754,7 +754,7 @@ namespace OpenTK.Platform.MacOS } } - public WindowBorder WindowBorder + public override WindowBorder WindowBorder { get { @@ -795,7 +795,7 @@ namespace OpenTK.Platform.MacOS return (NSWindowStyle)0; } - public System.Drawing.Rectangle Bounds + public override System.Drawing.Rectangle Bounds { get { @@ -820,105 +820,13 @@ namespace OpenTK.Platform.MacOS } } - public System.Drawing.Point Location + public override System.Drawing.Size ClientSize { get - { - return Bounds.Location; - } - set - { - var b = Bounds; - b.Location = value; - Bounds = b; - } - } - - public System.Drawing.Size Size - { - get - { - return Bounds.Size; - } - set - { - var b = Bounds; - b.Y -= Bounds.Height; - b.Y += value.Height; - b.Size = value; - Bounds = b; - } - } - - public int X - { - get - { - return Bounds.X; - } - set - { - var b = Bounds; - b.X = value; - Bounds = b; - } - } - - public int Y - { - get - { - return Bounds.Y; - } - set - { - var b = Bounds; - b.Y = value; - Bounds = b; - } - } - - public int Width - { - get { return ClientRectangle.Width; } - set - { - var s = ClientSize; - s.Width = value; - ClientSize = s; - } - } - - public int Height - { - get { return ClientRectangle.Height; } - set - { - var s = ClientSize; - s.Height = value; - ClientSize = s; - } - } - - public System.Drawing.Rectangle ClientRectangle - { - get { var contentViewBounds = Cocoa.SendRect(windowInfo.ViewHandle, selBounds); var bounds = Cocoa.SendRect(windowInfo.Handle, selConvertRectToBacking, contentViewBounds); - return new Rectangle((int)bounds.X, (int)bounds.Y, (int)bounds.Width, (int)bounds.Height); - } - set - { - ClientSize = value.Size; // Just set size, to be consistent with WinGLNative. - } - } - - public System.Drawing.Size ClientSize - { - get - { - return ClientRectangle.Size; + return new Size(0, 0, (int)bounds.Width, (int)bounds.Height); } set { @@ -928,14 +836,6 @@ namespace OpenTK.Platform.MacOS } } - public OpenTK.Input.IInputDriver InputDriver - { - get - { - return inputDriver; - } - } - public MouseCursor Cursor { get @@ -1075,13 +975,7 @@ namespace OpenTK.Platform.MacOS } } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) + protected override void Dispose(bool disposing) { if (disposed) return; @@ -1108,9 +1002,9 @@ namespace OpenTK.Platform.MacOS Disposed(this, EventArgs.Empty); } - ~CocoaNativeWindow() + public static IntPtr GetView(IntPtr windowHandle) { - Dispose(false); + return Cocoa.SendIntPtr(windowHandle, selContentView); } private RectangleF GetContentViewFrame()