File size: 97,415 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 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 | using System;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
////TODO: Rename all the xxxSyntax structs to xxxAccessor
////TODO: Replace all 'WithXXX' in the accessors with just 'SetXXX'; the 'WithXXX' reads too awkwardly
namespace UnityEngine.InputSystem
{
/// <summary>
/// Methods to change the setup of <see cref="InputAction"/>, <see cref="InputActionMap"/>,
/// and <see cref="InputActionAsset"/> objects.
/// </summary>
/// <remarks>
/// Unlike the methods in <see cref="InputActionRebindingExtensions"/>, the methods here are
/// generally destructive, i.e. they will rearrange the data for actions.
/// </remarks>
public static class InputActionSetupExtensions
{
/// <summary>
/// Create an action map with the given name and add it to the asset.
/// </summary>
/// <param name="asset">Asset to add the action map to</param>
/// <param name="name">Name to assign to the </param>
/// <returns>The newly added action map.</returns>
/// <exception cref="ArgumentNullException"><paramref name="asset"/> is <c>null</c> or
/// <exception cref="InvalidOperationException">An action map with the given <paramref name="name"/>
/// already exists in <paramref name="asset"/>.</exception>
/// <paramref name="name"/> is <c>null</c> or empty.</exception>
public static InputActionMap AddActionMap(this InputActionAsset asset, string name)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
if (asset.FindActionMap(name) != null)
throw new InvalidOperationException(
$"An action map called '{name}' already exists in the asset");
var map = new InputActionMap(name);
map.GenerateId();
asset.AddActionMap(map);
return map;
}
/// <summary>
/// Add an action map to the asset.
/// </summary>
/// <param name="asset">Asset to add the map to.</param>
/// <param name="map">A named action map.</param>
/// <exception cref="ArgumentNullException"><paramref name="map"/> or <paramref name="asset"/> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException"><paramref name="map"/> has no name or asset already contains a
/// map with the same name -or- <paramref name="map"/> is currently enabled -or- <paramref name="map"/> is part of
/// an <see cref="InputActionAsset"/> that has <see cref="InputActionMap"/>s that are enabled.</exception>
/// <seealso cref="InputActionAsset.actionMaps"/>
public static void AddActionMap(this InputActionAsset asset, InputActionMap map)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (map == null)
throw new ArgumentNullException(nameof(map));
if (string.IsNullOrEmpty(map.name))
throw new InvalidOperationException("Maps added to an input action asset must be named");
if (map.asset != null)
throw new InvalidOperationException(
$"Cannot add map '{map}' to asset '{asset}' as it has already been added to asset '{map.asset}'");
////REVIEW: some of the rules here seem stupid; just replace?
if (asset.FindActionMap(map.name) != null)
throw new InvalidOperationException(
$"An action map called '{map.name}' already exists in the asset");
map.OnWantToChangeSetup();
asset.OnWantToChangeSetup();
ArrayHelpers.Append(ref asset.m_ActionMaps, map);
map.m_Asset = asset;
asset.OnSetupChanged();
}
/// <summary>
/// Remove the given action map from the asset.
/// </summary>
/// <param name="asset">Asset to add the action map to.</param>
/// <param name="map">An action map. If the given map is not part of the asset, the method
/// does nothing.</param>
/// <exception cref="ArgumentNullException"><paramref name="asset"/> or <paramref name="map"/> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException"><paramref name="map"/> is currently enabled (see <see
/// cref="InputActionMap.enabled"/>) or is part of an <see cref="InputActionAsset"/> that has <see cref="InputActionMap"/>s
/// that are currently enabled.</exception>
/// <seealso cref="RemoveActionMap(InputActionAsset,string)"/>
/// <seealso cref="InputActionAsset.actionMaps"/>
public static void RemoveActionMap(this InputActionAsset asset, InputActionMap map)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (map == null)
throw new ArgumentNullException(nameof(map));
map.OnWantToChangeSetup();
asset.OnWantToChangeSetup();
// Ignore if not part of this asset.
if (map.m_Asset != asset)
return;
ArrayHelpers.Erase(ref asset.m_ActionMaps, map);
map.m_Asset = null;
asset.OnSetupChanged();
}
/// <summary>
/// Remove the action map with the given name or ID from the asset.
/// </summary>
/// <param name="asset">Asset to remove the action map from.</param>
/// <param name="nameOrId">The name or ID (see <see cref="InputActionMap.id"/>) of a map in the
/// asset. Note that lookup is case-insensitive. If no map with the given name or ID is found,
/// the method does nothing.</param>
/// <exception cref="ArgumentNullException"><paramref name="asset"/> or <paramref name="nameOrId"/> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException">The map referenced by <paramref name="nameOrId"/> is currently enabled
/// (see <see cref="InputActionMap.enabled"/>).</exception>
/// <seealso cref="RemoveActionMap(InputActionAsset,string)"/>
/// <seealso cref="InputActionAsset.actionMaps"/>
public static void RemoveActionMap(this InputActionAsset asset, string nameOrId)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (nameOrId == null)
throw new ArgumentNullException(nameof(nameOrId));
var map = asset.FindActionMap(nameOrId);
if (map != null)
asset.RemoveActionMap(map);
}
////TODO: add method to add an existing InputAction to a map
/// <summary>
/// Add a new <see cref="InputAction"/> to the given <paramref name="map"/>.
/// </summary>
/// <param name="map">Action map to add the action to. The action will be appended to
/// <see cref="InputActionMap.actions"/> of the map. The map must be disabled (see
/// <see cref="InputActionMap.enabled"/>).</param>
/// <param name="name">Name to give to the action. Must not be <c>null</c> or empty. Also,
/// no other action that already exists in <paramref name="map"/> must have this name already.</param>
/// <param name="type">Action type. See <see cref="InputAction.type"/>.</param>
/// <param name="binding">If not <c>null</c>, a binding is automatically added to the newly created action
/// with the value of this parameter being used as the binding's <see cref="InputBinding.path"/>.</param>
/// <param name="interactions">If <paramref name="binding"/> is not <c>null</c>, this string is used for
/// <see cref="InputBinding.interactions"/> of the binding that is automatically added for the action.</param>
/// <param name="processors">If <paramref name="binding"/> is not <c>null</c>, this string is used for
/// <see cref="InputBinding.processors"/> of the binding that is automatically added for the action.</param>
/// <param name="groups">If <paramref name="binding"/> is not <c>null</c>, this string is used for
/// <see cref="InputBinding.groups"/> of the binding that is automatically added for the action.</param>
/// <param name="expectedControlLayout">Value for <see cref="InputAction.expectedControlType"/>; <c>null</c>
/// by default.</param>
/// <returns>The newly added input action.</returns>
/// <exception cref="ArgumentNullException"><paramref name="map"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is <c>null</c> or empty.</exception>
/// <exception cref="InvalidOperationException"><paramref name="map"/> is enabled (see <see cref="InputActionMap.enabled"/>)
/// or is part of an <see cref="InputActionAsset"/> that has <see cref="InputActionMap"/>s that are <see cref="InputActionMap.enabled"/>
/// -or- <paramref name="map"/> already contains an action called <paramref name="name"/> (case-insensitive).</exception>
public static InputAction AddAction(this InputActionMap map, string name, InputActionType type = default, string binding = null,
string interactions = null, string processors = null, string groups = null, string expectedControlLayout = null)
{
if (map == null)
throw new ArgumentNullException(nameof(map));
if (string.IsNullOrEmpty(name))
throw new ArgumentException("Action must have name", nameof(name));
map.OnWantToChangeSetup();
if (map.FindAction(name) != null)
throw new InvalidOperationException(
$"Cannot add action with duplicate name '{name}' to set '{map.name}'");
// Append action to array.
var action = new InputAction(name, type)
{
expectedControlType = expectedControlLayout
};
action.GenerateId();
ArrayHelpers.Append(ref map.m_Actions, action);
action.m_ActionMap = map;
// Add binding, if supplied.
if (!string.IsNullOrEmpty(binding))
{
// Will trigger OnSetupChanged.
action.AddBinding(binding, interactions: interactions, processors: processors, groups: groups);
}
else
{
if (!string.IsNullOrEmpty(groups))
throw new ArgumentException(
$"No binding path was specified for action '{action}' but groups was specified ('{groups}'); cannot apply groups without binding",
nameof(groups));
// If no binding has been supplied but there are interactions and processors, they go on the action itself.
action.m_Interactions = interactions;
action.m_Processors = processors;
map.OnSetupChanged();
}
return action;
}
/// <summary>
/// Remove the given action from its <see cref="InputActionMap"/>.
/// </summary>
/// <param name="action">An input action that is part of an <see cref="InputActionMap"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="action"/> is a standalone action
/// that is not part of an <see cref="InputActionMap"/> and thus cannot be removed from anything.</exception>
/// <exception cref="InvalidOperationException"><paramref name="action"/> is part of an <see cref="InputActionMap"/>
/// or <see cref="InputActionAsset"/> that has at least one enabled action.</exception>
/// <remarks>
/// After removal, the action's <see cref="InputAction.actionMap"/> will be set to <c>null</c>
/// and the action will effectively become a standalone action that is not associated with
/// any action map. Bindings on the action will be preserved. On the action map, the bindings
/// for the action will be removed.
/// </remarks>
/// <seealso cref="AddAction"/>
public static void RemoveAction(this InputAction action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var actionMap = action.actionMap;
if (actionMap == null)
throw new ArgumentException(
$"Action '{action}' does not belong to an action map; nowhere to remove from", nameof(action));
actionMap.OnWantToChangeSetup();
var bindingsForAction = action.bindings.ToArray();
var index = actionMap.m_Actions.IndexOfReference(action);
Debug.Assert(index != -1, "Could not find action in map");
ArrayHelpers.EraseAt(ref actionMap.m_Actions, index);
action.m_ActionMap = null;
action.m_SingletonActionBindings = bindingsForAction;
// Remove bindings to action from map.
var newActionMapBindingCount = actionMap.m_Bindings.Length - bindingsForAction.Length;
if (newActionMapBindingCount == 0)
{
actionMap.m_Bindings = null;
}
else
{
var newActionMapBindings = new InputBinding[newActionMapBindingCount];
var oldActionMapBindings = actionMap.m_Bindings;
var bindingIndex = 0;
for (var i = 0; i < oldActionMapBindings.Length; ++i)
{
var binding = oldActionMapBindings[i];
if (bindingsForAction.IndexOf(b => b == binding) == -1)
newActionMapBindings[bindingIndex++] = binding;
}
actionMap.m_Bindings = newActionMapBindings;
}
actionMap.OnSetupChanged();
}
/// <summary>
/// Remove the action with the given name from the asset.
/// </summary>
/// <param name="asset">Asset to remove the action from.</param>
/// <param name="nameOrId">Name or ID of the action. See <see cref="InputActionAsset.FindAction(string,bool)"/> for
/// details.</param>
/// <exception cref="ArgumentNullException"><paramref name="asset"/> is <c>null</c> -or- <paramref name="nameOrId"/>
/// is <c>null</c> or empty.</exception>
/// <seealso cref="RemoveAction(InputAction)"/>
public static void RemoveAction(this InputActionAsset asset, string nameOrId)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (nameOrId == null)
throw new ArgumentNullException(nameof(nameOrId));
var action = asset.FindAction(nameOrId);
action?.RemoveAction();
}
/// <summary>
/// Add a new binding to the given action.
/// </summary>
/// <param name="action">Action to add the binding to. If the action is part of an <see cref="InputActionMap"/>,
/// the newly added binding will be visible on <see cref="InputActionMap.bindings"/>.</param>
/// <param name="path">Binding path string. See <see cref="InputBinding.path"/> for details.</param>
/// <param name="interactions">Optional list of interactions to apply to the binding. See <see
/// cref="InputBinding.interactions"/> for details.</param>
/// <param name="processors">Optional list of processors to apply to the binding. See <see
/// cref="InputBinding.processors"/> for details.</param>
/// <param name="groups">Optional list of binding groups that should be assigned to the binding. See
/// <see cref="InputBinding.groups"/> for details.</param>
/// <returns>Fluent-style syntax to further configure the binding.</returns>
public static BindingSyntax AddBinding(this InputAction action, string path, string interactions = null,
string processors = null, string groups = null)
{
return AddBinding(action, new InputBinding
{
path = path,
interactions = interactions,
processors = processors,
groups = groups
});
}
/// <summary>
/// Add a binding that references the given <paramref name="control"/> and triggers
/// the given <paramref cref="action"/>.
/// </summary>
/// <param name="action">Action to trigger.</param>
/// <param name="control">Control to bind to. The full <see cref="InputControl.path"/> of the control will
/// be used in the resulting <see cref="InputBinding">binding</see>.</param>
/// <returns>Syntax to configure the binding further.</returns>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null or <paramref name="control"/> is null.</exception>
/// <seealso cref="InputAction.bindings"/>
public static BindingSyntax AddBinding(this InputAction action, InputControl control)
{
if (control == null)
throw new ArgumentNullException(nameof(control));
return AddBinding(action, control.path);
}
/// <summary>
/// Add a new binding to the action.
/// </summary>
/// <param name="action">An action to add the binding to.</param>
/// <param name="binding">Binding to add to the action or default. Binding can be further configured via
/// the struct returned by the method.</param>
/// <returns>
/// Returns a fluent-style syntax structure that allows performing additional modifications
/// based on the new binding.
/// </returns>
/// <remarks>
/// This works both with actions that are part of an action set as well as with actions that aren't.
///
/// Note that actions must be disabled while altering their binding sets. Also, if the action belongs
/// to a set, all actions in the set must be disabled.
///
/// <example>
/// <code>
/// fireAction.AddBinding()
/// .WithPath("<Gamepad>/buttonSouth")
/// .WithGroup("Gamepad");
/// </code>
/// </example>
/// </remarks>
public static BindingSyntax AddBinding(this InputAction action, InputBinding binding = default)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
////REVIEW: should this reference actions by ID?
Debug.Assert(action.m_Name != null || action.isSingletonAction);
binding.action = action.name;
var actionMap = action.GetOrCreateActionMap();
var bindingIndex = AddBindingInternal(actionMap, binding);
return new BindingSyntax(actionMap, bindingIndex);
}
/// <summary>
/// Add a new binding to the given action map.
/// </summary>
/// <param name="actionMap">Action map to add the binding to.</param>
/// <param name="path">Path of the control(s) to bind to. See <see cref="InputControlPath"/> and
/// <see cref="InputBinding.path"/>.</param>
/// <param name="interactions">Names and parameters for interactions to apply to the
/// binding. See <see cref="InputBinding.interactions"/>.</param>
/// <param name="groups">Optional list of groups to apply to the binding. See <see cref="InputBinding.groups"/>.</param>
/// <param name="action">Action to trigger from the binding. See <see cref="InputBinding.action"/>.</param>
/// <param name="processors">Optional list of processors to apply to the binding. See <see cref="InputBinding.processors"/>.</param>
/// <returns>A write-accessor to the newly added binding.</returns>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception>
/// <remarks>
/// <example>
/// <code>
/// // Add a binding for the A button the gamepad and make it trigger
/// // the "fire" action.
/// var gameplayActions = playerInput.actions.FindActionMap("gameplay");
/// gameplayActions.AddBinding("<Gamepad>/buttonSouth", action: "fire");
/// </code>
/// </example>
/// </remarks>
/// <seealso cref="InputBinding"/>
/// <seealso cref="InputActionMap.bindings"/>
public static BindingSyntax AddBinding(this InputActionMap actionMap, string path,
string interactions = null, string groups = null, string action = null, string processors = null)
{
if (path == null)
throw new ArgumentNullException(nameof(path), "Binding path cannot be null");
return AddBinding(actionMap,
new InputBinding
{
path = path,
interactions = interactions,
groups = groups,
action = action,
processors = processors,
});
}
/// <summary>
/// Add a new binding that triggers the given action to the given action map.
/// </summary>
/// <param name="actionMap">Action map to add the binding to.</param>
/// <param name="action">Action to trigger from the binding. See <see cref="InputBinding.action"/>.
/// Must be part of <paramref name="actionMap"/>.</param>
/// <param name="path">Path of the control(s) to bind to. See <see cref="InputControlPath"/> and
/// <see cref="InputBinding.path"/>.</param>
/// <param name="interactions">Names and parameters for interactions to apply to the
/// binding. See <see cref="InputBinding.interactions"/>.</param>
/// <param name="groups">Binding groups to apply to the binding. See <see cref="InputBinding.groups"/>.</param>
/// <returns>A write-accessor to the newly added binding.</returns>
/// <exception cref="ArgumentException"><paramref name="action"/> is not part of <paramref name="actionMap"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception>
/// <seealso cref="InputBinding"/>
/// <seealso cref="InputActionMap.bindings"/>
public static BindingSyntax AddBinding(this InputActionMap actionMap, string path, InputAction action,
string interactions = null, string groups = null)
{
if (action != null && action.actionMap != actionMap)
throw new ArgumentException(
$"Action '{action}' is not part of action map '{actionMap}'", nameof(action));
if (action == null)
return AddBinding(actionMap, path: path, interactions: interactions, groups: groups);
return AddBinding(actionMap, path: path, interactions: interactions, groups: groups,
action: action.id);
}
/// <summary>
/// Add a new binding that triggers the action given by GUID <paramref name="action"/>.
/// </summary>
/// <param name="actionMap">Action map to add the binding to.</param>
/// <param name="path">Path of the control(s) to bind to. See <see cref="InputControlPath"/> and <see cref="InputBinding.path"/>.</param>
/// <param name="action">ID of the action as per <see cref="InputAction.id"/>.</param>
/// <param name="interactions">Optional list of names and parameters for interactions to apply to the
/// binding. See <see cref="InputBinding.interactions"/>.</param>
/// <param name="groups">Optional list of groups to apply to the binding. See <see cref="InputBinding.groups"/>.</param>
/// <returns>A write-accessor to the newly added binding.</returns>
/// <exception cref="ArgumentException"><paramref name="action"/> is not part of <paramref name="actionMap"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception>
/// <seealso cref="InputBinding"/>
/// <seealso cref="InputActionMap.bindings"/>
/// <remarks>
/// Example of adding a binding to an action map that binds to a Gamepad device "leftStick" control and associates it with an action:
/// <example>
/// <code>
/// var map = new InputActionMap();
/// var action = map.AddAction("action");
/// map.AddBinding("<Gamepad>/leftStick", action: action.id);
/// </code>
/// </example>
/// </remarks>
public static BindingSyntax AddBinding(this InputActionMap actionMap, string path, Guid action,
string interactions = null, string groups = null)
{
if (action == Guid.Empty)
return AddBinding(actionMap, path: path, interactions: interactions, groups: groups);
return AddBinding(actionMap, path: path, interactions: interactions, groups: groups,
action: action.ToString());
}
/// <summary>
/// Add a binding to the given action map.
/// </summary>
/// <param name="actionMap">Action map to add the binding to.</param>
/// <param name="binding">Binding to add to the action map.</param>
/// <returns>A write-accessor to the newly added binding.</returns>
/// <exception cref="ArgumentException"><paramref name="action"/> is not part of <paramref name="actionMap"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception>
/// <seealso cref="InputBinding"/>
/// <seealso cref="InputActionMap.bindings"/>
public static BindingSyntax AddBinding(this InputActionMap actionMap, InputBinding binding)
{
if (actionMap == null)
throw new ArgumentNullException(nameof(actionMap));
if (binding.path == null)
throw new ArgumentException("Binding path cannot be null", nameof(binding));
var bindingIndex = AddBindingInternal(actionMap, binding);
return new BindingSyntax(actionMap, bindingIndex);
}
/// <summary>
/// Add a composite binding to the <see cref="InputAction.bindings"/> of <paramref name="action"/>.
/// </summary>
/// <param name="action">Action to add the binding to.</param>
/// <param name="composite">Type of composite to add. This needs to be the name the composite
/// has been registered under using <see cref="InputSystem.RegisterBindingComposite{T}"/>. Case-insensitive.</param>
/// <param name="interactions">Interactions to add to the binding. See <see cref="InputBinding.interactions"/>.</param>
/// <param name="processors">Processors to add to the binding. See <see cref="InputBinding.processors"/>.</param>
/// <returns>A write accessor to the newly added composite binding.</returns>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="composite"/> is <c>null</c> or empty.</exception>
public static CompositeSyntax AddCompositeBinding(this InputAction action, string composite,
string interactions = null, string processors = null)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
if (string.IsNullOrEmpty(composite))
throw new ArgumentException("Composite name cannot be null or empty", nameof(composite));
var actionMap = action.GetOrCreateActionMap();
var binding = new InputBinding
{
name = NameAndParameters.ParseName(composite),
path = composite,
interactions = interactions,
processors = processors,
isComposite = true,
action = action.name
};
var bindingIndex = AddBindingInternal(actionMap, binding);
return new CompositeSyntax(actionMap, action, bindingIndex);
}
////TODO: AddCompositeBinding<T>
private static int AddBindingInternal(InputActionMap map, InputBinding binding, int bindingIndex = -1)
{
Debug.Assert(map != null);
// Make sure the binding has an ID.
if (string.IsNullOrEmpty(binding.m_Id))
binding.GenerateId();
// Append to bindings in set.
if (bindingIndex < 0)
bindingIndex = ArrayHelpers.Append(ref map.m_Bindings, binding);
else
ArrayHelpers.InsertAt(ref map.m_Bindings, bindingIndex, binding);
// Make sure this asset is reloaded from disk when exiting play mode so it isn't inadvertently
// changed between play sessions. Only applies when running in the editor.
if (map.asset != null)
map.asset.MarkAsDirty();
// If we're looking at a singleton action, make sure m_Bindings is up to date just
// in case the action gets serialized.
if (map.m_SingletonAction != null)
map.m_SingletonAction.m_SingletonActionBindings = map.m_Bindings;
// NOTE: We treat this as a mere binding modification, even though we have added something.
// InputAction.RestoreActionStatesAfterReResolvingBindings() can deal with bindings
// having been removed or added.
map.OnBindingModified();
return bindingIndex;
}
/// <summary>
/// Get write access to the binding in <see cref="InputAction.bindings"/> of <paramref name="action"/>
/// at the given <paramref name="index"/>.
/// </summary>
/// <param name="action">Action whose bindings to change.</param>
/// <param name="index">Index in <paramref name="action"/>'s <see cref="InputAction.bindings"/> of the binding to be changed.</param>
/// <returns>A write accessor to the given binding.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Grab "fire" action from PlayerInput.
/// var fireAction = playerInput.actions["fire"];
///
/// // Change its second binding to go to the left mouse button.
/// fireAction.ChangeBinding(1)
/// .WithPath("<Mouse>/leftButton");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is out of range (as per <see cref="InputAction.bindings"/>
/// of <paramref name="action"/>).</exception>
public static BindingSyntax ChangeBinding(this InputAction action, int index)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var indexOnMap = action.BindingIndexOnActionToBindingIndexOnMap(index);
return new BindingSyntax(action.GetOrCreateActionMap(), indexOnMap, action);
}
/// <summary>
/// Get write access to the binding in <see cref="InputAction.bindings"/> of <paramref name="action"/>
/// with the given <paramref name="name"/>.
/// </summary>
/// <param name="action">Action whose bindings to change.</param>
/// <param name="name">Name of the binding to be changed <see cref="InputAction.bindings"/>.</param>
/// <returns>A write accessor to the given binding.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Grab "fire" action from PlayerInput.
/// var fireAction = playerInput.actions["fire"];
///
/// // Change its second binding to go to the left mouse button.
/// fireAction.ChangeBinding("fire")
/// .WithPath("<Mouse>/leftButton");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is out of range (as per <see cref="InputAction.bindings"/>
/// of <paramref name="action"/>).</exception>
public static BindingSyntax ChangeBinding(this InputAction action, string name)
{
return action.ChangeBinding(new InputBinding { name = name });
}
/// <summary>
/// Get write access to the binding in <see cref="InputActionMap.bindings"/> of <paramref name="actionMap"/>
/// at the given <paramref name="index"/>.
/// </summary>
/// <param name="actionMap">Action map whose bindings to change.</param>
/// <param name="index">Index in <paramref name="actionMap"/>'s <see cref="InputActionMap.bindings"/> of the binding to be changed.</param>
/// <returns>A write accessor to the given binding.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Grab "gameplay" actions from PlayerInput.
/// var gameplayActions = playerInput.actions.FindActionMap("gameplay");
///
/// // Change its second binding to go to the left mouse button.
/// gameplayActions.ChangeBinding(1)
/// .WithPath("<Mouse>/leftButton");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="actionMap"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is out of range (as per <see cref="InputActionMap.bindings"/>
/// of <paramref name="actionMap"/>).</exception>
public static BindingSyntax ChangeBinding(this InputActionMap actionMap, int index)
{
if (actionMap == null)
throw new ArgumentNullException(nameof(actionMap));
if (index < 0 || index >= actionMap.m_Bindings.LengthSafe())
throw new ArgumentOutOfRangeException(nameof(index));
return new BindingSyntax(actionMap, index);
}
/// <summary>
/// Get write access to the binding in <see cref="InputAction.bindings"/> of <paramref name="action"/>
/// that has the given <paramref name="id"/>.
/// </summary>
/// <param name="action">Action whose bindings to change.</param>
/// <param name="id">ID of the binding as per <see cref="InputBinding.id"/>.</param>
/// <returns>A write accessor to the binding with the given ID.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Grab "fire" action from PlayerInput.
/// var fireAction = playerInput.actions["fire"];
///
/// // Change the binding with the given ID to go to the left mouse button.
/// fireAction.ChangeBindingWithId("c3de9215-31c3-4654-8562-854bf2f7864f")
/// .WithPath("<Mouse>/leftButton");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">No binding with the given <paramref name="id"/> exists
/// on <paramref name="action"/>.</exception>
public static BindingSyntax ChangeBindingWithId(this InputAction action, string id)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
return action.ChangeBinding(new InputBinding {m_Id = id});
}
/// <summary>
/// Get write access to the binding in <see cref="InputAction.bindings"/> of <paramref name="action"/>
/// that has the given <paramref name="id"/>.
/// </summary>
/// <param name="action">Action whose bindings to change.</param>
/// <param name="id">ID of the binding as per <see cref="InputBinding.id"/>.</param>
/// <returns>A write accessor to the binding with the given ID.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Grab "fire" action from PlayerInput.
/// var fireAction = playerInput.actions["fire"];
///
/// // Change the binding with the given ID to go to the left mouse button.
/// fireAction.ChangeBindingWithId(new Guid("c3de9215-31c3-4654-8562-854bf2f7864f"))
/// .WithPath("<Mouse>/leftButton");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">No binding with the given <paramref name="id"/> exists
/// on <paramref name="action"/>.</exception>
public static BindingSyntax ChangeBindingWithId(this InputAction action, Guid id)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
return action.ChangeBinding(new InputBinding {id = id});
}
/// <summary>
/// Get write access to the first binding in <see cref="InputAction.bindings"/> of <paramref name="action"/>
/// that is assigned to the given binding <paramref name="group"/>.
/// </summary>
/// <param name="action">Action whose bindings to change.</param>
/// <param name="group">Name of the binding group as per <see cref="InputBinding.groups"/>.</param>
/// <returns>A write accessor to the first binding on <paramref name="action"/> that is assigned to the
/// given binding <paramref name="group"/>.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Grab "fire" action from PlayerInput.
/// var fireAction = playerInput.actions["fire"];
///
/// // Change the binding in the "Keyboard&Mouse" group to go to the left mouse button.
/// fireAction.ChangeBindingWithGroup("Keyboard&Mouse")
/// .WithPath("<Mouse>/leftButton");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">No binding on the <paramref name="action"/> is assigned
/// to the given binding <paramref name="group"/>.</exception>
public static BindingSyntax ChangeBindingWithGroup(this InputAction action, string group)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
return action.ChangeBinding(new InputBinding {groups = group});
}
/// <summary>
/// Get write access to the binding in <see cref="InputAction.bindings"/> of <paramref name="action"/>
/// that is bound to the given <paramref name="path"/>.
/// </summary>
/// <param name="action">Action whose bindings to change.</param>
/// <param name="path">Path of the binding as per <see cref="InputBinding.path"/>.</param>
/// <returns>A write accessor to the binding on <paramref name="action"/> that is assigned the
/// given <paramref name="path"/>.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Grab "fire" action from PlayerInput.
/// var fireAction = playerInput.actions["fire"];
///
/// // Change the binding to the right mouse button to go to the left mouse button instead.
/// fireAction.ChangeBindingWithPath("<Mouse>/rightButton")
/// .WithPath("<Mouse>/leftButton");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">No binding on the <paramref name="action"/> is assigned
/// the given <paramref name="path"/>.</exception>
public static BindingSyntax ChangeBindingWithPath(this InputAction action, string path)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
return action.ChangeBinding(new InputBinding {path = path});
}
/// <summary>
/// Get write access to the binding on <paramref name="action"/> that matches the given
/// <paramref name="match"/>.
/// </summary>
/// <param name="action">Action whose bindings to match against.</param>
/// <param name="match">A binding mask. See <see cref="InputBinding.Matches"/> for
/// details.</param>
/// <returns>A write-accessor to the first binding matching <paramref name="match"/> or
/// an invalid accessor (see <see cref="BindingSyntax.valid"/>) if no binding was found to
/// match the mask.</returns>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
public static BindingSyntax ChangeBinding(this InputAction action, InputBinding match)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var actionMap = action.GetOrCreateActionMap();
int bindingIndexInMap = -1;
var id = action.idDontGenerate;
if (id != null)
{
// Prio1: Attempt to match action id (stronger)
match.action = action.id.ToString();
bindingIndexInMap = actionMap.FindBindingRelativeToMap(match);
}
if (bindingIndexInMap == -1)
{
// Prio2: Attempt to match action name (weaker)
match.action = action.name;
bindingIndexInMap = actionMap.FindBindingRelativeToMap(match);
}
if (bindingIndexInMap == -1)
return default;
return new BindingSyntax(actionMap, bindingIndexInMap, action);
}
/// <summary>
/// Get a write accessor to the binding of <paramref name="action"/> that is both a composite
/// (see <see cref="InputBinding.isComposite"/>) and has the given binding name or composite
/// type.
/// </summary>
/// <param name="action">Action to look up the binding on. All bindings in the action's
/// <see cref="InputAction.bindings"/> property will be considered.</param>
/// <param name="compositeName">Either the name of the composite binding (see <see cref="InputBinding.name"/>)
/// to look for or the name of the composite type used in the binding (such as "1DAxis"). Case-insensitive.</param>
/// <returns>A write accessor to the given composite binding or an invalid accessor if no composite
/// matching <paramref name="compositeName"/> could be found on <paramref name="action"/>.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Add arrow keys as alternatives to the WASD Vector2 composite.
/// playerInput.actions["move"]
/// .ChangeCompositeBinding("WASD")
/// .InsertPartBinding("Up", "<Keyboard>/upArrow")
/// .InsertPartBinding("Down", "<Keyboard>/downArrow")
/// .InsertPartBinding("Left", "<Keyboard>/leftArrow")
/// .InsertPartBinding("Right", "<Keyboard>/rightArrow");
/// </code>
/// </example>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c> -or- <paramref name="compositeName"/>
/// is <c>null</c> or empty.</exception>
/// <seealso cref="InputBinding.isComposite"/>
/// <seealso cref="InputBindingComposite"/>
public static BindingSyntax ChangeCompositeBinding(this InputAction action, string compositeName)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
if (string.IsNullOrEmpty(compositeName))
throw new ArgumentNullException(nameof(compositeName));
var actionMap = action.GetOrCreateActionMap();
var bindings = actionMap.m_Bindings;
var numBindings = bindings.LengthSafe();
for (var i = 0; i < numBindings; ++i)
{
ref var binding = ref bindings[i];
if (!binding.isComposite || !binding.TriggersAction(action))
continue;
////REVIEW: should this do a registration lookup to deal with aliases?
if (compositeName.Equals(binding.name, StringComparison.InvariantCultureIgnoreCase)
|| compositeName.Equals(NameAndParameters.ParseName(binding.path),
StringComparison.InvariantCultureIgnoreCase))
return new BindingSyntax(actionMap, i, action);
}
return default;
}
////TODO: update binding mask if necessary
/// <summary>
/// Rename an existing action.
/// </summary>
/// <param name="action">Action to assign a new name to. Can be singleton action or action that
/// is part of a map.</param>
/// <param name="newName">New name to assign to action. Cannot be empty.</param>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null or <paramref name="newName"/> is
/// null or empty.</exception>
/// <exception cref="InvalidOperationException"><see cref="InputAction.actionMap"/> of <paramref name="action"/>
/// already contains an action called <paramref name="newName"/>.</exception>
/// <remarks>
/// Renaming an action will also update the bindings that refer to the action.
/// </remarks>
public static void Rename(this InputAction action, string newName)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
if (string.IsNullOrEmpty(newName))
throw new ArgumentNullException(nameof(newName));
if (action.name == newName)
return;
// Make sure name isn't already taken in map.
var actionMap = action.actionMap;
if (actionMap?.FindAction(newName) != null)
throw new InvalidOperationException(
$"Cannot rename '{action}' to '{newName}' in map '{actionMap}' as the map already contains an action with that name");
var oldName = action.m_Name;
action.m_Name = newName;
actionMap?.ClearActionLookupTable();
if (actionMap?.asset != null)
actionMap?.asset.MarkAsDirty();
// Update bindings.
var bindings = action.GetOrCreateActionMap().m_Bindings;
var bindingCount = bindings.LengthSafe();
for (var i = 0; i < bindingCount; ++i)
if (string.Compare(bindings[i].action, oldName, StringComparison.InvariantCultureIgnoreCase) == 0)
bindings[i].action = newName;
}
/// <summary>
/// Add a new control scheme to the asset.
/// </summary>
/// <param name="asset">Asset to add the control scheme to.</param>
/// <param name="controlScheme">Control scheme to add.</param>
/// <exception cref="ArgumentException"><paramref name="controlScheme"/> has no name.</exception>
/// <exception cref="ArgumentNullException"><paramref name="asset"/> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException">A control scheme with the same name as <paramref name="controlScheme"/>
/// already exists in the asset.</exception>
/// <remarks>
/// </remarks>
public static void AddControlScheme(this InputActionAsset asset, InputControlScheme controlScheme)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (string.IsNullOrEmpty(controlScheme.name))
throw new ArgumentException("Cannot add control scheme without name to asset " + asset.name, nameof(controlScheme));
if (asset.FindControlScheme(controlScheme.name) != null)
throw new InvalidOperationException(
$"Asset '{asset.name}' already contains a control scheme called '{controlScheme.name}'");
ArrayHelpers.Append(ref asset.m_ControlSchemes, controlScheme);
asset.MarkAsDirty();
}
/// <summary>
/// Add a new control scheme to the given <paramref name="asset"/>.
/// </summary>
/// <param name="asset">Asset to add the control scheme to.</param>
/// <param name="name">Name to give to the control scheme. Must be unique within the control schemes of the
/// asset. Also used as default name of <see cref="InputControlScheme.bindingGroup">binding group</see> associated
/// with the control scheme.</param>
/// <returns>Syntax to allow providing additional configuration for the newly added control scheme.</returns>
/// <exception cref="ArgumentNullException"><paramref name="asset"/> is <c>null</c> -or- <paramref name="name"/>
/// is <c>null</c> or empty.</exception>
/// <remarks>
/// <example>
/// <code>
/// // Create an .inputactions asset.
/// var asset = ScriptableObject.CreateInstance<InputActionAsset>();
///
/// // Add an action map to it.
/// var actionMap = asset.AddActionMap("actions");
///
/// // Add an action to it and bind it to the A button on the gamepad.
/// // Also, associate that binding with the "Gamepad" control scheme.
/// var action = actionMap.AddAction("action");
/// action.AddBinding("<Gamepad>/buttonSouth", groups: "Gamepad");
///
/// // Add a control scheme called "Gamepad" that requires a Gamepad device.
/// asset.AddControlScheme("Gamepad")
/// .WithRequiredDevice<Gamepad>();
/// </code>
/// </example>
/// </remarks>
public static ControlSchemeSyntax AddControlScheme(this InputActionAsset asset, string name)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
var index = asset.controlSchemes.Count;
asset.AddControlScheme(new InputControlScheme(name));
return new ControlSchemeSyntax(asset, index);
}
/// <summary>
/// Remove the control scheme with the given name from the asset.
/// </summary>
/// <param name="asset">Asset to remove the control scheme from.</param>
/// <param name="name">Name of the control scheme. Matching is case-insensitive.</param>
/// <exception cref="ArgumentNullException"><paramref name="asset"/> is null -or- <paramref name="name"/>
/// is <c>null</c> or empty.</exception>
/// <remarks>
/// If no control scheme with the given name can be found, the method does nothing.
/// </remarks>
public static void RemoveControlScheme(this InputActionAsset asset, string name)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
var index = asset.FindControlSchemeIndex(name);
if (index != -1)
ArrayHelpers.EraseAt(ref asset.m_ControlSchemes, index);
asset.MarkAsDirty();
}
/// <summary>
/// Associates the control scheme given by <paramref name="scheme"/> with the binding group given by <paramref name="bindingGroup"/>.
/// </summary>
/// <param name="scheme">The control scheme to modify.</param>
/// <param name="bindingGroup">The binding group to be associated with the control scheme.</param>
/// <returns><paramref name="scheme"/></returns>
public static InputControlScheme WithBindingGroup(this InputControlScheme scheme, string bindingGroup)
{
return new ControlSchemeSyntax(scheme).WithBindingGroup(bindingGroup).Done();
}
public static InputControlScheme WithDevice(this InputControlScheme scheme, string controlPath, bool required)
{
if (required)
return new ControlSchemeSyntax(scheme).WithRequiredDevice(controlPath).Done();
return new ControlSchemeSyntax(scheme).WithOptionalDevice(controlPath).Done();
}
public static InputControlScheme WithRequiredDevice(this InputControlScheme scheme, string controlPath)
{
return new ControlSchemeSyntax(scheme).WithRequiredDevice(controlPath).Done();
}
public static InputControlScheme WithOptionalDevice(this InputControlScheme scheme, string controlPath)
{
return new ControlSchemeSyntax(scheme).WithOptionalDevice(controlPath).Done();
}
public static InputControlScheme OrWithRequiredDevice(this InputControlScheme scheme, string controlPath)
{
return new ControlSchemeSyntax(scheme).OrWithRequiredDevice(controlPath).Done();
}
public static InputControlScheme OrWithOptionalDevice(this InputControlScheme scheme, string controlPath)
{
return new ControlSchemeSyntax(scheme).OrWithOptionalDevice(controlPath).Done();
}
/// <summary>
/// Write accessor to a binding on either an <see cref="InputAction"/> or an
/// <see cref="InputActionMap"/>.
/// </summary>
/// <remarks>
/// Both <see cref="InputAction.bindings"/> and <see cref="InputActionMap.bindings"/> are
/// read-only. To modify bindings (other than setting overrides which you can do
/// through <see cref="InputActionRebindingExtensions.ApplyBindingOverride(InputAction,int,InputBinding)"/>),
/// it is necessary to gain indirect write access through this structure.
///
/// <example>
/// <code>
/// playerInput.actions["fire"]
/// .ChangeBinding(0)
/// .WithPath("<Keyboard>/space");
/// </code>
/// </example>
/// </remarks>
/// <seealso cref="AddBinding(InputAction,InputBinding)"/>
/// <seealso cref="ChangeBinding(InputAction,int)"/>
public struct BindingSyntax
{
private readonly InputActionMap m_ActionMap;
private readonly InputAction m_Action;
internal readonly int m_BindingIndexInMap;
/// <summary>
/// True if the if binding accessor is valid.
/// </summary>
public bool valid => m_ActionMap != null && m_BindingIndexInMap >= 0 && m_BindingIndexInMap < m_ActionMap.m_Bindings.LengthSafe();
/// <summary>
/// Index of the binding that the accessor refers to.
/// </summary>
/// <remarks>
/// When accessing bindings on an <see cref="InputAction"/>, this is the index in
/// <see cref="InputAction.bindings"/> of the action. When accessing bindings on an
/// <see cref="InputActionMap"/>, it is the index <see cref="InputActionMap.bindings"/>
/// of the map.
/// </remarks>
public int bindingIndex
{
get
{
if (!valid)
return -1;
if (m_Action != null)
return m_Action.BindingIndexOnMapToBindingIndexOnAction(m_BindingIndexInMap);
return m_BindingIndexInMap;
}
}
/// <summary>
/// The current binding in entirety.
/// </summary>
/// <exception cref="InvalidOperationException">The accessor is not <see cref="valid"/>.</exception>
public InputBinding binding
{
get
{
if (!valid)
throw new InvalidOperationException("BindingSyntax accessor is not valid");
return m_ActionMap.m_Bindings[m_BindingIndexInMap];
}
}
internal BindingSyntax(InputActionMap map, int bindingIndexInMap, InputAction action = null)
{
m_ActionMap = map;
m_BindingIndexInMap = bindingIndexInMap;
m_Action = action;
}
/// <summary>
/// Set the <see cref="InputBinding.name"/> of the binding.
/// </summary>
/// <param name="name">Name for the binding.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is not <see cref="valid"/>.</exception>
/// <seealso cref="InputBinding.name"/>
public BindingSyntax WithName(string name)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
m_ActionMap.m_Bindings[m_BindingIndexInMap].name = name;
m_ActionMap.OnBindingModified();
return this;
}
/// <summary>
/// Set the <see cref="InputBinding.path"/> of the binding.
/// </summary>
/// <param name="path">Path for the binding.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is not <see cref="valid"/>.</exception>
/// <seealso cref="InputBinding.path"/>
public BindingSyntax WithPath(string path)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
m_ActionMap.m_Bindings[m_BindingIndexInMap].path = path;
m_ActionMap.OnBindingModified();
return this;
}
/// <summary>
/// Add <paramref name="group"/> to the list of <see cref="InputBinding.groups"/> of the binding.
/// </summary>
/// <param name="group">Name of the binding group (such as "Gamepad").</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="ArgumentException"><paramref name="group"/> is <c>null</c> or empty -or- it contains
/// a <see cref="InputBinding.Separator"/> character.</exception>
public BindingSyntax WithGroup(string group)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
if (string.IsNullOrEmpty(group))
throw new ArgumentException("Group name cannot be null or empty", nameof(group));
if (group.IndexOf(InputBinding.Separator) != -1)
throw new ArgumentException(
$"Group name cannot contain separator character '{InputBinding.Separator}'", nameof(group));
return WithGroups(group);
}
/// <summary>
/// Add all the groups specified in <paramref name="groups"/> to the list of <see cref="InputBinding.groups"/> of the binding.
/// </summary>
/// <param name="groups">A semi-colon separated list of group names.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
public BindingSyntax WithGroups(string groups)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
if (string.IsNullOrEmpty(groups))
return this;
// Join with existing group, if any.
var currentGroups = m_ActionMap.m_Bindings[m_BindingIndexInMap].groups;
if (!string.IsNullOrEmpty(currentGroups))
groups = string.Join(InputBinding.kSeparatorString, currentGroups, groups);
// Set groups on binding.
m_ActionMap.m_Bindings[m_BindingIndexInMap].groups = groups;
m_ActionMap.OnBindingModified();
return this;
}
/// <summary>
/// Add an interaction via specified name in <paramref name="interaction"/> to the list of interactions.
/// </summary>
/// <param name="interaction">Interaction class name</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
/// <exception cref="ArgumentException">If interaction name is null or empty.</exception>
public BindingSyntax WithInteraction(string interaction)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
if (string.IsNullOrEmpty(interaction))
throw new ArgumentException("Interaction cannot be null or empty", nameof(interaction));
if (interaction.IndexOf(InputBinding.Separator) != -1)
throw new ArgumentException(
$"Interaction string cannot contain separator character '{InputBinding.Separator}'", nameof(interaction));
return WithInteractions(interaction);
}
/// <summary>
/// Add the set of interactions specified in <paramref name="interactions"/> to the list of interactions.
/// </summary>
/// <param name="interactions">A semi-colon separated list of interaction names.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
public BindingSyntax WithInteractions(string interactions)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
if (string.IsNullOrEmpty(interactions))
return this;
// Join with existing interaction string, if any.
var currentInteractions = m_ActionMap.m_Bindings[m_BindingIndexInMap].interactions;
if (!string.IsNullOrEmpty(currentInteractions))
interactions = string.Join(InputBinding.kSeparatorString, currentInteractions, interactions);
// Set interactions on binding.
m_ActionMap.m_Bindings[m_BindingIndexInMap].interactions = interactions;
m_ActionMap.OnBindingModified();
return this;
}
/// <summary>
/// Add an interaction of type specified in <typeparamref name="TInteraction"/> to the list of interactions.
/// </summary>
/// <typeparam name="TInteraction"></typeparam>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
/// <exception cref="NotSupportedException">Interaction type has not been registered.</exception>
public BindingSyntax WithInteraction<TInteraction>()
where TInteraction : IInputInteraction
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
var interactionName = InputProcessor.s_Processors.FindNameForType(typeof(TInteraction));
if (interactionName.IsEmpty())
throw new NotSupportedException($"Type '{typeof(TInteraction)}' has not been registered as a processor");
return WithInteraction(interactionName);
}
/// <summary>
/// Add a processor to the list of <see cref="InputBinding.processors"/> of the binding.
/// </summary>
/// <param name="processor">Name of the processor, such as "Scale".</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
/// <exception cref="ArgumentException">The processor name is null or empty.</exception>
/// <exception cref="ArgumentException">The processor name contains a semi-colon.</exception>
public BindingSyntax WithProcessor(string processor)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
if (string.IsNullOrEmpty(processor))
throw new ArgumentException("Processor cannot be null or empty", nameof(processor));
if (processor.IndexOf(InputBinding.Separator) != -1)
throw new ArgumentException(
$"Processor string cannot contain separator character '{InputBinding.Separator}'", nameof(processor));
return WithProcessors(processor);
}
/// <summary>
/// Add processors to the list of <see cref="InputBinding.processors"/> of the binding.
/// </summary>
/// <param name="processors">A semi-colon separated list of processor names.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
public BindingSyntax WithProcessors(string processors)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
if (string.IsNullOrEmpty(processors))
return this;
// Join with existing processor string, if any.
var currentProcessors = m_ActionMap.m_Bindings[m_BindingIndexInMap].processors;
if (!string.IsNullOrEmpty(currentProcessors))
processors = string.Join(InputBinding.kSeparatorString, currentProcessors, processors);
// Set processors on binding.
m_ActionMap.m_Bindings[m_BindingIndexInMap].processors = processors;
m_ActionMap.OnBindingModified();
return this;
}
/// <summary>
/// Add a processor to the list of <see cref="InputBinding.processors"/> of the binding.
/// </summary>
/// <typeparam name="TProcessor">Type of processor.</typeparam>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
/// <exception cref="NotSupportedException">Processor type has not been registered.</exception>
public BindingSyntax WithProcessor<TProcessor>()
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
var processorName = InputProcessor.s_Processors.FindNameForType(typeof(TProcessor));
if (processorName.IsEmpty())
throw new NotSupportedException($"Type '{typeof(TProcessor)}' has not been registered as a processor");
return WithProcessor(processorName);
}
/// <summary>
/// Specify which action to trigger.
/// </summary>
/// <param name="action">Action to trigger.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="InvalidOperationException">The binding accessor is invalid.</exception>
/// <exception cref="ArgumentNullException">Provided action is null.</exception>
/// <exception cref="ArgumentException">Provided action is a singleton action (not part of any action maps).</exception>
public BindingSyntax Triggering(InputAction action)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
if (action == null)
throw new ArgumentNullException(nameof(action));
if (action.isSingletonAction)
throw new ArgumentException(
$"Cannot change the action a binding triggers on singleton action '{action}'", nameof(action));
m_ActionMap.m_Bindings[m_BindingIndexInMap].action = action.name;
m_ActionMap.OnBindingModified();
return this;
}
/// <summary>
/// Replace the current binding with the given one.
/// </summary>
/// <param name="binding">An input binding.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <remarks>
/// This method replaces the current binding wholesale, i.e. it will overwrite all fields.
/// Be aware that this has the potential of corrupting the binding data in case the given
/// binding is a composite.
/// </remarks>
public BindingSyntax To(InputBinding binding)
{
if (!valid)
throw new InvalidOperationException("Accessor is not valid");
m_ActionMap.m_Bindings[m_BindingIndexInMap] = binding;
// If it's a singleton action, we force the binding to stay with the action.
if (m_ActionMap.m_SingletonAction != null)
m_ActionMap.m_Bindings[m_BindingIndexInMap].action = m_ActionMap.m_SingletonAction.name;
m_ActionMap.OnBindingModified();
return this;
}
/// <summary>
/// Switch to configuring the next binding.
/// </summary>
/// <returns>An instance configured to edit the next binding or an invalid (see <see cref="valid"/>) instance if
/// there is no next binding.</returns>
/// <remarks>If the BindingSyntax is restricted to a single action, the result will be invalid (see <see cref="valid"/>)
/// if there is no next binding on the action. If the BindingSyntax is restricted to an <see cref="InputActionMap"/>, the result will
/// be be invalid if there is no next binding in the map.</remarks>
public BindingSyntax NextBinding()
{
return Iterate(true);
}
/// <summary>
/// Switch to configuring the previous binding.
/// </summary>
/// <returns>An instance configured to edit the previous binding or an invalid (see <see cref="valid"/>) instance if
/// there is no previous binding.</returns>
/// <remarks>If the BindingSyntax is restricted to a single action, the result will be invalid (see <see cref="valid"/>)
/// if there is no previous binding on the action. If the BindingSyntax is restricted to an <see cref="InputActionMap"/>, the result will
/// be be invalid if there is no previous binding in the map.</remarks>
public BindingSyntax PreviousBinding()
{
return Iterate(false);
}
/// <summary>
/// Iterate to the next part binding of the current composite with the given part name.
/// </summary>
/// <param name="partName">Name of the part of the binding, such as <c>"Positive"</c>.</param>
/// <returns>An accessor to the next part binding with the given name or an invalid (see <see cref="valid"/>)
/// accessor if there is no such binding.</returns>
/// <exception cref="ArgumentNullException"><paramref name="partName"/> is <c>null</c> or empty.</exception>
/// <remarks>
/// Each binding that is part of a composite is marked with <see cref="InputBinding.isPartOfComposite"/>
/// set to true. The name of the part is determined by <see cref="InputBinding.name"/> (comparison is
/// case-insensitive). Which parts are relevant to a specific composite is determined by the type of
/// composite. An <see cref="Composites.AxisComposite"/>, for example, has <c>"Negative"</c> and a
/// <c>"Positive"</c> part.
///
/// <example>
/// <code>
/// // Delete first "Positive" part of "Axis" composite.
/// action.ChangeCompositeBinding("Axis")
/// .NextPartBinding("Positive").Erase();
/// </code>
/// </example>
/// </remarks>
/// <seealso cref="InputBinding.isPartOfComposite"/>
/// <seealso cref="InputBinding.isComposite"/>
/// <seealso cref="InputBindingComposite"/>
public BindingSyntax NextPartBinding(string partName)
{
if (string.IsNullOrEmpty(partName))
throw new ArgumentNullException(nameof(partName));
return IteratePartBinding(true, partName);
}
/// <summary>
/// Iterate to the previous part binding of the current composite with the given part name.
/// </summary>
/// <param name="partName">Name of the part of the binding, such as <c>"Positive"</c>.</param>
/// <returns>An accessor to the previous part binding with the given name or an invalid (see <see cref="valid"/>)
/// accessor if there is no such binding.</returns>
/// <exception cref="ArgumentNullException"><paramref name="partName"/> is <c>null</c> or empty.</exception>
/// <remarks>
/// Each binding that is part of a composite is marked with <see cref="InputBinding.isPartOfComposite"/>
/// set to true. The name of the part is determined by <see cref="InputBinding.name"/> (comparison is
/// case-insensitive). Which parts are relevant to a specific composite is determined by the type of
/// composite. An <see cref="Composites.AxisComposite"/>, for example, has <c>"Negative"</c> and a
/// <c>"Positive"</c> part.
/// </remarks>
/// <seealso cref="InputBinding.isPartOfComposite"/>
/// <seealso cref="InputBinding.isComposite"/>
/// <seealso cref="InputBindingComposite"/>
public BindingSyntax PreviousPartBinding(string partName)
{
if (string.IsNullOrEmpty(partName))
throw new ArgumentNullException(nameof(partName));
return IteratePartBinding(false, partName);
}
/// <summary>
/// Iterate to the next composite binding.
/// </summary>
/// <param name="compositeName">If <c>null</c> (default), an accessor to the next composite binding,
/// regardless of name or type, is returned. If it is not <c>null</c>, can be either the name of
/// the binding (see <see cref="InputBinding.name"/>) or the name of the composite used in the
/// binding (see <see cref="InputSystem.RegisterBindingComposite"/></param>).
/// <returns>A write accessor to the next composite binding or an invalid accessor (see
/// <see cref="valid"/>) if no such binding was found.</returns>
public BindingSyntax NextCompositeBinding(string compositeName = null)
{
return IterateCompositeBinding(true, compositeName);
}
/// <summary>
/// Iterate to the previous composite binding.
/// </summary>
/// <param name="compositeName">If <c>null</c> (default), an accessor to the previous composite binding,
/// regardless of name or type, is returned. If it is not <c>null</c>, can be either the name of
/// the binding (see <see cref="InputBinding.name"/>) or the name of the composite used in the
/// binding (see <see cref="InputSystem.RegisterBindingComposite"/>).</param>
/// <returns>A write accessor to the previous composite binding or an invalid accessor (see
/// <see cref="valid"/>) if no such binding was found.</returns>
public BindingSyntax PreviousCompositeBinding(string compositeName = null)
{
return IterateCompositeBinding(false, compositeName);
}
private BindingSyntax Iterate(bool next)
{
if (m_ActionMap == null)
return default;
var bindings = m_ActionMap.m_Bindings;
if (bindings == null)
return default;
// To find the next binding for a specific action, we may have to jump
// over unrelated bindings in-between.
var index = m_BindingIndexInMap;
while (true)
{
index += next ? 1 : -1;
if (index < 0 || index >= bindings.Length)
return default;
if (m_Action == null || bindings[index].TriggersAction(m_Action))
break;
}
return new BindingSyntax(m_ActionMap, index, m_Action);
}
private BindingSyntax IterateCompositeBinding(bool next, string compositeName)
{
for (var accessor = Iterate(next); accessor.valid; accessor = accessor.Iterate(next))
{
if (!accessor.binding.isComposite)
continue;
if (compositeName == null)
return accessor;
// Try name of binding.
if (compositeName.Equals(accessor.binding.name, StringComparison.InvariantCultureIgnoreCase))
return accessor;
// Try composite type name.
var name = NameAndParameters.ParseName(accessor.binding.path);
if (compositeName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
return accessor;
}
return default;
}
private BindingSyntax IteratePartBinding(bool next, string partName)
{
if (!valid)
return default;
if (binding.isComposite)
{
// If we're at the composite, only proceed if we're iterating down
// instead of up.
if (!next)
return default;
}
else if (!binding.isPartOfComposite)
return default;
for (var accessor = Iterate(next); accessor.valid; accessor = accessor.Iterate(next))
{
if (!accessor.binding.isPartOfComposite)
return default;
if (partName.Equals(accessor.binding.name, StringComparison.InvariantCultureIgnoreCase))
return accessor;
}
return default;
}
////TODO: allow setting overrides through this accessor
/// <summary>
/// Remove the binding.
/// </summary>
/// <remarks>
/// If the binding is a composite (see <see cref="InputBinding.isComposite"/>), part bindings of the
/// composite will be removed as well.
///
/// Note that the accessor will not necessarily be invalidated. Instead, it will point to what used
/// to be the next binding in the array (though that means the accessor will be invalid if the binding
/// that got erased was the last one in the array).
/// </remarks>
/// <exception cref="InvalidOperationException">The instance is not <see cref="valid"/>.</exception>
public void Erase()
{
if (!valid)
throw new InvalidOperationException("Instance not valid");
var isComposite = m_ActionMap.m_Bindings[m_BindingIndexInMap].isComposite;
ArrayHelpers.EraseAt(ref m_ActionMap.m_Bindings, m_BindingIndexInMap);
// If it's a composite, also erase part bindings.
if (isComposite)
{
while (m_BindingIndexInMap < m_ActionMap.m_Bindings.LengthSafe() && m_ActionMap.m_Bindings[m_BindingIndexInMap].isPartOfComposite)
{
ArrayHelpers.EraseAt(ref m_ActionMap.m_Bindings, m_BindingIndexInMap);
}
}
m_Action.m_BindingsCount = m_ActionMap.m_Bindings.LengthSafe();
m_ActionMap.OnBindingModified();
// We have switched to a different binding array. For singleton actions, we need to
// sync up the reference that the action itself has.
if (m_ActionMap.m_SingletonAction != null)
m_ActionMap.m_SingletonAction.m_SingletonActionBindings = m_ActionMap.m_Bindings;
}
/// <summary>
/// Insert a composite part into a composite binding.
/// </summary>
/// <param name="partName">Name of the part in composite binding.</param>
/// <param name="path">Control path to bind to.</param>
/// <returns>The same binding syntax for further configuration.</returns>
/// <exception cref="ArgumentNullException">Part name is null or empty.</exception>
/// <exception cref="InvalidOperationException">The binding accessor is invalid or the binding accessor is not pointing to composite or part binding.</exception>
public BindingSyntax InsertPartBinding(string partName, string path)
{
if (string.IsNullOrEmpty(partName))
throw new ArgumentNullException(nameof(partName));
if (!valid)
throw new InvalidOperationException("Binding accessor is not valid");
var binding = this.binding;
if (!binding.isPartOfComposite && !binding.isComposite)
throw new InvalidOperationException("Binding accessor must point to composite or part binding");
AddBindingInternal(m_ActionMap,
new InputBinding { path = path, isPartOfComposite = true, name = partName },
m_BindingIndexInMap + 1);
return new BindingSyntax(m_ActionMap, m_BindingIndexInMap + 1, m_Action);
}
}
////TODO: remove this and merge it into BindingSyntax
/// <summary>
/// Write accessor to a composite binding.
/// </summary>
/// <remarks>
/// To add a composite binding to an action, you must first call <see cref="InputActionSetupExtensions.AddCompositeBinding"/>
/// and then use the CompositeSyntax struct to add composite parts.
/// <example>
/// <code>
/// playerInput.actions["fire"]
/// .ChangeBinding(0)
/// .AddCompositeBinding("2DVector")
/// .With("Up", "<Keyboard>/w")
/// .With("Down", "<Keyboard>/s")
/// .With("Left", "<Keyboard>/a")
/// .With("Right", "<Keyboard>/d");
/// </code>
/// </example>
/// </remarks>
/// <seealso cref="AddBinding(InputAction,InputBinding)"/>
/// <seealso cref="ChangeBinding(InputAction,int)"/>
public struct CompositeSyntax
{
private readonly InputAction m_Action;
private readonly InputActionMap m_ActionMap;
private int m_BindingIndexInMap;
/// <summary>
/// Index of the binding that the accessor refers to.
/// </summary>
/// <remarks>
/// When accessing bindings on an <see cref="InputAction"/>, this is the index in
/// <see cref="InputAction.bindings"/> of the action. When accessing bindings on an
/// <see cref="InputActionMap"/>, it is the index <see cref="InputActionMap.bindings"/>
/// of the map.
/// </remarks>
public int bindingIndex
{
get
{
if (m_ActionMap == null)
return -1;
if (m_Action != null)
return m_Action.BindingIndexOnMapToBindingIndexOnAction(m_BindingIndexInMap);
return m_BindingIndexInMap;
}
}
internal CompositeSyntax(InputActionMap map, InputAction action, int compositeIndex)
{
m_Action = action;
m_ActionMap = map;
m_BindingIndexInMap = compositeIndex;
}
/// <summary>
/// Add a part binding to the composite.
/// </summary>
/// <param name="name">Name of the part. This is dependent on the type of composite. For
/// <see cref="Composites.Vector2Composite"/>, for example, the valid parts are <c>"Up"</c>, <c>"Down"</c>,
/// <c>"Left"</c>, and <c>"Right"</c>.</param>
/// <param name="binding">Control path to binding to. See <see cref="InputBinding.path"/>.</param>
/// <param name="groups">Binding groups to assign to the part binding. See <see cref="InputBinding.groups"/>.</param>
/// <param name="processors">Optional list of processors to apply to the binding. See <see cref="InputBinding.processors"/>.</param>
/// <returns>The same composite syntax for further configuration.</returns>
public CompositeSyntax With(string name, string binding, string groups = null, string processors = null)
{
////TODO: check whether non-composite bindings have been added in-between
using (InputActionRebindingExtensions.DeferBindingResolution())
{
int bindingIndex;
if (m_Action != null)
bindingIndex = m_Action.AddBinding(path: binding, groups: groups, processors: processors)
.m_BindingIndexInMap;
else
bindingIndex = m_ActionMap.AddBinding(path: binding, groups: groups, processors: processors)
.m_BindingIndexInMap;
m_ActionMap.m_Bindings[bindingIndex].name = name;
m_ActionMap.m_Bindings[bindingIndex].isPartOfComposite = true;
}
return this;
}
}
/// <summary>
/// Write accessor to a control scheme.
/// </summary>
public struct ControlSchemeSyntax
{
// REVIEW: Consider removing this for major revision, e.g. v2 or let functionality used by it be internal to reduce API surface.
private readonly InputActionAsset m_Asset;
private readonly int m_ControlSchemeIndex;
private InputControlScheme m_ControlScheme;
internal ControlSchemeSyntax(InputActionAsset asset, int index)
{
m_Asset = asset;
m_ControlSchemeIndex = index;
m_ControlScheme = new InputControlScheme();
}
internal ControlSchemeSyntax(InputControlScheme controlScheme)
{
m_Asset = null;
m_ControlSchemeIndex = -1;
m_ControlScheme = controlScheme;
}
/// <summary>
/// Sets or overwrite the binding group for control scheme.
/// </summary>
/// <param name="bindingGroup">A binding group. See <see cref="InputBinding.groups"/>.</param>
/// <returns>The same control scheme syntax for further configuration.</returns>
/// <exception cref="ArgumentNullException">If provided name is null or empty.</exception>
public ControlSchemeSyntax WithBindingGroup(string bindingGroup)
{
if (string.IsNullOrEmpty(bindingGroup))
throw new ArgumentNullException(nameof(bindingGroup));
if (m_Asset == null)
m_ControlScheme.m_BindingGroup = bindingGroup;
else
m_Asset.m_ControlSchemes[m_ControlSchemeIndex].bindingGroup = bindingGroup;
return this;
}
/// <summary>
/// Adds a required device to control scheme.
/// </summary>
/// <typeparam name="TDevice">Type of device.</typeparam>
/// <returns>The same control scheme syntax for further configuration.</returns>
public ControlSchemeSyntax WithRequiredDevice<TDevice>()
where TDevice : InputDevice
{
return WithRequiredDevice(DeviceTypeToControlPath<TDevice>());
}
/// <summary>
/// Adds an optional device to control scheme.
/// </summary>
/// <typeparam name="TDevice">Type of device.</typeparam>
/// <returns>The same control scheme syntax for further configuration.</returns>
/// <see cref="InputControlScheme.DeviceRequirement.isOptional"/>
public ControlSchemeSyntax WithOptionalDevice<TDevice>()
where TDevice : InputDevice
{
return WithOptionalDevice(DeviceTypeToControlPath<TDevice>());
}
/// <summary>
/// Combines another required device with the previous device using boolean OR
/// logic such that either the previous device or this device are required to be present.
/// </summary>
/// <typeparam name="TDevice">Type of device.</typeparam>
/// <returns>The same control scheme syntax for further configuration.</returns>
/// <remarks>
/// <example>
/// <code>
/// // Create an .inputactions asset.
/// var asset = ScriptableObject.CreateInstance<InputActionAsset>();
///
/// asset.AddControlScheme("KeyboardAndMouseOrPen")
/// .WithRequiredDevice("<Keyboard>")
/// .WithRequiredDevice("<Mouse>")
/// .OrWithRequiredDevice("<Pen>")
/// </code>
/// </example>
/// </remarks>
/// <see cref="InputControlScheme.DeviceRequirement.isOR"/>
public ControlSchemeSyntax OrWithRequiredDevice<TDevice>()
where TDevice : InputDevice
{
return OrWithRequiredDevice(DeviceTypeToControlPath<TDevice>());
}
/// <summary>
/// Combines another optional device with the previous device using boolean OR
/// logic such that either the previous device or this device are required to be present.
/// If this is the last device in a chain of OR'd devices, the entire chain of
/// devices becomes optional.
/// </summary>
/// <typeparam name="TDevice">Type of device.</typeparam>
/// <returns>The same control scheme syntax for further configuration.</returns>
/// <see cref="InputControlScheme.DeviceRequirement.isOR"/>
public ControlSchemeSyntax OrWithOptionalDevice<TDevice>()
where TDevice : InputDevice
{
return OrWithOptionalDevice(DeviceTypeToControlPath<TDevice>());
}
/// <summary>
/// Adds a required device to control scheme.
/// </summary>
/// <param name="controlPath">Device path, like <Gamepad>.</param>
/// <returns>The same control scheme syntax for further configuration.</returns>
public ControlSchemeSyntax WithRequiredDevice(string controlPath)
{
AddDeviceEntry(controlPath, InputControlScheme.DeviceRequirement.Flags.None);
return this;
}
/// <summary>
/// Add an optional device to the control scheme.
/// </summary>
/// <param name="controlPath">The device path, like <Gamepad>.</param>
/// <returns>The same control scheme syntax for further configuration.</returns>
public ControlSchemeSyntax WithOptionalDevice(string controlPath)
{
AddDeviceEntry(controlPath, InputControlScheme.DeviceRequirement.Flags.Optional);
return this;
}
/// <summary>
/// Adds another possible required device to the control scheme.
/// </summary>
/// <param name="controlPath">Device path, like <Gamepad>.</param>
/// <returns>The same control scheme syntax for further configuration.</returns>
public ControlSchemeSyntax OrWithRequiredDevice(string controlPath)
{
AddDeviceEntry(controlPath, InputControlScheme.DeviceRequirement.Flags.Or);
return this;
}
/// <summary>
/// Adds another possible optional device to control scheme.
/// </summary>
/// <param name="controlPath">Device path, like <Gamepad>.</param>
/// <returns>The same control scheme syntax for further configuration.</returns>
public ControlSchemeSyntax OrWithOptionalDevice(string controlPath)
{
AddDeviceEntry(controlPath,
InputControlScheme.DeviceRequirement.Flags.Optional |
InputControlScheme.DeviceRequirement.Flags.Or);
return this;
}
private string DeviceTypeToControlPath<TDevice>()
where TDevice : InputDevice
{
var layoutName = InputControlLayout.s_Layouts.TryFindLayoutForType(typeof(TDevice)).ToString();
if (string.IsNullOrEmpty(layoutName))
layoutName = typeof(TDevice).Name;
return $"<{layoutName}>";
}
/// <summary>
/// Call this method when done building out the control scheme to return the associated instance.
/// </summary>
/// <returns>The associated control scheme</returns>
public InputControlScheme Done()
{
if (m_Asset != null)
return m_Asset.m_ControlSchemes[m_ControlSchemeIndex];
return m_ControlScheme;
}
private void AddDeviceEntry(string controlPath, InputControlScheme.DeviceRequirement.Flags flags)
{
if (string.IsNullOrEmpty(controlPath))
throw new ArgumentNullException(nameof(controlPath));
var scheme = m_Asset != null ? m_Asset.m_ControlSchemes[m_ControlSchemeIndex] : m_ControlScheme;
ArrayHelpers.Append(ref scheme.m_DeviceRequirements,
new InputControlScheme.DeviceRequirement
{
m_ControlPath = controlPath,
m_Flags = flags,
});
if (m_Asset == null)
m_ControlScheme = scheme;
else
m_Asset.m_ControlSchemes[m_ControlSchemeIndex] = scheme;
}
}
}
}
|