forked from Famous/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSizeSystem.js
More file actions
271 lines (251 loc) · 8.85 KB
/
SizeSystem.js
File metadata and controls
271 lines (251 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Famous Industries Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
var PathStore = require('./PathStore');
var Size = require('./Size');
var Dispatch = require('./Dispatch');
var PathUtils = require('./Path');
/**
* The size system is used to calculate size throughout the scene graph.
* It holds size components and operates upon them.
*
* @constructor
*/
function SizeSystem () {
this.pathStore = new PathStore();
}
/**
* Registers a size component to a give path. A size component can be passed as the second argument
* or a default one will be created. Throws if no size component has been added at the parent path.
*
* @method
*
* @param {String} path The path at which to register the size component
* @param {Size | undefined} size The size component to be registered or undefined.
*
* @return {undefined} undefined
*/
SizeSystem.prototype.registerSizeAtPath = function registerSizeAtPath (path, size) {
if (!PathUtils.depth(path)) return this.pathStore.insert(path, size ? size : new Size());
var parent = this.pathStore.get(PathUtils.parent(path));
if (!parent) throw new Error(
'No parent size registered at expected path: ' + PathUtils.parent(path)
);
if (size) size.setParent(parent);
this.pathStore.insert(path, size ? size : new Size(parent));
};
/**
* Removes the size component from the given path. Will throw if no component is at that
* path
*
* @method
*
* @param {String} path The path at which to remove the size.
*
* @return {undefined} undefined
*/
SizeSystem.prototype.deregisterSizeAtPath = function deregisterSizeAtPath(path) {
this.pathStore.remove(path);
};
/**
* Returns the size component stored at a given path. Returns undefined if no
* size component is registered to that path.
*
* @method
*
* @param {String} path The path at which to get the size component.
*
* @return {undefined} undefined
*/
SizeSystem.prototype.get = function get (path) {
return this.pathStore.get(path);
};
/**
* Updates the sizes in the scene graph. Called internally by the famous engine.
*
* @method
*
* @return {undefined} undefined
*/
SizeSystem.prototype.update = function update () {
var sizes = this.pathStore.getItems();
var paths = this.pathStore.getPaths();
var node;
var size;
var i;
var len;
var components;
for (i = 0, len = sizes.length ; i < len ; i++) {
node = Dispatch.getNode(paths[i]);
components = node.getComponents();
if (!node) continue;
size = sizes[i];
if (size.sizeModeChanged) sizeModeChanged(node, components, size);
if (size.absoluteSizeChanged) absoluteSizeChanged(node, components, size);
if (size.proportionalSizeChanged) proportionalSizeChanged(node, components, size);
if (size.differentialSizeChanged) differentialSizeChanged(node, components, size);
if (size.renderSizeChanged) renderSizeChanged(node, components, size);
if (size.fromComponents(components)) sizeChanged(node, components, size);
}
};
// private methods
/**
* Private method to alert the node and components that size mode changed.
*
* @method
* @private
*
* @param {Node} node Node to potentially call sizeModeChanged on
* @param {Array} components a list of the nodes' components
* @param {Size} size the size class for the Node
*
* @return {undefined} undefined
*/
function sizeModeChanged (node, components, size) {
var sizeMode = size.getSizeMode();
var x = sizeMode[0];
var y = sizeMode[1];
var z = sizeMode[2];
if (node.onSizeModeChange) node.onSizeModeChange(x, y, z);
for (var i = 0, len = components.length ; i < len ; i++)
if (components[i] && components[i].onSizeModeChange)
components[i].onSizeModeChange(x, y, z);
size.sizeModeChanged = false;
}
/**
* Private method to alert the node and components that absoluteSize changed.
*
* @method
* @private
*
* @param {Node} node Node to potentially call onAbsoluteSizeChange on
* @param {Array} components a list of the nodes' components
* @param {Size} size the size class for the Node
*
* @return {undefined} undefined
*/
function absoluteSizeChanged (node, components, size) {
var absoluteSize = size.getAbsolute();
var x = absoluteSize[0];
var y = absoluteSize[1];
var z = absoluteSize[2];
if (node.onAbsoluteSizeChange) node.onAbsoluteSizeChange(x, y, z);
for (var i = 0, len = components.length ; i < len ; i++)
if (components[i] && components[i].onAbsoluteSizeChange)
components[i].onAbsoluteSizeChange(x, y, z);
size.absoluteSizeChanged = false;
}
/**
* Private method to alert the node and components that the proportional size changed.
*
* @method
* @private
*
* @param {Node} node Node to potentially call onProportionalSizeChange on
* @param {Array} components a list of the nodes' components
* @param {Size} size the size class for the Node
*
* @return {undefined} undefined
*/
function proportionalSizeChanged (node, components, size) {
var proportionalSize = size.getProportional();
var x = proportionalSize[0];
var y = proportionalSize[1];
var z = proportionalSize[2];
if (node.onProportionalSizeChange) node.onProportionalSizeChange(x, y, z);
for (var i = 0, len = components.length ; i < len ; i++)
if (components[i] && components[i].onProportionalSizeChange)
components[i].onProportionalSizeChange(x, y, z);
size.proportionalSizeChanged = false;
}
/**
* Private method to alert the node and components that differential size changed.
*
* @method
* @private
*
* @param {Node} node Node to potentially call onDifferentialSize on
* @param {Array} components a list of the nodes' components
* @param {Size} size the size class for the Node
*
* @return {undefined} undefined
*/
function differentialSizeChanged (node, components, size) {
var differentialSize = size.getDifferential();
var x = differentialSize[0];
var y = differentialSize[1];
var z = differentialSize[2];
if (node.onDifferentialSizeChange) node.onDifferentialSizeChange(x, y, z);
for (var i = 0, len = components.length ; i < len ; i++)
if (components[i] && components[i].onDifferentialSizeChange)
components[i].onDifferentialSizeChange(x, y, z);
size.differentialSizeChanged = false;
}
/**
* Private method to alert the node and components that render size changed.
*
* @method
* @private
*
* @param {Node} node Node to potentially call onRenderSizeChange on
* @param {Array} components a list of the nodes' components
* @param {Size} size the size class for the Node
*
* @return {undefined} undefined
*/
function renderSizeChanged (node, components, size) {
var renderSize = size.getRenderSize();
var x = renderSize[0];
var y = renderSize[1];
var z = renderSize[2];
if (node.onRenderSizeChange) node.onRenderSizeChange(x, y, z);
for (var i = 0, len = components.length ; i < len ; i++)
if (components[i] && components[i].onRenderSizeChange)
components[i].onRenderSizeChange(x, y, z);
size.renderSizeChanged = false;
}
/**
* Private method to alert the node and components that the size changed.
*
* @method
* @private
*
* @param {Node} node Node to potentially call onSizeChange on
* @param {Array} components a list of the nodes' components
* @param {Size} size the size class for the Node
*
* @return {undefined} undefined
*/
function sizeChanged (node, components, size) {
var finalSize = size.get();
var x = finalSize[0];
var y = finalSize[1];
var z = finalSize[2];
if (node.onSizeChange) node.onSizeChange(x, y, z);
for (var i = 0, len = components.length ; i < len ; i++)
if (components[i] && components[i].onSizeChange)
components[i].onSizeChange(x, y, z);
size.sizeChanged = false;
}
module.exports = new SizeSystem();