/
InputQueryBuilder.cs
121 lines (109 loc) · 6.7 KB
/
InputQueryBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphQL.Types;
using Snowflake.Configuration;
using Snowflake.Framework.Remoting.GraphQL.Attributes;
using Snowflake.Framework.Remoting.GraphQL.Query;
using Snowflake.Input;
using Snowflake.Input.Controller;
using Snowflake.Input.Controller.Mapped;
using Snowflake.Input.Device;
using Snowflake.Services;
using Snowflake.Support.GraphQLFrameworkQueries.Inputs.MappedControllerElement;
using Snowflake.Support.GraphQLFrameworkQueries.Types.InputDevice;
using Snowflake.Support.GraphQLFrameworkQueries.Types.InputDevice.Mapped;
namespace Snowflake.Support.GraphQLFrameworkQueries.Queries
{
public class InputQueryBuilder : QueryBuilder
{
public IDeviceEnumerator DeviceEnumerator { get; }
public IControllerElementMappingsStore MappedElementStore { get; }
public IStoneProvider StoneProvider { get; }
public InputQueryBuilder(IDeviceEnumerator deviceEnumerator,
IControllerElementMappingsStore mappedElementCollectionStore,
IStoneProvider stoneProvider)
{
this.DeviceEnumerator = deviceEnumerator;
this.MappedElementStore = mappedElementCollectionStore;
this.StoneProvider = stoneProvider;
}
[Connection("inputDevices", "Gets all connected input devices on this computer.",
typeof(InputDeviceGraphType))]
public IEnumerable<IInputDevice> GetInputDevices()
{
return this.DeviceEnumerator.QueryConnectedDevices();
}
[Query("controllerProfile", "Gets a controller profile for the given Stone controller and real device",
typeof(MappedControllerElementCollectionGraphType))]
[Parameter(typeof(string), typeof(StringGraphType), "deviceName", "The hardware device name of the device.")]
[Parameter(typeof(int), typeof(IntGraphType), "vendorId", "The vendor ID number of the device.")]
[Parameter(typeof(InputDriverType), typeof(InputDriverEnum), "inputDriver", "The input driver of the instance for this controller profile.")]
[Parameter(typeof(string), typeof(StringGraphType), "controllerId", "The Stone Controller ID that this mapping represents.")]
[Parameter(typeof(string), typeof(StringGraphType), "profileName", "A profile name for the mapping.", nullable: true)]
public IControllerElementMappings GetProfile(string deviceName, int vendorId, InputDriverType inputDriver, string controllerId,
string profileName = "default")
{
return this.MappedElementStore.GetMappings(controllerId, inputDriver, deviceName, vendorId, profileName ?? "default");
}
[Connection("controllerProfiles", "Gets all a controller profiles for the given real device",
typeof(MappedControllerElementCollectionGraphType))]
[Parameter(typeof(string), typeof(StringGraphType), "deviceName", "The hardware device name of the device.")]
[Parameter(typeof(int), typeof(IntGraphType), "vendorId", "The vendor ID number of the device.")]
[Parameter(typeof(string), typeof(StringGraphType), "controllerId", "The Stone Controller ID that this mapping represents.")]
public IEnumerable<IControllerElementMappings> GetProfiles(string deviceName, int vendorId, string controllerId)
{
return this.MappedElementStore.GetMappings(controllerId, deviceName, vendorId);
}
[Query("defaultLayout",
"Gets the default mapping between Stone controller IDs and the provided device.",
typeof(ListGraphType<MappedControllerElementGraphType>))]
[Parameter(typeof(Guid), typeof(GuidGraphType), "deviceInstanceGuid", "The device instance to get the layout for.")]
[Parameter(typeof(InputDriverType), typeof(InputDriverEnum), "inputDriver", "The input driver of the instance for this controller profile.")]
public IEnumerable<MappedControllerElement> GetDefaultProfile(Guid deviceInstanceGuid, InputDriverType inputDriver)
{
var device = this.DeviceEnumerator.QueryConnectedDevices()
.FirstOrDefault(i => i.InstanceGuid == deviceInstanceGuid)?.Instances
.FirstOrDefault(i => i.Driver == inputDriver);
return device.DefaultLayout;
}
[Mutation("createControllerProfile",
"Creates the default controller profile for the given Stone controller and real device.",
typeof(MappedControllerElementCollectionGraphType))]
[Parameter(typeof(DefaultMappedControllerElementCollectionInputObject),
typeof(DefaultMappedControllerElementCollectionInputType), "input", "The input")]
public IControllerElementMappings CreateProfile(DefaultMappedControllerElementCollectionInputObject input)
{
var device = this.DeviceEnumerator.QueryConnectedDevices()
.FirstOrDefault(d => d.InstanceGuid == input.InstanceGuid);
var instance = device?.Instances
.FirstOrDefault(i => i.Driver == input.InputDriver);
// todo : throw error here?
if (instance == null) return null;
if (!this.StoneProvider.Controllers.TryGetValue(input.ControllerId, out var controllerLayout)) return null;
var defaults = new ControllerElementMappings(device!.DeviceName, controllerLayout,
input.InputDriver, device.VendorID, instance.DefaultLayout);
this.MappedElementStore.AddMappings(defaults, input.ProfileName);
return this.MappedElementStore.GetMappings(input.ControllerId, instance.Driver,
device.DeviceName, device.VendorID, input.ProfileName);
}
[Mutation("setControllerProfile",
"Sets the values of the given controller profile for the given Stone controller and real device.",
typeof(MappedControllerElementCollectionGraphType))]
[Parameter(typeof(MappedControllerElementCollectionInputObject),
typeof(MappedControllerElementCollectionInputType), "input", "The input")]
public IControllerElementMappings SetProfile(MappedControllerElementCollectionInputObject input)
{
var collection = this.MappedElementStore.GetMappings(input.ControllerId, input.InputDriver,
input.DeviceName, input.VendorID, input.ProfileName);
foreach (var mapping in input.Mappings)
{
collection[mapping.LayoutElement] = mapping.DeviceCapability;
}
this.MappedElementStore.UpdateMappings(collection, input.ProfileName);
return this.MappedElementStore.GetMappings(input.ControllerId, input.InputDriver,
input.DeviceName, input.VendorID, input.ProfileName);
}
}
}