-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Device: add API for capability labels
- Loading branch information
Showing
14 changed files
with
214 additions
and
8 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/Snowflake.Framework.Primitives/Input/Device/IDeviceCapabilityLabels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Snowflake.Input.Device | ||
{ | ||
/// <summary> | ||
/// Represents a mapping from <see cref="DeviceCapability"/> to a label on the real device. | ||
/// When enumerated, | ||
/// </summary> | ||
public interface IDeviceCapabilityLabels : IEnumerable<KeyValuePair<DeviceCapability, string>> | ||
{ | ||
/// <summary> | ||
/// Gets a friendly string representation of the provided device capability. | ||
/// </summary> | ||
/// <param name="capability">The <see cref="DeviceCapability"/> to get a name for.</param> | ||
/// <returns>A string representation of the device capbility</returns> | ||
string this[DeviceCapability capability] { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Snowflake.Input.Device; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace Snowflake.Input.Tests | ||
{ | ||
public class LabelsTests | ||
{ | ||
[Fact] | ||
public void DefaultLabels_Test() | ||
{ | ||
Assert.Equal("Button0", DefaultDeviceCapabilityLabels.DefaultLabels[DeviceCapability.Button0]); | ||
foreach(var (c, l) in DefaultDeviceCapabilityLabels.DefaultLabels) | ||
{ | ||
Assert.Equal(l, c.ToString()); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void DictLabels_Test() | ||
{ | ||
var dictLabels = new Dictionary<DeviceCapability, string>() | ||
{ | ||
{ DeviceCapability.Button0, "Button A" } | ||
}; | ||
|
||
IDeviceCapabilityLabels labels = new DictionaryDeviceCapabilityLabels(dictLabels); | ||
Assert.Equal("Button A", labels[DeviceCapability.Button0]); | ||
Assert.Single(labels); | ||
Assert.Equal(string.Empty, labels[DeviceCapability.Axis0Negative]); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Snowflake.Framework/Input/Device/DefaultDeviceCapabilityLabels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using EnumsNET; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Snowflake.Input.Device | ||
{ | ||
/// <summary> | ||
/// Implements a default label mapping using the name of the capability enum element. | ||
/// </summary> | ||
public sealed class DefaultDeviceCapabilityLabels : IDeviceCapabilityLabels | ||
{ | ||
/// <summary> | ||
/// Default labels using the name of the capability enum element. | ||
/// </summary> | ||
public static IDeviceCapabilityLabels DefaultLabels => _DefaultLabels; | ||
|
||
private static readonly DefaultDeviceCapabilityLabels _DefaultLabels = new DefaultDeviceCapabilityLabels(); | ||
|
||
public string this[DeviceCapability capability] => Enums.AsString(capability); | ||
|
||
public IEnumerator<KeyValuePair<DeviceCapability, string>> GetEnumerator() | ||
{ | ||
return Enums.GetMembers<DeviceCapability>() | ||
.Select(e => KeyValuePair.Create(e.Value, e.AsString())).GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Snowflake.Framework/Input/Device/DictionaryDeviceCapabilityLabels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using EnumsNET; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Snowflake.Input.Device | ||
{ | ||
/// <summary> | ||
/// Implements a label mapping using a backing dictionary. Missing labels are replaced with | ||
/// the empty string. | ||
/// </summary> | ||
public sealed class DictionaryDeviceCapabilityLabels : IDeviceCapabilityLabels | ||
{ | ||
/// <summary> | ||
/// Create a new <see cref="IDeviceCapabilityLabels"/> using a backing dictionary. | ||
/// </summary> | ||
/// <param name="labels">The backing dictionary to use.</param> | ||
public DictionaryDeviceCapabilityLabels(IDictionary<DeviceCapability, string> labels) | ||
{ | ||
this.Labels = labels; | ||
} | ||
|
||
private IDictionary<DeviceCapability, string> Labels { get; } | ||
|
||
public string this[DeviceCapability capability] => | ||
this.Labels.TryGetValue(capability, out string? value) ? value : string.Empty; | ||
|
||
public IEnumerator<KeyValuePair<DeviceCapability, string>> GetEnumerator() | ||
{ | ||
return this.Labels.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
...t.GraphQLFrameworkQueries/Types/InputDevice/DeviceCapabilityLabelKeyValuePairGraphType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using GraphQL.Types; | ||
using Snowflake.Input.Device; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Snowflake.Support.GraphQLFrameworkQueries.Types.InputDevice | ||
{ | ||
public class DeviceCapabilityLabelKeyValuePairGraphType : ObjectGraphType<KeyValuePair<DeviceCapability, string>> | ||
{ | ||
public DeviceCapabilityLabelKeyValuePairGraphType() | ||
{ | ||
Name = "DeviceCapabilityLabelKeyValuePair"; | ||
Description = "A label of a device capability"; | ||
Field<DeviceCapabilityEnum>("capability", description: "The capability this label describes.", resolve: c => c.Source.Key); | ||
Field<StringGraphType>("label", description: "The label of the capability.", resolve: c => c.Source.Value); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...lake.Support.GraphQLFrameworkQueries/Types/InputDevice/DeviceCapabilityLabelsGraphType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using GraphQL.Types; | ||
using Snowflake.Input.Device; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Snowflake.Support.GraphQLFrameworkQueries.Types.InputDevice | ||
{ | ||
public class DeviceCapabilityLabelsGraphType : ObjectGraphType<IDeviceCapabilityLabels> | ||
{ | ||
public DeviceCapabilityLabelsGraphType() | ||
{ | ||
Name = "DeviceCapabilityLabels"; | ||
Description = "A mapping of device capabilities to friendly string labels."; | ||
Field<ListGraphType<DeviceCapabilityLabelKeyValuePairGraphType>>("labels", description: "A list of labels in this device instance", | ||
resolve: c => c.Source); | ||
foreach (var (key, _) in DefaultDeviceCapabilityLabels.DefaultLabels) | ||
{ | ||
Field<StringGraphType>(key.ToString(), resolve: c => c.Source[key] ?? key.ToString()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters