| 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 ui = (function() { | 5 var ui = (function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * The scene class assists in managing element and animations in the scene. | 9 * The scene class assists in managing element and animations in the scene. |
| 10 * It allows scene update commands to be queued in batches, and manages | 10 * It allows scene update commands to be queued in batches, and manages |
| 11 * allocation of element and animation IDs. | 11 * allocation of element and animation IDs. |
| 12 * | 12 * |
| 13 * Examples: | 13 * Examples: |
| 14 * | 14 * |
| 15 * var scene = new ui.Scene(); | 15 * var scene = new ui.Scene(); |
| 16 * | 16 * |
| 17 * // Add an element. | 17 * // Add an element. |
| 18 * var el = new api.UiElement(100, 200, 50, 50); | 18 * var el = new api.UiElement(100, 200, 50, 50); |
| 19 * el.setSize(buttonWidth, buttonHeight); | 19 * el.setSize(buttonWidth, buttonHeight); |
| 20 * | 20 * |
| 21 * // Anchor it to the bottom of the content quad. | 21 * // Anchor it to the bottom of the content quad. |
| 22 * el.setParentId(api.getContentElementId()); | 22 * el.setParentId(contentQuadId); |
| 23 * el.setAnchoring(api.XAnchoring.XNONE, api.YAnchoring.YBOTTOM); | 23 * el.setAnchoring(api.XAnchoring.XNONE, api.YAnchoring.YBOTTOM); |
| 24 * | 24 * |
| 25 * // Place it just below the content quad edge. | 25 * // Place it just below the content quad edge. |
| 26 * el.setTranslation(0, -0.2, 0.0); | 26 * el.setTranslation(0, -0.2, 0.0); |
| 27 * | 27 * |
| 28 * // Add it to the scene. | 28 * // Add it to the scene. |
| 29 * var buttonId = scene.addElement(el); | 29 * var buttonId = scene.addElement(el); |
| 30 * scene.flush(); | 30 * scene.flush(); |
| 31 * | 31 * |
| 32 * // Make the button twice as big. | 32 * // Make the button twice as big. |
| 33 * var update = new api.UiElementUpdate(); | 33 * var update = new api.UiElementUpdate(); |
| 34 * update.setSize(bunttonWidth * 2, buttonHeight * 2); | 34 * update.setSize(bunttonWidth * 2, buttonHeight * 2); |
| 35 * scene.updateElement(buttonId, update); | 35 * scene.updateElement(buttonId, update); |
| 36 * scene.flush(); | 36 * scene.flush(); |
| 37 * | 37 * |
| 38 * // Animate the button size back to its original size, over 250 ms. | 38 * // Animate the button size back to its original size, over 250 ms. |
| 39 * var resize = new api.Animation(buttonId, 250); | 39 * var resize = new api.Animation(buttonId, 250); |
| 40 * resize.setSize(buttonWidth, buttonHeight); | 40 * resize.setSize(buttonWidth, buttonHeight); |
| 41 * scene.addAnimation(resize); | 41 * scene.addAnimation(resize); |
| 42 * scene.flush(); | 42 * scene.flush(); |
| 43 */ | 43 */ |
| 44 class Scene { | 44 class Scene { |
| 45 | 45 |
| 46 constructor() { | 46 constructor() { |
| 47 this.idIndex = api.getContentElementId() + 1; | 47 this.idIndex = 1; |
| 48 this.commands = []; | 48 this.commands = []; |
| 49 this.elements = new Set(); | 49 this.elements = new Set(); |
| 50 this.animations = {}; | 50 this.animations = {}; |
| 51 } | 51 } |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Flush all queued commands to native. | 54 * Flush all queued commands to native. |
| 55 */ | 55 */ |
| 56 flush() { | 56 flush() { |
| 57 api.sendCommands(this.commands); | 57 api.sendCommands(this.commands); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 73 } | 73 } |
| 74 | 74 |
| 75 /** | 75 /** |
| 76 * Update an existing element, according to a UiElementUpdate object. | 76 * Update an existing element, according to a UiElementUpdate object. |
| 77 */ | 77 */ |
| 78 updateElement(id, update) { | 78 updateElement(id, update) { |
| 79 // To-do: Make sure ID exists. | 79 // To-do: Make sure ID exists. |
| 80 update.id = id; | 80 update.id = id; |
| 81 this.commands.push({ | 81 this.commands.push({ |
| 82 'type': api.Command.UPDATE_ELEMENT, | 82 'type': api.Command.UPDATE_ELEMENT, |
| 83 'data': update | 83 'data': update.properties |
| 84 }); | 84 }); |
| 85 } | 85 } |
| 86 | 86 |
| 87 /* | 87 /* |
| 88 * Remove an element from the scene. | 88 * Remove an element from the scene. |
| 89 */ | 89 */ |
| 90 removeElement(id) { | 90 removeElement(id) { |
| 91 // To-do: Make sure ID exists. | 91 // To-do: Make sure ID exists. |
| 92 this.commands.push({ | 92 this.commands.push({ |
| 93 'type': api.Command.REMOVE_ELEMENT, | 93 'type': api.Command.REMOVE_ELEMENT, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 }); | 126 }); |
| 127 delete this.animations[id]; | 127 delete this.animations[id]; |
| 128 } | 128 } |
| 129 | 129 |
| 130 /* | 130 /* |
| 131 * Purge all elements in the scene. | 131 * Purge all elements in the scene. |
| 132 */ | 132 */ |
| 133 purge() { | 133 purge() { |
| 134 var ids = Object.keys(this.animations); | 134 var ids = Object.keys(this.animations); |
| 135 for (let id_key of ids) { | 135 for (let id_key of ids) { |
| 136 var id = parseInt(id_key); | 136 var id = parseInt(id_key, 10); |
| 137 this.removeAnimation(id); | 137 this.removeAnimation(id); |
| 138 } | 138 } |
| 139 var ids = this.elements.values(); | 139 var ids = this.elements.values(); |
| 140 for (let id of ids) { | 140 for (let id of ids) { |
| 141 this.removeElement(id); | 141 this.removeElement(id); |
| 142 } | 142 } |
| 143 this.flush(); | 143 this.flush(); |
| 144 } | 144 } |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 return { | 147 return { |
| 148 Scene: Scene, | 148 Scene: Scene, |
| 149 }; | 149 }; |
| 150 })(); | 150 })(); |
| OLD | NEW |