/
VRButtonViewModel.js
223 lines (201 loc) · 5.92 KB
/
VRButtonViewModel.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
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
import defaultValue from "../../Core/defaultValue.js";
import defined from "../../Core/defined.js";
import destroyObject from "../../Core/destroyObject.js";
import DeveloperError from "../../Core/DeveloperError.js";
import EventHelper from "../../Core/EventHelper.js";
import Fullscreen from "../../Core/Fullscreen.js";
import OrthographicFrustum from "../../Core/OrthographicFrustum.js";
import knockout from "../../ThirdParty/knockout.js";
import NoSleep from "../../ThirdParty/NoSleep.js";
import createCommand from "../createCommand.js";
import getElement from "../getElement.js";
function lockScreen(orientation) {
var locked = false;
var screen = window.screen;
if (defined(screen)) {
if (defined(screen.lockOrientation)) {
locked = screen.lockOrientation(orientation);
} else if (defined(screen.mozLockOrientation)) {
locked = screen.mozLockOrientation(orientation);
} else if (defined(screen.msLockOrientation)) {
locked = screen.msLockOrientation(orientation);
} else if (defined(screen.orientation && screen.orientation.lock)) {
locked = screen.orientation.lock(orientation);
}
}
return locked;
}
function unlockScreen() {
var screen = window.screen;
if (defined(screen)) {
if (defined(screen.unlockOrientation)) {
screen.unlockOrientation();
} else if (defined(screen.mozUnlockOrientation)) {
screen.mozUnlockOrientation();
} else if (defined(screen.msUnlockOrientation)) {
screen.msUnlockOrientation();
} else if (defined(screen.orientation && screen.orientation.unlock)) {
screen.orientation.unlock();
}
}
}
function toggleVR(viewModel, scene, isVRMode, isOrthographic) {
if (isOrthographic()) {
return;
}
if (isVRMode()) {
scene.useWebVR = false;
if (viewModel._locked) {
unlockScreen();
viewModel._locked = false;
}
viewModel._noSleep.disable();
Fullscreen.exitFullscreen();
isVRMode(false);
} else {
if (!Fullscreen.fullscreen) {
Fullscreen.requestFullscreen(viewModel._vrElement);
}
viewModel._noSleep.enable();
if (!viewModel._locked) {
viewModel._locked = lockScreen("landscape");
}
scene.useWebVR = true;
isVRMode(true);
}
}
/**
* The view model for {@link VRButton}.
* @alias VRButtonViewModel
* @constructor
*
* @param {Scene} scene The scene.
* @param {Element|String} [vrElement=document.body] The element or id to be placed into VR mode.
*/
function VRButtonViewModel(scene, vrElement) {
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError("scene is required.");
}
//>>includeEnd('debug');
var that = this;
var isEnabled = knockout.observable(Fullscreen.enabled);
var isVRMode = knockout.observable(false);
/**
* Gets whether or not VR mode is active.
*
* @type {Boolean}
*/
this.isVRMode = undefined;
knockout.defineProperty(this, "isVRMode", {
get: function () {
return isVRMode();
},
});
/**
* Gets or sets whether or not VR functionality should be enabled.
*
* @type {Boolean}
* @see Fullscreen.enabled
*/
this.isVREnabled = undefined;
knockout.defineProperty(this, "isVREnabled", {
get: function () {
return isEnabled();
},
set: function (value) {
isEnabled(value && Fullscreen.enabled);
},
});
/**
* Gets the tooltip. This property is observable.
*
* @type {String}
*/
this.tooltip = undefined;
knockout.defineProperty(this, "tooltip", function () {
if (!isEnabled()) {
return "VR mode is unavailable";
}
return isVRMode() ? "Exit VR mode" : "Enter VR mode";
});
var isOrthographic = knockout.observable(false);
this._isOrthographic = undefined;
knockout.defineProperty(this, "_isOrthographic", {
get: function () {
return isOrthographic();
},
});
this._eventHelper = new EventHelper();
this._eventHelper.add(scene.preRender, function () {
isOrthographic(scene.camera.frustum instanceof OrthographicFrustum);
});
this._locked = false;
this._noSleep = new NoSleep();
this._command = createCommand(function () {
toggleVR(that, scene, isVRMode, isOrthographic);
}, knockout.getObservable(this, "isVREnabled"));
this._vrElement = defaultValue(getElement(vrElement), document.body);
this._callback = function () {
if (!Fullscreen.fullscreen && isVRMode()) {
scene.useWebVR = false;
if (that._locked) {
unlockScreen();
that._locked = false;
}
that._noSleep.disable();
isVRMode(false);
}
};
document.addEventListener(Fullscreen.changeEventName, this._callback);
}
Object.defineProperties(VRButtonViewModel.prototype, {
/**
* Gets or sets the HTML element to place into VR mode when the
* corresponding button is pressed.
* @memberof VRButtonViewModel.prototype
*
* @type {Element}
*/
vrElement: {
//TODO:@exception {DeveloperError} value must be a valid HTML Element.
get: function () {
return this._vrElement;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug);
if (!(value instanceof Element)) {
throw new DeveloperError("value must be a valid Element.");
}
//>>includeEnd('debug');
this._vrElement = value;
},
},
/**
* Gets the Command to toggle VR mode.
* @memberof VRButtonViewModel.prototype
*
* @type {Command}
*/
command: {
get: function () {
return this._command;
},
},
});
/**
* @returns {Boolean} true if the object has been destroyed, false otherwise.
*/
VRButtonViewModel.prototype.isDestroyed = function () {
return false;
};
/**
* Destroys the view model. Should be called to
* properly clean up the view model when it is no longer needed.
*/
VRButtonViewModel.prototype.destroy = function () {
this._eventHelper.removeAll();
document.removeEventListener(Fullscreen.changeEventName, this._callback);
destroyObject(this);
};
export default VRButtonViewModel;