From 612652910fe9f4f5bc16308b889c2cc6f3846e97 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Fri, 2 May 2014 14:18:40 +0200 Subject: [PATCH] [Platform] Inherit NativeWindowBase --- Source/OpenTK/Input/MouseDevice.cs | 51 ++++++ Source/OpenTK/NativeWindow.cs | 20 +++ .../Platform/MacOS/CocoaNativeWindow.cs | 4 +- Source/OpenTK/Platform/NativeWindowBase.cs | 30 +++- .../OpenTK/Platform/SDL2/Sdl2NativeWindow.cs | 166 ++++-------------- Source/OpenTK/Platform/Windows/WinGLNative.cs | 43 ++--- Source/OpenTK/Platform/X11/X11GLNative.cs | 47 ++--- 7 files changed, 177 insertions(+), 184 deletions(-) diff --git a/Source/OpenTK/Input/MouseDevice.cs b/Source/OpenTK/Input/MouseDevice.cs index 16a81c97..9abae909 100644 --- a/Source/OpenTK/Input/MouseDevice.cs +++ b/Source/OpenTK/Input/MouseDevice.cs @@ -379,6 +379,7 @@ namespace OpenTK.Input #region Fields int x, y; + float wheel_x, wheel_y; #endregion @@ -411,6 +412,14 @@ namespace OpenTK.Input { } + internal MouseEventArgs(float x, float y, float wx, float wy) + { + X = (int)Math.Round(x); + Y = (int)Math.Round(y); + WheelX = wx; + WheelY = wy; + } + #endregion #region Public Members @@ -425,6 +434,48 @@ namespace OpenTK.Input /// public int Y { get { return y; } internal set { y = value; } } + /// + /// Gets the offset of the horizontal wheel, if one exists. + /// + public float WheelX { get; internal set; } + + /// + /// Gets the offset of the vertical wheel, if one exists. + /// + public float WheelY { get; internal set; } + + /// + /// Gets the offset of the vertical wheel, if one exists. + /// This is an alias to + /// + /// The wheel. + public float Wheel { get { return WheelY; } } + + /// + /// Gets the of the left mouse button. + /// + public ButtonState LeftButton { get; internal set; } + + /// + /// Gets the of the right mouse button. + /// + public ButtonState RightButton { get; internal set; } + + /// + /// Gets the of the middle mouse button. + /// + public ButtonState MiddleButton { get; internal set; } + + /// + /// Gets the of the first extra mouse button. + /// + public ButtonState X1Button { get; internal set; } + + /// + /// Gets the of the second extra mouse button. + /// + public ButtonState X2Button { get; internal set; } + /// /// Gets a System.Drawing.Points representing the location of the mouse for the event. /// diff --git a/Source/OpenTK/NativeWindow.cs b/Source/OpenTK/NativeWindow.cs index 795b67ef..dade7d81 100644 --- a/Source/OpenTK/NativeWindow.cs +++ b/Source/OpenTK/NativeWindow.cs @@ -680,6 +680,26 @@ namespace OpenTK /// public event EventHandler WindowStateChanged = delegate { }; + /// + /// Occurs when a is pressed. + /// + public event EventHandler MouseDown = delegate { }; + + /// + /// Occurs when a is released. + /// + public event EventHandler MouseUp = delegate { }; + + /// + /// Occurs whenever the mouse is moved. + /// + public event EventHandler MouseMove = delegate { }; + + /// + /// Occurs whenever a mouse wheel is moved; + /// + public event EventHandler MouseWheel = delegate { }; + #endregion #endregion diff --git a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs index 05011daa..8027e3f9 100644 --- a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs +++ b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs @@ -835,7 +835,7 @@ namespace OpenTK.Platform.MacOS } } - public MouseCursor Cursor + public override MouseCursor Cursor { get { @@ -957,7 +957,7 @@ namespace OpenTK.Platform.MacOS Cocoa.SendVoid(windowInfo.Handle, selInvalidateCursorRectsForView, windowInfo.ViewHandle); } - public bool CursorVisible + public override bool CursorVisible { get { return cursorVisible; } set diff --git a/Source/OpenTK/Platform/NativeWindowBase.cs b/Source/OpenTK/Platform/NativeWindowBase.cs index ee974391..20440b05 100644 --- a/Source/OpenTK/Platform/NativeWindowBase.cs +++ b/Source/OpenTK/Platform/NativeWindowBase.cs @@ -35,7 +35,7 @@ using OpenTK.Input; namespace OpenTK.Platform { - + // Common base class for all INativeWindow implementations abstract class NativeWindowBase : INativeWindow { readonly LegacyInputDriver LegacyInputDriver = @@ -123,6 +123,26 @@ namespace OpenTK.Platform MouseEnter(this, e); } + protected void OnMouseDown(MouseButtonEventArgs e) + { + MouseDown(this, e); + } + + protected void OnMouseUp(MouseButtonEventArgs e) + { + MouseUp(this, e); + } + + protected void OnMouseDown(MouseMoveEventArgs e) + { + MouseMove(this, e); + } + + protected void OnMouseWheel(MouseWheelEventArgs e) + { + MouseWheel(this, e); + } + #endregion #region INativeWindow Members @@ -143,6 +163,10 @@ namespace OpenTK.Platform public event EventHandler KeyUp = delegate { }; public event EventHandler MouseLeave = delegate { }; public event EventHandler MouseEnter = delegate { }; + public event EventHandler MouseDown = delegate { }; + public event EventHandler MouseUp = delegate { }; + public event EventHandler MouseMove = delegate { }; + public event EventHandler MouseWheel = delegate { }; public abstract void Close(); @@ -182,7 +206,7 @@ namespace OpenTK.Platform } } - public Size Size + public virtual Size Size { get { @@ -270,6 +294,8 @@ namespace OpenTK.Platform public abstract bool CursorVisible { get; set; } + public abstract MouseCursor Cursor { get; set; } + #endregion #region IDisposable Members diff --git a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs index 62e3c0ce..efd4d21e 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs @@ -41,7 +41,7 @@ using System.Text; namespace OpenTK.Platform.SDL2 { - class Sdl2NativeWindow : INativeWindow, IInputDriver + class Sdl2NativeWindow : NativeWindowBase, IInputDriver { readonly object sync = new object(); @@ -236,11 +236,11 @@ namespace OpenTK.Platform.SDL2 window.key_args.Modifiers = window.input_driver.Keyboard[0].GetModifiers(); if (key_pressed) { - window.KeyDown(window, window.key_args); + window.OnKeyDown(window.key_args); } else { - window.KeyUp(window, window.key_args); + window.OnKeyUp(window.key_args); } //window.keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed); } @@ -274,7 +274,7 @@ namespace OpenTK.Platform.SDL2 for (int i = 0; i < decoded_length; i++) { window.keypress_args.KeyChar = window.DecodeTextBuffer[i]; - window.KeyPress(window, window.keypress_args); + window.OnKeyPress(window.keypress_args); } } @@ -299,7 +299,7 @@ namespace OpenTK.Platform.SDL2 try { window.is_in_closing_event = true; - window.Closing(window, close_args); + window.OnClosing(close_args); } finally { @@ -308,17 +308,17 @@ namespace OpenTK.Platform.SDL2 if (!close_args.Cancel) { - window.Closed(window, EventArgs.Empty); + window.OnClosed(EventArgs.Empty); window.must_destroy = true; } break; case WindowEventID.ENTER: - window.MouseEnter(window, EventArgs.Empty); + window.OnMouseEnter(EventArgs.Empty); break; case WindowEventID.LEAVE: - window.MouseLeave(window, EventArgs.Empty); + window.OnMouseLeave(EventArgs.Empty); break; case WindowEventID.EXPOSED: @@ -327,47 +327,47 @@ namespace OpenTK.Platform.SDL2 case WindowEventID.FOCUS_GAINED: window.is_focused = true; - window.FocusedChanged(window, EventArgs.Empty); + window.OnFocusedChanged(EventArgs.Empty); break; case WindowEventID.FOCUS_LOST: window.is_focused = false; - window.FocusedChanged(window, EventArgs.Empty); + window.OnFocusedChanged(EventArgs.Empty); break; case WindowEventID.HIDDEN: window.is_visible = false; - window.VisibleChanged(window, EventArgs.Empty); + window.OnVisibleChanged(EventArgs.Empty); break; case WindowEventID.SHOWN: window.is_visible = true; - window.VisibleChanged(window, EventArgs.Empty); + window.OnVisibleChanged(EventArgs.Empty); break; case WindowEventID.MAXIMIZED: window.window_state = WindowState.Maximized; - window.WindowStateChanged(window, EventArgs.Empty); + window.OnWindowStateChanged(EventArgs.Empty); break; case WindowEventID.MINIMIZED: window.previous_window_state = window.window_state; window.window_state = WindowState.Minimized; - window.WindowStateChanged(window, EventArgs.Empty); + window.OnWindowStateChanged(EventArgs.Empty); break; case WindowEventID.RESTORED: window.window_state = window.previous_window_state; - window.WindowStateChanged(window, EventArgs.Empty); + window.OnWindowStateChanged(EventArgs.Empty); break; case WindowEventID.MOVED: - window.Move(window, EventArgs.Empty); + window.OnMove(EventArgs.Empty); break; case WindowEventID.RESIZED: case WindowEventID.SIZE_CHANGED: - window.Resize(window, EventArgs.Empty); + window.OnResize(EventArgs.Empty); break; default: @@ -443,24 +443,7 @@ namespace OpenTK.Platform.SDL2 #region INativeWindow Members - 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 MouseEnter = delegate { }; - public event EventHandler MouseLeave = delegate { }; - - public MouseCursor Cursor + public override MouseCursor Cursor { get { @@ -540,7 +523,7 @@ namespace OpenTK.Platform.SDL2 } } - public void Close() + public override void Close() { lock (sync) { @@ -560,7 +543,7 @@ namespace OpenTK.Platform.SDL2 } } - public void ProcessEvents() + public override void ProcessEvents() { lock (sync) { @@ -581,21 +564,21 @@ namespace OpenTK.Platform.SDL2 } } - public Point PointToClient(Point point) + public override Point PointToClient(Point point) { var origin = DisplayDevice.Default.Bounds.Location; var client = Location; return new Point(point.X + client.X - origin.X, point.Y + client.Y - origin.Y); } - public Point PointToScreen(Point point) + public override Point PointToScreen(Point point) { var origin = DisplayDevice.Default.Bounds.Location; var client = Location; return new Point(point.X + origin.X - client.X, point.Y + origin.Y - client.Y); } - public Icon Icon + public override Icon Icon { get { @@ -639,13 +622,13 @@ namespace OpenTK.Platform.SDL2 } icon = value; - IconChanged(this, EventArgs.Empty); + OnIconChanged(EventArgs.Empty); } } } } - public string Title + public override string Title { get { @@ -671,7 +654,7 @@ namespace OpenTK.Platform.SDL2 } } - public bool Focused + public override bool Focused { get { @@ -679,7 +662,7 @@ namespace OpenTK.Platform.SDL2 } } - public bool Visible + public override bool Visible { get { @@ -700,7 +683,7 @@ namespace OpenTK.Platform.SDL2 } } - public bool Exists + public override bool Exists { get { @@ -708,7 +691,7 @@ namespace OpenTK.Platform.SDL2 } } - public IWindowInfo WindowInfo + public override IWindowInfo WindowInfo { get { @@ -716,7 +699,7 @@ namespace OpenTK.Platform.SDL2 } } - public WindowState WindowState + public override WindowState WindowState { get { @@ -774,7 +757,7 @@ namespace OpenTK.Platform.SDL2 } } - public WindowBorder WindowBorder + public override WindowBorder WindowBorder { get { @@ -810,13 +793,13 @@ namespace OpenTK.Platform.SDL2 if (Exists) { - WindowBorderChanged(this, EventArgs.Empty); + OnWindowBorderChanged(EventArgs.Empty); } } } } - public Rectangle Bounds + public override Rectangle Bounds { get { @@ -829,7 +812,7 @@ namespace OpenTK.Platform.SDL2 } } - public Point Location + public override Point Location { get { @@ -856,7 +839,7 @@ namespace OpenTK.Platform.SDL2 } } - public Size Size + public override Size Size { get { @@ -883,67 +866,7 @@ namespace OpenTK.Platform.SDL2 } } - public int X - { - get - { - return Location.X; - } - set - { - Location = new Point(value, Y); - } - } - - public int Y - { - get - { - return Location.Y; - } - set - { - Location = new Point(X, value); - } - } - - public int Width - { - get - { - return ClientSize.Width; - } - set - { - ClientSize = new Size(value, Height); - } - } - - public int Height - { - get - { - return ClientSize.Height; - } - set - { - ClientSize = new Size(Width, value); - } - } - - public Rectangle ClientRectangle - { - get - { - return new Rectangle(new Point(), ClientSize); - } - set - { - ClientSize = value.Size; - } - } - - public Size ClientSize + public override Size ClientSize { get { @@ -967,7 +890,7 @@ namespace OpenTK.Platform.SDL2 } } - public IInputDriver InputDriver + public override IInputDriver InputDriver { get { @@ -975,7 +898,7 @@ namespace OpenTK.Platform.SDL2 } } - public bool CursorVisible + public override bool CursorVisible { get { @@ -1043,7 +966,7 @@ namespace OpenTK.Platform.SDL2 #region IDisposable implementation - void Dispose(bool manual) + protected override void Dispose(bool manual) { if (!disposed) { @@ -1082,17 +1005,6 @@ namespace OpenTK.Platform.SDL2 } } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~Sdl2NativeWindow() - { - Dispose(true); - } - #endregion } } diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 9cca4c44..fdf615cf 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -44,7 +44,7 @@ namespace OpenTK.Platform.Windows /// Drives GameWindow on Windows. /// This class supports OpenTK, and is not intended for use by OpenTK programs. /// - internal sealed class WinGLNative : INativeWindow, IInputDriver + internal sealed class WinGLNative : NativeWindowBase, IInputDriver { #region Fields @@ -977,7 +977,7 @@ namespace OpenTK.Platform.Windows #region Bounds - public Rectangle Bounds + public override Rectangle Bounds { get { return bounds; } set @@ -1042,7 +1042,7 @@ namespace OpenTK.Platform.Windows #region ClientSize - public Size ClientSize + public override Size ClientSize { get { @@ -1101,7 +1101,7 @@ namespace OpenTK.Platform.Windows #region Icon - public Icon Icon + public override Icon Icon { get { @@ -1126,7 +1126,7 @@ namespace OpenTK.Platform.Windows #region Focused - public bool Focused + public override bool Focused { get { return focused; } } @@ -1136,7 +1136,7 @@ namespace OpenTK.Platform.Windows #region Title StringBuilder sb_title = new StringBuilder(256); - public string Title + public override string Title { get { @@ -1160,7 +1160,7 @@ namespace OpenTK.Platform.Windows #region Visible - public bool Visible + public override bool Visible { get { @@ -1193,13 +1193,13 @@ namespace OpenTK.Platform.Windows #region Exists - public bool Exists { get { return exists; } } + public override bool Exists { get { return exists; } } #endregion #region Cursor - public MouseCursor Cursor + public override MouseCursor Cursor { get { @@ -1270,8 +1270,8 @@ namespace OpenTK.Platform.Windows #endregion #region CursorVisible - - public bool CursorVisible + + public override bool CursorVisible { get { return cursor_visible_count >= 0; } // Not used set @@ -1303,7 +1303,7 @@ namespace OpenTK.Platform.Windows #region Close - public void Close() + public override void Close() { Functions.PostMessage(window.Handle, WindowMessage.CLOSE, IntPtr.Zero, IntPtr.Zero); } @@ -1312,7 +1312,7 @@ namespace OpenTK.Platform.Windows #region public WindowState WindowState - public WindowState WindowState + public override WindowState WindowState { get { @@ -1406,7 +1406,7 @@ namespace OpenTK.Platform.Windows #region public WindowBorder WindowBorder - public WindowBorder WindowBorder + public override WindowBorder WindowBorder { get { @@ -1500,7 +1500,7 @@ namespace OpenTK.Platform.Windows #region PointToClient - public Point PointToClient(Point point) + public override Point PointToClient(Point point) { if (!Functions.ScreenToClient(window.Handle, ref point)) throw new InvalidOperationException(String.Format( @@ -1514,7 +1514,7 @@ namespace OpenTK.Platform.Windows #region PointToScreen - public Point PointToScreen(Point point) + public override Point PointToScreen(Point point) { if (!Functions.ClientToScreen(window.Handle, ref point)) throw new InvalidOperationException(String.Format( @@ -1554,7 +1554,7 @@ namespace OpenTK.Platform.Windows #region public void ProcessEvents() MSG msg; - public void ProcessEvents() + public override void ProcessEvents() { while (Functions.PeekMessage(ref msg, IntPtr.Zero, 0, 0, PeekMessageFlags.Remove)) { @@ -1576,7 +1576,7 @@ namespace OpenTK.Platform.Windows #region public IWindowInfo WindowInfo - public IWindowInfo WindowInfo + public override IWindowInfo WindowInfo { get { return child_window; } } @@ -1640,7 +1640,7 @@ namespace OpenTK.Platform.Windows GC.SuppressFinalize(this); } - void Dispose(bool calledManually) + protected override void Dispose(bool calledManually) { if (!disposed) { @@ -1667,11 +1667,6 @@ namespace OpenTK.Platform.Windows } } - ~WinGLNative() - { - Dispose(false); - } - #endregion } } diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index a3cc9126..e5e032c8 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -45,7 +45,7 @@ namespace OpenTK.Platform.X11 /// Drives GameWindow on X11. /// This class supports OpenTK, and is not intended for use by OpenTK programs. /// - internal sealed class X11GLNative : INativeWindow, IDisposable + internal sealed class X11GLNative : NativeWindowBase { // TODO: Disable screensaver. // TODO: What happens if we can't disable decorations through motif? @@ -785,7 +785,7 @@ namespace OpenTK.Platform.X11 #region ProcessEvents - public void ProcessEvents() + public override void ProcessEvents() { // Process all pending events while (Exists && window != null) @@ -1015,7 +1015,7 @@ namespace OpenTK.Platform.X11 #region Bounds - public Rectangle Bounds + public override Rectangle Bounds { get { @@ -1116,7 +1116,7 @@ namespace OpenTK.Platform.X11 #region ClientSize - public Size ClientSize + public override Size ClientSize { get { @@ -1172,7 +1172,7 @@ namespace OpenTK.Platform.X11 #region Icon - public Icon Icon + public override Icon Icon { get { @@ -1246,7 +1246,7 @@ namespace OpenTK.Platform.X11 #region Focused - public bool Focused + public override bool Focused { get { @@ -1258,7 +1258,7 @@ namespace OpenTK.Platform.X11 #region WindowState - public OpenTK.WindowState WindowState + public override OpenTK.WindowState WindowState { get { @@ -1395,7 +1395,7 @@ namespace OpenTK.Platform.X11 #region WindowBorder - public OpenTK.WindowBorder WindowBorder + public override OpenTK.WindowBorder WindowBorder { get { @@ -1465,7 +1465,7 @@ namespace OpenTK.Platform.X11 #region Cursor - public MouseCursor Cursor + public override MouseCursor Cursor { get { @@ -1505,7 +1505,7 @@ namespace OpenTK.Platform.X11 #region CursorVisible - public bool CursorVisible + public override bool CursorVisible { get { return cursor_visible; } set @@ -1552,7 +1552,7 @@ namespace OpenTK.Platform.X11 /// /// Returns true if a render window/context exists. /// - public bool Exists + public override bool Exists { get { return exists; } } @@ -1586,7 +1586,7 @@ namespace OpenTK.Platform.X11 /// TODO: Use atoms for this property. /// Gets or sets the GameWindow title. /// - public string Title + public override string Title { get { @@ -1618,7 +1618,7 @@ namespace OpenTK.Platform.X11 #region public bool Visible - public bool Visible + public override bool Visible { get { @@ -1647,14 +1647,14 @@ namespace OpenTK.Platform.X11 #region public IWindowInfo WindowInfo - public IWindowInfo WindowInfo + public override IWindowInfo WindowInfo { get { return window; } } #endregion - public void Close() { Exit(); } + public override void Close() { Exit(); } #region public void Exit() @@ -1691,7 +1691,7 @@ namespace OpenTK.Platform.X11 #region PointToClient - public Point PointToClient(Point point) + public override Point PointToClient(Point point) { int ox, oy; IntPtr child; @@ -1711,7 +1711,7 @@ namespace OpenTK.Platform.X11 #region PointToScreen - public Point PointToScreen(Point point) + public override Point PointToScreen(Point point) { int ox, oy; IntPtr child; @@ -1733,13 +1733,7 @@ namespace OpenTK.Platform.X11 #region IDisposable Members - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } - - private void Dispose(bool manuallyCalled) + protected override void Dispose(bool manuallyCalled) { if (!disposed) { @@ -1775,11 +1769,6 @@ namespace OpenTK.Platform.X11 } } - ~X11GLNative() - { - this.Dispose(false); - } - #endregion } }