PopupField.cs
5.75 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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.PackageManager.UI
{
#if UNITY_2018_3_OR_NEWER
internal class PopupField<T> : Experimental.UIElements.PopupField<T>
{
private Func<T, string> m_Callback;
public override T value
{
get { return base.value; }
set
{
base.value = value;
if (m_Callback != null)
m_TextElement.text = m_Callback(m_Value);
else
m_TextElement.text = m_Value.ToString();
}
}
/// <summary>
/// Callback that will return the string to be set in the field's label.
/// </summary>
/// <param name="callback"></param>
public void SetLabelCallback(Func<T, string> callback)
{
m_Callback = callback;
}
public PopupField(List<T> choices, T defaultValue) :
base(choices, defaultValue)
{
}
public PopupField(List<T> choices, int defaultIndex) :
base(choices, defaultIndex)
{
}
}
#else
internal class PopupField<T> : BaseTextElement, INotifyValueChanged<T>
{
private readonly List<T> m_PossibleValues;
private Func<T, string> m_Callback;
private EventCallback<ChangeEvent<T>> m_valueCallback;
private T m_Value;
public T value
{
get { return m_Value; }
set
{
if (EqualityComparer<T>.Default.Equals(m_Value, value))
return;
if (!m_PossibleValues.Contains(value))
throw new ArgumentException(string.Format("Value {0} is not present in the list of possible values", value));
m_Value = value;
m_Index = m_PossibleValues.IndexOf(m_Value);
if (m_Callback != null)
text = m_Callback(m_Value);
else
text = m_Value.ToString();
#if UNITY_2018_3_OR_NEWER
MarkDirtyRepaint();
#else
Dirty(ChangeType.Repaint);
#endif
}
}
private int m_Index = -1;
public int index
{
get { return m_Index; }
set
{
if (value != m_Index)
{
if (value >= m_PossibleValues.Count || value < 0)
throw new ArgumentException(string.Format("Index {0} is beyond the scope of possible value", value));
m_Index = value;
this.value = m_PossibleValues[m_Index];
}
}
}
/// <summary>
/// Callback that will return the string to be set in the field's label.
/// </summary>
/// <param name="callback"></param>
public void SetLabelCallback(Func<T, string> callback)
{
m_Callback = callback;
}
private PopupField(List<T> possibleValues)
{
if (possibleValues == null)
throw new ArgumentNullException("possibleValues can't be null");
m_PossibleValues = possibleValues;
AddToClassList("popupField");
}
public PopupField(List<T> possibleValues, T defaultValue) :
this(possibleValues)
{
if (defaultValue == null)
throw new ArgumentNullException("defaultValue can't be null");
if (!m_PossibleValues.Contains(defaultValue))
throw new ArgumentException(string.Format("Default value {0} is not present in the list of possible values", defaultValue));
// note: idx will be set when setting value
value = defaultValue;
}
public PopupField(List<T> possibleValues, int defaultIndex) :
this(possibleValues)
{
if (defaultIndex >= m_PossibleValues.Count || defaultIndex < 0)
throw new ArgumentException(string.Format("Default Index {0} is beyond the scope of possible value", value));
// note: value will be set when setting idx
index = defaultIndex;
}
public void SetValueAndNotify(T newValue)
{
if (!EqualityComparer<T>.Default.Equals(newValue, value))
{
using (ChangeEvent<T> evt = ChangeEvent<T>.GetPooled(value, newValue))
{
value = newValue;
if (m_valueCallback != null)
m_valueCallback(evt);
}
}
}
public void OnValueChanged(EventCallback<ChangeEvent<T>> callback)
{
m_valueCallback = callback;
RegisterCallback(callback);
}
protected override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
if (evt.GetEventTypeId() == MouseDownEvent.TypeId())
OnMouseDown();
}
private void OnMouseDown()
{
var menu = new GenericMenu();
foreach (T item in m_PossibleValues)
{
bool isSelected = EqualityComparer<T>.Default.Equals(item, value);
menu.AddItem(new GUIContent(item.ToString()), isSelected,
() => ChangeValueFromMenu(item));
}
var menuPosition = new Vector2(0.0f, layout.height);
menuPosition = this.LocalToWorld(menuPosition);
var menuRect = new Rect(menuPosition, Vector2.zero);
menu.DropDown(menuRect);
}
private void ChangeValueFromMenu(T menuItem)
{
SetValueAndNotify(menuItem);
}
}
#endif
}