forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClampedDragger.cs
More file actions
88 lines (71 loc) · 2.83 KB
/
ClampedDragger.cs
File metadata and controls
88 lines (71 loc) · 2.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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
namespace UnityEngine.UIElements
{
internal class ClampedDragger<T> : Clickable
where T : IComparable<T>
{
[Flags]
public enum DragDirection
{
None = 0,
LowToHigh = 1 << 0, // i.e. left-to-right, or top-to-bottom drag
HighToLow = 1 << 1, // i.e. right-to-left, or bottom-to-top
Free = 1 << 2 // i.e. user is dragging using the drag element, free of any direction constraint
}
public event System.Action dragging;
public DragDirection dragDirection { get; set; }
BaseSlider<T> slider { get; set; }
public Vector2 startMousePosition { get; private set; }
public Vector2 delta => (lastMousePosition - startMousePosition);
public ClampedDragger(BaseSlider<T> slider, System.Action clickHandler, System.Action dragHandler)
:
base(clickHandler, ScrollWaitDefinitions.firstWait, ScrollWaitDefinitions.regularWait)
{
dragDirection = DragDirection.None;
this.slider = slider;
dragging += dragHandler;
}
protected override void RegisterCallbacksOnTarget()
{
target.RegisterCallback<MouseDownEvent>(OnMouseDown);
target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
target.RegisterCallback<MouseUpEvent>(OnMouseUp);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
}
new void OnMouseDown(MouseDownEvent evt)
{
if (CanStartManipulation(evt))
{
startMousePosition = evt.localMousePosition;
dragDirection = DragDirection.None;
base.OnMouseDown(evt);
}
}
new void OnMouseMove(MouseMoveEvent evt)
{
if (active)
{
// Let base class Clickable handle the mouse event first
// (although nothing much happens in the base class on mouse drags)
base.OnMouseMove(evt);
// The drag element does the real work
// Take control if we can
if (dragDirection == DragDirection.None)
dragDirection = DragDirection.Free;
// If and when we have control, set value from drag element
if (dragDirection == DragDirection.Free)
{
dragging?.Invoke();
}
}
}
}
}