Add initial d&d support for GameWindow. Add SDL2 d&d support

This commit is contained in:
Vlad K 2017-05-04 23:03:50 +03:00
parent 1e73f38ecd
commit e792bd80fe
7 changed files with 109 additions and 0 deletions

View file

@ -285,6 +285,7 @@ namespace OpenTK
//event EventHandler<MouseEventArgs> MouseClick;
//event EventHandler<MouseEventArgs> MouseDoubleClick;
event EventHandler<OpenTK.Input.DropEventArgs> Drop;
//event EventHandler<DragEventArgs> DragDrop;
//event EventHandler<DragEventArgs> DragEnter;
//event EventHandler<DragEventArgs> DragOver;

View file

@ -0,0 +1,51 @@
#region License
//
// ConfigurationType.cs
//
// Author:
// Stefanos A. <stapostol@gmail.com>
//
// 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;
}
}
}
}

View file

@ -709,6 +709,11 @@ namespace OpenTK
/// </summary>
public event EventHandler<MouseWheelEventArgs> MouseWheel = delegate { };
/// <summary>
/// Occurs whenever a file dropped in window;
/// </summary>
public event EventHandler<DropEventArgs> Drop = delegate { };
#endregion
#endregion
@ -981,6 +986,11 @@ namespace OpenTK
MouseWheel(this, e);
}
protected virtual void OnDrop(DropEventArgs e)
{
Drop(this, e);
}
#region OnResize
/// <summary>
@ -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

View file

@ -795,6 +795,7 @@
<Compile Include="Platform\Linux\Bindings\Evdev.cs" />
<Compile Include="Platform\Linux\DefaultCursor.cs" />
<Compile Include="Platform\Linux\Bindings\Kms.cs" />
<Compile Include="Input\DropEventArgs.cs" />
<None Include="OpenTK.csproj.paket.template" />
<None Include="paket.references" />
</ItemGroup>

View file

@ -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
/// <summary>
/// Call this method to simulate KeyDown/KeyUp events
@ -318,6 +327,7 @@ namespace OpenTK.Platform
public event EventHandler<MouseButtonEventArgs> MouseUp = delegate { };
public event EventHandler<MouseMoveEventArgs> MouseMove = delegate { };
public event EventHandler<MouseWheelEventArgs> MouseWheel = delegate { };
public event EventHandler<DropEventArgs> Drop = delegate { };
public abstract void Close();

View file

@ -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
}

View file

@ -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)