forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentDragger.cs
More file actions
134 lines (106 loc) · 4.32 KB
/
Copy pathContentDragger.cs
File metadata and controls
134 lines (106 loc) · 4.32 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
// 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;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.Experimental.UIElements.GraphView
{
// drags the contentContainer of a graphview around
// add to the GraphView
public class ContentDragger : MouseManipulator
{
private Vector2 m_Start;
public Vector2 panSpeed { get; set; }
public bool clampToParentEdges { get; set; }
bool m_Active;
public ContentDragger()
{
m_Active = false;
activators.Add(new ManipulatorActivationFilter {button = MouseButton.LeftMouse, modifiers = EventModifiers.Alt});
activators.Add(new ManipulatorActivationFilter {button = MouseButton.MiddleMouse});
panSpeed = new Vector2(1, 1);
clampToParentEdges = false;
}
protected Rect CalculatePosition(float x, float y, float width, float height)
{
var rect = new Rect(x, y, width, height);
if (clampToParentEdges)
{
if (rect.x < target.parent.layout.xMin)
rect.x = target.parent.layout.xMin;
else if (rect.xMax > target.parent.layout.xMax)
rect.x = target.parent.layout.xMax - rect.width;
if (rect.y < target.parent.layout.yMin)
rect.y = target.parent.layout.yMin;
else if (rect.yMax > target.parent.layout.yMax)
rect.y = target.parent.layout.yMax - rect.height;
// Reset size, we never intended to change them in the first place
rect.width = width;
rect.height = height;
}
return rect;
}
protected override void RegisterCallbacksOnTarget()
{
var graphView = target as GraphView;
if (graphView == null)
{
throw new InvalidOperationException("Manipulator can only be added to a GraphView");
}
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 e)
{
if (m_Active)
{
e.StopImmediatePropagation();
return;
}
if (!CanStartManipulation(e))
return;
var graphView = target as GraphView;
if (graphView == null)
return;
m_Start = graphView.ChangeCoordinatesTo(graphView.contentViewContainer, e.localMousePosition);
m_Active = true;
target.TakeMouseCapture();
e.StopPropagation();
}
protected void OnMouseMove(MouseMoveEvent e)
{
if (!m_Active || !target.HasMouseCapture())
return;
var graphView = target as GraphView;
if (graphView == null)
return;
Vector2 diff = graphView.ChangeCoordinatesTo(graphView.contentViewContainer, e.localMousePosition) - m_Start;
// During the drag update only the view
Vector3 s = graphView.contentViewContainer.transform.scale;
graphView.viewTransform.position += Vector3.Scale(diff, s);
e.StopPropagation();
}
protected void OnMouseUp(MouseUpEvent e)
{
if (!m_Active || !CanStopManipulation(e))
return;
var graphView = target as GraphView;
if (graphView == null)
return;
Vector3 p = graphView.contentViewContainer.transform.position;
Vector3 s = graphView.contentViewContainer.transform.scale;
graphView.UpdateViewTransform(p, s);
m_Active = false;
target.ReleaseMouseCapture();
e.StopPropagation();
}
}
}