No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputBinding.cs 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using UnityEngine.InputSystem.Layouts;
  5. using UnityEngine.InputSystem.Utilities;
  6. ////REVIEW: do we really need overridable processors and interactions?
  7. // Downsides to the current approach:
  8. // - Being able to address entire batches of controls through a single control is awesome. Especially
  9. // when combining it type-kind of queries (e.g. "<MyDevice>/<Button>"). However, it complicates things
  10. // in quite a few areas. There's quite a few bits in InputActionState that could be simplified if a
  11. // binding simply maps to a control.
  12. namespace UnityEngine.InputSystem
  13. {
  14. /// <summary>
  15. /// A mapping of controls to an action.
  16. /// </summary>
  17. /// <remarks>
  18. /// Each binding represents a value received from controls (see <see cref="InputControl"/>).
  19. /// There are two main types of bindings: "normal" bindings and "composite" bindings.
  20. ///
  21. /// Normal bindings directly bind to control(s) by means of <see cref="path"/> which is a "control path"
  22. /// (see <see cref="InputControlPath"/> for details about how to form paths). At runtime, the
  23. /// path of such a binding may match none, one, or multiple controls. Each control matched by the
  24. /// path will feed input into the binding.
  25. ///
  26. /// Composite bindings do not bind to controls themselves. Instead, they receive their input
  27. /// from their "part" bindings and then return a value representing a "composition" of those
  28. /// inputs. What composition specifically is performed depends on the type of the composite.
  29. /// <see cref="Composites.AxisComposite"/>, for example, will return a floating-point axis value
  30. /// computed from the state of two buttons.
  31. ///
  32. /// The action that is triggered by a binding is determined by its <see cref="action"/> property.
  33. /// The resolution to an <see cref="InputAction"/> depends on where the binding is used. For example,
  34. /// bindings that are part of <see cref="InputActionMap.bindings"/> will resolve action names to
  35. /// actions in the same <see cref="InputActionMap"/>.
  36. ///
  37. /// A binding can also be used as a form of search mask or filter. In this use, <see cref="path"/>,
  38. /// <see cref="action"/>, and <see cref="groups"/> become search criteria that are matched
  39. /// against other bindings. See <see cref="Matches(InputBinding)"/> for details. This use
  40. /// is employed in places such as <see cref="InputActionRebindingExtensions"/> as well as in
  41. /// binding masks on actions (<see cref="InputAction.bindingMask"/>), action maps (<see
  42. /// cref="InputActionMap.bindingMask"/>), and assets (<see cref="InputActionAsset.bindingMask"/>).
  43. /// </remarks>
  44. [Serializable]
  45. public struct InputBinding : IEquatable<InputBinding>
  46. {
  47. /// <summary>
  48. /// Character that is used to separate elements in places such as <see cref="groups"/>,
  49. /// <see cref="interactions"/>, and <see cref="processors"/>.
  50. /// </summary>
  51. /// Some strings on bindings represent lists of elements. An example is <see cref="groups"/>
  52. /// which may associate a binding with several binding groups, each one delimited by the
  53. /// separator.
  54. ///
  55. /// <remarks>
  56. /// <example>
  57. /// <code>
  58. /// // A binding that belongs to the "Keyboard&amp;Mouse" and "Gamepad" group.
  59. /// new InputBinding
  60. /// {
  61. /// path = "*/{PrimaryAction},
  62. /// groups = "Keyboard&amp;Mouse;Gamepad"
  63. /// };
  64. /// </code>
  65. /// </example>
  66. /// </remarks>
  67. public const char Separator = ';';
  68. internal const string kSeparatorString = ";";
  69. /// <summary>
  70. /// Optional name for the binding.
  71. /// </summary>
  72. /// <value>Name of the binding.</value>
  73. /// <remarks>
  74. /// For bindings that are part of composites (see <see cref="isPartOfComposite"/>), this is
  75. /// the name of the field on the binding composite object that should be initialized with
  76. /// the control target of the binding.
  77. /// </remarks>
  78. public string name
  79. {
  80. get => m_Name;
  81. set => m_Name = value;
  82. }
  83. /// <summary>
  84. /// Unique ID of the binding.
  85. /// </summary>
  86. /// <value>Unique ID of the binding.</value>
  87. /// <remarks>
  88. /// This can be used, for example, when storing binding overrides in local user configurations.
  89. /// Using the binding ID, an override can remain associated with one specific binding.
  90. /// </remarks>
  91. public Guid id
  92. {
  93. get
  94. {
  95. ////REVIEW: this is inconsistent with InputActionMap and InputAction which generate IDs, if necessary
  96. if (string.IsNullOrEmpty(m_Id))
  97. return default;
  98. return new Guid(m_Id);
  99. }
  100. set => m_Id = value.ToString();
  101. }
  102. /// <summary>
  103. /// Control path being bound to.
  104. /// </summary>
  105. /// <value>Path of control(s) to source input from.</value>
  106. /// <remarks>
  107. /// Bindings reference <see cref="InputControl"/>s using a regular expression-like
  108. /// language. See <see cref="InputControlPath"/> for details.
  109. ///
  110. /// If the binding is a composite (<see cref="isComposite"/>), the path is the composite
  111. /// string instead. For example, for a <see cref="Composites.Vector2Composite"/>, the
  112. /// path could be something like <c>"Vector2(normalize=false)"</c>.
  113. ///
  114. /// The path of a binding may be non-destructively override at runtime using <see cref="overridePath"/>
  115. /// which unlike this property is not serialized. <see cref="effectivePath"/> represents the
  116. /// final, effective path.
  117. /// </remarks>
  118. /// <example>
  119. /// <code>
  120. /// // A binding that references the left mouse button.
  121. /// new InputBinding { path = "&lt;Mouse&gt;/leftButton" }
  122. /// </code>
  123. /// </example>
  124. /// <seealso cref="overridePath"/>
  125. /// <seealso cref="InputControlPath"/>
  126. /// <seealso cref="InputControlPath.Parse"/>
  127. /// <seealso cref="InputControl.path"/>
  128. /// <seealso cref="InputSystem.FindControl"/>
  129. public string path
  130. {
  131. get => m_Path;
  132. set => m_Path = value;
  133. }
  134. /// <summary>
  135. /// If the binding is overridden, this is the overriding path.
  136. /// Otherwise it is <c>null</c>.
  137. /// </summary>
  138. /// <value>Path to override the <see cref="path"/> property with.</value>
  139. /// <remarks>
  140. /// Unlike the <see cref="path"/> property, the value of the override path is not serialized.
  141. /// If set, it will take precedence and determine the result of <see cref="effectivePath"/>.
  142. ///
  143. /// This property can be set to an empty string to disable the binding. During resolution,
  144. /// bindings with an empty <see cref="effectivePath"/> will get skipped.
  145. ///
  146. /// To set the override on an existing binding, use the methods supplied by <see cref="InputActionRebindingExtensions"/>
  147. /// such as <see cref="InputActionRebindingExtensions.ApplyBindingOverride(InputAction,string,string,string)"/>.
  148. ///
  149. /// <example>
  150. /// <code>
  151. /// // Override the binding to &lt;Gamepad&gt;/buttonSouth on
  152. /// // myAction with a binding to &lt;Gamepad&gt;/buttonNorth.
  153. /// myAction.ApplyBindingOverride(
  154. /// new InputBinding
  155. /// {
  156. /// path = "&lt;Gamepad&gt;/buttonSouth",
  157. /// overridePath = "&lt;Gamepad&gt;/buttonNorth"
  158. /// });
  159. /// </code>
  160. /// </example>
  161. /// </remarks>
  162. /// <seealso cref="path"/>
  163. /// <seealso cref="overrideInteractions"/>
  164. /// <seealso cref="overrideProcessors"/>
  165. /// <seealso cref="hasOverrides"/>
  166. /// <seealso cref="InputActionRebindingExtensions.SaveBindingOverridesAsJson(IInputActionCollection2)"/>
  167. /// <seealso cref="InputActionRebindingExtensions.LoadBindingOverridesFromJson(IInputActionCollection2,string,bool)"/>
  168. /// <seealso cref="InputActionRebindingExtensions.ApplyBindingOverride(InputAction,int,InputBinding)"/>
  169. public string overridePath
  170. {
  171. get => m_OverridePath;
  172. set => m_OverridePath = value;
  173. }
  174. /// <summary>
  175. /// Optional list of interactions and their parameters.
  176. /// </summary>
  177. /// <value>Interactions to put on the binding.</value>
  178. /// <remarks>
  179. /// Each element in the list is a name of an interaction (as registered with
  180. /// <see cref="InputSystem.RegisterInteraction{T}"/>) followed by an optional
  181. /// list of parameters.
  182. ///
  183. /// For example, <c>"slowTap(duration=1.2,pressPoint=0.123)"</c> is one element
  184. /// that puts a <see cref="Interactions.SlowTapInteraction"/> on the binding and
  185. /// sets <see cref="Interactions.SlowTapInteraction.duration"/> to 1.2 and
  186. /// <see cref="Interactions.SlowTapInteraction.pressPoint"/> to 0.123.
  187. ///
  188. /// Multiple interactions can be put on a binding by separating them with a comma.
  189. /// For example, <c>"tap,slowTap(duration=1.2)"</c> puts both a
  190. /// <see cref="Interactions.TapInteraction"/> and <see cref="Interactions.SlowTapInteraction"/>
  191. /// on the binding. See <see cref="IInputInteraction"/> for why the order matters.
  192. /// </remarks>
  193. /// <seealso cref="IInputInteraction"/>
  194. /// <seealso cref="overrideInteractions"/>
  195. /// <seealso cref="hasOverrides"/>
  196. /// <seealso cref="InputActionRebindingExtensions.SaveBindingOverridesAsJson(IInputActionCollection2)"/>
  197. /// <seealso cref="InputActionRebindingExtensions.LoadBindingOverridesFromJson(IInputActionCollection2,string,bool)"/>
  198. /// <seealso cref="InputActionRebindingExtensions.ApplyBindingOverride(InputAction,int,InputBinding)"/>
  199. public string interactions
  200. {
  201. get => m_Interactions;
  202. set => m_Interactions = value;
  203. }
  204. /// <summary>
  205. /// Interaction settings to override <see cref="interactions"/> with.
  206. /// </summary>
  207. /// <value>Override string for <see cref="interactions"/> or <c>null</c>.</value>
  208. /// <remarks>
  209. /// If this is not <c>null</c>, it replaces the value of <see cref="interactions"/>.
  210. /// </remarks>
  211. /// <seealso cref="effectiveInteractions"/>
  212. /// <seealso cref="interactions"/>
  213. /// <seealso cref="overridePath"/>
  214. /// <seealso cref="overrideProcessors"/>
  215. /// <seealso cref="hasOverrides"/>
  216. /// <seealso cref="InputActionRebindingExtensions.SaveBindingOverridesAsJson(IInputActionCollection2)"/>
  217. /// <seealso cref="InputActionRebindingExtensions.LoadBindingOverridesFromJson(IInputActionCollection2,string,bool)"/>
  218. /// <seealso cref="InputActionRebindingExtensions.ApplyBindingOverride(InputAction,int,InputBinding)"/>
  219. public string overrideInteractions
  220. {
  221. get => m_OverrideInteractions;
  222. set => m_OverrideInteractions = value;
  223. }
  224. /// <summary>
  225. /// Optional list of processors to apply to control values.
  226. /// </summary>
  227. /// <value>Value processors to apply to the binding.</value>
  228. /// <remarks>
  229. /// This string has the same format as <see cref="InputControlAttribute.processors"/>.
  230. /// </remarks>
  231. /// <seealso cref="InputProcessor{TValue}"/>
  232. /// <seealso cref="overrideProcessors"/>
  233. public string processors
  234. {
  235. get => m_Processors;
  236. set => m_Processors = value;
  237. }
  238. /// <summary>
  239. /// Processor settings to override <see cref="processors"/> with.
  240. /// </summary>
  241. /// <value>Override string for <see cref="processors"/> or <c>null</c>.</value>
  242. /// <remarks>
  243. /// If this is not <c>null</c>, it replaces the value of <see cref="processors"/>.
  244. /// </remarks>
  245. /// <seealso cref="effectiveProcessors"/>
  246. /// <seealso cref="processors"/>
  247. /// <seealso cref="overridePath"/>
  248. /// <seealso cref="overrideInteractions"/>
  249. /// <seealso cref="hasOverrides"/>
  250. public string overrideProcessors
  251. {
  252. get => m_OverrideProcessors;
  253. set => m_OverrideProcessors = value;
  254. }
  255. /// <summary>
  256. /// Optional list of binding groups that the binding belongs to.
  257. /// </summary>
  258. /// <value>List of binding groups or <c>null</c>.</value>
  259. /// <remarks>
  260. /// This is used, for example, to divide bindings into <see cref="InputControlScheme"/>s.
  261. /// Each control scheme is associated with a unique binding group through <see
  262. /// cref="InputControlScheme.bindingGroup"/>.
  263. ///
  264. /// A binding may be associated with multiple groups by listing each group name
  265. /// separate by a semicolon (<see cref="Separator"/>).
  266. ///
  267. /// <example>
  268. /// <code>
  269. /// new InputBinding
  270. /// {
  271. /// path = "*/{PrimaryAction},
  272. /// // Associate the binding both with the "KeyboardMouse" and
  273. /// // the "Gamepad" group.
  274. /// groups = "KeyboardMouse;Gamepad",
  275. /// }
  276. /// </code>
  277. /// </example>
  278. ///
  279. /// Note that the system places no restriction on what binding groups are used
  280. /// for in practice. Their use by <see cref="InputControlScheme"/> is only one
  281. /// possible one, but which groups to apply and how to use them is ultimately
  282. /// up to you.
  283. /// </remarks>
  284. /// <seealso cref="InputControlScheme.bindingGroup"/>
  285. public string groups
  286. {
  287. get => m_Groups;
  288. set => m_Groups = value;
  289. }
  290. /// <summary>
  291. /// Name or ID of the action triggered by the binding.
  292. /// </summary>
  293. /// <remarks>
  294. /// This is null if the binding does not trigger an action.
  295. ///
  296. /// For InputBindings that are used as masks, this can be a "mapName/actionName" combination
  297. /// or "mapName/*" to match all actions in the given map.
  298. /// </remarks>
  299. /// <seealso cref="InputAction.name"/>
  300. /// <seealso cref="InputAction.id"/>
  301. public string action
  302. {
  303. get => m_Action;
  304. set => m_Action = value;
  305. }
  306. /// <summary>
  307. /// Whether the binding is a composite.
  308. /// </summary>
  309. /// <value>True if the binding is a composite.</value>
  310. /// <remarks>
  311. /// Composite bindings to not bind to controls to themselves but rather source their
  312. /// input from one or more "part binding" (see <see cref="isPartOfComposite"/>).
  313. ///
  314. /// See <see cref="InputBindingComposite{TValue}"/> for more details.
  315. /// </remarks>
  316. /// <seealso cref="InputBindingComposite{TValue}"/>
  317. public bool isComposite
  318. {
  319. get => (m_Flags & Flags.Composite) == Flags.Composite;
  320. set
  321. {
  322. if (value)
  323. m_Flags |= Flags.Composite;
  324. else
  325. m_Flags &= ~Flags.Composite;
  326. }
  327. }
  328. /// <summary>
  329. /// Whether the binding is a "part binding" of a composite.
  330. /// </summary>
  331. /// <value>True if the binding is part of a composite.</value>
  332. /// <remarks>
  333. /// The bindings that make up a composite are laid out sequentially in <see cref="InputActionMap.bindings"/>.
  334. /// First comes the composite itself which is flagged with <see cref="isComposite"/>. It mentions
  335. /// the composite and its parameters in its <see cref="path"/> property. After the composite itself come
  336. /// the part bindings. All subsequent bindings marked as <c>isPartOfComposite</c> will be associated
  337. /// with the composite.
  338. /// </remarks>
  339. /// <seealso cref="isComposite"/>
  340. /// <seealso cref="InputBindingComposite{TValue}"/>
  341. public bool isPartOfComposite
  342. {
  343. get => (m_Flags & Flags.PartOfComposite) == Flags.PartOfComposite;
  344. set
  345. {
  346. if (value)
  347. m_Flags |= Flags.PartOfComposite;
  348. else
  349. m_Flags &= ~Flags.PartOfComposite;
  350. }
  351. }
  352. /// <summary>
  353. /// True if any of the override properties, that is, <see cref="overridePath"/>, <see cref="overrideProcessors"/>,
  354. /// and/or <see cref="overrideInteractions"/>, are set (not <c>null</c>).
  355. /// </summary>
  356. public bool hasOverrides => overridePath != null || overrideProcessors != null || overrideInteractions != null;
  357. /// <summary>
  358. /// Initialize a new binding.
  359. /// </summary>
  360. /// <param name="path">Path for the binding.</param>
  361. /// <param name="action">Action to trigger from the binding.</param>
  362. /// <param name="groups">Semicolon-separated list of binding <see cref="InputBinding.groups"/> the binding is associated with.</param>
  363. /// <param name="processors">Comma-separated list of <see cref="InputBinding.processors"/> to apply to the binding.</param>
  364. /// <param name="interactions">Comma-separated list of <see cref="InputBinding.interactions"/> to apply to the
  365. /// binding.</param>
  366. /// <param name="name">Optional name for the binding.</param>
  367. public InputBinding(string path, string action = null, string groups = null, string processors = null,
  368. string interactions = null, string name = null)
  369. {
  370. m_Path = path;
  371. m_Action = action;
  372. m_Groups = groups;
  373. m_Processors = processors;
  374. m_Interactions = interactions;
  375. m_Name = name;
  376. m_Id = default;
  377. m_Flags = default;
  378. m_OverridePath = default;
  379. m_OverrideInteractions = default;
  380. m_OverrideProcessors = default;
  381. }
  382. public string GetNameOfComposite()
  383. {
  384. if (!isComposite)
  385. return null;
  386. return NameAndParameters.Parse(effectivePath).name;
  387. }
  388. internal void GenerateId()
  389. {
  390. m_Id = Guid.NewGuid().ToString();
  391. }
  392. internal void RemoveOverrides()
  393. {
  394. m_OverridePath = null;
  395. m_OverrideInteractions = null;
  396. m_OverrideProcessors = null;
  397. }
  398. public static InputBinding MaskByGroup(string group)
  399. {
  400. return new InputBinding {groups = group};
  401. }
  402. public static InputBinding MaskByGroups(params string[] groups)
  403. {
  404. return new InputBinding {groups = string.Join(kSeparatorString, groups.Where(x => !string.IsNullOrEmpty(x)))};
  405. }
  406. [SerializeField] private string m_Name;
  407. [SerializeField] internal string m_Id;
  408. [Tooltip("Path of the control to bind to. Matched at runtime to controls from InputDevices present at the time.\n\nCan either be "
  409. + "graphically from the control picker dropdown UI or edited manually in text mode by clicking the 'T' button. Internally, both "
  410. + "methods result in control path strings that look like, for example, \"<Gamepad>/buttonSouth\".")]
  411. [SerializeField] private string m_Path;
  412. [SerializeField] private string m_Interactions;
  413. [SerializeField] private string m_Processors;
  414. [SerializeField] internal string m_Groups;
  415. [SerializeField] private string m_Action;
  416. [SerializeField] internal Flags m_Flags;
  417. [NonSerialized] private string m_OverridePath;
  418. [NonSerialized] private string m_OverrideInteractions;
  419. [NonSerialized] private string m_OverrideProcessors;
  420. /// <summary>
  421. /// This is the bindings path which is effectively being used.
  422. /// </summary>
  423. /// <remarks>
  424. /// This is either <see cref="overridePath"/> if that is set, or <see cref="path"/> otherwise.
  425. /// </remarks>
  426. public string effectivePath => overridePath ?? path;
  427. /// <summary>
  428. /// This is the interaction config which is effectively being used.
  429. /// </summary>
  430. /// <remarks>
  431. /// This is either <see cref="overrideInteractions"/> if that is set, or <see cref="interactions"/> otherwise.
  432. /// </remarks>
  433. public string effectiveInteractions => overrideInteractions ?? interactions;
  434. /// <summary>
  435. /// This is the processor config which is effectively being used.
  436. /// </summary>
  437. /// <remarks>
  438. /// This is either <see cref="overrideProcessors"/> if that is set, or <see cref="processors"/> otherwise.
  439. /// </remarks>
  440. public string effectiveProcessors => overrideProcessors ?? processors;
  441. internal bool isEmpty =>
  442. string.IsNullOrEmpty(effectivePath) && string.IsNullOrEmpty(action) &&
  443. string.IsNullOrEmpty(groups);
  444. /// <summary>
  445. /// Check whether the binding is equivalent to the given binding.
  446. /// </summary>
  447. /// <param name="other">Another binding.</param>
  448. /// <returns>True if the two bindings are equivalent.</returns>
  449. /// <remarks>
  450. /// Bindings are equivalent if their <see cref="effectivePath"/>, <see cref="effectiveInteractions"/>,
  451. /// and <see cref="effectiveProcessors"/>, plus their <see cref="action"/> and <see cref="groups"/>
  452. /// properties are the same. Note that the string comparisons ignore both case and culture.
  453. /// </remarks>
  454. public bool Equals(InputBinding other)
  455. {
  456. return string.Equals(effectivePath, other.effectivePath, StringComparison.InvariantCultureIgnoreCase) &&
  457. string.Equals(effectiveInteractions, other.effectiveInteractions, StringComparison.InvariantCultureIgnoreCase) &&
  458. string.Equals(effectiveProcessors, other.effectiveProcessors, StringComparison.InvariantCultureIgnoreCase) &&
  459. string.Equals(groups, other.groups, StringComparison.InvariantCultureIgnoreCase) &&
  460. string.Equals(action, other.action, StringComparison.InvariantCultureIgnoreCase);
  461. }
  462. /// <summary>
  463. /// Compare the binding to the given object.
  464. /// </summary>
  465. /// <param name="obj">An object. May be <c>null</c>.</param>
  466. /// <returns>True if the given object is an <c>InputBinding</c> that equals this one.</returns>
  467. /// <seealso cref="Equals(InputBinding)"/>
  468. public override bool Equals(object obj)
  469. {
  470. if (ReferenceEquals(null, obj))
  471. return false;
  472. return obj is InputBinding binding && Equals(binding);
  473. }
  474. /// <summary>
  475. /// Compare the two bindings for equality.
  476. /// </summary>
  477. /// <param name="left">The first binding.</param>
  478. /// <param name="right">The second binding.</param>
  479. /// <returns>True if the two bindings are equal.</returns>
  480. /// <seealso cref="Equals(InputBinding)"/>
  481. public static bool operator==(InputBinding left, InputBinding right)
  482. {
  483. return left.Equals(right);
  484. }
  485. /// <summary>
  486. /// Compare the two bindings for inequality.
  487. /// </summary>
  488. /// <param name="left">The first binding.</param>
  489. /// <param name="right">The second binding.</param>
  490. /// <returns>True if the two bindings are not equal.</returns>
  491. /// <seealso cref="Equals(InputBinding)"/>
  492. public static bool operator!=(InputBinding left, InputBinding right)
  493. {
  494. return !(left == right);
  495. }
  496. /// <summary>
  497. /// Compute a hash code for the binding.
  498. /// </summary>
  499. /// <returns>A hash code.</returns>
  500. public override int GetHashCode()
  501. {
  502. unchecked
  503. {
  504. var hashCode = (effectivePath != null ? effectivePath.GetHashCode() : 0);
  505. hashCode = (hashCode * 397) ^ (effectiveInteractions != null ? effectiveInteractions.GetHashCode() : 0);
  506. hashCode = (hashCode * 397) ^ (effectiveProcessors != null ? effectiveProcessors.GetHashCode() : 0);
  507. hashCode = (hashCode * 397) ^ (groups != null ? groups.GetHashCode() : 0);
  508. hashCode = (hashCode * 397) ^ (action != null ? action.GetHashCode() : 0);
  509. return hashCode;
  510. }
  511. }
  512. /// <summary>
  513. /// Return a string representation of the binding useful for debugging.
  514. /// </summary>
  515. /// <returns>A string representation of the binding.</returns>
  516. /// <example>
  517. /// <code>
  518. /// var binding = new InputBinding
  519. /// {
  520. /// action = "fire",
  521. /// path = "&lt;Gamepad&gt;/buttonSouth",
  522. /// groups = "Gamepad"
  523. /// };
  524. ///
  525. /// // Returns "fire: &lt;Gamepad&gt;/buttonSouth [Gamepad]".
  526. /// binding.ToString();
  527. /// </code>
  528. /// </example>
  529. public override string ToString()
  530. {
  531. var builder = new StringBuilder();
  532. // Add action.
  533. if (!string.IsNullOrEmpty(action))
  534. {
  535. builder.Append(action);
  536. builder.Append(':');
  537. }
  538. // Add path.
  539. var path = effectivePath;
  540. if (!string.IsNullOrEmpty(path))
  541. builder.Append(path);
  542. // Add groups.
  543. if (!string.IsNullOrEmpty(groups))
  544. {
  545. builder.Append('[');
  546. builder.Append(groups);
  547. builder.Append(']');
  548. }
  549. return builder.ToString();
  550. }
  551. /// <summary>
  552. /// A set of flags to turn individual default behaviors of <see cref="InputBinding.ToDisplayString(DisplayStringOptions,InputControl)"/> off.
  553. /// </summary>
  554. [Flags]
  555. public enum DisplayStringOptions
  556. {
  557. /// <summary>
  558. /// Do not use short names of controls as set up by <see cref="InputControlAttribute.shortDisplayName"/>
  559. /// and the <c>"shortDisplayName"</c> property in JSON. This will, for example, not use LMB instead of "left Button"
  560. /// on <see cref="Mouse.leftButton"/>.
  561. /// </summary>
  562. DontUseShortDisplayNames = 1 << 0,
  563. /// <summary>
  564. /// By default device names are omitted from display strings. With this option, they are included instead.
  565. /// For example, <c>"A"</c> will be <c>"A [Gamepad]"</c> instead.
  566. /// </summary>
  567. DontOmitDevice = 1 << 1,
  568. /// <summary>
  569. /// By default, interactions on bindings are included in the resulting display string. For example, a binding to
  570. /// the gamepad's A button that has a "Hold" interaction on it, would come out as "Hold A". This can be suppressed
  571. /// with this option in which case the same setup would come out as just "A".
  572. /// </summary>
  573. DontIncludeInteractions = 1 << 2,
  574. /// <summary>
  575. /// By default, <see cref="effectivePath"/> is used for generating a display name. Using this option, the display
  576. /// string can be forced to <see cref="path"/> instead.
  577. /// </summary>
  578. IgnoreBindingOverrides = 1 << 3,
  579. }
  580. /// <summary>
  581. /// Turn the binding into a string suitable for display in a UI.
  582. /// </summary>
  583. /// <param name="options">Optional set of formatting options.</param>
  584. /// <param name="control">Optional control to which the binding has been resolved. If this is supplied,
  585. /// the resulting string can reflect things such as the current keyboard layout or hardware/platform-specific
  586. /// naming of controls (e.g. Xbox vs PS4 controllers as opposed to naming things generically based on the
  587. /// <see cref="Gamepad"/> layout).</param>
  588. /// <returns>A string representation of the binding suitable for display in a UI.</returns>
  589. /// <remarks>
  590. /// This method works only for bindings that are not composites. If the method is called on a binding
  591. /// that is a composite (<see cref="isComposite"/> is true), an empty string will be returned. To automatically
  592. /// handle composites, use <see cref="InputActionRebindingExtensions.GetBindingDisplayString(InputAction,DisplayStringOptions,string)"/>
  593. /// instead.
  594. ///
  595. /// <example>
  596. /// <code>
  597. /// var gamepadBinding = new InputBinding("&lt;Gamepad&gt;/buttonSouth");
  598. /// var mouseBinding = new InputBinding("&lt;Mouse&gt;/leftButton");
  599. /// var keyboardBinding = new InputBinding("&lt;Keyboard&gt;/a");
  600. ///
  601. /// // Prints "A" except on PS4 where it prints "Cross".
  602. /// Debug.Log(gamepadBinding.ToDisplayString());
  603. ///
  604. /// // Prints "LMB".
  605. /// Debug.Log(mouseBinding.ToDisplayString());
  606. ///
  607. /// // Print "Left Button".
  608. /// Debug.Log(mouseBinding.ToDisplayString(DisplayStringOptions.DontUseShortDisplayNames));
  609. ///
  610. /// // Prints the character associated with the "A" key on the current keyboard layout.
  611. /// Debug.Log(keyboardBinding, control: Keyboard.current);
  612. /// </code>
  613. /// </example>
  614. /// </remarks>
  615. /// <seealso cref="InputControlPath.ToHumanReadableString(string,InputControlPath.HumanReadableStringOptions,InputControl)"/>
  616. /// <seealso cref="InputActionRebindingExtensions.GetBindingDisplayString(InputAction,int,InputBinding.DisplayStringOptions)"/>
  617. public string ToDisplayString(DisplayStringOptions options = default, InputControl control = default)
  618. {
  619. return ToDisplayString(out var _, out var _, options, control);
  620. }
  621. /// <summary>
  622. /// Turn the binding into a string suitable for display in a UI.
  623. /// </summary>
  624. /// <param name="options">Optional set of formatting options.</param>
  625. /// <param name="control">Optional control to which the binding has been resolved. If this is supplied,
  626. /// the resulting string can reflect things such as the current keyboard layout or hardware/platform-specific
  627. /// naming of controls (e.g. Xbox vs PS4 controllers as opposed to naming things generically based on the
  628. /// <see cref="Gamepad"/> layout).</param>
  629. /// <returns>A string representation of the binding suitable for display in a UI.</returns>
  630. /// <remarks>
  631. /// This method is the same as <see cref="ToDisplayString(DisplayStringOptions,InputControl)"/> except that it
  632. /// will also return the name of the device layout and path of the control, if applicable to the binding. This is
  633. /// useful when needing more context on the resulting display string, for example to decide on an icon to display
  634. /// instead of the textual display string.
  635. ///
  636. /// <example>
  637. /// <code>
  638. /// var displayString = new InputBinding("&lt;Gamepad&gt;/dpad/up")
  639. /// .ToDisplayString(out deviceLayout, out controlPath);
  640. ///
  641. /// // Will print "D-Pad Up".
  642. /// Debug.Log(displayString);
  643. ///
  644. /// // Will print "Gamepad".
  645. /// Debug.Log(deviceLayout);
  646. ///
  647. /// // Will print "dpad/up".
  648. /// Debug.Log(controlPath);
  649. /// </code>
  650. /// </example>
  651. /// </remarks>
  652. /// <seealso cref="InputControlPath.ToHumanReadableString(string,out string,out string,InputControlPath.HumanReadableStringOptions,InputControl)"/>
  653. /// <seealso cref="InputActionRebindingExtensions.GetBindingDisplayString(InputAction,int,out string,out string,InputBinding.DisplayStringOptions)"/>
  654. public string ToDisplayString(out string deviceLayoutName, out string controlPath, DisplayStringOptions options = default,
  655. InputControl control = default)
  656. {
  657. if (isComposite)
  658. {
  659. deviceLayoutName = null;
  660. controlPath = null;
  661. return string.Empty;
  662. }
  663. var readableStringOptions = default(InputControlPath.HumanReadableStringOptions);
  664. if ((options & DisplayStringOptions.DontOmitDevice) == 0)
  665. readableStringOptions |= InputControlPath.HumanReadableStringOptions.OmitDevice;
  666. if ((options & DisplayStringOptions.DontUseShortDisplayNames) == 0)
  667. readableStringOptions |= InputControlPath.HumanReadableStringOptions.UseShortNames;
  668. var pathToUse = (options & DisplayStringOptions.IgnoreBindingOverrides) != 0
  669. ? path
  670. : effectivePath;
  671. var result = InputControlPath.ToHumanReadableString(pathToUse, out deviceLayoutName, out controlPath, readableStringOptions, control);
  672. if (!string.IsNullOrEmpty(effectiveInteractions) && (options & DisplayStringOptions.DontIncludeInteractions) == 0)
  673. {
  674. var interactionString = string.Empty;
  675. var parsedInteractions = NameAndParameters.ParseMultiple(effectiveInteractions);
  676. foreach (var element in parsedInteractions)
  677. {
  678. var interaction = element.name;
  679. var interactionDisplayName = InputInteraction.GetDisplayName(interaction);
  680. // An interaction can avoid being mentioned explicitly by just setting its display
  681. // name to an empty string.
  682. if (string.IsNullOrEmpty(interactionDisplayName))
  683. continue;
  684. ////TODO: this will need support for localization
  685. if (!string.IsNullOrEmpty(interactionString))
  686. interactionString = $"{interactionString} or {interactionDisplayName}";
  687. else
  688. interactionString = interactionDisplayName;
  689. }
  690. if (!string.IsNullOrEmpty(interactionString))
  691. result = $"{interactionString} {result}";
  692. }
  693. return result;
  694. }
  695. internal bool TriggersAction(InputAction action)
  696. {
  697. // Match both name and ID on binding.
  698. return string.Compare(action.name, this.action, StringComparison.InvariantCultureIgnoreCase) == 0
  699. || this.action == action.m_Id;
  700. }
  701. ////TODO: also support matching by name (taking the binding tree into account so that components
  702. //// of composites can be referenced through their parent)
  703. /// <summary>
  704. /// Check whether <paramref name="binding"/> matches the mask
  705. /// represented by the current binding.
  706. /// </summary>
  707. /// <param name="binding">An input binding.</param>
  708. /// <returns>True if <paramref name="binding"/> is matched by the mask represented
  709. /// by <c>this</c>.</returns>
  710. /// <remarks>
  711. /// In this method, the current binding acts as a "mask". When used this way, only
  712. /// three properties of the binding are taken into account: <see cref="path"/>,
  713. /// <see cref="groups"/>, and <see cref="action"/>.
  714. ///
  715. /// For each of these properties, the method checks whether they are set on the current
  716. /// binding and, if so, matches them against the respective property in <paramref name="binding"/>.
  717. ///
  718. /// The way this matching works is that the value of the property in the current binding is
  719. /// allowed to be a semicolon-separated list where each element specifies one possible value
  720. /// that will produce a match.
  721. ///
  722. /// Note that all comparisons are case-insensitive.
  723. ///
  724. /// <example>
  725. /// <code>
  726. /// // Create a couple bindings which we can test against.
  727. /// var keyboardBinding = new InputBinding
  728. /// {
  729. /// path = "&lt;Keyboard&gt;/space",
  730. /// groups = "Keyboard",
  731. /// action = "Fire"
  732. /// };
  733. /// var gamepadBinding = new InputBinding
  734. /// {
  735. /// path = "&lt;Gamepad&gt;/buttonSouth",
  736. /// groups = "Gamepad",
  737. /// action = "Jump"
  738. /// };
  739. /// var touchBinding = new InputBinding
  740. /// {
  741. /// path = "&lt;Touchscreen&gt;/*/tap",
  742. /// groups = "Touch",
  743. /// action = "Jump"
  744. /// };
  745. ///
  746. /// // Example 1: Match any binding in the "Keyboard" or "Gamepad" group.
  747. /// var mask1 = new InputBinding
  748. /// {
  749. /// // We put two elements in the list here and separate them with a semicolon.
  750. /// groups = "Keyboard;Gamepad"
  751. /// };
  752. ///
  753. /// mask1.Matches(keyboardBinding); // True
  754. /// mask1.Matches(gamepadBinding); // True
  755. /// mask1.Matches(touchBinding); // False
  756. ///
  757. /// // Example 2: Match any binding to the "Jump" or the "Roll" action
  758. /// // (the latter we don't actually have a binding for)
  759. /// var mask2 = new InputBinding
  760. /// {
  761. /// action = "Jump;Roll"
  762. /// };
  763. ///
  764. /// mask2.Matches(keyboardBinding); // False
  765. /// mask2.Matches(gamepadBinding); // True
  766. /// mask2.Matches(touchBinding); // True
  767. ///
  768. /// // Example: Match any binding to the space or enter key in the
  769. /// // "Keyboard" group.
  770. /// var mask3 = new InputBinding
  771. /// {
  772. /// path = "&lt;Keyboard&gt;/space;&lt;Keyboard&gt;/enter",
  773. /// groups = "Keyboard"
  774. /// };
  775. ///
  776. /// mask3.Matches(keyboardBinding); // True
  777. /// mask3.Matches(gamepadBinding); // False
  778. /// mask3.Matches(touchBinding); // False
  779. /// </code>
  780. /// </example>
  781. /// </remarks>
  782. public bool Matches(InputBinding binding)
  783. {
  784. return Matches(ref binding);
  785. }
  786. // Internally we pass by reference to not unnecessarily copy the struct.
  787. internal bool Matches(ref InputBinding binding, MatchOptions options = default)
  788. {
  789. // Match name.
  790. if (name != null)
  791. {
  792. if (binding.name == null
  793. || !StringHelpers.CharacterSeparatedListsHaveAtLeastOneCommonElement(name, binding.name, Separator))
  794. return false;
  795. }
  796. // Match path.
  797. if (path != null)
  798. {
  799. ////REVIEW: should this use binding.effectivePath?
  800. ////REVIEW: should this support matching by prefix only? should this use control path matching instead of just string comparisons?
  801. ////TODO: handle things like ignoring leading '/'
  802. if (binding.path == null
  803. || !StringHelpers.CharacterSeparatedListsHaveAtLeastOneCommonElement(path, binding.path, Separator))
  804. return false;
  805. }
  806. // Match action.
  807. if (action != null)
  808. {
  809. ////TODO: handle "map/action" format
  810. ////TODO: handle "map/*" format
  811. ////REVIEW: this will not be able to handle cases where one binding references an action by ID and the other by name but both do mean the same action
  812. if (binding.action == null
  813. || !StringHelpers.CharacterSeparatedListsHaveAtLeastOneCommonElement(action, binding.action, Separator))
  814. return false;
  815. }
  816. // Match groups.
  817. if (groups != null)
  818. {
  819. var haveGroupsOnBinding = !string.IsNullOrEmpty(binding.groups);
  820. if (!haveGroupsOnBinding && (options & MatchOptions.EmptyGroupMatchesAny) == 0)
  821. return false;
  822. if (haveGroupsOnBinding
  823. && !StringHelpers.CharacterSeparatedListsHaveAtLeastOneCommonElement(groups, binding.groups, Separator))
  824. return false;
  825. }
  826. // Match ID.
  827. if (!string.IsNullOrEmpty(m_Id))
  828. {
  829. if (binding.id != id)
  830. return false;
  831. }
  832. return true;
  833. }
  834. [Flags]
  835. internal enum MatchOptions
  836. {
  837. EmptyGroupMatchesAny = 1 << 0,
  838. }
  839. [Flags]
  840. internal enum Flags
  841. {
  842. None = 0,
  843. Composite = 1 << 2,
  844. PartOfComposite = 1 << 3,
  845. }
  846. }
  847. }