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 = new Object(); |
6 'use strict'; | 6 |
| 7 /** |
| 8 * The scene class assists in managing element and animations in the scene. |
| 9 * It allows scene update commands to be queued in batches, and manages |
| 10 * allocation of element and animation IDs. |
| 11 * |
| 12 * Examples: |
| 13 * |
| 14 * var scene = new ui.Scene(); |
| 15 * |
| 16 * // Add an element. |
| 17 * var el = new api.UiElement(100, 200, 50, 50); |
| 18 * el.setSize(buttonWidth, buttonHeight); |
| 19 * |
| 20 * // Anchor it to the bottom of the content quad. |
| 21 * el.setParentId(contentQuadId); |
| 22 * el.setAnchoring(api.XAnchoring.XNONE, api.YAnchoring.YBOTTOM); |
| 23 * |
| 24 * // Place it just below the content quad edge. |
| 25 * el.setTranslation(0, -0.2, 0.0); |
| 26 * |
| 27 * // Add it to the scene. |
| 28 * var buttonId = scene.addElement(el); |
| 29 * scene.flush(); |
| 30 * |
| 31 * // Make the button twice as big. |
| 32 * var update = new api.UiElementUpdate(); |
| 33 * update.setSize(bunttonWidth * 2, buttonHeight * 2); |
| 34 * scene.updateElement(buttonId, update); |
| 35 * scene.flush(); |
| 36 * |
| 37 * // Animate the button size back to its original size, over 250 ms. |
| 38 * var resize = new api.Animation(buttonId, 250); |
| 39 * resize.setSize(buttonWidth, buttonHeight); |
| 40 * scene.addAnimation(resize); |
| 41 * scene.flush(); |
| 42 * |
| 43 * @struct |
| 44 */ |
| 45 ui.Scene = class { |
| 46 constructor() { |
| 47 /** @private {number} */ |
| 48 this.idIndex = 1; |
| 49 /** @private {Array<Object>} */ |
| 50 this.commands = []; |
| 51 /** @private {!Set<number>} */ |
| 52 this.elements = new Set(); |
| 53 /** @private {!Object} */ |
| 54 this.animations = []; |
| 55 } |
7 | 56 |
8 /** | 57 /** |
9 * The scene class assists in managing element and animations in the scene. | 58 * Flush all queued commands to native. |
10 * It allows scene update commands to be queued in batches, and manages | 59 */ |
11 * allocation of element and animation IDs. | 60 flush() { |
| 61 api.sendCommands(this.commands); |
| 62 this.commands = []; |
| 63 } |
| 64 |
| 65 /** |
| 66 * Add a new UiElement to the scene, returning the ID assigned. |
| 67 * @param {api.UiElement} element |
| 68 */ |
| 69 addElement(element) { |
| 70 var id = this.idIndex++; |
| 71 element.setId(id); |
| 72 this.commands.push( |
| 73 {'type': api.Command.ADD_ELEMENT, 'data': element.properties}); |
| 74 this.elements.add(id); |
| 75 return id; |
| 76 } |
| 77 |
| 78 /** |
| 79 * Update an existing element, according to a UiElementUpdate object. |
| 80 * @param {number} id |
| 81 * @param {api.UiElementUpdate} update |
| 82 */ |
| 83 updateElement(id, update) { |
| 84 // To-do: Make sure ID exists. |
| 85 update.setId(id); |
| 86 this.commands.push( |
| 87 {'type': api.Command.UPDATE_ELEMENT, 'data': update.properties}); |
| 88 } |
| 89 |
| 90 /** |
| 91 * Remove an element from the scene. |
| 92 * @param {number} id |
| 93 */ |
| 94 removeElement(id) { |
| 95 // To-do: Make sure ID exists. |
| 96 this.commands.push( |
| 97 {'type': api.Command.REMOVE_ELEMENT, 'data': {'id': id}}); |
| 98 this.elements.delete(id); |
| 99 } |
| 100 |
| 101 /** |
| 102 * Add a new Animation to the scene, returning the ID assigned. |
| 103 * @param {api.Animation} animation |
| 104 */ |
| 105 addAnimation(animation) { |
| 106 var id = this.idIndex++; |
| 107 animation.setId(id); |
| 108 this.commands.push({'type': api.Command.ADD_ANIMATION, 'data': animation}); |
| 109 this.animations[id] = animation.meshId; |
| 110 return id; |
| 111 } |
| 112 |
| 113 /** |
| 114 * Remove an animation from the scene. |
12 * | 115 * |
13 * Examples: | 116 * 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 |
| 118 * override the original so there is no need to remove in that scenario |
| 119 * either. |
14 * | 120 * |
15 * var scene = new ui.Scene(); | 121 * @param {number} id |
16 * | |
17 * // Add an element. | |
18 * var el = new api.UiElement(100, 200, 50, 50); | |
19 * el.setSize(buttonWidth, buttonHeight); | |
20 * | |
21 * // Anchor it to the bottom of the content quad. | |
22 * el.setParentId(api.getContentElementId()); | |
23 * el.setAnchoring(api.XAnchoring.XNONE, api.YAnchoring.YBOTTOM); | |
24 * | |
25 * // Place it just below the content quad edge. | |
26 * el.setTranslation(0, -0.2, 0.0); | |
27 * | |
28 * // Add it to the scene. | |
29 * var buttonId = scene.addElement(el); | |
30 * scene.flush(); | |
31 * | |
32 * // Make the button twice as big. | |
33 * var update = new api.UiElementUpdate(); | |
34 * update.setSize(bunttonWidth * 2, buttonHeight * 2); | |
35 * scene.updateElement(buttonId, update); | |
36 * scene.flush(); | |
37 * | |
38 * // Animate the button size back to its original size, over 250 ms. | |
39 * var resize = new api.Animation(buttonId, 250); | |
40 * resize.setSize(buttonWidth, buttonHeight); | |
41 * scene.addAnimation(resize); | |
42 * scene.flush(); | |
43 */ | 122 */ |
44 class Scene { | 123 removeAnimation(id) { |
| 124 // To-do: Make sure ID exists. |
| 125 this.commands.push({ |
| 126 'type': api.Command.REMOVE_ANIMATION, |
| 127 'data': {'id': id, 'meshId': this.animations[id]} |
| 128 }); |
| 129 delete this.animations[id]; |
| 130 } |
45 | 131 |
46 constructor() { | 132 /** |
47 this.idIndex = api.getContentElementId() + 1; | 133 * Purge all elements in the scene. |
48 this.commands = []; | 134 */ |
49 this.elements = new Set(); | 135 purge() { |
50 this.animations = {}; | 136 var ids = Object.keys(this.animations); |
| 137 for (let id_key of ids) { |
| 138 var id = parseInt(id_key, 10); |
| 139 this.removeAnimation(id); |
51 } | 140 } |
52 | 141 var ids = this.elements.values(); |
53 /** | 142 for (let id of ids) { |
54 * Flush all queued commands to native. | 143 this.removeElement(id); |
55 */ | |
56 flush() { | |
57 api.sendCommands(this.commands); | |
58 this.commands = []; | |
59 } | 144 } |
60 | 145 this.flush(); |
61 /** | 146 } |
62 * Add a new UiElement to the scene, returning the ID assigned. | 147 }; |
63 */ | |
64 addElement(element) { | |
65 var id = this.idIndex++; | |
66 element.id = id; | |
67 this.commands.push({ | |
68 'type': api.Command.ADD_ELEMENT, | |
69 'data': element | |
70 }); | |
71 this.elements.add(id); | |
72 return id; | |
73 } | |
74 | |
75 /** | |
76 * Update an existing element, according to a UiElementUpdate object. | |
77 */ | |
78 updateElement(id, update) { | |
79 // To-do: Make sure ID exists. | |
80 update.id = id; | |
81 this.commands.push({ | |
82 'type': api.Command.UPDATE_ELEMENT, | |
83 'data': update | |
84 }); | |
85 } | |
86 | |
87 /* | |
88 * Remove an element from the scene. | |
89 */ | |
90 removeElement(id) { | |
91 // To-do: Make sure ID exists. | |
92 this.commands.push({ | |
93 'type': api.Command.REMOVE_ELEMENT, | |
94 'data': {'id': id} | |
95 }); | |
96 this.elements.delete(id); | |
97 } | |
98 | |
99 /** | |
100 * Add a new Animation to the scene, returning the ID assigned. | |
101 */ | |
102 addAnimation(animation) { | |
103 var id = this.idIndex++; | |
104 animation.id = id; | |
105 this.commands.push({ | |
106 'type': api.Command.ADD_ANIMATION, | |
107 'data': animation | |
108 }); | |
109 this.animations[id] = animation.meshId; | |
110 return id; | |
111 } | |
112 | |
113 /* | |
114 * Remove an animation from the scene. | |
115 * | |
116 * 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 | |
118 * override the original so there is no need to remove in that scenario | |
119 * either. | |
120 */ | |
121 removeAnimation(id) { | |
122 // To-do: Make sure ID exists. | |
123 this.commands.push({ | |
124 'type': api.Command.REMOVE_ANIMATION, | |
125 'data': {'id': id, 'meshId': this.animations[id]} | |
126 }); | |
127 delete this.animations[id]; | |
128 } | |
129 | |
130 /* | |
131 * Purge all elements in the scene. | |
132 */ | |
133 purge() { | |
134 var ids = Object.keys(this.animations); | |
135 for (let id_key of ids) { | |
136 var id = parseInt(id_key); | |
137 this.removeAnimation(id); | |
138 } | |
139 var ids = this.elements.values(); | |
140 for (let id of ids) { | |
141 this.removeElement(id); | |
142 } | |
143 this.flush(); | |
144 } | |
145 }; | |
146 | |
147 return { | |
148 Scene: Scene, | |
149 }; | |
150 })(); | |
OLD | NEW |