123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938 |
- using System;
- using UnityEngine.InputSystem.Layouts;
- using UnityEngine.InputSystem.Utilities;
- namespace UnityEngine.InputSystem
- {
-
-
-
-
-
-
-
-
- public static class InputActionSetupExtensions
- {
-
-
-
-
-
-
-
-
-
-
- public static InputActionMap AddActionMap(this InputActionAsset asset, string name)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (string.IsNullOrEmpty(name))
- throw new ArgumentNullException(nameof(name));
- if (asset.FindActionMap(name) != null)
- throw new InvalidOperationException(
- $"An action map called '{name}' already exists in the asset");
- var map = new InputActionMap(name);
- map.GenerateId();
- asset.AddActionMap(map);
- return map;
- }
-
-
-
-
-
-
-
-
-
- public static void AddActionMap(this InputActionAsset asset, InputActionMap map)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (map == null)
- throw new ArgumentNullException(nameof(map));
- if (string.IsNullOrEmpty(map.name))
- throw new InvalidOperationException("Maps added to an input action asset must be named");
- if (map.asset != null)
- throw new InvalidOperationException(
- $"Cannot add map '{map}' to asset '{asset}' as it has already been added to asset '{map.asset}'");
-
- if (asset.FindActionMap(map.name) != null)
- throw new InvalidOperationException(
- $"An action map called '{map.name}' already exists in the asset");
- ArrayHelpers.Append(ref asset.m_ActionMaps, map);
- map.m_Asset = asset;
- }
-
-
-
-
-
-
-
-
-
-
- public static void RemoveActionMap(this InputActionAsset asset, InputActionMap map)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (map == null)
- throw new ArgumentNullException(nameof(map));
- if (map.enabled)
- throw new InvalidOperationException("Cannot remove an action map from the asset while it is enabled");
-
- if (map.m_Asset != asset)
- return;
- ArrayHelpers.Erase(ref asset.m_ActionMaps, map);
- map.m_Asset = null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static void RemoveActionMap(this InputActionAsset asset, string nameOrId)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (nameOrId == null)
- throw new ArgumentNullException(nameof(nameOrId));
- var map = asset.FindActionMap(nameOrId);
- if (map != null)
- asset.RemoveActionMap(map);
- }
-
- public static InputAction AddAction(this InputActionMap map, string name, InputActionType type = default, string binding = null,
- string interactions = null, string processors = null, string groups = null, string expectedControlLayout = null)
- {
- if (map == null)
- throw new ArgumentNullException(nameof(map));
- if (string.IsNullOrEmpty(name))
- throw new ArgumentException("Action must have name", nameof(name));
- if (map.enabled)
- throw new InvalidOperationException(
- $"Cannot add action '{name}' to map '{map}' while it the map is enabled");
- if (map.FindAction(name) != null)
- throw new InvalidOperationException(
- $"Cannot add action with duplicate name '{name}' to set '{map.name}'");
-
- var action = new InputAction(name, type)
- {
- expectedControlType = expectedControlLayout
- };
- action.GenerateId();
- ArrayHelpers.Append(ref map.m_Actions, action);
- action.m_ActionMap = map;
-
-
- if (!string.IsNullOrEmpty(binding))
- {
- action.AddBinding(binding, interactions: interactions, processors: processors, groups: groups);
- }
- else
- {
- if (!string.IsNullOrEmpty(groups))
- throw new ArgumentException(
- $"No binding path was specified for action '{action}' but groups was specified ('{groups}'); cannot apply groups without binding",
- nameof(groups));
-
- action.m_Interactions = interactions;
- action.m_Processors = processors;
- }
- return action;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void RemoveAction(this InputAction action)
- {
- if (action == null)
- throw new ArgumentNullException(nameof(action));
- var actionMap = action.actionMap;
- if (actionMap == null)
- throw new ArgumentException(
- $"Action '{action}' does not belong to an action map; nowhere to remove from", nameof(action));
- if (actionMap.enabled)
- throw new ArgumentException($"Cannot remove action '{action}' while its action map is enabled");
- var bindingsForAction = action.bindings.ToArray();
- var index = ArrayHelpers.IndexOfReference(actionMap.m_Actions, action);
- Debug.Assert(index != -1, "Could not find action in map");
- ArrayHelpers.EraseAt(ref actionMap.m_Actions, index);
- action.m_ActionMap = null;
- action.m_SingletonActionBindings = bindingsForAction;
- actionMap.ClearPerActionCachedBindingData();
-
- var newActionMapBindingCount = actionMap.m_Bindings.Length - bindingsForAction.Length;
- if (newActionMapBindingCount == 0)
- actionMap.m_Bindings = null;
- else
- {
- var newActionMapBindings = new InputBinding[newActionMapBindingCount];
- var oldActionMapBindings = actionMap.m_Bindings;
- var bindingIndex = 0;
- for (var i = 0; i < oldActionMapBindings.Length; ++i)
- {
- var binding = oldActionMapBindings[i];
- if (bindingsForAction.IndexOf(b => b == binding) == -1)
- newActionMapBindings[bindingIndex++] = binding;
- }
- actionMap.m_Bindings = newActionMapBindings;
- }
- }
-
-
-
-
-
-
-
-
-
- public static void RemoveAction(this InputActionAsset asset, string nameOrId)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (nameOrId == null)
- throw new ArgumentNullException(nameof(nameOrId));
- var action = asset.FindAction(nameOrId);
- action?.RemoveAction();
- }
-
-
-
-
-
-
-
-
-
-
-
- public static BindingSyntax AddBinding(this InputAction action, string path, string interactions = null,
- string processors = null, string groups = null)
- {
- return AddBinding(action, new InputBinding
- {
- path = path,
- interactions = interactions,
- processors = processors,
- groups = groups
- });
- }
-
-
-
-
-
-
-
-
-
-
- public static BindingSyntax AddBinding(this InputAction action, InputControl control)
- {
- if (control == null)
- throw new ArgumentNullException(nameof(control));
- return AddBinding(action, control.path);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static BindingSyntax AddBinding(this InputAction action, InputBinding binding = default)
- {
- if (action == null)
- throw new ArgumentNullException(nameof(action));
-
- Debug.Assert(action.m_Name != null || action.isSingletonAction);
- binding.action = action.name;
- var actionMap = action.GetOrCreateActionMap();
- var bindingIndex = AddBindingInternal(actionMap, binding);
- return new BindingSyntax(actionMap, action, bindingIndex);
- }
- public static BindingSyntax AddBinding(this InputActionMap actionMap, string path,
- string interactions = null, string groups = null, string action = null)
- {
- if (path == null)
- throw new ArgumentException("Binding path cannot be null", nameof(path));
- return AddBinding(actionMap, new InputBinding
- {
- path = path,
- interactions = interactions,
- groups = groups,
- action = action
- });
- }
- public static BindingSyntax AddBinding(this InputActionMap actionMap, string path, InputAction action,
- string interactions = null, string groups = null)
- {
- if (action != null && action.actionMap != actionMap)
- throw new ArgumentException(
- $"Action '{action}' is not part of action map '{actionMap}'", nameof(action));
- if (action == null)
- return AddBinding(actionMap, path: path, interactions: interactions, groups: groups);
- return AddBinding(actionMap, path: path, interactions: interactions, groups: groups,
- action: action.id);
- }
- public static BindingSyntax AddBinding(this InputActionMap actionMap, string path, Guid action,
- string interactions = null, string groups = null)
- {
- if (action == Guid.Empty)
- return AddBinding(actionMap, path: path, interactions: interactions, groups: groups);
- return AddBinding(actionMap, path: path, interactions: interactions, groups: groups,
- action: action.ToString());
- }
- public static BindingSyntax AddBinding(this InputActionMap actionMap, InputBinding binding)
- {
- if (actionMap == null)
- throw new ArgumentNullException(nameof(actionMap));
- if (binding.path == null)
- throw new ArgumentException("Binding path cannot be null", nameof(binding));
- var bindingIndex = AddBindingInternal(actionMap, binding);
- return new BindingSyntax(actionMap, null, bindingIndex);
- }
- public static CompositeSyntax AddCompositeBinding(this InputAction action, string composite, string interactions = null, string processors = null)
- {
- if (action == null)
- throw new ArgumentNullException(nameof(action));
- if (string.IsNullOrEmpty(composite))
- throw new ArgumentException("Composite name cannot be null or empty", nameof(composite));
- var actionMap = action.GetOrCreateActionMap();
-
- var binding = new InputBinding {path = composite, interactions = interactions, processors = processors, isComposite = true, action = action.name};
- var bindingIndex = AddBindingInternal(actionMap, binding);
- return new CompositeSyntax(actionMap, action, bindingIndex);
- }
- private static int AddBindingInternal(InputActionMap map, InputBinding binding)
- {
- Debug.Assert(map != null);
-
- if (string.IsNullOrEmpty(binding.m_Id))
- binding.GenerateId();
-
- var bindingIndex = ArrayHelpers.Append(ref map.m_Bindings, binding);
-
-
- map.ClearPerActionCachedBindingData();
-
- map.LazyResolveBindings();
-
-
- if (map.m_SingletonAction != null)
- map.m_SingletonAction.m_SingletonActionBindings = map.m_Bindings;
- return bindingIndex;
- }
- public static BindingSyntax ChangeBinding(this InputAction action, int index)
- {
- if (action == null)
- throw new ArgumentNullException(nameof(action));
- var indexOnMap = action.BindingIndexOnActionToBindingIndexOnMap(index);
- return new BindingSyntax(action.GetOrCreateActionMap(), action, indexOnMap);
- }
- public static BindingSyntax ChangeBindingWithId(this InputAction action, string id)
- {
- return action.ChangeBinding(new InputBinding {m_Id = id});
- }
- public static BindingSyntax ChangeBindingWithId(this InputAction action, Guid id)
- {
- return action.ChangeBinding(new InputBinding {id = id});
- }
- public static BindingSyntax ChangeBindingWithGroup(this InputAction action, string group)
- {
- return action.ChangeBinding(new InputBinding {groups = group});
- }
- public static BindingSyntax ChangeBindingWithPath(this InputAction action, string path)
- {
- return action.ChangeBinding(new InputBinding {path = path});
- }
- public static BindingSyntax ChangeBinding(this InputAction action, InputBinding match)
- {
- if (action == null)
- throw new ArgumentNullException(nameof(action));
- var actionMap = action.GetOrCreateActionMap();
- var bindingIndex = actionMap.FindBinding(match);
- if (bindingIndex == -1)
- throw new ArgumentException($"Cannot find binding matching '{match}' in '{action}'", nameof(match));
- return new BindingSyntax(actionMap, action, bindingIndex);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void Rename(this InputAction action, string newName)
- {
- if (action == null)
- throw new ArgumentNullException(nameof(action));
- if (string.IsNullOrEmpty(newName))
- throw new ArgumentNullException(nameof(newName));
- if (action.name == newName)
- return;
-
- var actionMap = action.actionMap;
- if (actionMap?.FindAction(newName) != null)
- throw new InvalidOperationException(
- $"Cannot rename '{action}' to '{newName}' in map '{actionMap}' as the map already contains an action with that name");
- var oldName = action.m_Name;
- action.m_Name = newName;
-
- var bindings = action.GetOrCreateActionMap().m_Bindings;
- var bindingCount = bindings.LengthSafe();
- for (var i = 0; i < bindingCount; ++i)
- if (string.Compare(bindings[i].action, oldName, StringComparison.InvariantCultureIgnoreCase) == 0)
- bindings[i].action = newName;
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void AddControlScheme(this InputActionAsset asset, InputControlScheme controlScheme)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (string.IsNullOrEmpty(controlScheme.name))
- throw new ArgumentException("Cannot add control scheme without name to asset " + asset.name, nameof(controlScheme));
- if (asset.FindControlScheme(controlScheme.name) != null)
- throw new InvalidOperationException(
- $"Asset '{asset.name}' already contains a control scheme called '{controlScheme.name}'");
- ArrayHelpers.Append(ref asset.m_ControlSchemes, controlScheme);
- }
-
-
-
-
-
-
-
-
-
-
- public static ControlSchemeSyntax AddControlScheme(this InputActionAsset asset, string name)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (string.IsNullOrEmpty(name))
- throw new ArgumentNullException(nameof(name));
- var index = asset.controlSchemes.Count;
- asset.AddControlScheme(new InputControlScheme(name));
- return new ControlSchemeSyntax(asset, index);
- }
-
-
-
-
-
-
-
-
-
-
- public static void RemoveControlScheme(this InputActionAsset asset, string name)
- {
- if (asset == null)
- throw new ArgumentNullException(nameof(asset));
- if (string.IsNullOrEmpty(name))
- throw new ArgumentNullException(nameof(name));
- var index = asset.FindControlSchemeIndex(name);
- if (index != -1)
- ArrayHelpers.EraseAt(ref asset.m_ControlSchemes, index);
- }
- public static InputControlScheme WithBindingGroup(this InputControlScheme scheme, string bindingGroup)
- {
- return new ControlSchemeSyntax(scheme).WithBindingGroup(bindingGroup).Done();
- }
- public static InputControlScheme WithRequiredDevice(this InputControlScheme scheme, string controlPath)
- {
- return new ControlSchemeSyntax(scheme).WithRequiredDevice(controlPath).Done();
- }
- public static InputControlScheme WithOptionalDevice(this InputControlScheme scheme, string controlPath)
- {
- return new ControlSchemeSyntax(scheme).WithOptionalDevice(controlPath).Done();
- }
- public static InputControlScheme OrWithRequiredDevice(this InputControlScheme scheme, string controlPath)
- {
- return new ControlSchemeSyntax(scheme).OrWithRequiredDevice(controlPath).Done();
- }
- public static InputControlScheme OrWithOptionalDevice(this InputControlScheme scheme, string controlPath)
- {
- return new ControlSchemeSyntax(scheme).OrWithOptionalDevice(controlPath).Done();
- }
-
-
-
-
-
- public struct BindingSyntax
- {
- private readonly InputActionMap m_ActionMap;
- private readonly InputAction m_Action;
- internal readonly int m_BindingIndex;
- internal BindingSyntax(InputActionMap map, InputAction action, int bindingIndex)
- {
- m_ActionMap = map;
- m_Action = action;
- m_BindingIndex = bindingIndex;
- }
-
-
-
-
-
-
-
- public BindingSyntax WithName(string name)
- {
- m_ActionMap.m_Bindings[m_BindingIndex].name = name;
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
- return this;
- }
-
-
-
-
-
-
- public BindingSyntax WithPath(string path)
- {
- m_ActionMap.m_Bindings[m_BindingIndex].path = path;
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
- return this;
- }
- public BindingSyntax WithGroup(string group)
- {
- if (string.IsNullOrEmpty(group))
- throw new ArgumentException("Group name cannot be null or empty", nameof(group));
- if (group.IndexOf(InputBinding.Separator) != -1)
- throw new ArgumentException(
- $"Group name cannot contain separator character '{InputBinding.Separator}'", nameof(group));
- return WithGroups(group);
- }
- public BindingSyntax WithGroups(string groups)
- {
- if (string.IsNullOrEmpty(groups))
- return this;
-
- var currentGroups = m_ActionMap.m_Bindings[m_BindingIndex].groups;
- if (!string.IsNullOrEmpty(currentGroups))
- groups = string.Join(InputBinding.kSeparatorString, currentGroups, groups);
-
- m_ActionMap.m_Bindings[m_BindingIndex].groups = groups;
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
- return this;
- }
- public BindingSyntax WithInteraction(string interaction)
- {
- if (string.IsNullOrEmpty(interaction))
- throw new ArgumentException("Interaction cannot be null or empty", nameof(interaction));
- if (interaction.IndexOf(InputBinding.Separator) != -1)
- throw new ArgumentException(
- $"Interaction string cannot contain separator character '{InputBinding.Separator}'", nameof(interaction));
- return WithInteractions(interaction);
- }
- public BindingSyntax WithInteractions(string interactions)
- {
- if (string.IsNullOrEmpty(interactions))
- return this;
-
- var currentInteractions = m_ActionMap.m_Bindings[m_BindingIndex].interactions;
- if (!string.IsNullOrEmpty(currentInteractions))
- interactions = string.Join(InputBinding.kSeparatorString, currentInteractions, interactions);
-
- m_ActionMap.m_Bindings[m_BindingIndex].interactions = interactions;
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
- return this;
- }
- public BindingSyntax WithInteraction<TInteraction>()
- where TInteraction : IInputInteraction
- {
- var interactionName = InputProcessor.s_Processors.FindNameForType(typeof(TInteraction));
- if (interactionName.IsEmpty())
- throw new NotSupportedException($"Type '{typeof(TInteraction)}' has not been registered as a processor");
- return WithInteraction(interactionName);
- }
- public BindingSyntax WithProcessor(string processor)
- {
- if (string.IsNullOrEmpty(processor))
- throw new ArgumentException("Processor cannot be null or empty", nameof(processor));
- if (processor.IndexOf(InputBinding.Separator) != -1)
- throw new ArgumentException(
- $"Interaction string cannot contain separator character '{InputBinding.Separator}'", nameof(processor));
- return WithProcessors(processor);
- }
- public BindingSyntax WithProcessors(string processors)
- {
- if (string.IsNullOrEmpty(processors))
- return this;
-
- var currentProcessors = m_ActionMap.m_Bindings[m_BindingIndex].processors;
- if (!string.IsNullOrEmpty(currentProcessors))
- processors = string.Join(InputBinding.kSeparatorString, currentProcessors, processors);
-
- m_ActionMap.m_Bindings[m_BindingIndex].processors = processors;
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
- return this;
- }
- public BindingSyntax WithProcessor<TProcessor>()
- {
- var processorName = InputProcessor.s_Processors.FindNameForType(typeof(TProcessor));
- if (processorName.IsEmpty())
- throw new NotSupportedException($"Type '{typeof(TProcessor)}' has not been registered as a processor");
- return WithProcessor(processorName);
- }
- public BindingSyntax Triggering(InputAction action)
- {
- if (action == null)
- throw new ArgumentNullException(nameof(action));
- if (action.isSingletonAction)
- throw new ArgumentException(
- $"Cannot change the action a binding triggers on singleton action '{action}'", nameof(action));
- m_ActionMap.m_Bindings[m_BindingIndex].action = action.name;
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
- return this;
- }
- public BindingSyntax To(InputBinding binding)
- {
- m_ActionMap.m_Bindings[m_BindingIndex] = binding;
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
-
- if (m_ActionMap.m_SingletonAction != null)
- m_ActionMap.m_Bindings[m_BindingIndex].action = m_Action.name;
- return this;
- }
- public void Erase()
- {
- ArrayHelpers.EraseAt(ref m_ActionMap.m_Bindings, m_BindingIndex);
- m_ActionMap.ClearPerActionCachedBindingData();
- m_ActionMap.LazyResolveBindings();
-
-
- if (m_ActionMap.m_SingletonAction != null)
- m_ActionMap.m_SingletonAction.m_SingletonActionBindings = m_ActionMap.m_Bindings;
- }
- internal BindingSyntax And => throw new NotImplementedException();
- }
- public struct CompositeSyntax
- {
- private readonly InputAction m_Action;
- private readonly InputActionMap m_ActionMap;
- private int m_CompositeIndex;
- internal CompositeSyntax(InputActionMap map, InputAction action, int compositeIndex)
- {
- m_Action = action;
- m_ActionMap = map;
- m_CompositeIndex = compositeIndex;
- }
- public CompositeSyntax With(string name, string binding, string groups = null)
- {
-
- int bindingIndex;
- if (m_Action != null)
- bindingIndex = m_Action.AddBinding(path: binding, groups: groups)
- .m_BindingIndex;
- else
- bindingIndex = m_ActionMap.AddBinding(path: binding, groups: groups)
- .m_BindingIndex;
- m_ActionMap.m_Bindings[bindingIndex].name = name;
- m_ActionMap.m_Bindings[bindingIndex].isPartOfComposite = true;
- return this;
- }
- }
- public struct ControlSchemeSyntax
- {
- private readonly InputActionAsset m_Asset;
- private readonly int m_ControlSchemeIndex;
- private InputControlScheme m_ControlScheme;
- internal ControlSchemeSyntax(InputActionAsset asset, int index)
- {
- m_Asset = asset;
- m_ControlSchemeIndex = index;
- m_ControlScheme = new InputControlScheme();
- }
- internal ControlSchemeSyntax(InputControlScheme controlScheme)
- {
- m_Asset = null;
- m_ControlSchemeIndex = -1;
- m_ControlScheme = controlScheme;
- }
- public ControlSchemeSyntax WithBindingGroup(string bindingGroup)
- {
- if (string.IsNullOrEmpty(bindingGroup))
- throw new ArgumentNullException(nameof(bindingGroup));
- if (m_Asset == null)
- m_ControlScheme.m_BindingGroup = bindingGroup;
- else
- m_Asset.m_ControlSchemes[m_ControlSchemeIndex].bindingGroup = bindingGroup;
- return this;
- }
- public ControlSchemeSyntax WithRequiredDevice<TDevice>()
- where TDevice : InputDevice
- {
- return WithRequiredDevice(DeviceTypeToControlPath<TDevice>());
- }
- public ControlSchemeSyntax WithOptionalDevice<TDevice>()
- where TDevice : InputDevice
- {
- return WithOptionalDevice(DeviceTypeToControlPath<TDevice>());
- }
- public ControlSchemeSyntax OrWithRequiredDevice<TDevice>()
- where TDevice : InputDevice
- {
- return WithRequiredDevice(DeviceTypeToControlPath<TDevice>());
- }
- public ControlSchemeSyntax OrWithOptionalDevice<TDevice>()
- where TDevice : InputDevice
- {
- return WithOptionalDevice(DeviceTypeToControlPath<TDevice>());
- }
- public ControlSchemeSyntax WithRequiredDevice(string controlPath)
- {
- AddDeviceEntry(controlPath, InputControlScheme.DeviceRequirement.Flags.None);
- return this;
- }
- public ControlSchemeSyntax WithOptionalDevice(string controlPath)
- {
- AddDeviceEntry(controlPath, InputControlScheme.DeviceRequirement.Flags.Optional);
- return this;
- }
- public ControlSchemeSyntax OrWithRequiredDevice(string controlPath)
- {
- AddDeviceEntry(controlPath, InputControlScheme.DeviceRequirement.Flags.Or);
- return this;
- }
- public ControlSchemeSyntax OrWithOptionalDevice(string controlPath)
- {
- AddDeviceEntry(controlPath,
- InputControlScheme.DeviceRequirement.Flags.Optional |
- InputControlScheme.DeviceRequirement.Flags.Or);
- return this;
- }
- private string DeviceTypeToControlPath<TDevice>()
- where TDevice : InputDevice
- {
- var layoutName = InputControlLayout.s_Layouts.TryFindLayoutForType(typeof(TDevice)).ToString();
- if (string.IsNullOrEmpty(layoutName))
- layoutName = typeof(TDevice).Name;
- return $"<{layoutName}>";
- }
- public InputControlScheme Done()
- {
- if (m_Asset != null)
- return m_Asset.m_ControlSchemes[m_ControlSchemeIndex];
- return m_ControlScheme;
- }
- private void AddDeviceEntry(string controlPath, InputControlScheme.DeviceRequirement.Flags flags)
- {
- if (string.IsNullOrEmpty(controlPath))
- throw new ArgumentNullException(nameof(controlPath));
- var scheme = m_Asset != null ? m_Asset.m_ControlSchemes[m_ControlSchemeIndex] : m_ControlScheme;
- ArrayHelpers.Append(ref scheme.m_DeviceRequirements,
- new InputControlScheme.DeviceRequirement
- {
- m_ControlPath = controlPath,
- m_Flags = flags,
- });
- if (m_Asset == null)
- m_ControlScheme = scheme;
- else
- m_Asset.m_ControlSchemes[m_ControlSchemeIndex] = scheme;
- }
- }
- }
- }
|