From a94a2937323d90476779f50dee5fe99bf17ec0b9 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Mon, 6 Jan 2014 14:25:05 +0100 Subject: [PATCH] [Platform] Implemented legacy IJoystickDriver LegacyJoystickDriver implements the legacy IJoystickDriver interface (GameWindow.Joysticks) in terms of the new IJoystickDriver2 interface (OpenTK.Input.Joystick). This removes a large chunk of code from each platform backend, as they no longer need to implement IJoystickDriver themselves. Additionally, it adds support for device hot plugging which was previously missing. --- Source/OpenTK/OpenTK.csproj | 1 + .../OpenTK/Platform/LegacyJoystickDriver.cs | 106 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 Source/OpenTK/Platform/LegacyJoystickDriver.cs diff --git a/Source/OpenTK/OpenTK.csproj b/Source/OpenTK/OpenTK.csproj index d9b969c9..b30dd262 100644 --- a/Source/OpenTK/OpenTK.csproj +++ b/Source/OpenTK/OpenTK.csproj @@ -798,6 +798,7 @@ + diff --git a/Source/OpenTK/Platform/LegacyJoystickDriver.cs b/Source/OpenTK/Platform/LegacyJoystickDriver.cs new file mode 100644 index 00000000..1f8e6c3e --- /dev/null +++ b/Source/OpenTK/Platform/LegacyJoystickDriver.cs @@ -0,0 +1,106 @@ +#region License +// +// LegacyJoystickDriver.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; +using System.Collections.Generic; +using OpenTK.Input; + +namespace OpenTK.Platform +{ + internal class LegacyJoystickDriver : IJoystickDriver + { + static readonly string ConnectedName = "Connected Joystick"; + static readonly string DisconnectedName = "Disconnected Joystick"; + readonly List joysticks = new List(); + readonly IList joysticks_readonly; + + class LegacyJoystickDevice : JoystickDevice + { + public LegacyJoystickDevice(int id, int axes, int buttons) + : base(id, axes, buttons) + { } + } + + internal LegacyJoystickDriver() + { + joysticks_readonly = joysticks.AsReadOnly(); + for (int i = 0; i < 4; i++) + { + joysticks.Add(new LegacyJoystickDevice(i, 0, 0)); + joysticks[i].Description = DisconnectedName; + } + } + + public void Poll() + { + for (int i = 0; i < 4; i++) + { + JoystickCapabilities caps = Joystick.GetCapabilities(i); + if (caps.IsConnected && joysticks[i].Description == DisconnectedName) + { + // New joystick connected + JoystickDevice device = new LegacyJoystickDevice(i, caps.AxisCount, caps.ButtonCount); + //device.Description = Joystick.GetName(i); + device.Description = ConnectedName; + } + else if (!caps.IsConnected && joysticks[i].Description != DisconnectedName) + { + // Joystick disconnected + joysticks[i] = new LegacyJoystickDevice(i, 0, 0); + joysticks[i].Description = DisconnectedName; + } + + JoystickState state = Joystick.GetState(i); + for (int axis_index = 0; axis_index < (int)JoystickAxis.Last; axis_index++) + { + JoystickAxis axis = JoystickAxis.Axis0 + axis_index; + joysticks[i].SetAxis(axis, state.GetAxis(axis)); + } + for (int button_index = 0; button_index < (int)JoystickButton.Last; button_index++) + { + JoystickButton button = JoystickButton.Button0 + button_index; + joysticks[i].SetButton(button, state.GetButton(button) == ButtonState.Pressed); + } + } + } + + #region IJoystickDriver Members + + public IList Joysticks + { + get + { + return joysticks_readonly; + } + } + + #endregion + } +} +