|
11 | 11 | using UnityEngine.InputSystem.Utilities; |
12 | 12 | using UnityEngine.TestTools; |
13 | 13 | using UnityEngine.TestTools.Utils; |
| 14 | +using UnityEngine.InputSystem.XR; |
| 15 | + |
14 | 16 | #if UNITY_EDITOR |
15 | 17 | using UnityEditor; |
16 | 18 | using UnityEngine.InputSystem.Editor; |
@@ -728,32 +730,104 @@ public void Trigger(InputAction action) |
728 | 730 | throw new ArgumentException( |
729 | 731 | $"Action '{action}' must be bound to controls in order to be able to trigger it", nameof(action)); |
730 | 732 |
|
731 | | - // See if we have a button we can trigger. |
| 733 | + // We iterate controls to see if there are any that we know how to trigger |
732 | 734 | for (var i = 0; i < controls.Count; ++i) |
733 | 735 | { |
734 | | - if (!(controls[i] is ButtonControl button)) |
735 | | - continue; |
| 736 | + var control = controls[i]; |
736 | 737 |
|
737 | | - // Press and release button. |
738 | | - Set(button, 1); |
739 | | - Set(button, 0); |
| 738 | + // for buttons, we literally trigger them pressed and released |
| 739 | + if (control is ButtonControl buttonControl) |
| 740 | + { |
| 741 | + Set(buttonControl, 1); |
| 742 | + Set(buttonControl, 0); |
740 | 743 |
|
741 | | - return; |
742 | | - } |
| 744 | + return; |
| 745 | + } |
743 | 746 |
|
744 | | - // See if we have an axis we can slide a bit. |
745 | | - for (var i = 0; i < controls.Count; ++i) |
746 | | - { |
747 | | - if (!(controls[i] is AxisControl axis)) |
748 | | - continue; |
| 747 | + // for the other types of controls we simply perform a small change in value as applicable |
| 748 | + const float minorChange = 0.01f; |
749 | 749 |
|
750 | | - // We do, so nudge its value a bit. |
751 | | - Set(axis, axis.ReadValue() + 0.01f); |
| 750 | + if (control is AxisControl axisControl) |
| 751 | + { |
| 752 | + Set(axisControl, axisControl.ReadValue() + minorChange); |
752 | 753 |
|
753 | | - return; |
| 754 | + return; |
| 755 | + } |
| 756 | + |
| 757 | + if (control is Vector2Control vec2Control) |
| 758 | + { |
| 759 | + Set(vec2Control, vec2Control.ReadValue() + Vector2.one * minorChange); |
| 760 | + |
| 761 | + return; |
| 762 | + } |
| 763 | + |
| 764 | + if (control is Vector3Control vec3Control) |
| 765 | + { |
| 766 | + Set(vec3Control, vec3Control.ReadValue() + Vector3.one * minorChange); |
| 767 | + |
| 768 | + return; |
| 769 | + } |
| 770 | + |
| 771 | + if (control is QuaternionControl quatControl) |
| 772 | + { |
| 773 | + var q = quatControl.ReadValue(); |
| 774 | + q.ToAngleAxis(out float angle, out Vector3 axis); |
| 775 | + Set(quatControl, Quaternion.AngleAxis(angle + minorChange, axis)); |
| 776 | + |
| 777 | + return; |
| 778 | + } |
| 779 | + |
| 780 | + if (control is IntegerControl integerControl) |
| 781 | + { |
| 782 | + Set(integerControl, integerControl.ReadValue() + 1); |
| 783 | + |
| 784 | + return; |
| 785 | + } |
| 786 | + |
| 787 | + if (control is TouchControl touchControl) |
| 788 | + { |
| 789 | + var state = touchControl.ReadValue(); |
| 790 | + state.position += Vector2.one * minorChange; |
| 791 | + Set(touchControl, state); |
| 792 | + |
| 793 | + return; |
| 794 | + } |
| 795 | + |
| 796 | + // this one is a bit weird, but there's not much to change other than picking the next phase in the list |
| 797 | + if (control is TouchPhaseControl touchPhaseControl) |
| 798 | + { |
| 799 | + var phase = touchPhaseControl.ReadValue(); |
| 800 | + var values = Enum.GetValues(typeof(TouchPhase)); |
| 801 | + var index = Array.IndexOf(values, phase); |
| 802 | + var newIndex = (index + 1) % values.Length; |
| 803 | + Set(touchPhaseControl, (TouchPhase)values.GetValue(newIndex)); |
| 804 | + |
| 805 | + return; |
| 806 | + } |
| 807 | + |
| 808 | + if (control is BoneControl boneControl) |
| 809 | + { |
| 810 | + var bone = boneControl.ReadValue(); |
| 811 | + bone.position += minorChange * Vector3.one; |
| 812 | + Set(boneControl, bone); |
| 813 | + |
| 814 | + return; |
| 815 | + } |
| 816 | + |
| 817 | + if (control is EyesControl eyesControl) |
| 818 | + { |
| 819 | + var eyes = eyesControl.ReadValue(); |
| 820 | + eyes.leftEyePosition += minorChange * Vector3.one; |
| 821 | + Set(eyesControl, eyes); |
| 822 | + |
| 823 | + return; |
| 824 | + } |
754 | 825 | } |
755 | 826 |
|
756 | | - ////TODO: support a wider range of controls |
| 827 | + // Initially I wanted to implement PoseControl too, but it's got a very complicated ifdef that enables it. |
| 828 | + // Didn't want to carry that #ifdef here. Let's see how many people really need to trigger PoseControl. |
| 829 | + |
| 830 | + // If it's not a control that we know how to trigger - it's not implemented yet |
757 | 831 | throw new NotImplementedException(); |
758 | 832 | } |
759 | 833 |
|
|
0 commit comments