-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanvasTransitionController.cs
286 lines (286 loc) · 8.9 KB
/
CanvasTransitionController.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class CanvasTransitionController : MonoBehaviour {
bool dragging = false;
public RectTransform center;
public ScrollRect scrollRect;
public GameObject[] screens = new GameObject[3];
public RectTransform content;
public float[] screenDistanceToCenter = new float[3];
public Animator leaderboardAnimator;
public Animator IAPAnimator;
int indexOfClosest = 0;
float positionToGoTowards;
float positionToGoTowards1;
public float[] positioning;
public RectTransform panelContainer;
float speedToSnap = 0.22f;
bool calculating = true;
private float lastXPosition;
float velocity;
int index = 1;
float distance = 0;
RectTransform canvas;
bool buttonPressed = false;
public Animator infoAnimator;
public Animator challengesAnimator;
public Animator friendsAnimator;
public Animator userAnimator;
public Animator moreInfo;
public Animator settingsAnimator;
// Use this for initialization
void Start () {
canvas = GameObject.FindGameObjectWithTag ("CombinedSceneCavas").GetComponent<RectTransform> ();
positioning = new float[3] {
canvas.rect.width, 0, -canvas.rect.width
};
speedToSnap = 0.22f;
UpdateDistances ();
content.localPosition = new Vector3 (positioning[UserData.instance.combinedIndex], content.localPosition.y, content.localPosition.z);
scrollRect.decelerationRate = 5f;
}
// Update is called once per frame
void Update () {
UpdateDistances ();
if (buttonPressed == false) {
#if (UNITY_IOS|| UNITY_ANDROID) && !UNITY_EDITOR
// print (" notEditor");
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
//print (panelContainer.anchoredPosition.x);
if (dragging == false) {
//Started dragging
//calculating = true;
//StartCoroutine (CalculateVelocity ());
dragging = true;
indexOfClosest = System.Array.IndexOf (screenDistanceToCenter, Mathf.Min (screenDistanceToCenter));
//print ("start pos: " + Input.mousePosition.x / Screen.width);
}
} else {
if (dragging) {
//Stopped dragging
//print ("stopped dragging");
dragging = false;
// makeSwooshSound = false;
// scrollRect.inertia = true;
StartCoroutine (SnapToScreen ());
}
}
#endif
#if UNITY_EDITOR
// print ("Editor");
if (Input.GetMouseButton (0)) {
//print (panelContainer.anchoredPosition.x);
if (dragging == false) {
//Started dragging
//calculating = true;
//StartCoroutine (CalculateVelocity ());
dragging = true;
indexOfClosest = System.Array.IndexOf (screenDistanceToCenter, Mathf.Min (screenDistanceToCenter));
//print ("start pos: " + Input.mousePosition.x / Screen.width);
}
} else {
if (dragging) {
//Stopped dragging
//print ("stopped dragging");
dragging = false;
// scrollRect.inertia = true;
StartCoroutine (SnapToScreen ());
}
}
#endif
}
if (dragging == false) { }
}
void UpdateDistances () {
for (int i = 0; i < screens.Length; i++) {
screenDistanceToCenter[i] = Mathf.Abs (screens[i].GetComponent<RectTransform> ().position.x - center.position.x);
}
//indexOfClosest = System.Array.IndexOf (screenDistanceToCenter, Mathf.Min (screenDistanceToCenter));
//print (indexOfClosest);
}
IEnumerator CalculateVelocity () {
while (calculating) {
yield return null;
velocity = (panelContainer.anchoredPosition.x - lastXPosition) / Time.unscaledDeltaTime;
//Negative velocity goes right
//Positive velocity goes left
//time = Mathf.Clamp ((Screen.width / velocity), 0.2f, 0.8f);
lastXPosition = panelContainer.anchoredPosition.x;
}
}
IEnumerator SnapToScreen () {
//yield return new WaitForSecondsRealtime (waitBeforeSnap);
lastXPosition = panelContainer.anchoredPosition.x;
yield return null;
velocity = (panelContainer.anchoredPosition.x - lastXPosition) / Time.unscaledDeltaTime;
//print (panelContainer.anchoredPosition.x);
if (Mathf.Abs (velocity) > 200f) {
if (velocity < 0f) {
//Going right, swiping left
//print ("going right");
if (Mathf.Abs (panelContainer.anchoredPosition.x) < 1080) {
if (indexOfClosest + 1 <= 2) {
index = indexOfClosest + 1;
}
}
} else {
//Going left, swiping right
// print ("going left");
if (Mathf.Abs (panelContainer.anchoredPosition.x) < 1080) {
if (indexOfClosest - 1 >= 0) {
index = indexOfClosest - 1;
}
}
}
} else {
index = System.Array.IndexOf (screenDistanceToCenter, Mathf.Min (screenDistanceToCenter));
}
//print (index);
positionToGoTowards = positioning[index];
distance = Mathf.Abs (positionToGoTowards - content.localPosition.x);
speedToSnap = 0.22f * (Mathf.Abs (distance / velocity));
speedToSnap = Mathf.Clamp (speedToSnap, 0.15f, 0.3f);
for (float i = 0; i < 1; i += Time.unscaledDeltaTime / speedToSnap) {
if (buttonPressed == false) {
if (dragging == false) {
// calculating = false;
yield return null;
content.localPosition = Vector3.Lerp (content.localPosition, new Vector3 (positionToGoTowards, content.localPosition.y, content.localPosition.z), i);
scrollRect.inertia = false;
if (Mathf.Abs (content.localPosition.x - positionToGoTowards) < 50) {
if (distance > 300) {
//PlayAudio.PlaySound ("swoosh");
// iOSHapticFeedback.Instance.Trigger ((iOSHapticFeedback.iOSFeedbackType) 1);
distance = 0;
}
}
} else {
// scrollRect.inertia = true;
break;
}
} else {
// scrollRect.inertia = true;
break;
}
}
if (dragging == false) {
//scrollRect.inertia = false;
content.localPosition = new Vector3 (positionToGoTowards, content.localPosition.y, content.localPosition.z);
/*if (distance > 300) {
PlayAudio.PlaySound ("swoosh");
}*/
}
//yield return new WaitForSecondsRealtime (0.05f);
yield return null;
scrollRect.inertia = true;
ResetPanels (index);
//ResetPanels ();
}
public void ShowLeaderboards () {
leaderboardAnimator.SetTrigger ("showLeaderboards");
}
public void HideLeaderboards () {
leaderboardAnimator.SetTrigger ("hideLeaderboards");
}
public void ShowUser () {
userAnimator.SetTrigger ("showInfo");
}
public void HideUser () {
userAnimator.SetTrigger ("hideInfo");
}
public void ShowIAP () {
IAPAnimator.SetTrigger ("showIAP");
}
public void HideIAP () {
IAPAnimator.SetTrigger ("hideIAP");
}
public void ViewMoreInfo () {
moreInfo.SetTrigger ("showLeaderboards");
}
public void HideMoreInfo () {
moreInfo.SetTrigger ("hideLeaderboards");
}
public void ShowStats () {
infoAnimator.SetTrigger ("showInfo");
}
public void HideStats () {
infoAnimator.SetTrigger ("hideInfo");
}
public void ShowChallenges () {
challengesAnimator.SetTrigger ("showChallenges");
}
public void HideChallenges () {
challengesAnimator.SetTrigger ("hideChallenges");
}
public void ShowFriends () {
friendsAnimator.SetTrigger ("showInfo");
}
public void HideFriends () {
friendsAnimator.SetTrigger ("hideInfo");
}
public void ShowScreen (int ind) {
StartCoroutine (TransitionTo (ind));
buttonPressed = true;
}
public void ShowSettings () {
settingsAnimator.SetTrigger ("showInfo");
}
public void HideSettings () {
settingsAnimator.SetTrigger ("hideInfo");
}
void ResetPanels (int ToScreen) {
switch (ToScreen) {
case 0:
//To User Info
leaderboardAnimator.Play ("LeaderboardsIdle");
IAPAnimator.Play ("idle");
break;
case 1:
//To Start
infoAnimator.Play ("infoIdle");
userAnimator.Play ("infoIdle");
moreInfo.Play ("LeaderboardsIdle");
challengesAnimator.Play ("challengesIdle");
friendsAnimator.Play ("infoIdle");
settingsAnimator.Play ("infoIdle");
IAPAnimator.Play ("idle");
break;
case 2:
//To Store
leaderboardAnimator.Play ("LeaderboardsIdle");
settingsAnimator.Play ("infoIdle");
friendsAnimator.Play ("infoIdle");
infoAnimator.Play ("infoIdle");
userAnimator.Play ("infoIdle");
moreInfo.Play ("LeaderboardsIdle");
challengesAnimator.Play ("challengesIdle");
break;
default:
break;
}
}
IEnumerator TransitionTo (int ind) {
while (scrollRect.inertia == false) {
yield return null;
}
positionToGoTowards1 = positioning[ind];
distance = Mathf.Abs (positionToGoTowards - content.localPosition.x);
//print (distance);
scrollRect.inertia = false;
for (float i = 0; i < 1; i += Time.unscaledDeltaTime / 0.15f) {
yield return null;
// print (content.localPosition);
//print (positionToGoTowards);
content.localPosition = Vector3.Lerp (content.localPosition, new Vector3 (positionToGoTowards1, content.localPosition.y, content.localPosition.z), i);
}
content.localPosition = new Vector3 (positionToGoTowards1, content.localPosition.y, content.localPosition.z);
buttonPressed = false;
yield return new WaitForSecondsRealtime (0.15f);
scrollRect.inertia = true;
ResetPanels (index);
//ResetPanels ();
}
}