8000 Additional work on GLFW input by devvoid · Pull Request #26 · dotnet/Silk.NET · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Additional work on GLFW input #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Silk.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.Input.Desktop", "s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.Input", "src\Input\Silk.NET.Input\Silk.NET.Input.csproj", "{020A8E88-B607-4281-BA0D-5ED03484A201}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InputTest", "examples\InputTest\InputTest.csproj", "{3E744E54-F450-4051-8919-04D9B591688E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1058,6 +1060,18 @@ Global
{020A8E88-B607-4281-BA0D-5ED03484A201}.Release|x64.Build.0 = Release|Any CPU
{020A8E88-B607-4281-BA0D-5ED03484A201}.Release|x86.ActiveCfg = Release|Any CPU
{020A8E88-B607-4281-BA0D-5ED03484A201}.Release|x86.Build.0 = Release|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Debug|x64.ActiveCfg = Debug|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Debug|x64.Build.0 = Debug|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Debug|x86.ActiveCfg = Debug|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Debug|x86.Build.0 = Debug|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Release|Any CPU.Build.0 = Release|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Release|x64.ActiveCfg = Release|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Release|x64.Build.0 = Release|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Release|x86.ActiveCfg = Release|Any CPU
{3E744E54-F450-4051-8919-04D9B591688E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BFE429EB-4C2E-4BF3-A302-C9C5A2FDA6D7} = {23324041-2076-477C-A4BF-B385B8066C6C}
Expand Down Expand Up @@ -1134,5 +1148,6 @@ Global
{136C6154-D300-4B82-80D3-17B637841A2B} = {0651C5EF-50AA-4598-8D9C-8F210ADD8490}
{3908DEF6-7403-49F5-B8EC-5B3B12C325D4} = {FA9D1C95-5585-4DEC-B226-1447A486C376}
{020A8E88-B607-4281-BA0D-5ED03484A201} = {FA9D1C95-5585-4DEC-B226-1447A486C376}
{3E744E54-F450-4051-8919-04D9B591688E} = {E1F91563-7277-4E9B-A3B7-8D5FD9802A4A}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions examples/InputTest/InputTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Input\Silk.NET.Input.Desktop\Silk.NET.Input.Desktop.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions examples/InputTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Silk.NET.Input.Common;
using Silk.NET.Input.Desktop;
using Silk.NET.Windowing.Common;
using Silk.NET.Windowing.Desktop;

namespace InputTest
{
internal class Program
{
private static void Main()
{
var window = new GlfwWindow(WindowOptions.Default);

var inputContext = new GlfwInputContext(window);
inputContext.ConnectionChanged += ControllerConnected;
}

public static void ControllerConnected(IInputDevice device, bool isConnected)
{
Console.WriteLine(isConnected
? $"Controller {device.Name} connected"
: $"Controller {device.Name} disconnected");
}
}
}
22 changes: 22 additions & 0 deletions src/Input/Silk.NET.Input.Common/Deadzone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System;

namespace Silk.NET.Input.Common
{
/// <summary>
Expand Down Expand Up @@ -30,5 +32,25 @@ public Deadzone(float value, DeadzoneMethod method)
Value = value;
Method = method;
}

/// <summary>
/// Applies this deadzone to a raw input value.
/// </summary>
/// <param name="raw">The raw input value to apply the deadzone to.</param>
/// <returns>The input with deadzone applied.</returns>
/// <exception cref="ArgumentOutOfRangeException">If the deadzone method isn't part of
/// <see cref="DeadzoneMethod"/></exception>
public float Apply(float raw)
{
switch (Method)
{
case DeadzoneMethod.Traditional:
return Math.Abs(raw) < Value ? 0 : raw;
case DeadzoneMethod.AdaptiveGradient:
return (1 - Value) * raw + Value * Math.Sign(raw);
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
5 changes: 5 additions & 0 deletions src/Input/Silk.NET.Input.Common/Interfaces/IInputContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ public interface IInputContext : IDisposable
/// support other devices.
/// </remarks>
IReadOnlyList<IInputDevice> OtherDevices { get; }

/// <summary>
/// Called when the connection status of a device changes.
/// </summary>
event Action<IInputDevice, bool> ConnectionChanged;
}
}
7 changes: 0 additions & 7 deletions src/Input/Silk.NET.Input.Common/Interfaces/IInputDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System;

namespace Silk.NET.Input.Common
{
/// <summary>
Expand All @@ -26,10 +24,5 @@ public interface IInputDevice
/// Whether or not this device is currently connected.
/// </summary>
bool IsConnected { get; }

/// <summary>
/// Called when the connection of this device changes.
/// </summary>
event Action<IInputDevice, bool> ConnectionChanged;
}
}
3 changes: 3 additions & 0 deletions src/Input/Silk.NET.Input.Desktop/GlfwInputContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ public void WindowUpdate(double obj)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public event Action<IInputDevice, bool> ConnectionChanged;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public GlfwGamepad(int i)
public string Name => Util.Do(() => Util.Glfw.GetGamepadName(Index));
public int Index { get; }
public bool IsConnected => Util.Do(() => Util.Glfw.JoystickIsGamepad(Index));
public event Action<IInputDevice, bool> ConnectionChanged;
public IReadOnlyList<Button> Buttons { get; }
public IReadOnlyList<Thumbstick> Thumbsticks { get; }
public IReadOnlyList<Trigger> Triggers { get; }
Expand Down
Original file line number Diff line number Diff line F438 change
Expand Up @@ -21,7 +21,6 @@ public GlfwJoystick(int i)
public string Name => Util.Do(() => Util.Glfw.GetJoystickName(Index));
public int Index { get; }
public bool IsConnected => Util.Do(() => Util.Glfw.JoystickPresent(Index));
public event Action<IInputDevice, bool> ConnectionChanged;
public IReadOnlyList<Axis> Axes => GetAxes(Index, this);
public IReadOnlyList<Button> Buttons { get; }
public IReadOnlyList<Hat> Hats { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public GlfwKeyboard(GlfwInputContext gic)
public string Name { get; } = $"Silk.NET Keyboard (GLFW)";
public int Index { get; } = 0;
public bool IsConnected { get; } = true;
public event Action<IInputDevice, bool> ConnectionChanged;
public IReadOnlyList<Key> SupportedKeys { get; } = Util.SupportedKeys;
public unsafe bool IsKeyPressed(Key key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class GlfwMouse : IMouse
public string Name { get; } = "Silk.NET Mouse (GLFW)";
public int Index { get; } = 0;
public bool IsConnected { get; } = true;
public event Action<IInputDevice, bool> ConnectionChanged;
public IReadOnlyList<MouseButton> SupportedButtons { get; }
public IReadOnlyList<ScrollWheel> ScrollWheels { get; }
public bool IsButtonPressed(MouseButton btn)
Expand Down
13 changes: 0 additions & 13 deletions src/Input/Silk.NET.Input.Desktop/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ public static T Do<T>(Func<T> @delegate)
return GlfwProvider.ThreadDispatcher.Invoke(@delegate);
}

public static float ApplyDeadzone(Deadzone deadzone, float val)
{
switch (deadzone.Method)
{
case DeadzoneMethod.Traditional:
return Math.Abs(val) < deadzone.Value ? 0 : val;
case DeadzoneMethod.AdaptiveGradient:
return (1 - deadzone.Value) * val + deadzone.Value * Math.Sign(val);
default:
throw new ArgumentOutOfRangeException();
}
}

public static Glfw Glfw => GlfwProvider.GLFW.Value;
public static Key[] SupportedKeys { get; } // this is expensive, but only runs once.
= Enum.GetNames(typeof(Key)).Where(_glfwKeys.Contains).Select(StringToSilkKey).ToArray();
Expand Down
0