File size: 53,043 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 | using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unity.Collections;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
#if UNITY_EDITOR
using UnityEditor;
#endif
////TODO: introduce the concept of a "variation"
//// - a variation is just a variant of a control scheme, not a full control scheme by itself
//// - an individual variation can be toggled on and off independently
//// - while a control is is active, all its variations that are toggled on are also active
//// - assignment to variations works the same as assignment to control schemes
//// use case: left/right stick toggles, left/right bumper toggles, etc
////TODO: introduce concept of precedence where one control scheme will be preferred over another that is also a match
//// (might be its enough to represent this simply through ordering by giving the user control over the ordering through the UI)
////REVIEW: allow associating control schemes with platforms, too?
namespace UnityEngine.InputSystem
{
/// <summary>
/// A named set of zero or more device requirements along with an associated binding group.
/// </summary>
/// <remarks>
/// Control schemes provide an additional layer on top of binding groups. While binding
/// groups allow differentiating sets of bindings (e.g. a "Keyboard&Mouse" group versus
/// a "Gamepad" group), control schemes impose a set of devices requirements that must be
/// met in order for a specific set of bindings to be usable.
///
/// Note that control schemes can only be defined at the <see cref="InputActionAsset"/> level.
/// </remarks>
/// <seealso cref="InputActionAsset.controlSchemes"/>
/// <seealso cref="InputActionSetupExtensions.AddControlScheme(InputActionAsset,string)"/>
[Serializable]
public struct InputControlScheme : IEquatable<InputControlScheme>
{
/// <summary>
/// Name of the control scheme. Not <c>null</c> or empty except if InputControlScheme
/// instance is invalid (i.e. default-initialized).
/// </summary>
/// <value>Name of the scheme.</value>
/// <remarks>
/// May be empty or null except if the control scheme is part of an <see cref="InputActionAsset"/>.
/// </remarks>
/// <seealso cref="InputActionSetupExtensions.AddControlScheme(InputActionAsset,string)"/>
public string name => m_Name;
/// <summary>
/// Binding group that is associated with the control scheme. Not <c>null</c> or empty
/// except if InputControlScheme is invalid (i.e. default-initialized).
/// </summary>
/// <value>Binding group for the scheme.</value>
/// <remarks>
/// All bindings in this group are considered to be part of the control scheme.
/// </remarks>
/// <seealso cref="InputBinding.groups"/>
public string bindingGroup
{
get => m_BindingGroup;
set => m_BindingGroup = value;
}
/// <summary>
/// Devices used by the control scheme.
/// </summary>
/// <value>Device requirements of the scheme.</value>
/// <remarks>
/// No two entries will be allowed to match the same control or device at runtime in order for the requirements
/// of the control scheme to be considered satisfied. If, for example, one entry requires a "<Gamepad>" and
/// another entry requires a "<Gamepad>", then at runtime two gamepads will be required even though a single
/// one will match both requirements individually. However, if, for example, one entry requires "<Gamepad>/leftStick"
/// and another requires "<Gamepad>, the same device can match both requirements as each one resolves to
/// a different control.
///
/// It it allowed to define control schemes without device requirements, i.e. for which this
/// property will be an empty array. Note, however, that features such as automatic control scheme
/// switching in <see cref="PlayerInput"/> will not work with such control schemes.
/// </remarks>
public ReadOnlyArray<DeviceRequirement> deviceRequirements =>
new ReadOnlyArray<DeviceRequirement>(m_DeviceRequirements);
/// <summary>
/// Initialize the control scheme with the given name, device requirements,
/// and binding group.
/// </summary>
/// <param name="name">Name to use for the scheme. Required.</param>
/// <param name="devices">List of device requirements.</param>
/// <param name="bindingGroup">Name to use for the binding group (see <see cref="InputBinding.groups"/>)
/// associated with the control scheme. If this is <c>null</c> or empty, <paramref name="name"/> is
/// used instead (with <see cref="InputBinding.Separator"/> characters stripped from the name).</param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
public InputControlScheme(string name, IEnumerable<DeviceRequirement> devices = null, string bindingGroup = null)
: this()
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
SetNameAndBindingGroup(name, bindingGroup);
m_DeviceRequirements = null;
if (devices != null)
{
m_DeviceRequirements = devices.ToArray();
if (m_DeviceRequirements.Length == 0)
m_DeviceRequirements = null;
}
}
#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_UI_TK_ASSET_EDITOR
internal InputControlScheme(SerializedProperty sp)
{
var requirements = new List<DeviceRequirement>();
var deviceRequirementsArray = sp.FindPropertyRelative(nameof(m_DeviceRequirements));
if (deviceRequirementsArray == null)
throw new ArgumentException("The serialized property does not contain an InputControlScheme object.");
foreach (SerializedProperty deviceRequirement in deviceRequirementsArray)
{
requirements.Add(new DeviceRequirement
{
controlPath = deviceRequirement.FindPropertyRelative(nameof(DeviceRequirement.m_ControlPath)).stringValue,
m_Flags = (DeviceRequirement.Flags)deviceRequirement.FindPropertyRelative(nameof(DeviceRequirement.m_Flags)).enumValueFlag
});
}
m_Name = sp.FindPropertyRelative(nameof(m_Name)).stringValue;
m_DeviceRequirements = requirements.ToArray();
m_BindingGroup = sp.FindPropertyRelative(nameof(m_BindingGroup)).stringValue;
}
#endif
internal void SetNameAndBindingGroup(string name, string bindingGroup = null)
{
m_Name = name;
if (!string.IsNullOrEmpty(bindingGroup))
m_BindingGroup = bindingGroup;
else
m_BindingGroup = name.Contains(InputBinding.Separator)
? name.Replace(InputBinding.kSeparatorString, "")
: name;
}
/// <summary>
/// Given a list of devices and a list of control schemes, find the most suitable control
/// scheme to use with the devices.
/// </summary>
/// <param name="devices">A list of devices. If the list is empty, only schemes with
/// empty <see cref="deviceRequirements"/> lists will get matched.</param>
/// <param name="schemes">A list of control schemes.</param>
/// <param name="mustIncludeDevice">If not <c>null</c>, a successful match has to include the given device.</param>
/// <param name="allowUnsuccesfulMatch">If true, then allow returning a match that has unsatisfied requirements but still
/// matched at least some requirement. If there are several unsuccessful matches, the returned scheme is still the highest
/// scoring one among those.</param>
/// <typeparam name="TDevices">Collection type to use for the list of devices.</typeparam>
/// <typeparam name="TSchemes">Collection type to use for the list of schemes.</typeparam>
/// <returns>The control scheme that best matched the given devices or <c>null</c> if no
/// scheme was found suitable.</returns>
/// <exception cref="ArgumentNullException"><paramref name="devices"/> is <c>null</c> -or-
/// <paramref name="schemes"/> is <c>null</c>.</exception>
/// <remarks>
/// Any successful match (see <see cref="MatchResult.isSuccessfulMatch"/>) will be considered.
/// The one that matches the most amount of devices (see <see cref="MatchResult.devices"/>)
/// will be returned. If more than one schemes matches equally well, the first one encountered
/// in the list is returned.
///
/// Note that schemes are not required to match all devices available in the list. The result
/// will simply be the scheme that matched the most devices of what was devices. Use <see
/// cref="PickDevicesFrom{TDevices}"/> to find the devices that a control scheme selects.
///
/// This method is parameterized over <typeparamref name="TDevices"/> and <typeparamref name="TSchemes"/>
/// to allow avoiding GC heap allocations from boxing of structs such as <see cref="ReadOnlyArray{TValue}"/>.
///
/// <example>
/// <code>
/// // Create an .inputactions asset.
/// var asset = ScriptableObject.CreateInstance<InputActionAsset>();
///
/// // Add some control schemes to the asset.
/// asset.AddControlScheme("KeyboardMouse")
/// .WithRequiredDevice<Keyboard>()
/// .WithRequiredDevice<Mouse>());
/// asset.AddControlScheme("Gamepad")
/// .WithRequiredDevice<Gamepad>());
/// asset.AddControlScheme("DualGamepad")
/// .WithRequiredDevice<Gamepad>())
/// .WithOptionalGamepad<Gamepad>());
///
/// // Add some devices that we can test with.
/// var keyboard = InputSystem.AddDevice<Keyboard>();
/// var mouse = InputSystem.AddDevice<Mouse>();
/// var gamepad1 = InputSystem.AddDevice<Gamepad>();
/// var gamepad2 = InputSystem.AddDevice<Gamepad>();
///
/// // Matching with just a keyboard won't match any scheme.
/// InputControlScheme.FindControlSchemeForDevices(
/// new InputDevice[] { keyboard }, asset.controlSchemes);
///
/// // Matching with a keyboard and mouse with match the "KeyboardMouse" scheme.
/// InputControlScheme.FindControlSchemeForDevices(
/// new InputDevice[] { keyboard, mouse }, asset.controlSchemes);
///
/// // Matching with a single gamepad will match the "Gamepad" scheme.
/// // Note that since the second gamepad is optional in "DualGamepad" could
/// // match the same set of devices but it doesn't match any better than
/// // "Gamepad" and that one comes first in the list.
/// InputControlScheme.FindControlSchemeForDevices(
/// new InputDevice[] { gamepad1 }, asset.controlSchemes);
///
/// // Matching with two gamepads will match the "DualGamepad" scheme.
/// // Note that "Gamepad" will match this device list as well. If "DualGamepad"
/// // didn't exist, "Gamepad" would be the result here. However, "DualGamepad"
/// // matches the list better than "Gamepad" so that's what gets returned here.
/// InputControlScheme.FindControlSchemeForDevices(
/// new InputDevice[] { gamepad1, gamepad2 }, asset.controlSchemes);
/// </code>
/// </example>
/// </remarks>
public static InputControlScheme? FindControlSchemeForDevices<TDevices, TSchemes>(TDevices devices, TSchemes schemes, InputDevice mustIncludeDevice = null, bool allowUnsuccesfulMatch = false)
where TDevices : IReadOnlyList<InputDevice>
where TSchemes : IEnumerable<InputControlScheme>
{
if (devices == null)
throw new ArgumentNullException(nameof(devices));
if (schemes == null)
throw new ArgumentNullException(nameof(schemes));
if (!FindControlSchemeForDevices(devices, schemes, out var controlScheme, out var matchResult, mustIncludeDevice, allowUnsuccesfulMatch))
return null;
matchResult.Dispose();
return controlScheme;
}
public static bool FindControlSchemeForDevices<TDevices, TSchemes>(TDevices devices, TSchemes schemes,
out InputControlScheme controlScheme, out MatchResult matchResult, InputDevice mustIncludeDevice = null, bool allowUnsuccessfulMatch = false)
where TDevices : IReadOnlyList<InputDevice>
where TSchemes : IEnumerable<InputControlScheme>
{
if (devices == null)
throw new ArgumentNullException(nameof(devices));
if (schemes == null)
throw new ArgumentNullException(nameof(schemes));
MatchResult? bestResult = null;
InputControlScheme? bestScheme = null;
foreach (var scheme in schemes)
{
var result = scheme.PickDevicesFrom(devices, favorDevice: mustIncludeDevice);
// Ignore if scheme doesn't fit devices.
if (!result.isSuccessfulMatch && (!allowUnsuccessfulMatch || result.score <= 0))
{
result.Dispose();
continue;
}
// Ignore if we have a device we specifically want to be part of the result and
// the current match doesn't have it.
if (mustIncludeDevice != null && !result.devices.Contains(mustIncludeDevice))
{
result.Dispose();
continue;
}
// Ignore if it does fit but we already have a better fit.
if (bestResult != null && bestResult.Value.score >= result.score)
{
result.Dispose();
continue;
}
bestResult?.Dispose();
bestResult = result;
bestScheme = scheme;
}
matchResult = bestResult ?? default;
controlScheme = bestScheme ?? default;
return bestResult.HasValue;
}
////FIXME: docs are wrong now
/// <summary>
/// Return the first control schemes from the given list that supports the given
/// device (see <see cref="SupportsDevice"/>).
/// </summary>
/// <param name="device">An input device.</param>
/// <param name="schemes">A list of control schemes. Can be empty.</param>
/// <typeparam name="TSchemes">Collection type to use for the list of schemes.</typeparam>
/// <returns>The first schemes from <paramref name="schemes"/> that supports <paramref name="device"/>
/// or <c>null</c> if none of the schemes is usable with the device.</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> is <c>null</c> -or-
/// <paramref name="schemes"/> is <c>null</c>.</exception>
public static InputControlScheme? FindControlSchemeForDevice<TSchemes>(InputDevice device, TSchemes schemes)
where TSchemes : IEnumerable<InputControlScheme>
{
if (schemes == null)
throw new ArgumentNullException(nameof(schemes));
if (device == null)
throw new ArgumentNullException(nameof(device));
return FindControlSchemeForDevices(new OneOrMore<InputDevice, ReadOnlyArray<InputDevice>>(device), schemes);
}
/// <summary>
/// Whether the control scheme has a requirement in <see cref="deviceRequirements"/> that
/// targets the given device.
/// </summary>
/// <param name="device">An input device.</param>
/// <returns>True if the control scheme has a device requirement matching the device.</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> is <c>null</c>.</exception>
/// <remarks>
/// Note that both optional (see <see cref="DeviceRequirement.isOptional"/>) and non-optional
/// device requirements are taken into account.
///
/// </remarks>
public bool SupportsDevice(InputDevice device)
{
if (device == null)
throw new ArgumentNullException(nameof(device));
////REVIEW: does this need to take AND and OR into account?
for (var i = 0; i < m_DeviceRequirements.Length; ++i)
{
var control = InputControlPath.TryFindControl(device, m_DeviceRequirements[i].controlPath);
if (control != null)
return true;
}
return false;
}
////REVIEW: have mode where instead of matching only the first device that matches a requirement, we match as many
//// as we can get? (could be useful for single-player)
/// <summary>
/// Based on a list of devices, make a selection that matches the <see cref="deviceRequirements">requirements</see>
/// imposed by the control scheme.
/// </summary>
/// <param name="devices">A list of devices to choose from.</param>
/// <param name="favorDevice">If not null, the device will be favored over other devices in <paramref name="devices"/>.
/// Note that the device must be present in the list also.</param>
/// <returns>A <see cref="MatchResult"/> structure containing the result of the pick. Note that this structure
/// must be manually <see cref="MatchResult.Dispose">disposed</see> or unmanaged memory will be leaked.</returns>
/// <remarks>
/// Does not allocate managed memory.
/// </remarks>
public MatchResult PickDevicesFrom<TDevices>(TDevices devices, InputDevice favorDevice = null)
where TDevices : IReadOnlyList<InputDevice>
{
// Empty device requirements match anything while not really picking anything.
if (m_DeviceRequirements == null || m_DeviceRequirements.Length == 0)
{
return new MatchResult
{
m_Result = MatchResult.Result.AllSatisfied,
// Prevent zero score on successful match but make less than one which would
// result from having a single requirement.
m_Score = 0.5f,
};
}
// Go through each requirement and match it.
// NOTE: Even if `devices` is empty, we don't know yet whether we have a NoMatch.
// All our devices may be optional.
var haveAllRequired = true;
var haveAllOptional = true;
var requirementCount = m_DeviceRequirements.Length;
var score = 0f;
var controls = new InputControlList<InputControl>(Allocator.Persistent, requirementCount);
try
{
var orChainIsSatisfied = false;
var orChainHasRequiredDevices = false;
for (var i = 0; i < requirementCount; ++i)
{
var isOR = m_DeviceRequirements[i].isOR;
var isOptional = m_DeviceRequirements[i].isOptional;
// If this is an OR requirement and we already have a match in this OR chain,
// skip this requirement.
if (isOR && orChainIsSatisfied)
{
// Skill need to add an entry for this requirement.
controls.Add(null);
continue;
}
// Null and empty paths shouldn't make it into the list but make double
// sure here. Simply ignore entries that don't have a path.
var path = m_DeviceRequirements[i].controlPath;
if (string.IsNullOrEmpty(path))
{
score += 1;
controls.Add(null);
continue;
}
// Find the first matching control among the devices we have.
InputControl match = null;
for (var n = 0; n < devices.Count; ++n)
{
var device = devices[n];
// If we should favor a device, we swap it in at index #0 regardless
// of where in the list the device occurs (it MUST, however, occur in the list).
if (favorDevice != null)
{
if (n == 0)
device = favorDevice;
else if (device == favorDevice)
device = devices[0];
}
// See if we have a match.
var matchedControl = InputControlPath.TryFindControl(device, path);
if (matchedControl == null)
continue; // No.
// We have a match but if we've already matched the same control through another requirement,
// we can't use the match.
if (controls.Contains(matchedControl))
continue;
match = matchedControl;
// Compute score for match.
var deviceLayoutOfControlPath = new InternedString(InputControlPath.TryGetDeviceLayout(path));
if (deviceLayoutOfControlPath.IsEmpty())
{
// Generic match adds 1 to score.
score += 1;
}
else
{
var deviceLayoutOfControl = matchedControl.device.m_Layout;
if (InputControlLayout.s_Layouts.ComputeDistanceInInheritanceHierarchy(deviceLayoutOfControlPath,
deviceLayoutOfControl, out var distance))
{
score += 1 + 1f / (Math.Abs(distance) + 1);
}
else
{
// Shouldn't really get here as for the control to be a match for the path, the device layouts
// would be expected to be related to each other. But just add 1 for a generic match and go on.
score += 1;
}
}
break;
}
// Check requirements in AND and OR chains. We look ahead here to find out whether
// the next requirement is starting an OR chain. As the OR combines with the previous
// requirement in the list, this affects our current requirement.
var nextIsOR = i + 1 < requirementCount && m_DeviceRequirements[i + 1].isOR;
if (nextIsOR)
{
// Shouldn't get here if the chain is already satisfied. Should be handled
// at beginning of loop and we shouldn't even be looking at finding controls
// in that case.
Debug.Assert(!orChainIsSatisfied);
// It's an OR with the next requirement. Depends on the outcome of other matches whether
// we're good or not.
if (match != null)
{
// First match in this chain.
orChainIsSatisfied = true;
}
else
{
// Chain not satisfied yet.
if (!isOptional)
orChainHasRequiredDevices = true;
}
}
else if (isOR && i == requirementCount - 1)
{
// It's an OR at the very end of the requirements list. Terminate
// the OR chain.
if (match == null)
{
if (orChainHasRequiredDevices)
haveAllRequired = false;
else
haveAllOptional = false;
}
}
else
{
// It's an AND.
if (match == null)
{
if (isOptional)
haveAllOptional = false;
else
haveAllRequired = false;
}
// Terminate ongoing OR chain.
if (i > 0 && m_DeviceRequirements[i - 1].isOR)
{
if (!orChainIsSatisfied)
{
if (orChainHasRequiredDevices)
haveAllRequired = false;
else
haveAllOptional = false;
}
orChainIsSatisfied = false;
}
}
// Add match to list. Maybe null.
controls.Add(match);
}
// We should have matched each of our requirements.
Debug.Assert(controls.Count == requirementCount);
}
catch (Exception)
{
controls.Dispose();
throw;
}
return new MatchResult
{
m_Result = !haveAllRequired
? MatchResult.Result.MissingRequired
: !haveAllOptional
? MatchResult.Result.MissingOptional
: MatchResult.Result.AllSatisfied,
m_Controls = controls,
m_Requirements = m_DeviceRequirements,
m_Score = score,
};
}
public bool Equals(InputControlScheme other)
{
if (!(string.Equals(m_Name, other.m_Name, StringComparison.InvariantCultureIgnoreCase) &&
string.Equals(m_BindingGroup, other.m_BindingGroup, StringComparison.InvariantCultureIgnoreCase)))
return false;
// Compare device requirements.
if (m_DeviceRequirements == null || m_DeviceRequirements.Length == 0)
return other.m_DeviceRequirements == null || other.m_DeviceRequirements.Length == 0;
if (other.m_DeviceRequirements == null || m_DeviceRequirements.Length != other.m_DeviceRequirements.Length)
return false;
var deviceCount = m_DeviceRequirements.Length;
for (var i = 0; i < deviceCount; ++i)
{
var device = m_DeviceRequirements[i];
var haveMatch = false;
for (var n = 0; n < deviceCount; ++n)
{
if (other.m_DeviceRequirements[n] == device)
{
haveMatch = true;
break;
}
}
if (!haveMatch)
return false;
}
return true;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
return obj is InputControlScheme && Equals((InputControlScheme)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (m_Name != null ? m_Name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (m_BindingGroup != null ? m_BindingGroup.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (m_DeviceRequirements != null ? m_DeviceRequirements.GetHashCode() : 0);
return hashCode;
}
}
public override string ToString()
{
if (string.IsNullOrEmpty(m_Name))
return base.ToString();
if (m_DeviceRequirements == null)
return m_Name;
var builder = new StringBuilder();
builder.Append(m_Name);
builder.Append('(');
var isFirst = true;
foreach (var device in m_DeviceRequirements)
{
if (!isFirst)
builder.Append(',');
builder.Append(device.controlPath);
isFirst = false;
}
builder.Append(')');
return builder.ToString();
}
public static bool operator==(InputControlScheme left, InputControlScheme right)
{
return left.Equals(right);
}
public static bool operator!=(InputControlScheme left, InputControlScheme right)
{
return !left.Equals(right);
}
[SerializeField] internal string m_Name;
[SerializeField] internal string m_BindingGroup;
[SerializeField] internal DeviceRequirement[] m_DeviceRequirements;
/// <summary>
/// The result of matching a list of <see cref="InputDevice">devices</see> against a list of
/// <see cref="DeviceRequirement">requirements</see> in an <see cref="InputControlScheme"/>.
/// </summary>
/// <remarks>
/// This struct uses <see cref="InputControlList{TControl}"/> which allocates unmanaged memory
/// and thus must be disposed in order to not leak unmanaged heap memory.
/// </remarks>
/// <seealso cref="InputControlScheme.PickDevicesFrom{TDevices}"/>
public struct MatchResult : IEnumerable<MatchResult.Match>, IDisposable
{
/// <summary>
/// Overall, relative measure for how well the control scheme matches.
/// </summary>
/// <value>Scoring value for the control scheme match.</value>
/// <remarks>
/// Two control schemes may, for example, both support gamepads but one may be tailored to a specific
/// gamepad whereas the other one is a generic gamepad control scheme. To differentiate the two, we need
/// to know not only that a control schemes but how well it matches relative to other schemes. This is
/// what the score value is used for.
///
/// Scores are computed primarily based on layouts referenced from device requirements. To start with, each
/// matching device requirement (whether optional or mandatory) will add 1 to the score. This the base
/// score of a match. Then, for each requirement a delta is computed from the device layout referenced by
/// the requirement to the device layout used by the matching control. For example, if the requirement is
/// <c>"<Gamepad></c> and the matching control uses the <see cref="DualShock.DualShock4GamepadHID"/>
/// layout, the delta is 2 as the latter layout is derived from <see cref="Gamepad"/> via the intermediate
/// <see cref="DualShock.DualShockGamepad"/> layout, i.e. two steps in the inheritance hierarchy. The
/// <em>inverse</em> of the delta plus one, i.e. <c>1/(delta+1)</c> is then added to the score. This means
/// that an exact match will add an additional 1 to the score and less exact matches will add progressively
/// smaller values to the score (proportional to the distance of the actual layout to the one used in the
/// requirement).
///
/// What this leads to is that, for example, a control scheme with a <c>"<Gamepad>"</c> requirement
/// will match a <see cref="DualShock.DualShock4GamepadHID"/> with a <em>lower</em> score than a control
/// scheme with a <c>"<DualShockGamepad>"</c> requirement as the <see cref="Gamepad"/> layout is
/// further removed (i.e. smaller inverse delta) from <see cref="DualShock.DualShock4GamepadHID"/> than
/// <see cref="DualShock.DualShockGamepad"/>.
/// </remarks>
public float score => m_Score;
/// <summary>
/// Whether the device requirements got successfully matched.
/// </summary>
/// <value>True if the scheme's device requirements were satisfied.</value>
public bool isSuccessfulMatch => m_Result != Result.MissingRequired;
/// <summary>
/// Whether there are missing required devices.
/// </summary>
/// <value>True if there are missing, non-optional devices.</value>
/// <seealso cref="DeviceRequirement.isOptional"/>
public bool hasMissingRequiredDevices => m_Result == Result.MissingRequired;
/// <summary>
/// Whether there are missing optional devices. This does not prevent
/// a successful match.
/// </summary>
/// <value>True if there are missing optional devices.</value>
/// <seealso cref="DeviceRequirement.isOptional"/>
public bool hasMissingOptionalDevices => m_Result == Result.MissingOptional;
/// <summary>
/// The devices that got picked from the available devices.
/// </summary>
public InputControlList<InputDevice> devices
{
get
{
// Lazily construct the device list. If we have missing required
// devices, though, always return an empty list. The user can still see
// the individual matches on each of the requirement entries but we
// consider the device picking itself failed.
if (m_Devices.Count == 0 && !hasMissingRequiredDevices)
{
var controlCount = m_Controls.Count;
if (controlCount != 0)
{
m_Devices.Capacity = controlCount;
for (var i = 0; i < controlCount; ++i)
{
var control = m_Controls[i];
if (control == null)
continue;
var device = control.device;
if (m_Devices.Contains(device))
continue; // Duplicate match of same device.
m_Devices.Add(device);
}
}
}
return m_Devices;
}
}
public Match this[int index]
{
get
{
if (index < 0 || m_Requirements == null || index >= m_Requirements.Length)
throw new ArgumentOutOfRangeException("index");
return new Match
{
m_RequirementIndex = index,
m_Requirements = m_Requirements,
m_Controls = m_Controls,
};
}
}
/// <summary>
/// Enumerate the match for each individual <see cref="DeviceRequirement"/> in the control scheme.
/// </summary>
/// <returns>An enumerate going over each individual match.</returns>
public IEnumerator<Match> GetEnumerator()
{
return new Enumerator
{
m_Index = -1,
m_Requirements = m_Requirements,
m_Controls = m_Controls,
};
}
/// <summary>
/// Enumerate the match for each individual <see cref="DeviceRequirement"/> in the control scheme.
/// </summary>
/// <returns>An enumerate going over each individual match.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Discard the list of devices.
/// </summary>
public void Dispose()
{
m_Controls.Dispose();
m_Devices.Dispose();
}
internal Result m_Result;
internal float m_Score;
internal InputControlList<InputDevice> m_Devices;
internal InputControlList<InputControl> m_Controls;
internal DeviceRequirement[] m_Requirements;
internal enum Result
{
AllSatisfied,
MissingRequired,
MissingOptional,
}
////REVIEW: would be great to not have to repeatedly copy InputControlLists around
/// <summary>
/// A single matched <see cref="DeviceRequirement"/>.
/// </summary>
/// <remarks>
/// Links the control that was matched with the respective device requirement.
/// </remarks>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Justification = "Conflicts with UnityEngine.Networking.Match, which is deprecated and will go away.")]
public struct Match
{
/// <summary>
/// The control that was match from the requirement's <see cref="DeviceRequirement.controlPath"/>
/// </summary>
/// <remarks>
/// This is the same as <see cref="device"/> if the <see cref="DeviceRequirement.controlPath">control
/// path</see> matches the device directly rather than matching a control on the device.
///
/// Note that while a control path can match arbitrary many controls, only the first matched control
/// will be returned here. To get all controls that were matched by a specific requirement, a
/// manual query must be performed using <see cref="InputControlPath"/>.
///
/// If the match failed, this will be null.
/// </remarks>
public InputControl control => m_Controls[m_RequirementIndex];
/// <summary>
/// The device that got matched.
/// </summary>
/// <remarks>
/// If a specific control on the device was matched, this will be <see cref="InputControl.device"/> or
/// <see cref="control"/>. If a device was matched directly, this will be the same as <see cref="control"/>.
/// </remarks>
public InputDevice device
{
get
{
var control = this.control;
return control?.device;
}
}
/// <summary>
/// Index of the requirement in <see cref="InputControlScheme.deviceRequirements"/>.
/// </summary>
public int requirementIndex => m_RequirementIndex;
/// <summary>
/// The device requirement that got matched.
/// </summary>
public DeviceRequirement requirement => m_Requirements[m_RequirementIndex];
public bool isOptional => requirement.isOptional;
internal int m_RequirementIndex;
internal DeviceRequirement[] m_Requirements;
internal InputControlList<InputControl> m_Controls;
}
private struct Enumerator : IEnumerator<Match>
{
public bool MoveNext()
{
++m_Index;
return m_Requirements != null && m_Index < m_Requirements.Length;
}
public void Reset()
{
m_Index = -1;
}
public Match Current
{
get
{
if (m_Requirements == null || m_Index < 0 || m_Index >= m_Requirements.Length)
throw new InvalidOperationException("Enumerator is not valid");
return new Match
{
m_RequirementIndex = m_Index,
m_Requirements = m_Requirements,
m_Controls = m_Controls,
};
}
}
object IEnumerator.Current => Current;
public void Dispose()
{
}
internal int m_Index;
internal DeviceRequirement[] m_Requirements;
internal InputControlList<InputControl> m_Controls;
}
}
/// <summary>
///
/// </summary>
/// <remarks>
/// Note that device requirements may require specific controls to be present rather than only requiring
/// the presence of a certain type of device. For example, a requirement with a <see cref="controlPath"/>
/// of "*/{PrimaryAction}" will be satisfied by any device that has a control marked as <see cref="CommonUsages.PrimaryAction"/>.
///
/// Requirements are ordered in a list and can combine with their previous requirement in either <see cref="isAND">
/// AND</see> or in <see cref="isOR">OR</see> fashion. The default is for requirements to combine with AND.
///
/// Note that it is not possible to express nested constraints like <c>(a AND b) OR (c AND d)</c>. Also note that
/// operator precedence is the opposite of C#, meaning that OR has *higher* precedence than AND. This means
/// that <c>a OR b AND c OR d</c> reads as <c>(a OR b) AND (c OR d)</c> (in C# it would read as <c>a OR
/// (b AND c) OR d</c>.
///
/// More complex expressions can often be expressed differently. For example, <c>(a AND b) OR (c AND d)</c>
/// can be expressed as <c>a OR c AND b OR d</c>.
/// </remarks>
[Serializable]
public struct DeviceRequirement : IEquatable<DeviceRequirement>
{
/// <summary>
/// <see cref="InputControlPath">Control path</see> that is matched against a device to determine
/// whether it qualifies for the control scheme.
/// </summary>
/// <remarks>
/// </remarks>
/// <example>
/// <code>
/// // A left-hand XR controller.
/// "<XRController>{LeftHand}"
///
/// // A gamepad.
/// "<Gamepad>"
/// </code>
/// </example>
public string controlPath
{
get => m_ControlPath;
set => m_ControlPath = value;
}
/// <summary>
/// If true, a device with the given <see cref="controlPath">device path</see> is employed by the
/// control scheme if one is available. If none is available, the control scheme is still
/// functional.
/// </summary>
public bool isOptional
{
get => (m_Flags & Flags.Optional) != 0;
set
{
if (value)
m_Flags |= Flags.Optional;
else
m_Flags &= ~Flags.Optional;
}
}
/// <summary>
/// Whether the requirement combines with the previous requirement (if any) as a boolean AND.
/// </summary>
/// <remarks>
/// This is the default. For example, to require both a left hand and a right XR controller,
/// the first requirement would be for "<XRController>{LeftHand}" and the second
/// requirement would be for ">XRController>{RightHand}" and would return true for this
/// property.
/// </remarks>
/// <seealso cref="isOR"/>
public bool isAND
{
get => !isOR;
set => isOR = !value;
}
/// <summary>
/// Whether the requirement combines with the previous requirement (if any) as a boolean OR.
/// </summary>
/// <remarks>
/// This allows designing control schemes that flexibly work with combinations of devices such that
/// if one specific device isn't present, another device can substitute for it.
///
/// For example, to design a mouse+keyboard control scheme that can alternatively work with a pen
/// instead of a mouse, the first requirement could be for "<Keyboard>", the second one
/// could be for "<Mouse>" and the third one could be for "<Pen>" and return true
/// for this property. Both the mouse and the pen would be marked as required (i.e. not <see cref="isOptional"/>)
/// but the device requirements are satisfied even if either device is present.
///
/// Note that if both a pen and a mouse are present at the same time, still only one device is
/// picked. In this case, the mouse "wins" as it comes first in the list of requirements.
/// </remarks>
public bool isOR
{
get => (m_Flags & Flags.Or) != 0;
set
{
if (value)
m_Flags |= Flags.Or;
else
m_Flags &= ~Flags.Or;
}
}
[SerializeField] internal string m_ControlPath;
[SerializeField] internal Flags m_Flags;
[Flags]
internal enum Flags
{
None = 0,
Optional = 1 << 0,
Or = 1 << 1,
}
public override string ToString()
{
if (!string.IsNullOrEmpty(controlPath))
{
if (isOptional)
return controlPath + " (Optional)";
return controlPath + " (Required)";
}
return base.ToString();
}
public bool Equals(DeviceRequirement other)
{
return string.Equals(m_ControlPath, other.m_ControlPath) && m_Flags == other.m_Flags &&
string.Equals(controlPath, other.controlPath) && isOptional == other.isOptional;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
return obj is DeviceRequirement && Equals((DeviceRequirement)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (m_ControlPath != null ? m_ControlPath.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ m_Flags.GetHashCode();
hashCode = (hashCode * 397) ^ (controlPath != null ? controlPath.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ isOptional.GetHashCode();
return hashCode;
}
}
public static bool operator==(DeviceRequirement left, DeviceRequirement right)
{
return left.Equals(right);
}
public static bool operator!=(DeviceRequirement left, DeviceRequirement right)
{
return !left.Equals(right);
}
}
/// <summary>
/// JSON-serialized form of a control scheme.
/// </summary>
[Serializable]
internal struct SchemeJson
{
public string name;
public string bindingGroup;
public DeviceJson[] devices;
[Serializable]
public struct DeviceJson
{
public string devicePath;
public bool isOptional;
public bool isOR;
public DeviceRequirement ToDeviceEntry()
{
return new DeviceRequirement
{
controlPath = devicePath,
isOptional = isOptional,
isOR = isOR,
};
}
public static DeviceJson From(DeviceRequirement requirement)
{
return new DeviceJson
{
devicePath = requirement.controlPath,
isOptional = requirement.isOptional,
isOR = requirement.isOR,
};
}
}
public InputControlScheme ToScheme()
{
DeviceRequirement[] deviceRequirements = null;
if (devices != null && devices.Length > 0)
{
var count = devices.Length;
deviceRequirements = new DeviceRequirement[count];
for (var i = 0; i < count; ++i)
deviceRequirements[i] = devices[i].ToDeviceEntry();
}
return new InputControlScheme
{
m_Name = string.IsNullOrEmpty(name) ? null : name,
m_BindingGroup = string.IsNullOrEmpty(bindingGroup) ? null : bindingGroup,
m_DeviceRequirements = deviceRequirements,
};
}
public static SchemeJson ToJson(InputControlScheme scheme)
{
DeviceJson[] devices = null;
if (scheme.m_DeviceRequirements != null && scheme.m_DeviceRequirements.Length > 0)
{
var count = scheme.m_DeviceRequirements.Length;
devices = new DeviceJson[count];
for (var i = 0; i < count; ++i)
devices[i] = DeviceJson.From(scheme.m_DeviceRequirements[i]);
}
return new SchemeJson
{
name = scheme.m_Name,
bindingGroup = scheme.m_BindingGroup,
devices = devices,
};
}
public static SchemeJson[] ToJson(InputControlScheme[] schemes)
{
if (schemes == null || schemes.Length == 0)
return null;
var count = schemes.Length;
var result = new SchemeJson[count];
for (var i = 0; i < count; ++i)
result[i] = ToJson(schemes[i]);
return result;
}
public static InputControlScheme[] ToSchemes(SchemeJson[] schemes)
{
if (schemes == null || schemes.Length == 0)
return null;
var count = schemes.Length;
var result = new InputControlScheme[count];
for (var i = 0; i < count; ++i)
result[i] = schemes[i].ToScheme();
return result;
}
}
}
}
|