-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUIDraggableCamera.cs
200 lines (178 loc) · 4.57 KB
/
UIDraggableCamera.cs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using UnityEngine;
[AddComponentMenu("NGUI/Interaction/Draggable Camera")]
[RequireComponent(typeof(Camera))]
public class UIDraggableCamera : MonoBehaviour
{
public Transform rootForBounds;
public Vector2 scale = Vector2.one;
public float scrollWheelFactor;
public UIDragObject.DragEffect dragEffect = UIDragObject.DragEffect.MomentumAndSpring;
public bool smoothDragStart = true;
public float momentumAmount = 35f;
private Camera mCam;
private Transform mTrans;
private bool mPressed;
private Vector2 mMomentum = Vector2.zero;
private Bounds mBounds;
private float mScroll;
private UIRoot mRoot;
private bool mDragStarted;
public Vector2 currentMomentum
{
get
{
return mMomentum;
}
set
{
mMomentum = value;
}
}
private void Start()
{
mCam = GetComponent<Camera>();
mTrans = base.transform;
mRoot = NGUITools.FindInParents<UIRoot>(base.gameObject);
if (rootForBounds == null)
{
Debug.LogError(NGUITools.GetHierarchy(base.gameObject) + " needs the 'Root For Bounds' parameter to be set", this);
base.enabled = false;
}
}
private Vector3 CalculateConstrainOffset()
{
if (rootForBounds == null || rootForBounds.childCount == 0)
{
return Vector3.zero;
}
Vector3 position = new Vector3(mCam.rect.xMin * (float)Screen.width, mCam.rect.yMin * (float)Screen.height, 0f);
Vector3 position2 = new Vector3(mCam.rect.xMax * (float)Screen.width, mCam.rect.yMax * (float)Screen.height, 0f);
position = mCam.ScreenToWorldPoint(position);
position2 = mCam.ScreenToWorldPoint(position2);
Vector3 min = mBounds.min;
float x = min.x;
Vector3 min2 = mBounds.min;
Vector2 minRect = new Vector2(x, min2.y);
Vector3 max = mBounds.max;
float x2 = max.x;
Vector3 max2 = mBounds.max;
Vector2 maxRect = new Vector2(x2, max2.y);
return NGUIMath.ConstrainRect(minRect, maxRect, position, position2);
}
public bool ConstrainToBounds(bool immediate)
{
if (mTrans != null && rootForBounds != null)
{
Vector3 vector = CalculateConstrainOffset();
if (vector.sqrMagnitude > 0f)
{
if (immediate)
{
mTrans.position -= vector;
}
else
{
SpringPosition springPosition = SpringPosition.Begin(base.gameObject, mTrans.position - vector, 13f);
springPosition.ignoreTimeScale = true;
springPosition.worldSpace = true;
}
return true;
}
}
return false;
}
public void Press(bool isPressed)
{
if (isPressed)
{
mDragStarted = false;
}
if (rootForBounds != null)
{
mPressed = isPressed;
if (isPressed)
{
mBounds = NGUIMath.CalculateAbsoluteWidgetBounds(rootForBounds);
mMomentum = Vector2.zero;
mScroll = 0f;
SpringPosition component = GetComponent<SpringPosition>();
if (component != null)
{
component.enabled = false;
}
}
else if (dragEffect == UIDragObject.DragEffect.MomentumAndSpring)
{
ConstrainToBounds(immediate: false);
}
}
}
public void Drag(Vector2 delta)
{
if (smoothDragStart && !mDragStarted)
{
mDragStarted = true;
}
else
{
UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
if (mRoot != null)
{
delta *= mRoot.pixelSizeAdjustment;
}
Vector2 vector = Vector2.Scale(delta, -scale);
mTrans.localPosition += (Vector3)vector;
mMomentum = Vector2.Lerp(mMomentum, mMomentum + vector * (0.01f * momentumAmount), 0.67f);
if (dragEffect != UIDragObject.DragEffect.MomentumAndSpring && ConstrainToBounds(immediate: true))
{
mMomentum = Vector2.zero;
mScroll = 0f;
}
}
}
public void Scroll(float delta)
{
if (base.enabled && NGUITools.GetActive(base.gameObject))
{
if (Mathf.Sign(mScroll) != Mathf.Sign(delta))
{
mScroll = 0f;
}
mScroll += delta * scrollWheelFactor;
}
}
private void Update()
{
float deltaTime = RealTime.deltaTime;
if (mPressed)
{
SpringPosition component = GetComponent<SpringPosition>();
if (component != null)
{
component.enabled = false;
}
mScroll = 0f;
}
else
{
mMomentum += scale * (mScroll * 20f);
mScroll = NGUIMath.SpringLerp(mScroll, 0f, 20f, deltaTime);
if (mMomentum.magnitude > 0.01f)
{
mTrans.localPosition += (Vector3)NGUIMath.SpringDampen(ref mMomentum, 9f, deltaTime);
mBounds = NGUIMath.CalculateAbsoluteWidgetBounds(rootForBounds);
if (!ConstrainToBounds(dragEffect == UIDragObject.DragEffect.None))
{
SpringPosition component2 = GetComponent<SpringPosition>();
if (component2 != null)
{
component2.enabled = false;
}
}
return;
}
mScroll = 0f;
}
NGUIMath.SpringDampen(ref mMomentum, 9f, deltaTime);
}
}