-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvr-mode-active-switch.js
69 lines (58 loc) · 2.09 KB
/
vr-mode-active-switch.js
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
import {Component, Type} from '@wonderlandengine/api';
/**
* Allows switching all other components on an object to active/inactive
* depending on whether a VR/AR session is active.
*
* Useful for hiding controllers until the user enters VR for example.
*/
export class VrModeActiveSwitch extends Component {
static TypeName = 'vr-mode-active-switch';
static Properties = {
/** When components should be active: In VR or when not in VR */
activateComponents: {
type: Type.Enum,
values: ['in VR', 'in non-VR'],
default: 'in VR',
},
/** Whether child object's components should be affected */
affectChildren: {type: Type.Bool, default: true},
};
start() {
this.components = [];
this.getComponents(this.object);
/* Initial activation/deactivation */
this.onXRSessionEnd();
this.onSessionStartCallback = this.onXRSessionStart.bind(this);
this.onSessionEndCallback = this.onXRSessionEnd.bind(this);
}
onActivate() {
this.engine.onXRSessionStart.add(this.onSessionStartCallback);
this.engine.onXRSessionEnd.add(this.onSessionEndCallback);
}
onDeactivate() {
this.engine.onXRSessionStart.remove(this.onSessionStartCallback);
this.engine.onXRSessionEnd.remove(this.onSessionEndCallback);
}
getComponents(obj) {
const comps = obj.getComponents().filter((c) => c.type !== 'vr-mode-active-switch');
this.components = this.components.concat(comps);
if (this.affectChildren) {
let children = obj.children;
for (let i = 0; i < children.length; ++i) {
this.getComponents(children[i]);
}
}
}
setComponentsActive(active) {
const comps = this.components;
for (let i = 0; i < comps.length; ++i) {
comps[i].active = active;
}
}
onXRSessionStart() {
this.setComponentsActive(this.activateComponents == 0);
}
onXRSessionEnd() {
this.setComponentsActive(this.activateComponents != 0);
}
}