mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-01-23 05:01:12 +00:00
Moved all input processing to Sdl2InputDriver
This reduces the duplication of code between Sdl2NativeWindow and Sdl2InputDriver. Sdl2InputDriver is now solely responsible for handling input.
This commit is contained in:
parent
bad2b7b6d7
commit
d81bedf5f8
|
@ -34,25 +34,33 @@ using OpenTK.Input;
|
||||||
|
|
||||||
namespace OpenTK.Platform.SDL2
|
namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
class Sdl2InputDriver : IInputDriver2
|
class Sdl2InputDriver : IInputDriver2, IInputDriver
|
||||||
{
|
{
|
||||||
readonly static Dictionary<IntPtr, Sdl2InputDriver> DriverHandles =
|
readonly static Dictionary<IntPtr, Sdl2InputDriver> DriverHandles =
|
||||||
new Dictionary<IntPtr, Sdl2InputDriver>();
|
new Dictionary<IntPtr, Sdl2InputDriver>();
|
||||||
|
|
||||||
|
readonly IntPtr driver_handle;
|
||||||
|
|
||||||
readonly Sdl2Keyboard keyboard_driver = new Sdl2Keyboard();
|
readonly Sdl2Keyboard keyboard_driver = new Sdl2Keyboard();
|
||||||
readonly Sdl2Mouse mouse_driver = new Sdl2Mouse();
|
readonly Sdl2Mouse mouse_driver = new Sdl2Mouse();
|
||||||
|
readonly Sdl2JoystickDriver joystick_driver = new Sdl2JoystickDriver();
|
||||||
|
|
||||||
readonly SDL.SDL_EventFilter EventFilterDelegate = FilterInputEvents;
|
readonly SDL.SDL_EventFilter EventFilterDelegate = FilterInputEvents;
|
||||||
|
readonly IntPtr EventFilterPointer;
|
||||||
|
|
||||||
static int count;
|
static int count;
|
||||||
bool disposed;
|
bool disposed;
|
||||||
|
|
||||||
public Sdl2InputDriver()
|
public Sdl2InputDriver()
|
||||||
{
|
{
|
||||||
|
EventFilterPointer = Marshal.GetFunctionPointerForDelegate(
|
||||||
|
EventFilterDelegate);
|
||||||
|
|
||||||
lock (SDL.Sync)
|
lock (SDL.Sync)
|
||||||
{
|
{
|
||||||
IntPtr driver_handle = new IntPtr(count++);
|
driver_handle = new IntPtr(count++);
|
||||||
DriverHandles.Add(driver_handle, this);
|
DriverHandles.Add(driver_handle, this);
|
||||||
SDL.SDL_AddEventWatch(EventFilterDelegate, driver_handle);
|
SDL.SDL_AddEventWatch(EventFilterPointer, driver_handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,6 +107,51 @@ namespace OpenTK.Platform.SDL2
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region IInputDriver Members
|
||||||
|
|
||||||
|
public void Poll()
|
||||||
|
{
|
||||||
|
joystick_driver.Poll();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IJoystickDriver Members
|
||||||
|
|
||||||
|
public IList<JoystickDevice> Joysticks
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return joystick_driver.Joysticks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IMouseDriver Members
|
||||||
|
|
||||||
|
public IList<MouseDevice> Mouse
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return mouse_driver.Mouse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IKeyboardDriver Members
|
||||||
|
|
||||||
|
public IList<KeyboardDevice> Keyboard
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return keyboard_driver.Keyboard;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region IInputDriver2 Members
|
#region IInputDriver2 Members
|
||||||
|
|
||||||
public IMouseDriver2 MouseDriver
|
public IMouseDriver2 MouseDriver
|
||||||
|
@ -136,10 +189,12 @@ namespace OpenTK.Platform.SDL2
|
||||||
if (manual)
|
if (manual)
|
||||||
{
|
{
|
||||||
Debug.Print("Disposing {0}", GetType());
|
Debug.Print("Disposing {0}", GetType());
|
||||||
|
joystick_driver.Dispose();
|
||||||
lock (SDL.Sync)
|
lock (SDL.Sync)
|
||||||
{
|
{
|
||||||
SDL.SDL_DelEventWatch(EventFilterDelegate, IntPtr.Zero);
|
SDL.SDL_DelEventWatch(EventFilterPointer, IntPtr.Zero);
|
||||||
}
|
}
|
||||||
|
DriverHandles.Remove(driver_handle);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,18 +26,30 @@
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using OpenTK.Input;
|
using OpenTK.Input;
|
||||||
|
|
||||||
namespace OpenTK.Platform.SDL2
|
namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
class Sdl2Keyboard : IKeyboardDriver2
|
class Sdl2Keyboard : IKeyboardDriver2, IKeyboardDriver
|
||||||
{
|
{
|
||||||
static readonly Sdl2KeyMap KeyMap = new Sdl2KeyMap();
|
static readonly Sdl2KeyMap KeyMap = new Sdl2KeyMap();
|
||||||
KeyboardState state;
|
KeyboardState state;
|
||||||
|
|
||||||
|
readonly List<KeyboardDevice> keyboards =
|
||||||
|
new List<KeyboardDevice>();
|
||||||
|
readonly IList<KeyboardDevice> keyboards_readonly;
|
||||||
|
|
||||||
public Sdl2Keyboard()
|
public Sdl2Keyboard()
|
||||||
{
|
{
|
||||||
state.IsConnected = true;
|
state.IsConnected = true;
|
||||||
|
|
||||||
|
keyboards.Add(new KeyboardDevice());
|
||||||
|
keyboards[0].Description = "Standard keyboard";
|
||||||
|
keyboards[0].NumberOfFunctionKeys = 12;
|
||||||
|
keyboards[0].NumberOfKeys = 101;
|
||||||
|
keyboards[0].NumberOfLeds = 3;
|
||||||
|
keyboards_readonly = keyboards.AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Private Members
|
#region Private Members
|
||||||
|
@ -74,9 +86,23 @@ namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
Key key;
|
Key key;
|
||||||
bool pressed = e.state != 0;
|
bool pressed = e.state != 0;
|
||||||
if (KeyMap.TryGetValue(e.keysym.scancode, out key))
|
var scancode = e.keysym.scancode;
|
||||||
|
if (KeyMap.TryGetValue(scancode, out key))
|
||||||
{
|
{
|
||||||
state.SetKeyState(key, (byte)e.keysym.scancode, pressed);
|
state.SetKeyState(key, (byte)scancode, pressed);
|
||||||
|
keyboards[0].SetKey(key, (byte)scancode, pressed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IKeyboardDriver Members
|
||||||
|
|
||||||
|
public IList<KeyboardDevice> Keyboard
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return keyboards_readonly;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,18 +26,30 @@
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Drawing;
|
||||||
using OpenTK.Input;
|
using OpenTK.Input;
|
||||||
|
|
||||||
namespace OpenTK.Platform.SDL2
|
namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
class Sdl2Mouse : IMouseDriver2
|
class Sdl2Mouse : IMouseDriver2, IMouseDriver
|
||||||
{
|
{
|
||||||
MouseState state;
|
MouseState state;
|
||||||
|
|
||||||
|
readonly List<MouseDevice> mice =
|
||||||
|
new List<MouseDevice>();
|
||||||
|
readonly IList<MouseDevice> mice_readonly;
|
||||||
|
|
||||||
public Sdl2Mouse()
|
public Sdl2Mouse()
|
||||||
{
|
{
|
||||||
state.IsConnected = true;
|
state.IsConnected = true;
|
||||||
|
|
||||||
|
mice.Add(new MouseDevice());
|
||||||
|
mice[0].Description = "Standard mouse";
|
||||||
|
mice[0].NumberOfButtons = 3;
|
||||||
|
mice[0].NumberOfWheels = 1;
|
||||||
|
mice_readonly = mice.AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Private Members
|
#region Private Members
|
||||||
|
@ -86,18 +98,33 @@ namespace OpenTK.Platform.SDL2
|
||||||
public void ProcessWheelEvent(SDL.SDL_MouseWheelEvent wheel)
|
public void ProcessWheelEvent(SDL.SDL_MouseWheelEvent wheel)
|
||||||
{
|
{
|
||||||
state.WheelPrecise += wheel.y;
|
state.WheelPrecise += wheel.y;
|
||||||
|
mice[0].WheelPrecise += wheel.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessMouseEvent(SDL.SDL_MouseMotionEvent motion)
|
public void ProcessMouseEvent(SDL.SDL_MouseMotionEvent motion)
|
||||||
{
|
{
|
||||||
state.X += motion.xrel;
|
state.X += motion.xrel;
|
||||||
state.Y += motion.yrel;
|
state.Y += motion.yrel;
|
||||||
|
mice[0].Position = new Point(motion.x, motion.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessMouseEvent(SDL.SDL_MouseButtonEvent button)
|
public void ProcessMouseEvent(SDL.SDL_MouseButtonEvent button)
|
||||||
{
|
{
|
||||||
bool pressed = button.state == SDL.SDL_PRESSED;
|
bool pressed = button.state == SDL.SDL_PRESSED;
|
||||||
SetButtonState(TranslateButton(button.button), pressed);
|
SetButtonState(TranslateButton(button.button), pressed);
|
||||||
|
mice[0][TranslateButton(button.button)] = pressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IMouseDriver Members
|
||||||
|
|
||||||
|
public IList<MouseDevice> Mouse
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return mice_readonly;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
@ -53,12 +53,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
Icon icon;
|
Icon icon;
|
||||||
string window_title;
|
string window_title;
|
||||||
|
|
||||||
KeyboardDevice keyboard = new KeyboardDevice();
|
readonly IInputDriver input_driver = new Sdl2InputDriver();
|
||||||
MouseDevice mouse = new MouseDevice();
|
|
||||||
IList<KeyboardDevice> keyboards = new List<KeyboardDevice>(1);
|
|
||||||
IList<MouseDevice> mice = new List<MouseDevice>(1);
|
|
||||||
|
|
||||||
readonly Sdl2JoystickDriver joystick_driver = new Sdl2JoystickDriver();
|
|
||||||
|
|
||||||
readonly SDL.SDL_EventFilter EventFilterDelegate = FilterEvents;
|
readonly SDL.SDL_EventFilter EventFilterDelegate = FilterEvents;
|
||||||
|
|
||||||
|
@ -95,18 +90,6 @@ namespace OpenTK.Platform.SDL2
|
||||||
windows.Add(window_id, this);
|
windows.Add(window_id, this);
|
||||||
window_title = title;
|
window_title = title;
|
||||||
|
|
||||||
keyboard.Description = "Standard keyboard";
|
|
||||||
keyboard.NumberOfFunctionKeys = 12;
|
|
||||||
keyboard.NumberOfKeys = 101;
|
|
||||||
keyboard.NumberOfLeds = 3;
|
|
||||||
|
|
||||||
mouse.Description = "Standard mouse";
|
|
||||||
mouse.NumberOfButtons = 3;
|
|
||||||
mouse.NumberOfWheels = 1;
|
|
||||||
|
|
||||||
keyboards.Add(keyboard);
|
|
||||||
mice.Add(mouse);
|
|
||||||
|
|
||||||
exists = true;
|
exists = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -220,7 +203,8 @@ namespace OpenTK.Platform.SDL2
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ev.button.button)
|
switch (ev.button.button)
|
||||||
{
|
{/*
|
||||||
|
|
||||||
case (byte)SDL.SDL_BUTTON_LEFT:
|
case (byte)SDL.SDL_BUTTON_LEFT:
|
||||||
window.mouse[MouseButton.Left] = button_pressed;
|
window.mouse[MouseButton.Left] = button_pressed;
|
||||||
break;
|
break;
|
||||||
|
@ -240,6 +224,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
case (byte)SDL.SDL_BUTTON_X2:
|
case (byte)SDL.SDL_BUTTON_X2:
|
||||||
window.mouse[MouseButton.Button2] = button_pressed;
|
window.mouse[MouseButton.Button2] = button_pressed;
|
||||||
break;
|
break;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,19 +232,19 @@ namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
bool key_pressed = ev.key.state == SDL.SDL_PRESSED;
|
bool key_pressed = ev.key.state == SDL.SDL_PRESSED;
|
||||||
var key = ev.key.keysym;
|
var key = ev.key.keysym;
|
||||||
window.keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed);
|
//window.keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ProcessMotionEvent(Sdl2NativeWindow window, SDL.SDL_Event ev)
|
static void ProcessMotionEvent(Sdl2NativeWindow window, SDL.SDL_Event ev)
|
||||||
{
|
{
|
||||||
float scale = window.ClientSize.Width / (float)window.Size.Width;
|
float scale = window.ClientSize.Width / (float)window.Size.Width;
|
||||||
window.mouse.Position = new Point(
|
//window.mouse.Position = new Point(
|
||||||
(int)(ev.motion.x * scale), (int)(ev.motion.y * scale));
|
// (int)(ev.motion.x * scale), (int)(ev.motion.y * scale));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ProcessWheelEvent(Sdl2NativeWindow window, SDL.SDL_Event ev)
|
static void ProcessWheelEvent(Sdl2NativeWindow window, SDL.SDL_Event ev)
|
||||||
{
|
{
|
||||||
window.mouse.Wheel += ev.wheel.y;
|
//window.mouse.Wheel += ev.wheel.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ProcessWindowEvent(Sdl2NativeWindow window, SDL.SDL_WindowEvent e)
|
static void ProcessWindowEvent(Sdl2NativeWindow window, SDL.SDL_WindowEvent e)
|
||||||
|
@ -835,7 +820,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return this;
|
return input_driver;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -864,7 +849,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
|
|
||||||
public void Poll()
|
public void Poll()
|
||||||
{
|
{
|
||||||
joystick_driver.Poll();
|
InputDriver.Poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -875,7 +860,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return joystick_driver.Joysticks;
|
return InputDriver.Joysticks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -887,7 +872,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return mice;
|
return InputDriver.Mouse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -899,7 +884,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return keyboards;
|
return InputDriver.Keyboard;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -910,18 +895,18 @@ namespace OpenTK.Platform.SDL2
|
||||||
void Dispose(bool manual)
|
void Dispose(bool manual)
|
||||||
{
|
{
|
||||||
if (!disposed)
|
if (!disposed)
|
||||||
|
{
|
||||||
|
lock (sync)
|
||||||
{
|
{
|
||||||
if (manual)
|
if (manual)
|
||||||
{
|
{
|
||||||
Debug.Print("Disposing {0}", GetType());
|
Debug.Print("Disposing {0}", GetType());
|
||||||
lock (sync)
|
InputDriver.Dispose();
|
||||||
{
|
|
||||||
if (Exists)
|
if (Exists)
|
||||||
{
|
{
|
||||||
DestroyWindow();
|
DestroyWindow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Sdl2NativeWindow leaked, did you forget to call Dispose()?");
|
Debug.WriteLine("Sdl2NativeWindow leaked, did you forget to call Dispose()?");
|
||||||
|
@ -929,6 +914,7 @@ namespace OpenTK.Platform.SDL2
|
||||||
disposed = true;
|
disposed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|
|
@ -3400,6 +3400,13 @@ namespace OpenTK.Platform.SDL2
|
||||||
IntPtr userdata
|
IntPtr userdata
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/* userdata refers to a void* */
|
||||||
|
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public static extern void SDL_AddEventWatch(
|
||||||
|
IntPtr filter,
|
||||||
|
IntPtr userdata
|
||||||
|
);
|
||||||
|
|
||||||
/* userdata refers to a void* */
|
/* userdata refers to a void* */
|
||||||
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
|
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
|
||||||
public static extern void SDL_DelEventWatch(
|
public static extern void SDL_DelEventWatch(
|
||||||
|
@ -3407,6 +3414,13 @@ namespace OpenTK.Platform.SDL2
|
||||||
IntPtr userdata
|
IntPtr userdata
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/* userdata refers to a void* */
|
||||||
|
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public static extern void SDL_DelEventWatch(
|
||||||
|
IntPtr filter,
|
||||||
|
IntPtr userdata
|
||||||
|
);
|
||||||
|
|
||||||
/* userdata refers to a void* */
|
/* userdata refers to a void* */
|
||||||
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
|
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
|
||||||
public static extern void SDL_FilterEvents(
|
public static extern void SDL_FilterEvents(
|
||||||
|
|
Loading…
Reference in a new issue