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
108 lines (92 loc) · 3.62 KB
/
Copy pathScroller.cs
File metadata and controls
108 lines (92 loc) · 3.62 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
// 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 UnityEngine.Experimental.UIElements.StyleEnums;
namespace UnityEngine.Experimental.UIElements
{
// TODO: Using ScrollerButton class for now, as Button class uses Skin styles by default because of the GUISkinStyle attribute
// ScrollerButton is a repeat button without any skin styles
public class ScrollerButton : VisualElement
{
public Clickable clickable;
public ScrollerButton(System.Action clickEvent, long delay, long interval)
{
clickable = new Clickable(clickEvent, delay, interval);
this.AddManipulator(clickable);
}
}
public class Scroller : VisualElement
{
// Usually set by the owner of the scroller
public event System.Action<float> valueChanged;
public Slider slider { get; private set; }
public ScrollerButton lowButton { get; private set; }
public ScrollerButton 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 Slider.Direction direction
{
get { return style.flexDirection == FlexDirection.Row ? Slider.Direction.Horizontal : Slider.Direction.Vertical; }
set
{
if (value == Slider.Direction.Horizontal)
{
style.flexDirection = FlexDirection.Row;
AddToClassList("horizontal");
}
else
{
style.flexDirection = FlexDirection.Column;
AddToClassList("vertical");
}
}
}
public Scroller(float lowValue, float highValue, System.Action<float> valueChanged, Slider.Direction direction = Slider.Direction.Vertical)
{
this.direction = direction;
this.valueChanged = valueChanged;
// Add children in correct order
slider = new Slider(lowValue, highValue, OnSliderValueChange, direction) {name = "Slider", persistenceKey = "Slider"};
Add(slider);
lowButton = new ScrollerButton(ScrollPageUp, ScrollWaitDefinitions.firstWait, ScrollWaitDefinitions.regularWait) {name = "LowButton"};
Add(lowButton);
highButton = new ScrollerButton(ScrollPageDown, ScrollWaitDefinitions.firstWait, ScrollWaitDefinitions.regularWait) {name = "HighButton"};
Add(highButton);
}
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(float newValue)
{
value = newValue;
if (valueChanged != null)
valueChanged(slider.value);
this.Dirty(ChangeType.Repaint);
}
public void ScrollPageUp()
{
value -= (slider.pageSize * (slider.lowValue < slider.highValue ? 1f : -1f));
}
public void ScrollPageDown()
{
value += (slider.pageSize * (slider.lowValue < slider.highValue ? 1f : -1f));
}
}
}