From e792bd80fe74685e9c237feddef5b7e584463448 Mon Sep 17 00:00:00 2001 From: Vlad K Date: Thu, 4 May 2017 23:03:50 +0300 Subject: [PATCH] Add initial d&d support for GameWindow. Add SDL2 d&d support --- src/OpenTK/INativeWindow.cs | 1 + src/OpenTK/Input/DropEventArgs.cs | 51 ++++++++++++++++++++ src/OpenTK/NativeWindow.cs | 14 ++++++ src/OpenTK/OpenTK.csproj | 1 + src/OpenTK/Platform/NativeWindowBase.cs | 10 ++++ src/OpenTK/Platform/SDL2/Sdl2.cs | 10 ++++ src/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs | 22 +++++++++ 7 files changed, 109 insertions(+) create mode 100644 src/OpenTK/Input/DropEventArgs.cs diff --git a/src/OpenTK/INativeWindow.cs b/src/OpenTK/INativeWindow.cs index 881bcc6c..f01ba276 100644 --- a/src/OpenTK/INativeWindow.cs +++ b/src/OpenTK/INativeWindow.cs @@ -285,6 +285,7 @@ namespace OpenTK //event EventHandler MouseClick; //event EventHandler MouseDoubleClick; + event EventHandler Drop; //event EventHandler DragDrop; //event EventHandler DragEnter; //event EventHandler DragOver; diff --git a/src/OpenTK/Input/DropEventArgs.cs b/src/OpenTK/Input/DropEventArgs.cs new file mode 100644 index 00000000..d5a9625f --- /dev/null +++ b/src/OpenTK/Input/DropEventArgs.cs @@ -0,0 +1,51 @@ +#region License +// +// ConfigurationType.cs +// +// Author: +// Stefanos A. +// +// Copyright (c) 2006-2014 Stefanos Apostolopoulos +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +#endregion + +using System; + +namespace OpenTK.Input +{ + public class DropEventArgs : EventArgs + { + string drop_string; + public DropEventArgs () { } + + public string DropString + { + get + { + return drop_string; + } + set + { + drop_string = value; + } + } + } +} diff --git a/src/OpenTK/NativeWindow.cs b/src/OpenTK/NativeWindow.cs index 78590633..a6980cf3 100644 --- a/src/OpenTK/NativeWindow.cs +++ b/src/OpenTK/NativeWindow.cs @@ -709,6 +709,11 @@ namespace OpenTK /// public event EventHandler MouseWheel = delegate { }; + /// + /// Occurs whenever a file dropped in window; + /// + public event EventHandler Drop = delegate { }; + #endregion #endregion @@ -981,6 +986,11 @@ namespace OpenTK MouseWheel(this, e); } + protected virtual void OnDrop(DropEventArgs e) + { + Drop(this, e); + } + #region OnResize /// @@ -1142,6 +1152,8 @@ namespace OpenTK private void OnMouseMoveInternal(object sender, MouseMoveEventArgs e) { OnMouseMove(e); } private void OnMouseWheelInternal(object sender, MouseWheelEventArgs e) { OnMouseWheel(e); } + private void OnDropInternal(object sender, DropEventArgs e) { OnDrop(e); } + #region OnMoveInternal private void OnMoveInternal(object sender, EventArgs e) { OnMove(e); } @@ -1214,6 +1226,7 @@ namespace OpenTK implementation.VisibleChanged += OnVisibleChangedInternal; implementation.WindowBorderChanged += OnWindowBorderChangedInternal; implementation.WindowStateChanged += OnWindowStateChangedInternal; + implementation.Drop += OnDropInternal; events = true; } else if (events) @@ -1238,6 +1251,7 @@ namespace OpenTK implementation.VisibleChanged -= OnVisibleChangedInternal; implementation.WindowBorderChanged -= OnWindowBorderChangedInternal; implementation.WindowStateChanged -= OnWindowStateChangedInternal; + implementation.Drop -= OnDropInternal; events = false; } else diff --git a/src/OpenTK/OpenTK.csproj b/src/OpenTK/OpenTK.csproj index 63c4345a..febacf34 100644 --- a/src/OpenTK/OpenTK.csproj +++ b/src/OpenTK/OpenTK.csproj @@ -795,6 +795,7 @@ + diff --git a/src/OpenTK/Platform/NativeWindowBase.cs b/src/OpenTK/Platform/NativeWindowBase.cs index 16579e53..ff3701ed 100644 --- a/src/OpenTK/Platform/NativeWindowBase.cs +++ b/src/OpenTK/Platform/NativeWindowBase.cs @@ -53,6 +53,8 @@ namespace OpenTK.Platform readonly KeyboardKeyEventArgs KeyUpArgs = new KeyboardKeyEventArgs(); readonly KeyPressEventArgs KeyPressArgs = new KeyPressEventArgs((char)0); + readonly DropEventArgs DropArgs = new DropEventArgs(); + // In order to simplify mouse event implementation, // we can store the current mouse state here. protected MouseState MouseState = new MouseState(); @@ -156,6 +158,13 @@ namespace OpenTK.Platform KeyUp(this, e); } + protected void OnDrop(string s) + { + var e = DropArgs; + DropArgs.DropString = s; + Drop(this, e); + } + /// \internal /// /// Call this method to simulate KeyDown/KeyUp events @@ -318,6 +327,7 @@ namespace OpenTK.Platform public event EventHandler MouseUp = delegate { }; public event EventHandler MouseMove = delegate { }; public event EventHandler MouseWheel = delegate { }; + public event EventHandler Drop = delegate { }; public abstract void Close(); diff --git a/src/OpenTK/Platform/SDL2/Sdl2.cs b/src/OpenTK/Platform/SDL2/Sdl2.cs index 35783957..bb437eeb 100644 --- a/src/OpenTK/Platform/SDL2/Sdl2.cs +++ b/src/OpenTK/Platform/SDL2/Sdl2.cs @@ -1442,6 +1442,8 @@ namespace OpenTK.Platform.SDL2 public ControllerButtonEvent ControllerButton; [FieldOffset(0)] public ControllerDeviceEvent ControllerDevice; + [FieldOffset(0)] + public DropEvent Drop; #if false [FieldOffset(0)] public QuitEvent quit; @@ -1755,6 +1757,14 @@ namespace OpenTK.Platform.SDL2 public Int32 Data2; } + struct DropEvent + { + public UInt32 Type; + public UInt32 Timestamp; + public IntPtr File; + public UInt32 WindowID; + } + #endregion } diff --git a/src/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs b/src/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs index 47b6d13c..d75ac170 100644 --- a/src/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs +++ b/src/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs @@ -197,6 +197,14 @@ namespace OpenTK.Platform.SDL2 } break; + case EventType.DROPFILE: + if (windows.TryGetValue(ev.Drop.WindowID, out window)) + { + ProcessDropEvent(window, ev.Drop); + processed = true; + } + break; + case EventType.QUIT: Debug.WriteLine("Sdl2 application quit"); break; @@ -293,6 +301,20 @@ namespace OpenTK.Platform.SDL2 window.OnMouseWheel(ev.X, ev.Y); } + static unsafe void ProcessDropEvent(Sdl2NativeWindow window, DropEvent ev) + { + byte* str = (byte*)ev.File; + + int length = 0; + for (; str[length] != 0; length++) + ; + + byte [] byteArray = new byte[length]; + Marshal.Copy(ev.File, byteArray, 0, length); + string dropString = System.Text.Encoding.UTF8.GetString (byteArray); + window.OnDrop(dropString); + } + static void ProcessWindowEvent(Sdl2NativeWindow window, WindowEvent e) { switch (e.Event)