| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var api = (function() { | 5 var api = new Object(); |
| 6 'use strict'; | 6 |
| 7 | 7 /** |
| 8 /** | 8 * Enumeration of scene update commands. |
| 9 * Enumeration of scene update commands. | 9 * @enum {number} |
| 10 * @enum {number} | 10 * @const |
| 11 * @const | 11 */ |
| 12 */ | 12 api.Command = { |
| 13 var Command = Object.freeze({ | 13 'ADD_ELEMENT': 0, |
| 14 'ADD_ELEMENT': 0, | 14 'UPDATE_ELEMENT': 1, |
| 15 'UPDATE_ELEMENT': 1, | 15 'REMOVE_ELEMENT': 2, |
| 16 'REMOVE_ELEMENT': 2, | 16 'ADD_ANIMATION': 3, |
| 17 'ADD_ANIMATION': 3, | 17 'REMOVE_ANIMATION': 4 |
| 18 'REMOVE_ANIMATION': 4 | 18 }; |
| 19 }); | 19 |
| 20 | 20 /** |
| 21 /** | 21 * Sends one or more commands to native scene management. Commands are used |
| 22 * Sends one or more commands to native scene management. Commands are used | 22 * to add, modify or remove elements and animations. For examples of how to |
| 23 * to add, modify or remove elements and animations. For examples of how to | 23 * format command parameters, refer to examples in scene.js. |
| 24 * format command parameters, refer to examples in scene.js. | 24 * @param {Array<Object>} commands |
| 25 */ | 25 */ |
| 26 function sendCommands(commands) { | 26 api.sendCommands = function(commands) { |
| 27 chrome.send('updateScene', commands); | 27 chrome.send('updateScene', commands); |
| 28 } | 28 }; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * Returns the element ID of the browser content quad, allowing the content to | 31 * Enumeration of valid Anchroing for X axis. |
| 32 * be manimulated in the scene. | 32 * An element can either be anchored to the left, right, or center of the main |
| 33 */ | 33 * content rect (or it can be absolutely positioned using NONE). Any |
| 34 function getContentElementId() { | 34 * translations applied will be relative to this anchoring. |
| 35 return 0; | 35 * @enum {number} |
| 36 } | 36 * @const |
| 37 | 37 */ |
| 38 /** | 38 api.XAnchoring = { |
| 39 * Enumeration of valid Anchroing for X axis. | 39 'XNONE': 0, |
| 40 * An element can either be anchored to the left, right, or center of the main | 40 'XLEFT': 1, |
| 41 * content rect (or it can be absolutely positioned using NONE). Any | 41 'XRIGHT': 2 |
| 42 * translations applied will be relative to this anchoring. | 42 }; |
| 43 * @enum {number} | 43 |
| 44 * @const | 44 /** |
| 45 */ | 45 * Enumeration of valid Anchroing for Y axis. |
| 46 var XAnchoring = Object.freeze({ | 46 * @enum {number} |
| 47 'XNONE': 0, | 47 * @const |
| 48 'XLEFT': 1, | 48 */ |
| 49 'XRIGHT': 2 | 49 api.YAnchoring = { |
| 50 }); | 50 'YNONE': 0, |
| 51 | 51 'YTOP': 1, |
| 52 /** | 52 'YBOTTOM': 2 |
| 53 * Enumeration of valid Anchroing for Y axis. | 53 }; |
| 54 * @enum {number} | 54 |
| 55 * @const | 55 /** |
| 56 */ | 56 * Enumeration of actions that can be triggered by the HTML UI. |
| 57 var YAnchoring = Object.freeze({ | 57 * @enum {number} |
| 58 'YNONE': 0, | 58 * @const |
| 59 'YTOP': 1, | 59 */ |
| 60 'YBOTTOM': 2 | 60 api.Action = { |
| 61 }); | 61 'HISTORY_BACK': 0, |
| 62 | 62 'HISTORY_FORWARD': 1, |
| 63 /** | 63 'RELOAD': 2, |
| 64 * Enumeration of actions that can be triggered by the HTML UI. | 64 'ZOOM_OUT': 3, |
| 65 * @enum {number} | 65 'ZOOM_IN': 4, |
| 66 * @const | 66 'RELOAD_UI': 5, |
| 67 */ | 67 }; |
| 68 var Action = Object.freeze({ | 68 |
| 69 'HISTORY_BACK': 0, | 69 /** |
| 70 'HISTORY_FORWARD': 1, | 70 * Enumeration of modes that can be specified by the native side. |
| 71 'RELOAD': 2, | 71 * @enum {number} |
| 72 'ZOOM_OUT': 3, | 72 * @const |
| 73 'ZOOM_IN': 4, | 73 */ |
| 74 'RELOAD_UI': 5, | 74 api.Mode = { |
| 75 }); | 75 'UNKNOWN': -1, |
| 76 | 76 'STANDARD': 0, |
| 77 /** | 77 'WEB_VR': 1, |
| 78 * Enumeration of modes that can be specified by the native side. | 78 }; |
| 79 * @enum {number} | 79 |
| 80 * @const | 80 /** |
| 81 */ | 81 * Triggers an Action. |
| 82 var Mode = Object.freeze({ | 82 * @param {api.Action} action |
| 83 'UNKNOWN': -1, | 83 */ |
| 84 'STANDARD': 0, | 84 api.doAction = function(action) { |
| 85 'WEB_VR': 1, | 85 chrome.send('doAction', [action]); |
| 86 }); | 86 }; |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Triggers an Action. | 89 * Notify native scene management that DOM loading has completed, at the |
| 90 */ | 90 * specified page size. |
| 91 function doAction(action) { | 91 */ |
| 92 chrome.send('doAction', [action]); | 92 api.domLoaded = function() { |
| 93 } | 93 chrome.send('domLoaded'); |
| 94 | 94 }; |
| 95 /** | 95 |
| 96 * Notify native scene management that DOM loading has completed, at the | 96 /** |
| 97 * specified page size. | 97 * Sets the CSS size for this page. |
| 98 */ | 98 * @param {number} width |
| 99 function domLoaded() { | 99 * @param {number} height |
| 100 chrome.send('domLoaded'); | 100 * @param {number} dpr |
| 101 } | 101 */ |
| 102 | 102 api.setUiCssSize = function(width, height, dpr) { |
| 103 /** | 103 chrome.send('setUiCssSize', [width, height, dpr]); |
| 104 * Sets the CSS size for this page. | 104 }; |
| 105 */ | 105 |
| 106 function setUiCssSize(width, height, dpr) { | 106 /** |
| 107 chrome.send('setUiCssSize', [width, height, dpr]); | 107 * Represents updates to UI element properties. Any properties set on this |
| 108 } | 108 * object are relayed to an underlying native element via scene command. |
| 109 | 109 * Properties that are not set on this object are left unchanged. |
| 110 /** | 110 * @struct |
| 111 * Represents updates to UI element properties. Any properties set on this | 111 */ |
| 112 * object are relayed to an underlying native element via scene command. | 112 api.UiElementUpdate = class { |
| 113 * Properties that are not set on this object are left unchanged. | 113 constructor() { |
| 114 */ | 114 /** @private {!Object} */ |
| 115 class UiElementUpdate { | 115 this.properties = {'id': -1}; |
| 116 | 116 } |
| 117 setIsContentQuad() { | 117 |
| 118 this.contentQuad = true; | 118 /** |
| 119 } | 119 * Set the id of the element to update. |
| 120 | 120 * @param {number} id |
| 121 /** | 121 */ |
| 122 * Specify a parent for this element. If set, this element is positioned | 122 setId(id) { |
| 123 * relative to its parent element, rather than absolutely. This allows | 123 this.properties['id'] = id; |
| 124 * elements to automatically move with a parent. | 124 } |
| 125 */ | 125 |
| 126 setParentId(id) { | 126 /** |
| 127 this.parentId = id; | 127 * Operates on an instance of MyClass and returns something. |
| 128 } | 128 */ |
| 129 | 129 setIsContentQuad() { |
| 130 /** | 130 this.properties['contentQuad'] = true; |
| 131 * Specify the width and height (in meters) of an element. | 131 } |
| 132 */ | 132 |
| 133 setSize(x, y) { | 133 /** |
| 134 this.size = { x: x, y: y }; | 134 * Specify a parent for this element. If set, this element is positioned |
| 135 } | 135 * relative to its parent element, rather than absolutely. This allows |
| 136 | 136 * elements to automatically move with a parent. |
| 137 /** | 137 * @param {number} id |
| 138 * Specify optional scaling of the element, and any children. | 138 */ |
| 139 */ | 139 setParentId(id) { |
| 140 setScale(x, y, z) { | 140 this.properties['parentId'] = id; |
| 141 this.scale = { x: x, y: y, z: z }; | 141 } |
| 142 } | 142 |
| 143 | 143 /** |
| 144 /** | 144 * Specify the width and height (in meters) of an element. |
| 145 * Specify rotation for the element. The rotation is specified in axis-angle | 145 * @param {number} x |
| 146 * representation (rotate around unit vector [x, y, z] by 'a' radians). | 146 * @param {number} y |
| 147 */ | 147 */ |
| 148 setRotation(x, y, z, a) { | 148 setSize(x, y) { |
| 149 this.rotation = { x: x, y: y, z: z, a: a }; | 149 this.properties['size'] = {x: x, y: y}; |
| 150 } | 150 } |
| 151 | 151 |
| 152 /** | 152 /** |
| 153 * Specify the translation of the element. If anchoring is specified, the | 153 * Specify optional scaling of the element, and any children. |
| 154 * offset is applied to the anchoring position rather than the origin. | 154 * @param {number} x |
| 155 * Translation is applied after scaling and rotation. | 155 * @param {number} y |
| 156 */ | 156 * @param {number} z |
| 157 setTranslation(x, y, z) { | 157 */ |
| 158 this.translation = { x: x, y: y, z: z }; | 158 setScale(x, y, z) { |
| 159 } | 159 this.properties['scale'] = {x: x, y: y, z: z}; |
| 160 | 160 } |
| 161 /** | 161 |
| 162 * Anchoring allows a rectangle to be positioned relative to the edge of | 162 /** |
| 163 * its parent, without being concerned about the size of the parent. | 163 * Specify rotation for the element. The rotation is specified in axis-angle |
| 164 * Values should be XAnchoring and YAnchoring elements. | 164 * representation (rotate around unit vector [x, y, z] by 'a' radians). |
| 165 * Example: element.setAnchoring(XAnchoring.XNONE, YAnchoring.YBOTTOM); | 165 * @param {number} x |
| 166 */ | 166 * @param {number} y |
| 167 setAnchoring(x, y) { | 167 * @param {number} z |
| 168 this.xAnchoring = x; | 168 * @param {number} a |
| 169 this.yAnchoring = y; | 169 */ |
| 170 } | 170 setRotation(x, y, z, a) { |
| 171 | 171 this.properties['rotation'] = {x: x, y: y, z: z, a: a}; |
| 172 /** | 172 } |
| 173 * Visibility controls whether the element is rendered. | 173 |
| 174 */ | 174 /** |
| 175 setVisible(visible) { | 175 * Specify the translation of the element. If anchoring is specified, the |
| 176 this.visible = !!visible; | 176 * offset is applied to the anchoring position rather than the origin. |
| 177 } | 177 * Translation is applied after scaling and rotation. |
| 178 | 178 * @param {number} x |
| 179 /** | 179 * @param {number} y |
| 180 * Hit-testable implies that the reticle will hit the element, if visible. | 180 * @param {number} z |
| 181 */ | 181 */ |
| 182 setHitTestable(testable) { | 182 setTranslation(x, y, z) { |
| 183 this.hitTestable = !!testable; | 183 this.properties['translation'] = {x: x, y: y, z: z}; |
| 184 } | 184 } |
| 185 | 185 |
| 186 /** | 186 /** |
| 187 * Causes an element to be rendered relative to the field of view, rather | 187 * Anchoring allows a rectangle to be positioned relative to the edge of |
| 188 * than the scene. Elements locked in this way should not have a parent. | 188 * its parent, without being concerned about the size of the parent. |
| 189 */ | 189 * Values should be XAnchoring and YAnchoring elements. |
| 190 setLockToFieldOfView(locked) { | 190 * Example: element.setAnchoring(XAnchoring.XNONE, YAnchoring.YBOTTOM); |
| 191 this.lockToFov = !!locked; | 191 * @param {number} x |
| 192 } | 192 * @param {number} y |
| 193 }; | 193 */ |
| 194 | 194 setAnchoring(x, y) { |
| 195 /** | 195 this.properties['xAnchoring'] = x; |
| 196 * Represents a new UI element. This object builds on UiElementUpdate, | 196 this.properties['yAnchoring'] = y; |
| 197 * forcing the underlying texture coordinates to be specified. | 197 } |
| 198 */ | 198 |
| 199 class UiElement extends UiElementUpdate { | 199 /** |
| 200 /** | 200 * Visibility controls whether the element is rendered. |
| 201 * Constructor of UiElement. | 201 * @param {boolean} visible |
| 202 * pixelX and pixelY values indicate the left upper corner; pixelWidth and | 202 */ |
| 203 * pixelHeight is width and height of the texture to be copied from the web | 203 setVisible(visible) { |
| 204 * contents. | 204 this.properties['visible'] = !!visible; |
| 205 */ | 205 } |
| 206 constructor(pixelX, pixelY, pixelWidth, pixelHeight) { | 206 |
| 207 super(); | 207 /** |
| 208 | 208 * Hit-testable implies that the reticle will hit the element, if visible. |
| 209 this.copyRect = { | 209 * @param {boolean} testable |
| 210 x: pixelX, | 210 */ |
| 211 y: pixelY, | 211 setHitTestable(testable) { |
| 212 width: pixelWidth, | 212 this.properties['hitTestable'] = !!testable; |
| 213 height: pixelHeight | 213 } |
| 214 }; | 214 |
| 215 } | 215 /** |
| 216 }; | 216 * Causes an element to be rendered relative to the field of view, rather |
| 217 | 217 * than the scene. Elements locked in this way should not have a parent. |
| 218 /** | 218 * @param {boolean} locked |
| 219 * Enumeration of animatable properties. | 219 */ |
| 220 * @enum {number} | 220 setLockToFieldOfView(locked) { |
| 221 * @const | 221 this.properties['lockToFov'] = !!locked; |
| 222 */ | 222 } |
| 223 var Property = Object.freeze({ | 223 }; |
| 224 'COPYRECT': 0, | 224 |
| 225 'SIZE': 1, | 225 /** |
| 226 'TRANSLATION': 2, | 226 * Represents a new UI element. This object builds on UiElementUpdate, |
| 227 'ORIENTATION': 3, | 227 * forcing the underlying texture coordinates to be specified. |
| 228 'ROTATION': 4 | 228 * @struct |
| 229 }); | 229 */ |
| 230 | 230 api.UiElement = class extends api.UiElementUpdate { |
| 231 /** | 231 /** |
| 232 * Enumeration of easing type. | 232 * Constructor of UiElement. |
| 233 * @enum {number} | 233 * pixelX and pixelY values indicate the left upper corner; pixelWidth and |
| 234 * @const | 234 * pixelHeight is width and height of the texture to be copied from the web |
| 235 */ | 235 * contents. |
| 236 var Easing = Object.freeze({ | 236 * @param {number} pixelX |
| 237 'LINEAR': 0, | 237 * @param {number} pixelY |
| 238 'CUBICBEZIER': 1, | 238 * @param {number} pixelWidth |
| 239 'EASEIN': 2, | 239 * @param {number} pixelHeight |
| 240 'EASEOUT': 3 | 240 */ |
| 241 }); | 241 constructor(pixelX, pixelY, pixelWidth, pixelHeight) { |
| 242 | 242 super(); |
| 243 /** | 243 |
| 244 * Base animation class. An animation can vary only one object property. | 244 /** @private {Object} */ |
| 245 */ | 245 this.properties['copyRect'] = |
| 246 class Animation { | 246 {x: pixelX, y: pixelY, width: pixelWidth, height: pixelHeight}; |
| 247 constructor(elementId, durationMs) { | 247 } |
| 248 this.meshId = elementId; | 248 }; |
| 249 this.to = {}; | 249 |
| 250 this.easing = {}; | 250 /** |
| 251 this.easing.type = api.Easing.LINEAR; | 251 * Enumeration of animatable properties. |
| 252 | 252 * @enum {number} |
| 253 // How many milliseconds in the future to start the animation. | 253 * @const |
| 254 this.startInMillis = 0.0; | 254 */ |
| 255 | 255 api.Property = { |
| 256 // Duration of the animation (milliseconds). | 256 'COPYRECT': 0, |
| 257 this.durationMillis = durationMs; | 257 'SIZE': 1, |
| 258 } | 258 'TRANSLATION': 2, |
| 259 | 259 'SCALE': 3, |
| 260 /** | 260 'ROTATION': 4 |
| 261 * Set the animation's final element size. | 261 }; |
| 262 */ | 262 |
| 263 setSize(width, height) { | 263 /** |
| 264 this.property = api.Property.SIZE; | 264 * Enumeration of easing type. |
| 265 this.to.x = width; | 265 * @enum {number} |
| 266 this.to.y = height; | 266 * @const |
| 267 } | 267 */ |
| 268 | 268 api.Easing = { |
| 269 /** | 269 'LINEAR': 0, |
| 270 * Set the animation's final element scale. | 270 'CUBICBEZIER': 1, |
| 271 */ | 271 'EASEIN': 2, |
| 272 setScale(x, y, z) { | 272 'EASEOUT': 3 |
| 273 this.property = api.Property.SCALE; | 273 }; |
| 274 this.to.x = x; | 274 |
| 275 this.to.y = y; | 275 /** |
| 276 this.to.z = z; | 276 * Base animation class. An animation can vary only one object property. |
| 277 } | 277 * @struct |
| 278 | 278 */ |
| 279 /** | 279 api.Animation = class { |
| 280 * Set the animation's final element rotation. | 280 constructor(elementId, durationMs) { |
| 281 */ | 281 /** @private {number} */ |
| 282 setRotation(x, y, z, a) { | 282 this.id = -1; |
| 283 this.property = api.Property.ROTATION; | 283 /** @private {number} */ |
| 284 this.to.x = x; | 284 this.meshId = elementId; |
| 285 this.to.y = y; | 285 /** @private {number} */ |
| 286 this.to.z = z; | 286 this.property = -1; |
| 287 this.to.a = a; | 287 /** @private {Object} */ |
| 288 } | 288 this.to = {}; |
| 289 | 289 /** @private {Object} */ |
| 290 /** | 290 this.easing = {}; |
| 291 * Set the animation's final element translation. | 291 |
| 292 */ | 292 // How many milliseconds in the future to start the animation. |
| 293 setTranslation(x, y, z) { | 293 /** @private {number} */ |
| 294 this.property = api.Property.TRANSLATION; | 294 this.startInMillis = 0.0; |
| 295 this.to.x = x; | 295 |
| 296 this.to.y = y; | 296 // Duration of the animation (milliseconds). |
| 297 this.to.z = z; | 297 /** @private {number} */ |
| 298 } | 298 this.durationMillis = durationMs; |
| 299 }; | 299 |
| 300 | 300 this.easing.type = api.Easing.LINEAR; |
| 301 return { | 301 } |
| 302 sendCommands: sendCommands, | 302 |
| 303 XAnchoring: XAnchoring, | 303 /** |
| 304 YAnchoring: YAnchoring, | 304 * Set the id of the animation. |
| 305 Property: Property, | 305 * @param {number} id |
| 306 Easing: Easing, | 306 */ |
| 307 Command: Command, | 307 setId(id) { |
| 308 Action: Action, | 308 this.id = id; |
| 309 Mode: Mode, | 309 } |
| 310 getContentElementId: getContentElementId, | 310 |
| 311 UiElement: UiElement, | 311 /** |
| 312 UiElementUpdate: UiElementUpdate, | 312 * Set the animation's final element size. |
| 313 Animation: Animation, | 313 * @param {number} width |
| 314 doAction: doAction, | 314 * @param {number} height |
| 315 domLoaded: domLoaded, | 315 */ |
| 316 setUiCssSize: setUiCssSize, | 316 setSize(width, height) { |
| 317 }; | 317 this.property = api.Property.SIZE; |
| 318 })(); | 318 this.to.x = width; |
| 319 this.to.y = height; |
| 320 } |
| 321 |
| 322 /** |
| 323 * Set the animation's final element scale. |
| 324 * @param {number} x |
| 325 * @param {number} y |
| 326 * @param {number} z |
| 327 */ |
| 328 setScale(x, y, z) { |
| 329 this.property = api.Property.SCALE; |
| 330 this.to.x = x; |
| 331 this.to.y = y; |
| 332 this.to.z = z; |
| 333 } |
| 334 |
| 335 /** |
| 336 * Set the animation's final element rotation. |
| 337 * @param {number} x |
| 338 * @param {number} y |
| 339 * @param {number} z |
| 340 * @param {number} a |
| 341 */ |
| 342 setRotation(x, y, z, a) { |
| 343 this.property = api.Property.ROTATION; |
| 344 this.to.x = x; |
| 345 this.to.y = y; |
| 346 this.to.z = z; |
| 347 this.to.a = a; |
| 348 } |
| 349 |
| 350 /** |
| 351 * Set the animation's final element translation. |
| 352 * @param {number} x |
| 353 * @param {number} y |
| 354 * @param {number} z |
| 355 */ |
| 356 setTranslation(x, y, z) { |
| 357 this.property = api.Property.TRANSLATION; |
| 358 this.to.x = x; |
| 359 this.to.y = y; |
| 360 this.to.z = z; |
| 361 } |
| 362 }; |
| OLD | NEW |