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(); |
Dan Beam
2017/01/04 20:29:02
don't invoke Object, just use {}
cjgrant
2017/01/04 21:23:17
Thanks; I'll fix that in a follow-on change.
| |
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 | |
47 constructor() { | |
48 /** @private {number} */ | |
49 this.idIndex = 1; | |
50 /** @private {Array<Object>} */ | |
51 this.commands = []; | |
52 /** @private {!Set<number>} */ | |
53 this.elements = new Set(); | |
54 /** @private {!Object} */ | |
55 this.animations = []; | |
56 } | |
7 | 57 |
8 /** | 58 /** |
9 * The scene class assists in managing element and animations in the scene. | 59 * Flush all queued commands to native. |
10 * It allows scene update commands to be queued in batches, and manages | 60 */ |
11 * allocation of element and animation IDs. | 61 flush() { |
62 api.sendCommands(this.commands); | |
63 this.commands = []; | |
64 } | |
65 | |
66 /** | |
67 * Add a new UiElement to the scene, returning the ID assigned. | |
68 * @param {api.UiElement} element | |
69 */ | |
70 addElement(element) { | |
71 var id = this.idIndex++; | |
72 element.setId(id); | |
73 this.commands.push({ | |
74 'type': api.Command.ADD_ELEMENT, | |
75 'data': element.properties | |
76 }); | |
77 this.elements.add(id); | |
78 return id; | |
79 } | |
80 | |
81 /** | |
82 * Update an existing element, according to a UiElementUpdate object. | |
83 * @param {number} id | |
84 * @param {api.UiElementUpdate} update | |
85 */ | |
86 updateElement(id, update) { | |
87 // To-do: Make sure ID exists. | |
88 update.setId(id); | |
89 this.commands.push({ | |
90 'type': api.Command.UPDATE_ELEMENT, | |
91 'data': update.properties | |
92 }); | |
93 } | |
94 | |
95 /** | |
96 * Remove an element from the scene. | |
97 * @param {number} id | |
98 */ | |
99 removeElement(id) { | |
100 // To-do: Make sure ID exists. | |
101 this.commands.push({ | |
102 'type': api.Command.REMOVE_ELEMENT, | |
103 'data': {'id': id} | |
104 }); | |
105 this.elements.delete(id); | |
106 } | |
107 | |
108 /** | |
109 * Add a new Animation to the scene, returning the ID assigned. | |
110 * @param {api.Animation} animation | |
111 */ | |
112 addAnimation(animation) { | |
113 var id = this.idIndex++; | |
114 animation.setId(id); | |
115 this.commands.push({ | |
116 'type': api.Command.ADD_ANIMATION, | |
117 'data': animation | |
118 }); | |
119 this.animations[id] = animation.meshId; | |
120 return id; | |
121 } | |
122 | |
123 /** | |
124 * Remove an animation from the scene. | |
12 * | 125 * |
13 * Examples: | 126 * Note that animations are flushed when they complete and are not required |
127 * to be removed. Also new animations of the same type will effectively | |
128 * override the original so there is no need to remove in that scenario | |
129 * either. | |
14 * | 130 * |
15 * var scene = new ui.Scene(); | 131 * @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 */ | 132 */ |
44 class Scene { | 133 removeAnimation(id) { |
134 // To-do: Make sure ID exists. | |
135 this.commands.push({ | |
136 'type': api.Command.REMOVE_ANIMATION, | |
137 'data': {'id': id, 'meshId': this.animations[id]} | |
138 }); | |
139 delete this.animations[id]; | |
140 } | |
45 | 141 |
46 constructor() { | 142 /** |
47 this.idIndex = api.getContentElementId() + 1; | 143 * Purge all elements in the scene. |
48 this.commands = []; | 144 */ |
49 this.elements = new Set(); | 145 purge() { |
50 this.animations = {}; | 146 var ids = Object.keys(this.animations); |
147 for (let id_key of ids) { | |
148 var id = parseInt(id_key, 10); | |
149 this.removeAnimation(id); | |
51 } | 150 } |
52 | 151 var ids = this.elements.values(); |
53 /** | 152 for (let id of ids) { |
54 * Flush all queued commands to native. | 153 this.removeElement(id); |
55 */ | |
56 flush() { | |
57 api.sendCommands(this.commands); | |
58 this.commands = []; | |
59 } | 154 } |
60 | 155 this.flush(); |
61 /** | 156 } |
62 * Add a new UiElement to the scene, returning the ID assigned. | 157 }; |
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 |