forked from Snapchat/Spectacles-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialGallery.ts
More file actions
95 lines (84 loc) · 2.05 KB
/
Copy pathSpatialGallery.ts
File metadata and controls
95 lines (84 loc) · 2.05 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
import { LoadingIndicator } from "./LoadingIndicator"
import { SpatialImageFrame } from "./SpatialImageFrame"
/**
* Provides a somewhat complex example of use of the spatial image components.
*
* @version 1.0.0
*/
@component
export class SpatialGallery extends BaseScriptComponent {
@typename
SpatialImage: keyof ComponentNameMap
/**
* The SIK container frame that holds the image.
*/
@input
frame: SpatialImageFrame
/**
* The spatial image custom component.
*/
@input("SpatialImage")
image: any
/**
* The loading indicator to tell that the image is being spatialized.
*/
@input
loadingIndicator: LoadingIndicator
/**
* The set of images that make up the gallery.
*/
@input
gallery: Texture[]
/**
* If true the order of the gallery will be shuffled on initialization.
*/
@input
shuffle: boolean
private index: number = 0
onAwake() {
if (this.shuffle) {
shuffle(this.gallery)
}
this.createEvent("OnStartEvent").bind(() => {
this.initialiseFrame()
})
}
/**
* Moves the gallery to the next image.
*/
public leftPressed(): void {
let newIndex = this.index - 1
if (newIndex < 0) {
newIndex += this.gallery.length
}
this.setIndex(newIndex)
}
/**
* Move the gallery to the previous image.
*/
public rightPressed(): void {
this.setIndex((this.index + 1) % this.gallery.length)
}
private initialiseFrame(): void {
this.setIndex(this.index)
this.image.onLoadingStart.add(() => {
this.loadingIndicator.sceneObject.enabled = true
this.loadingIndicator.reset()
})
this.image.onLoaded.add(() => {
this.loadingIndicator.sceneObject.enabled = false
})
}
private setIndex(newIndex: number) {
this.index = newIndex
this.frame.setImage(this.gallery[this.index], true)
}
}
// declare the function
function shuffle<T>(array: T[]) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
return array
}