forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseField.cs
More file actions
206 lines (180 loc) · 6.42 KB
/
BaseField.cs
File metadata and controls
206 lines (180 loc) · 6.42 KB
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
// 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;
namespace UnityEngine.UIElements
{
public abstract class BaseField<TValueType> : BindableElement, INotifyValueChanged<TValueType>
{
public new class UxmlTraits : BindableElement.UxmlTraits
{
UxmlStringAttributeDescription m_Label = new UxmlStringAttributeDescription { name = "label" };
public UxmlTraits()
{
focusIndex.defaultValue = 0;
focusable.defaultValue = true;
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((BaseField<TValueType>)ve).label = m_Label.GetValueFromBag(bag, cc);
}
}
public static readonly string ussClassName = "unity-base-field";
public static readonly string labelUssClassName = ussClassName + "__label";
public static readonly string inputUssClassName = ussClassName + "__input";
public static readonly string noLabelVariantUssClassName = ussClassName + "--no-label";
public static readonly string labelDraggerVariantUssClassName = labelUssClassName + "--with-dragger";
private VisualElement m_VisualInput;
internal VisualElement visualInput
{
get { return m_VisualInput; }
set
{
// Get rid of the older value...
if (m_VisualInput != null)
{
if (m_VisualInput.parent == this)
{
m_VisualInput.RemoveFromHierarchy();
}
m_VisualInput = null;
}
// Handle the new value...
if (value != null)
{
m_VisualInput = value;
}
else
{
m_VisualInput = new VisualElement() { pickingMode = PickingMode.Ignore };
}
m_VisualInput.focusable = true;
m_VisualInput.AddToClassList(inputUssClassName);
Add(m_VisualInput);
}
}
[SerializeField]
TValueType m_Value;
protected TValueType rawValue
{
get { return m_Value; }
set { m_Value = value; }
}
public virtual TValueType value
{
get
{
return m_Value;
}
set
{
if (!EqualityComparer<TValueType>.Default.Equals(m_Value, value))
{
if (panel != null)
{
using (ChangeEvent<TValueType> evt = ChangeEvent<TValueType>.GetPooled(m_Value, value))
{
evt.target = this;
SetValueWithoutNotify(value);
SendEvent(evt);
}
}
else
{
SetValueWithoutNotify(value);
}
}
}
}
public Label labelElement { get; private set; }
public string label
{
get
{
return labelElement.text;
}
set
{
if (labelElement.text != value)
{
labelElement.text = value;
if (string.IsNullOrEmpty(labelElement.text))
{
AddToClassList(noLabelVariantUssClassName);
labelElement.RemoveFromHierarchy();
}
else
{
if (!Contains(labelElement))
{
Insert(0, labelElement);
RemoveFromClassList(noLabelVariantUssClassName);
}
}
}
}
}
internal BaseField(string label)
{
isCompositeRoot = true;
focusable = true;
tabIndex = 0;
excludeFromFocusRing = true;
delegatesFocus = true;
AddToClassList(ussClassName);
labelElement = new Label() { focusable = true, tabIndex = -1 };
labelElement.AddToClassList(labelUssClassName);
if (label != null)
{
this.label = label;
}
else
{
AddToClassList(noLabelVariantUssClassName);
}
m_VisualInput = null;
}
protected BaseField(string label, VisualElement visualInput)
: this(label)
{
this.visualInput = visualInput;
}
public virtual void SetValueWithoutNotify(TValueType newValue)
{
m_Value = newValue;
if (!string.IsNullOrEmpty(viewDataKey))
SaveViewData();
MarkDirtyRepaint();
}
internal override void OnViewDataReady()
{
base.OnViewDataReady();
if (m_VisualInput != null)
{
var key = GetFullHierarchicalViewDataKey();
var oldValue = m_Value;
OverwriteFromViewData(this, key);
if (!EqualityComparer<TValueType>.Default.Equals(oldValue, m_Value))
{
using (ChangeEvent<TValueType> evt = ChangeEvent<TValueType>.GetPooled(oldValue, m_Value))
{
evt.target = this;
SendEvent(evt);
}
}
}
}
}
public class BaseFieldTraits<TValueType, TValueUxmlAttributeType> : BaseField<TValueType>.UxmlTraits
where TValueUxmlAttributeType : TypedUxmlAttributeDescription<TValueType>, new()
{
TValueUxmlAttributeType m_Value = new TValueUxmlAttributeType { name = "value" };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((INotifyValueChanged<TValueType>)ve).SetValueWithoutNotify(m_Value.GetValueFromBag(bag, cc));
}
}
}