File size: 4,596 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 | #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 UsageDropdownItem : InputControlDropdownItem
{
public override string controlPathWithDevice => $"{m_Device}/{{{m_ControlPath}}}";
public UsageDropdownItem(string usage)
: base(usage)
{
m_Device = "*";
m_ControlPath = usage;
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
|