forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseSlider.cs
More file actions
338 lines (286 loc) · 13.7 KB
/
Copy pathBaseSlider.cs
File metadata and controls
338 lines (286 loc) · 13.7 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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 enum SliderDirection
{
Horizontal,
Vertical
}
public abstract class BaseSlider<TValueType> : BaseField<TValueType>
where TValueType : System.IComparable<TValueType>
{
internal VisualElement dragElement { get; private set; }
private TValueType m_LowValue;
public TValueType lowValue
{
get { return m_LowValue; }
set
{
if (!EqualityComparer<TValueType>.Default.Equals(m_LowValue, value))
{
m_LowValue = value;
ClampValue();
UpdateDragElementPosition();
}
}
}
private TValueType m_HighValue;
public TValueType highValue
{
get { return m_HighValue; }
set
{
if (!EqualityComparer<TValueType>.Default.Equals(m_HighValue, value))
{
m_HighValue = value;
ClampValue();
UpdateDragElementPosition();
}
}
}
public TValueType range { get { return SliderRange(); } }
private float m_PageSize;
public virtual float pageSize
{
get { return m_PageSize; }
set { m_PageSize = value; }
}
internal ClampedDragger<TValueType> clampedDragger { get; private set; }
Rect m_DragElementStartPos;
TValueType Clamp(TValueType value, TValueType lowBound, TValueType highBound)
{
TValueType result = value;
if (lowBound.CompareTo(value) > 0)
{
result = lowBound;
}
else if (highBound.CompareTo(value) < 0)
{
result = highBound;
}
return result;
}
public override TValueType value
{
get { return base.value; }
set
{
// Clamp the value around the real lowest and highest range values.
TValueType lowest = lowValue, highest = highValue;
if (lowest.CompareTo(highest) > 0)
{
var t = lowest;
lowest = highest;
highest = t;
}
var newValue = Clamp(value, lowest, highest);
base.value = newValue;
UpdateDragElementPosition();
}
}
private SliderDirection m_Direction;
public SliderDirection direction
{
get { return m_Direction; }
set
{
m_Direction = value;
if (m_Direction == SliderDirection.Horizontal)
{
RemoveFromClassList(verticalVariantUssClassName);
AddToClassList(horizontalVariantUssClassName);
}
else
{
RemoveFromClassList(horizontalVariantUssClassName);
AddToClassList(verticalVariantUssClassName);
}
}
}
internal const float kDefaultPageSize = 0.0f;
public new static readonly string ussClassName = "unity-base-slider";
public new static readonly string labelUssClassName = ussClassName + "__label";
public new static readonly string inputUssClassName = ussClassName + "__input";
public static readonly string horizontalVariantUssClassName = ussClassName + "--horizontal";
public static readonly string verticalVariantUssClassName = ussClassName + "--vertical";
public static readonly string trackerUssClassName = ussClassName + "__tracker";
public static readonly string draggerUssClassName = ussClassName + "__dragger";
internal BaseSlider(string label, TValueType start, TValueType end, SliderDirection direction = SliderDirection.Horizontal, float pageSize = kDefaultPageSize)
: base(label, null)
{
AddToClassList(ussClassName);
labelElement.AddToClassList(labelUssClassName);
visualInput.AddToClassList(inputUssClassName);
this.direction = direction;
this.pageSize = pageSize;
lowValue = start;
highValue = end;
pickingMode = PickingMode.Ignore;
visualInput.pickingMode = PickingMode.Position;
var trackElement = new VisualElement() { name = "unity-tracker" };
trackElement.AddToClassList(trackerUssClassName);
visualInput.Add(trackElement);
dragElement = new VisualElement() { name = "unity-dragger" };
dragElement.RegisterCallback<GeometryChangedEvent>(UpdateDragElementPosition);
dragElement.AddToClassList(draggerUssClassName);
visualInput.Add(dragElement);
clampedDragger = new ClampedDragger<TValueType>(this, SetSliderValueFromClick, SetSliderValueFromDrag);
visualInput.AddManipulator(clampedDragger);
}
private void ClampValue()
{
// The property setter takes care of this
value = rawValue;
}
internal abstract TValueType SliderLerpUnclamped(TValueType a, TValueType b, float interpolant);
internal abstract float SliderNormalizeValue(TValueType currentValue, TValueType lowerValue, TValueType higherValue);
internal abstract TValueType SliderRange();
// Handles slider drags
void SetSliderValueFromDrag()
{
if (clampedDragger.dragDirection != ClampedDragger<TValueType>.DragDirection.Free)
return;
var delta = clampedDragger.delta;
if (direction == SliderDirection.Horizontal)
ComputeValueAndDirectionFromDrag(visualInput.resolvedStyle.width, dragElement.resolvedStyle.width, m_DragElementStartPos.x + delta.x);
else
ComputeValueAndDirectionFromDrag(visualInput.resolvedStyle.height, dragElement.resolvedStyle.height, m_DragElementStartPos.y + delta.y);
}
void ComputeValueAndDirectionFromDrag(float sliderLength, float dragElementLength, float dragElementPos)
{
var totalRange = sliderLength - dragElementLength;
if (Mathf.Abs(totalRange) < Mathf.Epsilon)
return;
float normalizedDragElementPosition = Mathf.Max(0f, Mathf.Min(dragElementPos, totalRange)) / totalRange;
value = SliderLerpUnclamped(lowValue, highValue, normalizedDragElementPosition);
}
// Handles slider clicks and page scrolls
void SetSliderValueFromClick()
{
if (clampedDragger.dragDirection == ClampedDragger<TValueType>.DragDirection.Free)
return;
if (clampedDragger.dragDirection == ClampedDragger<TValueType>.DragDirection.None)
{
if (Mathf.Approximately(pageSize, 0.0f))
{
// Jump drag element to current mouse position when user clicks on slider and pageSize == 0
var x = (direction == SliderDirection.Horizontal)
? clampedDragger.startMousePosition.x - (dragElement.resolvedStyle.width / 2f) : dragElement.style.left.value.value;
var y = (direction == SliderDirection.Horizontal) ?
dragElement.style.top.value.value : clampedDragger.startMousePosition.y - (dragElement.resolvedStyle.height / 2f);
dragElement.style.left = x;
dragElement.style.top = y;
m_DragElementStartPos = new Rect(x, y, dragElement.resolvedStyle.width, dragElement.resolvedStyle.height);
// Manipulation becomes a free form drag
clampedDragger.dragDirection = ClampedDragger<TValueType>.DragDirection.Free;
if (direction == SliderDirection.Horizontal)
ComputeValueAndDirectionFromDrag(visualInput.resolvedStyle.width, dragElement.resolvedStyle.width, m_DragElementStartPos.x);
else
ComputeValueAndDirectionFromDrag(visualInput.resolvedStyle.height, dragElement.resolvedStyle.height, m_DragElementStartPos.y);
return;
}
m_DragElementStartPos = new Rect(dragElement.resolvedStyle.left, dragElement.resolvedStyle.top, dragElement.resolvedStyle.width, dragElement.resolvedStyle.height);
}
if (direction == SliderDirection.Horizontal)
ComputeValueAndDirectionFromClick(visualInput.resolvedStyle.width, dragElement.resolvedStyle.width, dragElement.resolvedStyle.left, clampedDragger.lastMousePosition.x);
else
ComputeValueAndDirectionFromClick(visualInput.resolvedStyle.height, dragElement.resolvedStyle.height, dragElement.resolvedStyle.top, clampedDragger.lastMousePosition.y);
}
internal virtual void ComputeValueAndDirectionFromClick(float sliderLength, float dragElementLength, float dragElementPos, float dragElementLastPos)
{
var totalRange = sliderLength - dragElementLength;
if (Mathf.Abs(totalRange) < Mathf.Epsilon)
return;
if ((dragElementLastPos < dragElementPos) &&
(clampedDragger.dragDirection != ClampedDragger<TValueType>.DragDirection.LowToHigh))
{
clampedDragger.dragDirection = ClampedDragger<TValueType>.DragDirection.HighToLow;
float normalizedDragElementPosition = Mathf.Max(0f, Mathf.Min(dragElementPos - pageSize, totalRange)) / totalRange;
value = SliderLerpUnclamped(lowValue, highValue, normalizedDragElementPosition);
}
else if ((dragElementLastPos > (dragElementPos + dragElementLength)) &&
(clampedDragger.dragDirection != ClampedDragger<TValueType>.DragDirection.HighToLow))
{
clampedDragger.dragDirection = ClampedDragger<TValueType>.DragDirection.LowToHigh;
float normalizedDragElementPosition = Mathf.Max(0f, Mathf.Min(dragElementPos + pageSize, totalRange)) / totalRange;
value = SliderLerpUnclamped(lowValue, highValue, normalizedDragElementPosition);
}
}
public void AdjustDragElement(float factor)
{
// Any factor greater or equal to 1f eliminates the need for a drag element
bool needsElement = factor < 1f;
dragElement.visible = needsElement;
if (needsElement)
{
IStyle inlineStyles = dragElement.style;
dragElement.visible = true;
// Any factor smaller than 1f will necessitate a drag element
if (direction == SliderDirection.Horizontal)
{
// Make sure the minimum width of drag element is honoured
float elemMinWidth = resolvedStyle.minWidth == StyleKeyword.Auto ? 0 : resolvedStyle.minWidth.value;
inlineStyles.width = Mathf.Round(Mathf.Max(visualInput.layout.width * factor, elemMinWidth));
}
else
{
// Make sure the minimum height of drag element is honoured
float elemMinHeight = resolvedStyle.minHeight == StyleKeyword.Auto ? 0 : resolvedStyle.minHeight.value;
inlineStyles.height = Mathf.Round(Mathf.Max(visualInput.layout.height * factor, elemMinHeight));
}
}
}
void UpdateDragElementPosition(GeometryChangedEvent evt)
{
// Only affected by dimension changes
if (evt.oldRect.size == evt.newRect.size)
{
return;
}
UpdateDragElementPosition();
}
internal override void OnViewDataReady()
{
base.OnViewDataReady();
UpdateDragElementPosition();
}
void UpdateDragElementPosition()
{
// UpdateDragElementPosition() might be called at times where we have no panel
// we must skip the position calculation and wait for a layout pass
if (panel == null)
return;
float normalizedPosition = SliderNormalizeValue(value, lowValue, highValue);
float dragElementWidth = dragElement.resolvedStyle.width;
float dragElementHeight = dragElement.resolvedStyle.height;
if (direction == SliderDirection.Horizontal)
{
// This is the main calculation for the location of the thumbs / dragging element
float offsetForThumbFullWidth = -dragElement.resolvedStyle.marginLeft - dragElement.resolvedStyle.marginRight;
float totalWidth = visualInput.layout.width - dragElementWidth + offsetForThumbFullWidth;
dragElement.style.left = normalizedPosition * totalWidth;
}
else
{
float totalHeight = visualInput.layout.height - dragElementHeight;
dragElement.style.top = normalizedPosition * totalHeight;
}
}
protected override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
if (evt == null)
{
return;
}
if (evt.eventTypeId == GeometryChangedEvent.TypeId())
{
UpdateDragElementPosition((GeometryChangedEvent)evt);
}
}
}
}