-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerticalSwipe.cs
More file actions
291 lines (238 loc) · 9.65 KB
/
VerticalSwipe.cs
File metadata and controls
291 lines (238 loc) · 9.65 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
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
287
288
289
290
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine;
using TMPro;
public class SwipeY : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
public TextMeshProUGUI tmp_speed;
// connect to settings
int ScreenHeight;
float childPosition; // keeps Y position for next kit that will be added
Dropdown settings_treshold;
[SerializeField] int Max_Items_Number = 100; // by default 100
public enum tresholdType
{
zero = 0,
whole = 1,
half = 2,
forth = 4,
fifth = 5,
_60Percents_ = 6
}
[SerializeField]public tresholdType treshold;
[SerializeField]public float speedTreshold = 0f;
[SerializeField]public float changingTime = 0.35f ; // can be changed in inspector
// [SerializeField]int numberOfObjects;
[SerializeField]int currentObject = 1;
[SerializeField]int lastVisitedObject;
[SerializeField]int kitLimiter = 20;
[SerializeField]public int loadNumber = 5; // how many kits will be loaded ahead
bool isActive;
float startTime;
float deltaTime;
Vector3 panelPosition;
Vector3 initialPosition;
[NonSerialized]public List<GameObject> child = new List<GameObject>();
public List<GameObject> buffer = new List<GameObject>();
public event EventHandler OnKitChanged;
void Start()
{
// NOTE: swipePanel MUST BE INITIALIZED!
currentObject = 1;
lastVisitedObject = 1;
Resources.UnloadUnusedAssets();
initialize();
showAll();
}
public void initialize()
{
ScreenHeight = Screen.height;
// set scrolling parameters
isActive = false;
// set pivot to (0.5,1)
this.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 1f);
// set position to top middle
this.transform.position = new Vector2(Screen.width/2, Screen.height);
// set full-screen size
RectTransform rect = this.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(Screen.width, Max_Items_Number * ScreenHeight);
panelPosition = transform.position; // can be used later
initialPosition = panelPosition;
}
/*
▗▄▄▖ ▗▄▄▖▗▄▄▖ ▗▄▖ ▗▖ ▗▖
▐▌ ▐▌ ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌
▝▀▚▖▐▌ ▐▛▀▚▖▐▌ ▐▌▐▌ ▐▌
▗▄▄▞▘▝▚▄▄▖▐▌ ▐▌▝▚▄▞▘▐▙▄▄▖▐▙▄▄▖
*/
public void OnBeginDrag(PointerEventData eventData)
{
if(isActive)return;
startTime = Time.time;
// Debug.Log("Start time = " + startTime);
// Debug.Log(panelPosition);
}
public void OnDrag(PointerEventData data)
{
if(isActive)return;
float difference = (data.position.y - data.pressPosition.y); // always getting deltaY from Y0 to Yposition
// only drag vertically !
transform.position = panelPosition + new Vector3(0, difference, 0); // setting it, with keeping in mind Y0 before drag
this.deltaTime++;
}
public void OnEndDrag(PointerEventData data)
{
if(isActive)return;
// getting swipe speed parameter
float percentage = (data.pressPosition.y - data.position.y) / ScreenHeight;
deltaTime = Time.time - startTime;
float swipeSpeed = Math.Abs(percentage/deltaTime);
// Debug.Log("SwipeSpeed = " + swipeSpeed);
//Debug.Log("|deltaPosition.Y| = " + Math.Abs(transform.position.y - panelPosition.y) );
// getting treshold given in settings
float deltaPositionTreshold;
if(this.treshold == tresholdType.zero)deltaPositionTreshold = 0f;
else if(this.treshold == tresholdType._60Percents_)deltaPositionTreshold = 0.6f*ScreenHeight;
else if(this.treshold == tresholdType.whole)deltaPositionTreshold = 0.66f*ScreenHeight;
else
{
deltaPositionTreshold = ScreenHeight/(((int)treshold)*1f);
}
// getting deltaPosition of panel
float deltaPosition = Math.Abs(transform.position.y - panelPosition.y);
// making swipe based on parameters
if(percentage > 0 && currentObject > 1 && (swipeSpeed >= speedTreshold || deltaPosition >= deltaPositionTreshold) ) // instead of false put treshold code
{
// UP Swipe
currentObject--;
OnKitChanged?.Invoke(this, EventArgs.Empty);
StartCoroutine(SmoothMove(transform.position, panelPosition - new Vector3(0,ScreenHeight,0), changingTime));
}
else if(percentage < 0 && currentObject < child.Count && (swipeSpeed >= speedTreshold || deltaPosition >= deltaPositionTreshold))
{
// Down Swipe
currentObject++;
OnKitChanged?.Invoke(this, EventArgs.Empty);
StartCoroutine(SmoothMove(transform.position, panelPosition + new Vector3(0,ScreenHeight,0), changingTime));
}
else
{
StartCoroutine(SmoothMove(transform.position, panelPosition, changingTime));
}
}
IEnumerator SmoothMove(Vector3 startpos, Vector3 endpos, float changingTime){
isActive = true;
float t = 0f;
while(t <= 1.0){
t += Time.deltaTime / (changingTime);
transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0f, 1f, t));
yield return null;
}
panelPosition = transform.position;
isActive = false;
}
/*
▗▖ ▗▖ ▗▄▖ ▗▄▄▄▖▗▖ ▗▖
▐▛▚▞▜▌▐▌ ▐▌ █ ▐▛▚▖▐▌
▐▌ ▐▌▐▛▀▜▌ █ ▐▌ ▝▜▌
▐▌ ▐▌▐▌ ▐▌▗▄█▄▖▐▌ ▐▌
*/
public void showAll()
{
// clean panel if something left
//clear();
// set current object to lastVisited
currentObject = 1;
// add to panel objects attached
dropBuffer();
}
/*
▗▄▄▖ ▗▄▖ ▗▖ ▗▖▗▖ ▗▖ ▗▄▖ ▗▖ ▗▖
▐▌ ▐▌ ▐▌▐▛▚▞▜▌▐▛▚▞▜▌▐▌ ▐▌▐▛▚▖▐▌
▐▌ ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌ ▝▜▌
▝▚▄▄▖▝▚▄▞▘▐▌ ▐▌▐▌ ▐▌▝▚▄▞▘▐▌ ▐▌
*/
public void addKit(GameObject item)
{
if(item == null)return;
RectTransform rect = item.GetComponent<RectTransform>();
//set pivot
rect.pivot = new Vector2(0.5f, 1f);
// set full-screen
rect.sizeDelta = new Vector2(Screen.width,Screen.height);
// set transform of new adding kit before it becomes child of panel
item.transform.position = new Vector2(Screen.width/2, (child.Count == 0)? (Screen.height) : (child[child.Count - 1].transform.position.y - Screen.height) );
// set object as child of swipable panel
item.transform.SetParent(this.transform, true);
// add it immediately to main list of child kits
child.Add(item);
}
public void bufferAll()
{
foreach(GameObject kit in child)
{
kit.SetActive(false);
buffer.Add(kit);
}
child.Clear();
Debug.Log("BUFFERED ALL!");
}
public void dropBuffer()
{
foreach(GameObject bufferedItem in buffer) addKit(bufferedItem);
buffer.Clear();
Debug.Log("DROPPED BUFFER");
}
public void clear()
{
// delete all children
foreach(GameObject obj in child)
{
Destroy(obj);
}
child.Clear();
Debug.Log("Cleaning panel");
Resources.UnloadUnusedAssets();
// set panel to its initial position
transform.position = initialPosition;
panelPosition = transform.position;
// set current object to 1
currentObject = 1;
}
public void Cleaner()
{
// cleaner will clean all from [0; N-Limiter]
if(child.Count <= kitLimiter)return;
int initialNumber = child.Count;
// destroy part of top to satisfy limiter
for(int i=0; i < initialNumber - kitLimiter; i++)
{
Destroy(child[0]);
child[0] = null;
child.RemoveAt(0);
}
// unloadUnusedAssets
Resources.UnloadUnusedAssets();
// set panel and remaining objects to initialPosition as if we changed filter
transform.position = initialPosition;
panelPosition = transform.position;
for(int i=0; i < child.Count; i++)
{
Debug.Log("moving " + i + "th kit with position: " + child[i].transform.position);
//set transform of new adding kit before it becomes child of panel
Debug.Log("ScreeenHeight new: " + Screen.height);
child[i].transform.position = new Vector2(Screen.width/2, (i == 0)? (2778f/2778f*Screen.height ) : (child[i - 1].transform.position.y - Screen.height) );
}
// Unload texture remainders from memory leak
Resources.UnloadUnusedAssets();
// newCurrentObject = currentObject - (initialNumber - kitLimiter)
currentObject = currentObject - (initialNumber - kitLimiter);
// adjust panel position to show newCurrentObject
transform.position = new Vector2 (transform.position.x, transform.position.y + Screen.height * (currentObject-1));
panelPosition = transform.position;
}
}