-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
LayerMaskField.cs
186 lines (160 loc) · 6.52 KB
/
LayerMaskField.cs
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.UIElements
{
/// <summary>
/// A LayerMaskField editor. For more information, refer to [[wiki:UIE-uxml-element-LayerMaskField|UXML element LayerMaskField]].
/// </summary>
public class LayerMaskField : MaskField
{
[UnityEngine.Internal.ExcludeFromDocs, Serializable]
public new class UxmlSerializedData : MaskField.UxmlSerializedData
{
#pragma warning disable 649
[UxmlAttribute("value")]
[SerializeField] LayerMask layerMask;
[SerializeField, UxmlIgnore, HideInInspector] UxmlAttributeFlags layerMask_UxmlAttributeFlags;
[UxmlAttribute("choices")]
[SerializeField, HideInInspector] List<string> layerChoices;
[SerializeField, UxmlIgnore, HideInInspector] UxmlAttributeFlags layerChoices_UxmlAttributeFlags;
#pragma warning restore 649
public override object CreateInstance() => new LayerMaskField();
public override void Deserialize(object obj)
{
base.Deserialize(obj);
var e = (LayerMaskField)obj;
if (ShouldWriteAttributeValue(layerMask_UxmlAttributeFlags))
e.SetValueWithoutNotify(layerMask.value);
if (ShouldWriteAttributeValue(layerChoices_UxmlAttributeFlags))
e.layerChoices = layerChoices;
}
}
internal List<string> layerChoices
{
get => choices;
set
{
if (value != null )
choices = value;
}
}
internal LayerMask layerMask { get => value; set => this.value = value.value; }
/// <summary>
/// Instantiates a <see cref="LayerMaskField"/> using the data read from a UXML file.
/// </summary>
[Obsolete("UxmlFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
public new class UxmlFactory : UxmlFactory<LayerMaskField, UxmlTraits> {}
/// <summary>
/// Defines <see cref="UxmlTraits"/> for the <see cref="LayerMaskField"/>.
/// </summary>
[Obsolete("UxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
public new class UxmlTraits : BasePopupField<int, UxmlIntAttributeDescription>.UxmlTraits
{
readonly UxmlIntAttributeDescription m_MaskValue = new UxmlIntAttributeDescription { name = "value" };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
var layerMaskField = (LayerMaskField)ve;
// The mask is simply an int
layerMaskField.SetValueWithoutNotify(m_MaskValue.GetValueFromBag(bag, cc));
base.Init(ve, bag, cc);
}
}
/// <summary>
/// Unsupported.
/// </summary>
public override Func<string, string> formatSelectedValueCallback
{
get { return null; }
set
{
if (value != null)
{
Debug.LogWarning(L10n.Tr("LayerMaskField doesn't support the formatting of the selected value."));
}
m_FormatSelectedValueCallback = null;
}
}
/// <summary>
/// Unsupported.
/// </summary>
public override Func<string, string> formatListItemCallback
{
get { return null; }
set
{
if (value != null)
{
Debug.LogWarning(L10n.Tr("LayerMaskField doesn't support the formatting of the list items."));
}
m_FormatListItemCallback = null;
}
}
void UpdateLayersInfo()
{
// Get the layers : names and values
string[] layerNames = null;
int[] layerValues = null;
TagManager.GetDefinedLayers(ref layerNames, ref layerValues);
// Create the appropriate lists...
choices = new List<string>(layerNames);
choicesMasks = new List<int>(layerValues);
}
/// <summary>
/// USS class name of elements of this type.
/// </summary>
public new static readonly string ussClassName = "unity-layer-mask-field";
/// <summary>
/// USS class name of labels in elements of this type.
/// </summary>
public new static readonly string labelUssClassName = ussClassName + "__label";
/// <summary>
/// USS class name of input elements in elements of this type.
/// </summary>
public new static readonly string inputUssClassName = ussClassName + "__input";
/// <summary>
/// Constructor of the field.
/// </summary>
/// <param name="defaultMask">The mask to use for a first selection.</param>
public LayerMaskField(int defaultMask)
: this(null, defaultMask) {}
/// <summary>
/// Constructor of the field.
/// </summary>
/// <param name="label">The label to prefix the <see cref="LayerMaskField"/>.</param>
/// <param name="defaultMask">The mask to use for a first selection.</param>
public LayerMaskField(string label, int defaultMask)
: this(label)
{
SetValueWithoutNotify(defaultMask);
}
/// <summary>
/// Constructor of the field.
/// </summary>
public LayerMaskField()
: this(null) {}
/// <summary>
/// Constructor of the field.
/// </summary>
/// <param name="label">The label to prefix the <see cref="LayerMaskField"/>.</param>
public LayerMaskField(string label)
: base(label)
{
AddToClassList(ussClassName);
labelElement.AddToClassList(labelUssClassName);
visualInput.AddToClassList(inputUssClassName);
UpdateLayersInfo();
}
internal override void AddMenuItems(IGenericMenu menu)
{
// We must update the choices and the values since we don't know if they changed...
UpdateLayersInfo();
// Create the menu the usual way...
base.AddMenuItems(menu);
}
}
}