forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScroller.cs
More file actions
151 lines (125 loc) · 5.83 KB
/
Scroller.cs
File metadata and controls
151 lines (125 loc) · 5.83 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
// 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 class Scroller : VisualElement
{
public new class UxmlFactory : UxmlFactory<Scroller, UxmlTraits> {}
public new class UxmlTraits : VisualElement.UxmlTraits
{
UxmlFloatAttributeDescription m_LowValue = new UxmlFloatAttributeDescription { name = "low-value", obsoleteNames = new[] { "lowValue" } };
UxmlFloatAttributeDescription m_HighValue = new UxmlFloatAttributeDescription { name = "high-value", obsoleteNames = new[] { "highValue" } };
UxmlEnumAttributeDescription<SliderDirection> m_Direction = new UxmlEnumAttributeDescription<SliderDirection> { name = "direction", defaultValue = SliderDirection.Vertical};
UxmlFloatAttributeDescription m_Value = new UxmlFloatAttributeDescription { name = "value" };
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get { yield break; }
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
Scroller scroller = ((Scroller)ve);
scroller.slider.lowValue = m_LowValue.GetValueFromBag(bag, cc);
scroller.slider.highValue = m_HighValue.GetValueFromBag(bag, cc);
scroller.direction = m_Direction.GetValueFromBag(bag, cc);
scroller.value = m_Value.GetValueFromBag(bag, cc);
}
}
// Usually set by the owner of the scroller
public event System.Action<float> valueChanged;
public Slider slider { get; private set; }
public RepeatButton lowButton { get; private set; }
public RepeatButton highButton { get; private set; }
public float value
{
get { return slider.value; }
set { slider.value = value; }
}
public float lowValue
{
get { return slider.lowValue; }
set { slider.lowValue = value; }
}
public float highValue
{
get { return slider.highValue; }
set { slider.highValue = value; }
}
public SliderDirection direction
{
get { return resolvedStyle.flexDirection == FlexDirection.Row ? SliderDirection.Horizontal : SliderDirection.Vertical; }
set
{
if (value == SliderDirection.Horizontal)
{
style.flexDirection = FlexDirection.Row;
AddToClassList(horizontalVariantUssClassName);
RemoveFromClassList(verticalVariantUssClassName);
}
else
{
style.flexDirection = FlexDirection.Column;
AddToClassList(verticalVariantUssClassName);
RemoveFromClassList(horizontalVariantUssClassName);
}
}
}
internal const float kDefaultPageSize = 20.0f;
public static readonly string ussClassName = "unity-scroller";
public static readonly string horizontalVariantUssClassName = ussClassName + "--horizontal";
public static readonly string verticalVariantUssClassName = ussClassName + "--vertical";
public static readonly string sliderUssClassName = ussClassName + "__slider";
public static readonly string lowButtonUssClassName = ussClassName + "__low-button";
public static readonly string highButtonUssClassName = ussClassName + "__high-button";
public Scroller()
: this(0, 0, null) {}
public Scroller(float lowValue, float highValue, System.Action<float> valueChanged, SliderDirection direction = SliderDirection.Vertical)
{
AddToClassList(ussClassName);
this.direction = direction;
this.valueChanged = valueChanged;
// Add children in correct order
slider = new Slider(lowValue, highValue, direction, kDefaultPageSize) {name = "unity-slider", viewDataKey = "Slider"};
slider.AddToClassList(sliderUssClassName);
slider.RegisterValueChangedCallback(OnSliderValueChange);
lowButton = new RepeatButton(ScrollPageUp, ScrollWaitDefinitions.firstWait, ScrollWaitDefinitions.regularWait) { name = "unity-low-button" };
lowButton.AddToClassList(lowButtonUssClassName);
Add(lowButton);
highButton = new RepeatButton(ScrollPageDown, ScrollWaitDefinitions.firstWait, ScrollWaitDefinitions.regularWait) { name = "unity-high-button" };
highButton.AddToClassList(highButtonUssClassName);
Add(highButton);
Add(slider);
}
public void Adjust(float factor)
{
// Any factor smaller than 1f will enable the scroller (and its children)
SetEnabled(factor < 1f);
slider.AdjustDragElement(factor);
}
void OnSliderValueChange(ChangeEvent<float> evt)
{
value = evt.newValue;
valueChanged?.Invoke(slider.value);
this.IncrementVersion(VersionChangeType.Repaint);
}
public void ScrollPageUp()
{
ScrollPageUp(1.0f);
}
public void ScrollPageDown()
{
ScrollPageDown(1.0f);
}
public void ScrollPageUp(float factor)
{
value -= factor * (slider.pageSize * (slider.lowValue < slider.highValue ? 1f : -1f));
}
public void ScrollPageDown(float factor)
{
value += factor * (slider.pageSize * (slider.lowValue < slider.highValue ? 1f : -1f));
}
}
}