forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliderInt.cs
More file actions
110 lines (92 loc) · 4.8 KB
/
SliderInt.cs
File metadata and controls
110 lines (92 loc) · 4.8 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
// 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 SliderInt : BaseSlider<int>
{
public new class UxmlFactory : UxmlFactory<SliderInt, UxmlTraits> {}
public new class UxmlTraits : BaseFieldTraits<int, UxmlIntAttributeDescription>
{
UxmlIntAttributeDescription m_LowValue = new UxmlIntAttributeDescription { name = "low-value" };
UxmlIntAttributeDescription m_HighValue = new UxmlIntAttributeDescription { name = "high-value", defaultValue = kDefaultHighValue };
UxmlIntAttributeDescription m_PageSize = new UxmlIntAttributeDescription { name = "page-size", defaultValue = (int)kDefaultPageSize };
UxmlEnumAttributeDescription<SliderDirection> m_Direction = new UxmlEnumAttributeDescription<SliderDirection> { name = "direction", defaultValue = SliderDirection.Horizontal };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
var f = (SliderInt)ve;
f.lowValue = m_LowValue.GetValueFromBag(bag, cc);
f.highValue = m_HighValue.GetValueFromBag(bag, cc);
f.direction = m_Direction.GetValueFromBag(bag, cc);
f.pageSize = m_PageSize.GetValueFromBag(bag, cc);
base.Init(ve, bag, cc);
}
}
internal const int kDefaultHighValue = 10;
public new static readonly string ussClassName = "unity-slider-int";
public new static readonly string labelUssClassName = ussClassName + "__label";
public new static readonly string inputUssClassName = ussClassName + "__input";
public SliderInt()
: this(null, 0, kDefaultHighValue) {}
public SliderInt(int start, int end, SliderDirection direction = SliderDirection.Horizontal, float pageSize = kDefaultPageSize)
: this(null, start, end, direction, pageSize) {}
public SliderInt(string label, int start = 0, int end = kDefaultHighValue, SliderDirection direction = SliderDirection.Horizontal, float pageSize = kDefaultPageSize)
: base(label, start, end, direction, pageSize)
{
AddToClassList(ussClassName);
labelElement.AddToClassList(labelUssClassName);
visualInput.AddToClassList(inputUssClassName);
}
public override float pageSize
{
get { return base.pageSize; }
set { base.pageSize = Mathf.RoundToInt(value); }
}
internal override int SliderLerpUnclamped(int a, int b, float interpolant)
{
return Mathf.RoundToInt(Mathf.LerpUnclamped((float)a, (float)b, interpolant));
}
internal override float SliderNormalizeValue(int currentValue, int lowerValue, int higherValue)
{
return ((float)currentValue - (float)lowerValue) / ((float)higherValue - (float)lowerValue);
}
internal override int SliderRange()
{
return Math.Abs(highValue - lowValue);
}
internal override void ComputeValueAndDirectionFromClick(float sliderLength, float dragElementLength, float dragElementPos, float dragElementLastPos)
{
if (Mathf.Approximately(pageSize, 0.0f))
{
base.ComputeValueAndDirectionFromClick(sliderLength, dragElementLength, dragElementPos, dragElementLastPos);
}
else
{
var totalRange = sliderLength - dragElementLength;
if (Mathf.Abs(totalRange) < Mathf.Epsilon)
return;
var adjustedPageDirection = (int)pageSize;
if (lowValue > highValue)
{
adjustedPageDirection = -adjustedPageDirection;
}
if ((dragElementLastPos < dragElementPos) &&
(clampedDragger.dragDirection != ClampedDragger<int>.DragDirection.LowToHigh))
{
clampedDragger.dragDirection = ClampedDragger<int>.DragDirection.HighToLow;
// Compute the next value based on the page size.
value = value - adjustedPageDirection;
}
else if ((dragElementLastPos > (dragElementPos + dragElementLength)) &&
(clampedDragger.dragDirection != ClampedDragger<int>.DragDirection.HighToLow))
{
clampedDragger.dragDirection = ClampedDragger<int>.DragDirection.LowToHigh;
// Compute the next value based on the page size.
value = value + adjustedPageDirection;
}
}
}
}
}