forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickable.cs
More file actions
166 lines (142 loc) · 5.33 KB
/
Clickable.cs
File metadata and controls
166 lines (142 loc) · 5.33 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
namespace UnityEngine.UIElements
{
public class Clickable : MouseManipulator
{
public event System.Action<EventBase> clickedWithEventInfo;
public event System.Action clicked;
private readonly long m_Delay; // in milliseconds
private readonly long m_Interval; // in milliseconds
protected bool active { get; set; }
public Vector2 lastMousePosition { get; private set; }
private IVisualElementScheduledItem m_Repeater;
// delay is used to determine when the event begins. Applies if delay > 0.
// interval is used to determine the time delta between event repetitions. Applies if interval > 0.
public Clickable(System.Action handler, long delay, long interval) : this(handler)
{
m_Delay = delay;
m_Interval = interval;
active = false;
}
public Clickable(System.Action<EventBase> handler)
{
clickedWithEventInfo = handler;
activators.Add(new ManipulatorActivationFilter { button = MouseButton.LeftMouse });
}
// Click-once type constructor
public Clickable(System.Action handler)
{
clicked = handler;
activators.Add(new ManipulatorActivationFilter { button = MouseButton.LeftMouse });
active = false;
}
private void OnTimer(TimerState timerState)
{
if (clicked != null && IsRepeatable())
{
if (target.ContainsPoint(lastMousePosition))
{
clicked();
target.pseudoStates |= PseudoStates.Active;
}
else
{
target.pseudoStates &= ~PseudoStates.Active;
}
}
}
private bool IsRepeatable()
{
return (m_Delay > 0 || m_Interval > 0);
}
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);
}
protected void OnMouseDown(MouseDownEvent evt)
{
if (evt != null && CanStartManipulation(evt))
{
active = true;
target.CaptureMouse();
lastMousePosition = evt.localMousePosition;
if (IsRepeatable())
{
// Repeatable button clicks are performed on the MouseDown and at timer events
if (target.ContainsPoint(evt.localMousePosition))
{
if (clicked != null)
clicked();
else
clickedWithEventInfo?.Invoke(evt);
}
if (m_Repeater == null)
{
m_Repeater = target.schedule.Execute(OnTimer).Every(m_Interval).StartingIn(m_Delay);
}
else
{
m_Repeater.ExecuteLater(m_Delay);
}
}
target.pseudoStates |= PseudoStates.Active;
evt.StopImmediatePropagation();
}
}
protected void OnMouseMove(MouseMoveEvent evt)
{
if (evt != null && active)
{
lastMousePosition = evt.localMousePosition;
if (target.ContainsPoint(evt.localMousePosition))
{
target.pseudoStates |= PseudoStates.Active;
}
else
{
target.pseudoStates &= ~PseudoStates.Active;
}
evt.StopPropagation();
}
}
protected void OnMouseUp(MouseUpEvent evt)
{
if (evt != null && active && CanStopManipulation(evt))
{
active = false;
target.ReleaseMouse();
if (IsRepeatable())
{
// Repeatable button clicks are performed on the MouseDown and at timer events only
if (m_Repeater != null)
{
m_Repeater.Pause();
}
}
else
{
// Non repeatable button clicks are performed on the MouseUp
if (target.ContainsPoint(evt.localMousePosition))
{
if (clicked != null)
clicked();
else
clickedWithEventInfo?.Invoke(evt);
}
}
target.pseudoStates &= ~PseudoStates.Active;
evt.StopPropagation();
}
}
}
}