This commit is contained in:
2025-01-17 13:10:42 +01:00
commit 4536213c91
15115 changed files with 1442174 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
#if UNITY_EDITOR
using UnityEngine.InputSystem.Layouts;
namespace UnityEngine.InputSystem.Editor
{
internal interface IInputControlPickerLayout
{
void AddControlItem(InputControlPickerDropdown dropdown, DeviceDropdownItem parent,
ControlDropdownItem parentControl,
InputControlLayout.ControlItem control, string device, string usage, bool searchable);
}
}
#endif // UNITY_EDITOR

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4f4390a0cc25d424d9adc1d6d6699a1f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,139 @@
#if UNITY_EDITOR
using System.Text;
using UnityEditor;
using UnityEngine.InputSystem.Layouts;
namespace UnityEngine.InputSystem.Editor
{
internal abstract class InputControlDropdownItem : AdvancedDropdownItem
{
protected string m_ControlPath;
protected string m_Device;
protected string m_Usage;
protected bool m_Searchable;
public string controlPath => m_ControlPath;
public virtual string controlPathWithDevice
{
get
{
var path = new StringBuilder($"<{m_Device}>");
if (!string.IsNullOrEmpty(m_Usage))
path.Append($"{{{m_Usage}}}");
if (!string.IsNullOrEmpty(m_ControlPath))
path.Append($"/{m_ControlPath}");
return path.ToString();
}
}
public override string searchableName
{
get
{
// ToHumanReadableString is expensive, especially given that we build the whole tree
// every time the control picker comes up. Build searchable names only on demand
// to save some time.
if (m_SearchableName == null)
{
if (m_Searchable)
m_SearchableName = InputControlPath.ToHumanReadableString(controlPathWithDevice);
else
m_SearchableName = string.Empty;
}
return m_SearchableName;
}
}
protected InputControlDropdownItem(string name)
: base(name) {}
}
// NOTE: Optional control items, unlike normal control items, are displayed with their internal control
// names rather that their display names. The reason is that we're looking at controls that have
// the same internal name in one or more derived layouts but each of those derived layouts may
// give the control a different display name.
//
// Also, if we generate a control path for an optional binding, InputControlPath.ToHumanReadableName()
// not find the referenced control on the referenced device layout and will thus not be able to
// find a display name for it either. So, in the binding UI, these paths will also show with their
// internal control names rather than display names.
internal sealed class OptionalControlDropdownItem : InputControlDropdownItem
{
public OptionalControlDropdownItem(EditorInputControlLayoutCache.OptionalControl optionalControl, string deviceControlId, string commonUsage)
: base(optionalControl.name)
{
m_ControlPath = optionalControl.name;
m_Device = deviceControlId;
m_Usage = commonUsage;
// Not searchable.
}
}
internal sealed class ControlUsageDropdownItem : InputControlDropdownItem
{
public override string controlPathWithDevice => BuildControlPath();
private string BuildControlPath()
{
if (m_Device == "*")
{
var path = new StringBuilder(m_Device);
if (!string.IsNullOrEmpty(m_Usage))
path.Append($"{{{m_Usage}}}");
if (!string.IsNullOrEmpty(m_ControlPath))
path.Append($"/{m_ControlPath}");
return path.ToString();
}
else
return base.controlPathWithDevice;
}
public ControlUsageDropdownItem(string device, string usage, string controlUsage)
: base(usage)
{
m_Device = string.IsNullOrEmpty(device) ? "*" : device;
m_Usage = usage;
m_ControlPath = $"{{{ controlUsage }}}";
name = controlUsage;
id = controlPathWithDevice.GetHashCode();
m_Searchable = true;
}
}
internal sealed class DeviceDropdownItem : InputControlDropdownItem
{
public DeviceDropdownItem(InputControlLayout layout, string usage = null, bool searchable = true)
: base(layout.m_DisplayName ?? ObjectNames.NicifyVariableName(layout.name))
{
m_Device = layout.name;
m_Usage = usage;
if (usage != null)
name += " (" + usage + ")";
id = name.GetHashCode();
m_Searchable = searchable;
}
}
internal sealed class ControlDropdownItem : InputControlDropdownItem
{
public ControlDropdownItem(ControlDropdownItem parent, string controlName, string displayName, string device, string usage, bool searchable)
: base("")
{
m_Device = device;
m_Usage = usage;
m_Searchable = searchable;
if (parent != null)
m_ControlPath = $"{parent.controlPath}/{controlName}";
else
m_ControlPath = controlName;
name = !string.IsNullOrEmpty(displayName) ? displayName : ObjectNames.NicifyVariableName(controlName);
id = controlPathWithDevice.GetHashCode();
indent = parent?.indent + 1 ?? 0;
}
}
}
#endif // UNITY_EDITOR

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 83070f7befc0b4deeb2cfde76b5a9002
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,218 @@
#if UNITY_EDITOR || PACKAGE_DOCS_GENERATION
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine.InputSystem.Layouts;
namespace UnityEngine.InputSystem.Editor
{
/// <summary>
/// Custom editor UI for editing control paths.
/// </summary>
/// <remarks>
/// This is the implementation underlying <see cref="InputControlPathDrawer"/>. It is useful primarily when
/// greater control is required than is offered by the <see cref="PropertyDrawer"/> mechanism. In particular,
/// it allows applying additional constraints such as requiring control paths to match ...
/// </remarks>
public sealed class InputControlPathEditor : IDisposable
{
/// <summary>
/// Initialize the control path editor.
/// </summary>
/// <param name="pathProperty"><see cref="string"/> type property that will receive the picked input control path.</param>
/// <param name="pickerState">Persistent editing state of the path editor. Used to retain state across domain reloads.</param>
/// <param name="onModified">Delegate that is called when the path has been modified.</param>
/// <param name="label">Optional label to display instead of display name of <paramref name="pathProperty"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="pathProperty"/> is <c>null</c>.</exception>
public InputControlPathEditor(SerializedProperty pathProperty, InputControlPickerState pickerState, Action onModified, GUIContent label = null)
{
if (pathProperty == null)
throw new ArgumentNullException(nameof(pathProperty));
this.pathProperty = pathProperty;
this.onModified = onModified;
m_PickerState = pickerState ?? new InputControlPickerState();
m_PathLabel = label ?? new GUIContent(pathProperty.displayName, pathProperty.GetTooltip());
}
public void Dispose()
{
m_PickerDropdown?.Dispose();
}
public void SetControlPathsToMatch(IEnumerable<string> controlPaths)
{
m_ControlPathsToMatch = controlPaths.ToArray();
m_PickerDropdown?.SetControlPathsToMatch(m_ControlPathsToMatch);
}
/// <summary>
/// Constrain the type of control layout that can be picked.
/// </summary>
/// <param name="expectedControlLayout">Name of the layout. This it the name as registered with
/// <see cref="InputSystem.RegisterLayout"/>.</param>.
/// <remarks>
/// <example>
/// <code>
/// // Pick only button controls.
/// editor.SetExpectedControlLayout("Button");
/// </code>
/// </example>
/// </remarks>
public void SetExpectedControlLayout(string expectedControlLayout)
{
m_ExpectedControlLayout = expectedControlLayout;
m_PickerDropdown?.SetExpectedControlLayout(m_ExpectedControlLayout);
}
public void SetExpectedControlLayoutFromAttribute()
{
var field = pathProperty.GetField();
if (field == null)
return;
var attribute = field.GetCustomAttribute<InputControlAttribute>();
if (attribute != null)
SetExpectedControlLayout(attribute.layout);
}
public void OnGUI()
{
EditorGUILayout.BeginHorizontal();
////FIXME: for some reason, the left edge doesn't align properly in GetRect()'s result; indentation issue?
var rect = GUILayoutUtility.GetRect(0, EditorGUIUtility.singleLineHeight);
rect.x += EditorGUIUtility.standardVerticalSpacing + 2;
rect.width -= EditorGUIUtility.standardVerticalSpacing * 2 + 4;
OnGUI(rect);
EditorGUILayout.EndHorizontal();
}
public void OnGUI(Rect rect, GUIContent label = null, SerializedProperty property = null, Action modifiedCallback = null)
{
var pathLabel = label ?? m_PathLabel;
var serializedProperty = property ?? pathProperty;
var lineRect = rect;
var labelRect = lineRect;
labelRect.width = EditorGUIUtility.labelWidth;
EditorGUI.LabelField(labelRect, pathLabel);
lineRect.x += labelRect.width;
lineRect.width -= labelRect.width;
var bindingTextRect = lineRect;
var editButtonRect = lineRect;
var bindingTextRectOffset = 80;
bindingTextRect.width += bindingTextRectOffset;
bindingTextRect.x -= bindingTextRectOffset + 20;
editButtonRect.x = bindingTextRect.x + bindingTextRect.width; // Place it directly after the textRect
editButtonRect.width = 20;
editButtonRect.height = 15;
var path = String.Empty;
try
{
path = serializedProperty.stringValue;
}
catch
{
// This try-catch block is a temporary fix for ISX-1436
// The plan is to convert InputControlPathEditor entirely to UITK and therefore this fix will
// no longer be required.
return;
}
////TODO: this should be cached; generates needless GC churn
var displayName = InputControlPath.ToHumanReadableString(path);
// Either show dropdown control that opens path picker or show path directly as
// text, if manual path editing is toggled on.
if (m_PickerState.manualPathEditMode)
{
////FIXME: for some reason the text field does not fill all the rect but rather adds large padding on the left
bindingTextRect.x -= 15;
bindingTextRect.width += 15;
EditorGUI.BeginChangeCheck();
path = EditorGUI.DelayedTextField(bindingTextRect, path);
if (EditorGUI.EndChangeCheck())
{
serializedProperty.stringValue = path;
serializedProperty.serializedObject.ApplyModifiedProperties();
(modifiedCallback ?? onModified).Invoke();
}
}
else
{
// Dropdown that shows binding text and allows opening control picker.
if (EditorGUI.DropdownButton(bindingTextRect, new GUIContent(displayName), FocusType.Keyboard))
{
SetExpectedControlLayoutFromAttribute(serializedProperty);
////TODO: for bindings that are part of composites, use the layout information from the [InputControl] attribute on the field
ShowDropdown(bindingTextRect, serializedProperty, modifiedCallback ?? onModified);
}
}
// Button to toggle between text edit mode.
m_PickerState.manualPathEditMode = GUI.Toggle(editButtonRect, m_PickerState.manualPathEditMode, "T",
EditorStyles.miniButton);
}
private void ShowDropdown(Rect rect, SerializedProperty serializedProperty, Action modifiedCallback)
{
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
InputActionsEditorSettingsProvider.SetIMGUIDropdownVisible(true, false);
#endif
if (m_PickerDropdown == null)
{
m_PickerDropdown = new InputControlPickerDropdown(
m_PickerState,
path =>
{
serializedProperty.stringValue = path;
m_PickerState.manualPathEditMode = false;
modifiedCallback();
});
}
m_PickerDropdown.SetPickedCallback(path =>
{
serializedProperty.stringValue = path;
m_PickerState.manualPathEditMode = false;
modifiedCallback();
});
m_PickerDropdown.SetControlPathsToMatch(m_ControlPathsToMatch);
m_PickerDropdown.SetExpectedControlLayout(m_ExpectedControlLayout);
m_PickerDropdown.Show(rect);
}
private void SetExpectedControlLayoutFromAttribute(SerializedProperty property)
{
var field = property.GetField();
if (field == null)
return;
var attribute = field.GetCustomAttribute<InputControlAttribute>();
if (attribute != null)
SetExpectedControlLayout(attribute.layout);
}
public SerializedProperty pathProperty { get; }
public Action onModified { get; }
private GUIContent m_PathLabel;
private string m_ExpectedControlLayout;
private string[] m_ControlPathsToMatch;
private InputControlScheme[] m_ControlSchemes;
private bool m_NeedToClearProgressBar;
private InputControlPickerDropdown m_PickerDropdown;
private readonly InputControlPickerState m_PickerState;
private InputActionRebindingExtensions.RebindingOperation m_RebindingOperation;
}
}
#endif // UNITY_EDITOR

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 924d7617bcdc84f8ea1c0d0ce6466a30
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
#if UNITY_EDITOR || PACKAGE_DOCS_GENERATION
using System;
////REVIEW: should this be a PopupWindowContent?
namespace UnityEngine.InputSystem.Editor
{
/// <summary>
/// A popup that allows picking input controls graphically.
/// </summary>
public sealed class InputControlPicker : IDisposable
{
public InputControlPicker(Mode mode, Action<string> onPick, InputControlPickerState state)
{
m_State = state ?? new InputControlPickerState();
m_Dropdown = new InputControlPickerDropdown(state, onPick, mode: mode);
}
public void Show(Rect rect)
{
m_Dropdown.Show(rect);
}
public void Dispose()
{
m_Dropdown?.Dispose();
}
public InputControlPickerState state => m_State;
private readonly InputControlPickerDropdown m_Dropdown;
private readonly InputControlPickerState m_State;
public enum Mode
{
PickControl,
PickDevice,
}
}
}
#endif // UNITY_EDITOR

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e35c175d4f60c4820a36054488cb5524
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: db7a158b580fb4518ae82aa029a54892
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,27 @@
#if UNITY_EDITOR || PACKAGE_DOCS_GENERATION
using System;
namespace UnityEngine.InputSystem.Editor
{
/// <summary>
/// Persistent state for <see cref="InputControlPathEditor"/>.
/// </summary>
/// <remarks>
/// This class encapsulates the viewing state for an input control picker.
/// </remarks>
[Serializable]
public class InputControlPickerState
{
internal AdvancedDropdownState advancedDropdownState => m_AdvancedDropdownState;
internal bool manualPathEditMode
{
get => m_ManualPathEditMode;
set => m_ManualPathEditMode = value;
}
[SerializeField] private AdvancedDropdownState m_AdvancedDropdownState = new AdvancedDropdownState();
[SerializeField] private bool m_ManualPathEditMode;
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dfa8add0e3ed349708aeadc8b8c1ad0f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5b630b769a2a8aa499ce2d2093f40500
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
#if UNITY_EDITOR
using UnityEngine.InputSystem.Layouts;
namespace UnityEngine.InputSystem.Editor
{
internal class DefaultInputControlPickerLayout : IInputControlPickerLayout
{
public void AddControlItem(InputControlPickerDropdown dropdown, DeviceDropdownItem parent,
ControlDropdownItem parentControl,
InputControlLayout.ControlItem control, string device, string usage, bool searchable)
{
dropdown.AddControlItem(this, parent, parentControl, control, device, usage, searchable);
}
}
}
#endif // UNITY_EDITOR

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7717bf968da90024b9c047ba1a3394ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,36 @@
#if UNITY_EDITOR
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
namespace UnityEngine.InputSystem.Editor
{
internal class TouchscreenControlPickerLayout : IInputControlPickerLayout
{
public void AddControlItem(InputControlPickerDropdown dropdown, DeviceDropdownItem parent, ControlDropdownItem parentControl,
InputControlLayout.ControlItem control, string device, string usage, bool searchable)
{
// for the Press control, show two variants, one for single touch presses, and another for multi-touch presses
if (control.displayName == "Press")
{
dropdown.AddControlItem(this, parent, parentControl, new InputControlLayout.ControlItem
{
name = new InternedString("Press"),
displayName = new InternedString("Press (Single touch)"),
layout = control.layout
}, device, usage, searchable);
dropdown.AddControlItem(this, parent, parentControl, new InputControlLayout.ControlItem
{
name = new InternedString("Press"),
displayName = new InternedString("Press (Multi-touch)"),
layout = control.layout
}, device, usage, searchable, "touch*/Press");
}
else
{
dropdown.AddControlItem(this, parent, parentControl, control, device, usage, searchable);
}
}
}
}
#endif // UNITY_EDITOR

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 224236193350c244abab484f4514df16
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: