[SDL2] Implemented modifier keys

Additionally, removed event watch callback in favor of calling
PollEvent() directly in ProcessEvents, which should be more efficient.
This commit is contained in:
thefiddler 2014-02-17 23:20:29 +01:00
parent 221d4661d4
commit 09f9bb3a17

View file

@ -72,9 +72,6 @@ namespace OpenTK.Platform.SDL2
readonly IInputDriver input_driver; readonly IInputDriver input_driver;
readonly EventFilter EventFilterDelegate_GCUnsafe = FilterEvents;
readonly IntPtr EventFilterDelegate;
static readonly Dictionary<uint, Sdl2NativeWindow> windows = static readonly Dictionary<uint, Sdl2NativeWindow> windows =
new Dictionary<uint, Sdl2NativeWindow>(); new Dictionary<uint, Sdl2NativeWindow>();
@ -105,17 +102,14 @@ namespace OpenTK.Platform.SDL2
IntPtr handle; IntPtr handle;
lock (SDL.Sync) lock (SDL.Sync)
{ {
EventFilterDelegate = Marshal.GetFunctionPointerForDelegate(EventFilterDelegate_GCUnsafe);
handle = SDL.CreateWindow(title, bounds.Left + x, bounds.Top + y, width, height, flags); handle = SDL.CreateWindow(title, bounds.Left + x, bounds.Top + y, width, height, flags);
SDL.AddEventWatch(EventFilterDelegate, handle); exists = true;
SDL.PumpEvents();
} }
ProcessEvents();
window = new Sdl2WindowInfo(handle, null); window = new Sdl2WindowInfo(handle, null);
window_id = SDL.GetWindowID(handle); window_id = SDL.GetWindowID(handle);
windows.Add(window_id, this); windows.Add(window_id, this);
window_title = title; window_title = title;
exists = true;
} }
} }
@ -152,14 +146,13 @@ namespace OpenTK.Platform.SDL2
return TranslateKey(scan); return TranslateKey(scan);
} }
unsafe static int FilterEvents(IntPtr user_data, IntPtr e) int ProcessEvent(ref Event ev)
{ {
bool processed = false; bool processed = false;
try try
{ {
Sdl2NativeWindow window = null; Sdl2NativeWindow window = null;
Event ev = *(Event*)e;
switch (ev.Type) switch (ev.Type)
{ {
@ -245,6 +238,7 @@ namespace OpenTK.Platform.SDL2
var key = ev.Key.Keysym; var key = ev.Key.Keysym;
window.key_args.Key = TranslateKey(key.Scancode); window.key_args.Key = TranslateKey(key.Scancode);
window.key_args.ScanCode = (uint)key.Scancode; window.key_args.ScanCode = (uint)key.Scancode;
window.key_args.Modifiers = window.input_driver.Keyboard[0].GetModifiers();
if (key_pressed) if (key_pressed)
{ {
window.KeyDown(window, window.key_args); window.KeyDown(window, window.key_args);
@ -396,7 +390,6 @@ namespace OpenTK.Platform.SDL2
CursorVisible = true; CursorVisible = true;
lock (SDL.Sync) lock (SDL.Sync)
{ {
SDL.DelEventWatch(EventFilterDelegate, window.Handle);
if (windows.ContainsKey(window_id)) if (windows.ContainsKey(window_id))
{ {
windows.Remove(window_id); windows.Remove(window_id);
@ -499,6 +492,12 @@ namespace OpenTK.Platform.SDL2
if (Exists) if (Exists)
{ {
SDL.PumpEvents(); SDL.PumpEvents();
Event e;
while (SDL.PollEvent(out e) > 0)
{
ProcessEvent(ref e);
}
if (must_destroy) if (must_destroy)
{ {
DestroyWindow(); DestroyWindow();