InputBinding.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. public string path
  125. {
  126. get => m_Path;
  127. set => m_Path = value;
  128. }
  129. /// <summary>
  130. /// If the binding is overridden, this is the overriding path.
  131. /// Otherwise it is null.
  132. /// </summary>
  133. /// <value>Path to override the <see cref="path"/> property with.</value>
  134. /// <remarks>
  135. /// Unlike the <see cref="path"/> property, the value of the override path is not serialized.
  136. /// If set, it will take precedence and determine the result of <see cref="effectivePath"/>.
  137. ///
  138. /// This property can be set to an empty string to disable the binding. During resolution,
  139. /// bindings with an empty <see cref="effectivePath"/> will get skipped.
  140. ///
  141. /// To set the override on an existing binding, use the methods supplied by <see cref="InputActionRebindingExtensions"/>
  142. /// such as <see cref="InputActionRebindingExtensions.ApplyBindingOverride(InputAction,string,string,string)"/>.
  143. ///
  144. /// <example>
  145. /// <code>
  146. /// // Override the binding to &lt;Gamepad&gt;/buttonSouth on
  147. /// // myAction with a binding to &lt;Gamepad&gt;/buttonNorth.
  148. /// myAction.ApplyBindingOverride(
  149. /// new InputBinding
  150. /// {
  151. /// path = "&lt;Gamepad&gt;/buttonSouth",
  152. /// overridePath = "&lt;Gamepad&gt;/buttonNorth"
  153. /// });
  154. /// </code>
  155. /// </example>
  156. /// </remarks>
  157. public string overridePath
  158. {
  159. get => m_OverridePath;
  160. set => m_OverridePath = value;
  161. }
  162. /// <summary>
  163. /// Optional list of interactions and their parameters.
  164. /// </summary>
  165. /// <value>Interactions to put on the binding.</value>
  166. /// <remarks>
  167. /// Each element in the list is a name of an interaction (as registered with
  168. /// <see cref="InputSystem.RegisterInteraction{T}"/>) followed by an optional
  169. /// list of parameters.
  170. ///
  171. /// For example, <c>"slowTap(duration=1.2,pressPoint=0.123)"</c> is one element
  172. /// that puts a <see cref="Interactions.SlowTapInteraction"/> on the binding and
  173. /// sets <see cref="Interactions.SlowTapInteraction.duration"/> to 1.2 and
  174. /// <see cref="Interactions.SlowTapInteraction.pressPoint"/> to 0.123.
  175. ///
  176. /// Multiple interactions can be put on a binding by separating them with a comma.
  177. /// For example, <c>"tap,slowTap(duration=1.2)"</c> puts both a
  178. /// <see cref="Interactions.TapInteraction"/> and <see cref="Interactions.SlowTapInteraction"/>
  179. /// on the binding. See <see cref="IInputInteraction"/> for why the order matters.
  180. /// </remarks>
  181. /// <seealso cref="IInputInteraction"/>
  182. public string interactions
  183. {
  184. get => m_Interactions;
  185. set => m_Interactions = value;
  186. }
  187. /// <summary>
  188. /// Interaction settings to override <see cref="interactions"/> with.
  189. /// </summary>
  190. /// <value>Override string for <see cref="interactions"/> or <c>null</c>.</value>
  191. /// <remarks>
  192. /// If this is not <c>null</c>, it replaces the value of <see cref="interactions"/>.
  193. /// </remarks>
  194. /// <seealso cref="effectiveInteractions"/>
  195. public string overrideInteractions
  196. {
  197. get => m_OverrideInteractions;
  198. set => m_OverrideInteractions = value;
  199. }
  200. /// <summary>
  201. /// Optional list of processors to apply to control values.
  202. /// </summary>
  203. /// <value>Value processors to apply to the binding.</value>
  204. /// <remarks>
  205. /// This string has the same format as <see cref="InputControlAttribute.processors"/>.
  206. /// </remarks>
  207. /// <seealso cref="InputProcessor{TValue}"/>
  208. public string processors
  209. {
  210. get => m_Processors;
  211. set => m_Processors = value;
  212. }
  213. /// <summary>
  214. /// Processor settings to override <see cref="processors"/> with.
  215. /// </summary>
  216. /// <value>Override string for <see cref="processors"/> or <c>null</c>.</value>
  217. /// <remarks>
  218. /// If this is not <c>null</c>, it replaces the value of <see cref="processors"/>.
  219. /// </remarks>
  220. /// <seealso cref="effectiveProcessors"/>
  221. public string overrideProcessors
  222. {
  223. get => m_OverrideProcessors;
  224. set => m_OverrideProcessors = value;
  225. }
  226. /// <summary>
  227. /// Optional list of binding groups that the binding belongs to.
  228. /// </summary>
  229. /// <value>List of binding groups or <c>null</c>.</value>
  230. /// <remarks>
  231. /// This is used, for example, to divide bindings into <see cref="InputControlScheme"/>s.
  232. /// Each control scheme is associated with a unique binding group through <see
  233. /// cref="InputControlScheme.bindingGroup"/>.
  234. ///
  235. /// A binding may be associated with multiple groups by listing each group name
  236. /// separate by a semicolon (<see cref="Separator"/>).
  237. ///
  238. /// <example>
  239. /// <code>
  240. /// new InputBinding
  241. /// {
  242. /// path = "*/{PrimaryAction},
  243. /// // Associate the binding both with the "KeyboardMouse" and
  244. /// // the "Gamepad" group.
  245. /// groups = "KeyboardMouse;Gamepad",
  246. /// }
  247. /// </code>
  248. /// </example>
  249. ///
  250. /// Note that the system places no restriction on what binding groups are used
  251. /// for in practice. Their use by <see cref="InputControlScheme"/> is only one
  252. /// possible one, but which groups to apply and how to use them is ultimately
  253. /// up to you.
  254. /// </remarks>
  255. /// <seealso cref="InputControlScheme.bindingGroup"/>
  256. public string groups
  257. {
  258. get => m_Groups;
  259. set => m_Groups = value;
  260. }
  261. /// <summary>
  262. /// Name or ID of the action triggered by the binding.
  263. /// </summary>
  264. /// <remarks>
  265. /// This is null if the binding does not trigger an action.
  266. ///
  267. /// For InputBindings that are used as masks, this can be a "mapName/actionName" combination
  268. /// or "mapName/*" to match all actions in the given map.
  269. /// </remarks>
  270. /// <seealso cref="InputAction.name"/>
  271. /// <seealso cref="InputAction.id"/>
  272. public string action
  273. {
  274. get => m_Action;
  275. set => m_Action = value;
  276. }
  277. /// <summary>
  278. /// Whether the binding is a composite.
  279. /// </summary>
  280. /// <value>True if the binding is a composite.</value>
  281. /// <remarks>
  282. /// Composite bindings to not bind to controls to themselves but rather source their
  283. /// input from one or more "part binding" (see <see cref="isPartOfComposite"/>).
  284. ///
  285. /// See <see cref="InputBindingComposite{TValue}"/> for more details.
  286. /// </remarks>
  287. /// <seealso cref="InputBindingComposite{TValue}"/>
  288. public bool isComposite
  289. {
  290. get => (m_Flags & Flags.Composite) == Flags.Composite;
  291. set
  292. {
  293. if (value)
  294. m_Flags |= Flags.Composite;
  295. else
  296. m_Flags &= ~Flags.Composite;
  297. }
  298. }
  299. /// <summary>
  300. /// Whether the binding is a "part binding" of a composite.
  301. /// </summary>
  302. /// <value>True if the binding is part of a composite.</value>
  303. /// <remarks>
  304. /// The bindings that make up a composite are laid out sequentially in <see cref="InputActionMap.bindings"/>.
  305. /// First comes the composite itself which is flagged with <see cref="isComposite"/>. It mentions
  306. /// the composite and its parameters in its <see cref="path"/> property. After the composite itself come
  307. /// the part bindings. All subsequent bindings marked as <c>isPartOfComposite</c> will be associated
  308. /// with the composite.
  309. /// </remarks>
  310. /// <seealso cref="isComposite"/>
  311. /// <seealso cref="InputBindingComposite{TValue}"/>
  312. public bool isPartOfComposite
  313. {
  314. get => (m_Flags & Flags.PartOfComposite) == Flags.PartOfComposite;
  315. set
  316. {
  317. if (value)
  318. m_Flags |= Flags.PartOfComposite;
  319. else
  320. m_Flags &= ~Flags.PartOfComposite;
  321. }
  322. }
  323. /// <summary>
  324. /// Initialize a new binding.
  325. /// </summary>
  326. /// <param name="path">Path for the binding.</param>
  327. /// <param name="action">Action to trigger from the binding.</param>
  328. /// <param name="groups">Semicolon-separated list of binding <see cref="InputBinding.groups"/> the binding is associated with.</param>
  329. /// <param name="processors">Comma-separated list of <see cref="InputBinding.processors"/> to apply to the binding.</param>
  330. /// <param name="interactions">Comma-separated list of <see cref="InputBinding.interactions"/> to apply to the
  331. /// binding.</param>
  332. /// <param name="name">Optional name for the binding.</param>
  333. public InputBinding(string path, string action = null, string groups = null, string processors = null,
  334. string interactions = null, string name = null)
  335. {
  336. m_Path = path;
  337. m_Action = action;
  338. m_Groups = groups;
  339. m_Processors = processors;
  340. m_Interactions = interactions;
  341. m_Name = name;
  342. m_Id = default;
  343. m_Flags = default;
  344. m_OverridePath = default;
  345. m_OverrideInteractions = default;
  346. m_OverrideProcessors = default;
  347. }
  348. public string GetNameOfComposite()
  349. {
  350. if (!isComposite)
  351. return null;
  352. return NameAndParameters.Parse(effectivePath).name;
  353. }
  354. internal void GenerateId()
  355. {
  356. m_Id = Guid.NewGuid().ToString();
  357. }
  358. public static InputBinding MaskByGroup(string group)
  359. {
  360. return new InputBinding {groups = group};
  361. }
  362. public static InputBinding MaskByGroups(params string[] groups)
  363. {
  364. return new InputBinding {groups = string.Join(kSeparatorString, groups.Where(x => !string.IsNullOrEmpty(x)))};
  365. }
  366. [SerializeField] private string m_Name;
  367. [SerializeField] internal string m_Id;
  368. [SerializeField] private string m_Path;
  369. [SerializeField] private string m_Interactions;
  370. [SerializeField] private string m_Processors;
  371. [SerializeField] private string m_Groups;
  372. [SerializeField] private string m_Action;
  373. [SerializeField] internal Flags m_Flags;
  374. [NonSerialized] private string m_OverridePath;
  375. [NonSerialized] private string m_OverrideInteractions;
  376. [NonSerialized] private string m_OverrideProcessors;
  377. /// <summary>
  378. /// This is the bindings path which is effectively being used.
  379. /// </summary>
  380. /// <remarks>
  381. /// This is either <see cref="overridePath"/> if that is set, or <see cref="path"/> otherwise.
  382. /// </remarks>
  383. public string effectivePath => overridePath ?? path;
  384. /// <summary>
  385. /// This is the interaction config which is effectively being used.
  386. /// </summary>
  387. /// <remarks>
  388. /// This is either <see cref="overrideInteractions"/> if that is set, or <see cref="interactions"/> otherwise.
  389. /// </remarks>
  390. public string effectiveInteractions => overrideInteractions ?? interactions;
  391. /// <summary>
  392. /// This is the processor config which is effectively being used.
  393. /// </summary>
  394. /// <remarks>
  395. /// This is either <see cref="overrideProcessors"/> if that is set, or <see cref="processors"/> otherwise.
  396. /// </remarks>
  397. public string effectiveProcessors => overrideProcessors ?? processors;
  398. internal bool isEmpty =>
  399. string.IsNullOrEmpty(effectivePath) && string.IsNullOrEmpty(action) &&
  400. string.IsNullOrEmpty(groups);
  401. /// <summary>
  402. /// Check whether the binding is equivalent to the given binding.
  403. /// </summary>
  404. /// <param name="other">Another binding.</param>
  405. /// <returns>True if the two bindings are equivalent.</returns>
  406. /// <remarks>
  407. /// Bindings are equivalent if their <see cref="effectivePath"/>, <see cref="effectiveInteractions"/>,
  408. /// and <see cref="effectiveProcessors"/>, plus their <see cref="action"/> and <see cref="groups"/>
  409. /// properties are the same. Note that the string comparisons ignore both case and culture.
  410. /// </remarks>
  411. public bool Equals(InputBinding other)
  412. {
  413. return string.Equals(effectivePath, other.effectivePath, StringComparison.InvariantCultureIgnoreCase) &&
  414. string.Equals(effectiveInteractions, other.effectiveInteractions, StringComparison.InvariantCultureIgnoreCase) &&
  415. string.Equals(effectiveProcessors, other.effectiveProcessors, StringComparison.InvariantCultureIgnoreCase) &&
  416. string.Equals(groups, other.groups, StringComparison.InvariantCultureIgnoreCase) &&
  417. string.Equals(action, other.action, StringComparison.InvariantCultureIgnoreCase);
  418. }
  419. /// <summary>
  420. /// Compare the binding to the given object.
  421. /// </summary>
  422. /// <param name="obj">An object. May be <c>null</c>.</param>
  423. /// <returns>True if the given object is an <c>InputBinding</c> that equals this one.</returns>
  424. /// <seealso cref="Equals(InputBinding)"/>
  425. public override bool Equals(object obj)
  426. {
  427. if (ReferenceEquals(null, obj))
  428. return false;
  429. return obj is InputBinding binding && Equals(binding);
  430. }
  431. /// <summary>
  432. /// Compare the two bindings for equality.
  433. /// </summary>
  434. /// <param name="left">The first binding.</param>
  435. /// <param name="right">The second binding.</param>
  436. /// <returns>True if the two bindings are equal.</returns>
  437. /// <seealso cref="Equals(InputBinding)"/>
  438. public static bool operator==(InputBinding left, InputBinding right)
  439. {
  440. return left.Equals(right);
  441. }
  442. /// <summary>
  443. /// Compare the two bindings for inequality.
  444. /// </summary>
  445. /// <param name="left">The first binding.</param>
  446. /// <param name="right">The second binding.</param>
  447. /// <returns>True if the two bindings are not equal.</returns>
  448. /// <seealso cref="Equals(InputBinding)"/>
  449. public static bool operator!=(InputBinding left, InputBinding right)
  450. {
  451. return !(left == right);
  452. }
  453. /// <summary>
  454. /// Compute a hash code for the binding.
  455. /// </summary>
  456. /// <returns>A hash code.</returns>
  457. public override int GetHashCode()
  458. {
  459. unchecked
  460. {
  461. var hashCode = (effectivePath != null ? effectivePath.GetHashCode() : 0);
  462. hashCode = (hashCode * 397) ^ (effectiveInteractions != null ? effectiveInteractions.GetHashCode() : 0);
  463. hashCode = (hashCode * 397) ^ (effectiveProcessors != null ? effectiveProcessors.GetHashCode() : 0);
  464. hashCode = (hashCode * 397) ^ (groups != null ? groups.GetHashCode() : 0);
  465. hashCode = (hashCode * 397) ^ (action != null ? action.GetHashCode() : 0);
  466. return hashCode;
  467. }
  468. }
  469. /// <summary>
  470. /// Return a string representation of the binding useful for debugging.
  471. /// </summary>
  472. /// <returns>A string representation of the binding.</returns>
  473. /// <example>
  474. /// <code>
  475. /// var binding = new InputBinding
  476. /// {
  477. /// action = "fire",
  478. /// path = "&lt;Gamepad&gt;/buttonSouth",
  479. /// groups = "Gamepad"
  480. /// };
  481. ///
  482. /// // Returns "fire: &lt;Gamepad&gt;/buttonSouth [Gamepad]".
  483. /// binding.ToString();
  484. /// </code>
  485. /// </example>
  486. public override string ToString()
  487. {
  488. var builder = new StringBuilder();
  489. // Add action.
  490. if (!string.IsNullOrEmpty(action))
  491. {
  492. builder.Append(action);
  493. builder.Append(':');
  494. }
  495. // Add path.
  496. var path = effectivePath;
  497. if (!string.IsNullOrEmpty(path))
  498. builder.Append(path);
  499. // Add groups.
  500. if (!string.IsNullOrEmpty(groups))
  501. {
  502. builder.Append('[');
  503. builder.Append(groups);
  504. builder.Append(']');
  505. }
  506. return builder.ToString();
  507. }
  508. /// <summary>
  509. /// A set of flags to turn individual default behaviors of <see cref="InputBinding.ToDisplayString(DisplayStringOptions,InputControl)"/> off.
  510. /// </summary>
  511. [Flags]
  512. public enum DisplayStringOptions
  513. {
  514. /// <summary>
  515. /// Do not use short names of controls as set up by <see cref="InputControlAttribute.shortDisplayName"/>
  516. /// and the <c>"shortDisplayName"</c> property in JSON. This will, for example, not use LMB instead of "left Button"
  517. /// on <see cref="Mouse.leftButton"/>.
  518. /// </summary>
  519. DontUseShortDisplayNames = 1 << 0,
  520. /// <summary>
  521. /// By default device names are omitted from display strings. With this option, they are included instead.
  522. /// For example, <c>"A"</c> will be <c>"A [Gamepad]"</c> instead.
  523. /// </summary>
  524. DontOmitDevice = 1 << 1,
  525. /// <summary>
  526. /// By default, interactions on bindings are included in the resulting display string. For example, a binding to
  527. /// the gamepad's A button that has a "Hold" interaction on it, would come out as "Hold A". This can be suppressed
  528. /// with this option in which case the same setup would come out as just "A".
  529. /// </summary>
  530. DontIncludeInteractions = 1 << 2,
  531. /// <summary>
  532. /// By default, <see cref="effectivePath"/> is used for generating a display name. Using this option, the display
  533. /// string can be forced to <see cref="path"/> instead.
  534. /// </summary>
  535. IgnoreBindingOverrides = 1 << 3,
  536. }
  537. /// <summary>
  538. /// Turn the binding into a string suitable for display in a UI.
  539. /// </summary>
  540. /// <param name="options">Optional set of formatting options.</param>
  541. /// <param name="control">Optional control to which the binding has been resolved. If this is supplied,
  542. /// the resulting string can reflect things such as the current keyboard layout or hardware/platform-specific
  543. /// naming of controls (e.g. Xbox vs PS4 controllers as opposed to naming things generically based on the
  544. /// <see cref="Gamepad"/> layout).</param>
  545. /// <returns>A string representation of the binding suitable for display in a UI.</returns>
  546. /// <remarks>
  547. /// This method works only for bindings that are not composites. If the method is called on a binding
  548. /// that is a composite (<see cref="isComposite"/> is true), an empty string will be returned. To automatically
  549. /// handle composites, use <see cref="InputActionRebindingExtensions.GetBindingDisplayString(InputAction,DisplayStringOptions,string)"/>
  550. /// instead.
  551. ///
  552. /// <example>
  553. /// <code>
  554. /// var gamepadBinding = new InputBinding("&lt;Gamepad&gt;/buttonSouth");
  555. /// var mouseBinding = new InputBinding("&lt;Mouse&gt;/leftButton");
  556. /// var keyboardBinding = new InputBinding("&lt;Keyboard&gt;/a");
  557. ///
  558. /// // Prints "A" except on PS4 where it prints "Cross".
  559. /// Debug.Log(gamepadBinding.ToDisplayString());
  560. ///
  561. /// // Prints "LMB".
  562. /// Debug.Log(mouseBinding.ToDisplayString());
  563. ///
  564. /// // Print "Left Button".
  565. /// Debug.Log(mouseBinding.ToDisplayString(DisplayStringOptions.DontUseShortDisplayNames));
  566. ///
  567. /// // Prints the character associated with the "A" key on the current keyboard layout.
  568. /// Debug.Log(keyboardBinding, control: Keyboard.current);
  569. /// </code>
  570. /// </example>
  571. /// </remarks>
  572. /// <seealso cref="InputControlPath.ToHumanReadableString(string,InputControlPath.HumanReadableStringOptions,InputControl)"/>
  573. /// <seealso cref="InputActionRebindingExtensions.GetBindingDisplayString(InputAction,int,InputBinding.DisplayStringOptions)"/>
  574. public string ToDisplayString(DisplayStringOptions options = default, InputControl control = default)
  575. {
  576. return ToDisplayString(out var _, out var _, options, control);
  577. }
  578. /// <summary>
  579. /// Turn the binding into a string suitable for display in a UI.
  580. /// </summary>
  581. /// <param name="options">Optional set of formatting options.</param>
  582. /// <param name="control">Optional control to which the binding has been resolved. If this is supplied,
  583. /// the resulting string can reflect things such as the current keyboard layout or hardware/platform-specific
  584. /// naming of controls (e.g. Xbox vs PS4 controllers as opposed to naming things generically based on the
  585. /// <see cref="Gamepad"/> layout).</param>
  586. /// <returns>A string representation of the binding suitable for display in a UI.</returns>
  587. /// <remarks>
  588. /// This method is the same as <see cref="ToDisplayString(DisplayStringOptions,InputControl)"/> except that it
  589. /// will also return the name of the device layout and path of the control, if applicable to the binding. This is
  590. /// useful when needing more context on the resulting display string, for example to decide on an icon to display
  591. /// instead of the textual display string.
  592. ///
  593. /// <example>
  594. /// <code>
  595. /// var displayString = new InputBinding("&lt;Gamepad&gt;/dpad/up")
  596. /// .ToDisplayString(out deviceLayout, out controlPath);
  597. ///
  598. /// // Will print "D-Pad Up".
  599. /// Debug.Log(displayString);
  600. ///
  601. /// // Will print "Gamepad".
  602. /// Debug.Log(deviceLayout);
  603. ///
  604. /// // Will print "dpad/up".
  605. /// Debug.Log(controlPath);
  606. /// </code>
  607. /// </example>
  608. /// </remarks>
  609. /// <seealso cref="InputControlPath.ToHumanReadableString(string,out string,out string,InputControlPath.HumanReadableStringOptions,InputControl)"/>
  610. /// <seealso cref="InputActionRebindingExtensions.GetBindingDisplayString(InputAction,int,out string,out string,InputBinding.DisplayStringOptions)"/>
  611. public string ToDisplayString(out string deviceLayoutName, out string controlPath, DisplayStringOptions options = default,
  612. InputControl control = default)
  613. {
  614. if (isComposite)
  615. {
  616. deviceLayoutName = null;
  617. controlPath = null;
  618. return string.Empty;
  619. }
  620. var readableStringOptions = default(InputControlPath.HumanReadableStringOptions);
  621. if ((options & DisplayStringOptions.DontOmitDevice) == 0)
  622. readableStringOptions |= InputControlPath.HumanReadableStringOptions.OmitDevice;
  623. if ((options & DisplayStringOptions.DontUseShortDisplayNames) == 0)
  624. readableStringOptions |= InputControlPath.HumanReadableStringOptions.UseShortNames;
  625. var pathToUse = (options & DisplayStringOptions.IgnoreBindingOverrides) != 0
  626. ? path
  627. : effectivePath;
  628. var result = InputControlPath.ToHumanReadableString(pathToUse, out deviceLayoutName, out controlPath, readableStringOptions, control);
  629. if (!string.IsNullOrEmpty(effectiveInteractions) && (options & DisplayStringOptions.DontIncludeInteractions) == 0)
  630. {
  631. var interactionString = string.Empty;
  632. var parsedInteractions = NameAndParameters.ParseMultiple(effectiveInteractions);
  633. foreach (var element in parsedInteractions)
  634. {
  635. var interaction = element.name;
  636. var interactionDisplayName = InputInteraction.GetDisplayName(interaction);
  637. // An interaction can avoid being mentioned explicitly by just setting its display
  638. // name to an empty string.
  639. if (string.IsNullOrEmpty(interactionDisplayName))
  640. continue;
  641. ////TODO: this will need support for localization
  642. if (!string.IsNullOrEmpty(interactionString))
  643. interactionString = $"{interactionString} or {interactionDisplayName}";
  644. else
  645. interactionString = interactionDisplayName;
  646. }
  647. if (!string.IsNullOrEmpty(interactionString))
  648. result = $"{interactionString} {result}";
  649. }
  650. return result;
  651. }
  652. ////TODO: also support matching by name (taking the binding tree into account so that components
  653. //// of composites can be referenced through their parent)
  654. /// <summary>
  655. /// Check whether <paramref name="binding"/> matches the mask
  656. /// represented by the current binding.
  657. /// </summary>
  658. /// <param name="binding">An input binding.</param>
  659. /// <returns>True if <paramref name="binding"/> is matched by the mask represented
  660. /// by <c>this</c>.</returns>
  661. /// <remarks>
  662. /// In this method, the current binding acts as a "mask". When used this way, only
  663. /// three properties of the binding are taken into account: <see cref="path"/>,
  664. /// <see cref="groups"/>, and <see cref="action"/>.
  665. ///
  666. /// For each of these properties, the method checks whether they are set on the current
  667. /// binding and, if so, matches them against the respective property in <paramref name="binding"/>.
  668. ///
  669. /// The way this matching works is that the value of the property in the current binding is
  670. /// allowed to be a semicolon-separated list where each element specifies one possible value
  671. /// that will produce a match.
  672. ///
  673. /// Note that all comparisons are case-insensitive.
  674. ///
  675. /// <example>
  676. /// <code>
  677. /// // Create a couple bindings which we can test against.
  678. /// var keyboardBinding = new InputBinding
  679. /// {
  680. /// path = "&lt;Keyboard&gt;/space",
  681. /// groups = "Keyboard",
  682. /// action = "Fire"
  683. /// };
  684. /// var gamepadBinding = new InputBinding
  685. /// {
  686. /// path = "&lt;Gamepad&gt;/buttonSouth",
  687. /// groups = "Gamepad",
  688. /// action = "Jump"
  689. /// };
  690. /// var touchBinding = new InputBinding
  691. /// {
  692. /// path = "&lt;Touchscreen&gt;/*/tap",
  693. /// groups = "Touch",
  694. /// action = "Jump"
  695. /// };
  696. ///
  697. /// // Example 1: Match any binding in the "Keyboard" or "Gamepad" group.
  698. /// var mask1 = new InputBinding
  699. /// {
  700. /// // We put two elements in the list here and separate them with a semicolon.
  701. /// groups = "Keyboard;Gamepad"
  702. /// };
  703. ///
  704. /// mask1.Matches(keyboardBinding); // True
  705. /// mask1.Matches(gamepadBinding); // True
  706. /// mask1.Matches(touchBinding); // False
  707. ///
  708. /// // Example 2: Match any binding to the "Jump" or the "Roll" action
  709. /// // (the latter we don't actually have a binding for)
  710. /// var mask2 = new InputBinding
  711. /// {
  712. /// action = "Jump;Roll"
  713. /// };
  714. ///
  715. /// mask2.Matches(keyboardBinding); // False
  716. /// mask2.Matches(gamepadBinding); // True
  717. /// mask2.Matches(touchBinding); // True
  718. ///
  719. /// // Example: Match any binding to the space or enter key in the
  720. /// // "Keyboard" group.
  721. /// var mask3 = new InputBinding
  722. /// {
  723. /// path = "&lt;Keyboard&gt;/space;&lt;Keyboard&gt;/enter",
  724. /// groups = "Keyboard"
  725. /// };
  726. ///
  727. /// mask3.Matches(keyboardBinding); // True
  728. /// mask3.Matches(gamepadBinding); // False
  729. /// mask3.Matches(touchBinding); // False
  730. /// </code>
  731. /// </example>
  732. /// </remarks>
  733. public bool Matches(InputBinding binding)
  734. {
  735. return Matches(ref binding);
  736. }
  737. // Internally we pass by reference to not unnecessarily copy the struct.
  738. internal bool Matches(ref InputBinding binding, MatchOptions options = default)
  739. {
  740. ////TODO: add matching by ID
  741. if (path != null)
  742. {
  743. ////REVIEW: should this use binding.effectivePath?
  744. ////REVIEW: should this support matching by prefix only? should this use control path matching instead of just string comparisons?
  745. ////TODO: handle things like ignoring leading '/'
  746. if (binding.path == null
  747. || !StringHelpers.CharacterSeparatedListsHaveAtLeastOneCommonElement(path, binding.path, Separator))
  748. return false;
  749. }
  750. if (action != null)
  751. {
  752. ////TODO: handle "map/action" format
  753. ////TODO: handle "map/*" format
  754. ////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
  755. if (binding.action == null
  756. || !StringHelpers.CharacterSeparatedListsHaveAtLeastOneCommonElement(action, binding.action, Separator))
  757. return false;
  758. }
  759. if (groups != null)
  760. {
  761. var haveGroupsOnBinding = !string.IsNullOrEmpty(binding.groups);
  762. if (!haveGroupsOnBinding && (options & MatchOptions.EmptyGroupMatchesAny) == 0)
  763. return false;
  764. if (haveGroupsOnBinding
  765. && !StringHelpers.CharacterSeparatedListsHaveAtLeastOneCommonElement(groups, binding.groups, Separator))
  766. return false;
  767. }
  768. if (!string.IsNullOrEmpty(m_Id))
  769. {
  770. if (binding.id != id)
  771. return false;
  772. }
  773. return true;
  774. }
  775. [Flags]
  776. internal enum MatchOptions
  777. {
  778. EmptyGroupMatchesAny = 1 << 0,
  779. }
  780. [Flags]
  781. internal enum Flags
  782. {
  783. None = 0,
  784. Composite = 1 << 2,
  785. PartOfComposite = 1 << 3,
  786. }
  787. }
  788. }