| 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 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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 | |
| 46 constructor() { | 45 constructor() { |
| 47 this.idIndex = api.getContentElementId() + 1; | 46 this.idIndex = api.getContentElementId() + 1; |
| 48 this.commands = []; | 47 this.commands = []; |
| 49 this.elements = new Set(); | 48 this.elements = new Set(); |
| 50 this.animations = {}; | 49 this.animations = {}; |
| 51 } | 50 } |
| 52 | 51 |
| 53 /** | 52 /** |
| 54 * Flush all queued commands to native. | 53 * Flush all queued commands to native. |
| 55 */ | 54 */ |
| 56 flush() { | 55 flush() { |
| 57 api.sendCommands(this.commands); | 56 api.sendCommands(this.commands); |
| 58 this.commands = []; | 57 this.commands = []; |
| 59 } | 58 } |
| 60 | 59 |
| 61 /** | 60 /** |
| 62 * Add a new UiElement to the scene, returning the ID assigned. | 61 * Add a new UiElement to the scene, returning the ID assigned. |
| 63 */ | 62 */ |
| 64 addElement(element) { | 63 addElement(element) { |
| 65 var id = this.idIndex++; | 64 var id = this.idIndex++; |
| 66 element.id = id; | 65 element.id = id; |
| 67 this.commands.push({ | 66 this.commands.push({'type': api.Command.ADD_ELEMENT, 'data': element}); |
| 68 'type': api.Command.ADD_ELEMENT, | |
| 69 'data': element | |
| 70 }); | |
| 71 this.elements.add(id); | 67 this.elements.add(id); |
| 72 return id; | 68 return id; |
| 73 } | 69 } |
| 74 | 70 |
| 75 /** | 71 /** |
| 76 * Update an existing element, according to a UiElementUpdate object. | 72 * Update an existing element, according to a UiElementUpdate object. |
| 77 */ | 73 */ |
| 78 updateElement(id, update) { | 74 updateElement(id, update) { |
| 79 // To-do: Make sure ID exists. | 75 // To-do: Make sure ID exists. |
| 80 update.id = id; | 76 update.id = id; |
| 81 this.commands.push({ | 77 this.commands.push({'type': api.Command.UPDATE_ELEMENT, 'data': update}); |
| 82 'type': api.Command.UPDATE_ELEMENT, | |
| 83 'data': update | |
| 84 }); | |
| 85 } | 78 } |
| 86 | 79 |
| 87 /* | 80 /* |
| 88 * Remove an element from the scene. | 81 * Remove an element from the scene. |
| 89 */ | 82 */ |
| 90 removeElement(id) { | 83 removeElement(id) { |
| 91 // To-do: Make sure ID exists. | 84 // To-do: Make sure ID exists. |
| 92 this.commands.push({ | 85 this.commands.push( |
| 93 'type': api.Command.REMOVE_ELEMENT, | 86 {'type': api.Command.REMOVE_ELEMENT, 'data': {'id': id}}); |
| 94 'data': {'id': id} | |
| 95 }); | |
| 96 this.elements.delete(id); | 87 this.elements.delete(id); |
| 97 } | 88 } |
| 98 | 89 |
| 99 /** | 90 /** |
| 100 * Add a new Animation to the scene, returning the ID assigned. | 91 * Add a new Animation to the scene, returning the ID assigned. |
| 101 */ | 92 */ |
| 102 addAnimation(animation) { | 93 addAnimation(animation) { |
| 103 var id = this.idIndex++; | 94 var id = this.idIndex++; |
| 104 animation.id = id; | 95 animation.id = id; |
| 105 this.commands.push({ | 96 this.commands.push( |
| 106 'type': api.Command.ADD_ANIMATION, | 97 {'type': api.Command.ADD_ANIMATION, 'data': animation}); |
| 107 'data': animation | |
| 108 }); | |
| 109 this.animations[id] = animation.meshId; | 98 this.animations[id] = animation.meshId; |
| 110 return id; | 99 return id; |
| 111 } | 100 } |
| 112 | 101 |
| 113 /* | 102 /* |
| 114 * Remove an animation from the scene. | 103 * Remove an animation from the scene. |
| 115 * | 104 * |
| 116 * Note that animations are flushed when they complete and are not required | 105 * Note that animations are flushed when they complete and are not required |
| 117 * to be removed. Also new animations of the same type will effectively | 106 * to be removed. Also new animations of the same type will effectively |
| 118 * override the original so there is no need to remove in that scenario | 107 * override the original so there is no need to remove in that scenario |
| (...skipping 22 matching lines...) Expand all Loading... |
| 141 this.removeElement(id); | 130 this.removeElement(id); |
| 142 } | 131 } |
| 143 this.flush(); | 132 this.flush(); |
| 144 } | 133 } |
| 145 }; | 134 }; |
| 146 | 135 |
| 147 return { | 136 return { |
| 148 Scene: Scene, | 137 Scene: Scene, |
| 149 }; | 138 }; |
| 150 })(); | 139 })(); |
| OLD | NEW |