@@ -3,6 +3,7 @@ const twgl = require('twgl.js');
3
3
const Rectangle = require ( './Rectangle' ) ;
4
4
const RenderConstants = require ( './RenderConstants' ) ;
5
5
const ShaderManager = require ( './ShaderManager' ) ;
6
+ const Skin = require ( './Skin' ) ;
6
7
const EffectTransform = require ( './EffectTransform' ) ;
7
8
8
9
/**
@@ -100,6 +101,8 @@ class Drawable {
100
101
/** @todo move convex hull functionality, maybe bounds functionality overall, to Skin classes */
101
102
this . _convexHullPoints = null ;
102
103
this . _convexHullDirty = true ;
104
+
105
+ this . _skinWasAltered = this . _skinWasAltered . bind ( this ) ;
103
106
}
104
107
105
108
/**
@@ -138,7 +141,13 @@ class Drawable {
138
141
*/
139
142
set skin ( newSkin ) {
140
143
if ( this . _skin !== newSkin ) {
144
+ if ( this . _skin ) {
145
+ this . _skin . removeListener ( Skin . Events . WasAltered , this . _skinWasAltered ) ;
146
+ }
141
147
this . _skin = newSkin ;
148
+ if ( this . _skin ) {
149
+ this . _skin . addListener ( Skin . Events . WasAltered , this . _skinWasAltered ) ;
150
+ }
142
151
this . _skinWasAltered ( ) ;
143
152
}
144
153
}
@@ -175,55 +184,98 @@ class Drawable {
175
184
}
176
185
177
186
/**
178
- * Update the position, direction, scale, or effect properties of this Drawable .
179
- * @param {object.<string,* > } properties The new property values to set .
187
+ * Update the position if it is different. Marks the transform as dirty .
188
+ * @param {Array.<number > } position A new position .
180
189
*/
181
- updateProperties ( properties ) {
182
- let dirty = false ;
183
- if ( 'position' in properties && (
184
- this . _position [ 0 ] !== properties . position [ 0 ] ||
185
- this . _position [ 1 ] !== properties . position [ 1 ] ) ) {
186
- this . _position [ 0 ] = Math . round ( properties . position [ 0 ] ) ;
187
- this . _position [ 1 ] = Math . round ( properties . position [ 1 ] ) ;
188
- dirty = true ;
189
- }
190
- if ( 'direction' in properties && this . _direction !== properties . direction ) {
191
- this . _direction = properties . direction ;
190
+ updatePosition ( position ) {
191
+ if ( this . _position [ 0 ] !== position [ 0 ] ||
192
+ this . _position [ 1 ] !== position [ 1 ] ) {
193
+ this . _position [ 0 ] = Math . round ( position [ 0 ] ) ;
194
+ this . _position [ 1 ] = Math . round ( position [ 1 ] ) ;
195
+ this . setTransformDirty ( ) ;
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Update the direction if it is different. Marks the transform as dirty.
201
+ * @param {number } direction A new direction.
202
+ */
203
+ updateDirection ( direction ) {
204
+ if ( this . _direction !== direction ) {
205
+ this . _direction = direction ;
192
206
this . _rotationTransformDirty = true ;
193
- dirty = true ;
207
+ this . setTransformDirty ( ) ;
194
208
}
195
- if ( 'scale' in properties && (
196
- this . _scale [ 0 ] !== properties . scale [ 0 ] ||
197
- this . _scale [ 1 ] !== properties . scale [ 1 ] ) ) {
198
- this . _scale [ 0 ] = properties . scale [ 0 ] ;
199
- this . _scale [ 1 ] = properties . scale [ 1 ] ;
209
+ }
210
+
211
+ /**
212
+ * Update the scale if it is different. Marks the transform as dirty.
213
+ * @param {Array.<number> } scale A new scale.
214
+ */
215
+ updateScale ( scale ) {
216
+ if ( this . _scale [ 0 ] !== scale [ 0 ] ||
217
+ this . _scale [ 1 ] !== scale [ 1 ] ) {
218
+ this . _scale [ 0 ] = scale [ 0 ] ;
219
+ this . _scale [ 1 ] = scale [ 1 ] ;
200
220
this . _rotationCenterDirty = true ;
201
221
this . _skinScaleDirty = true ;
202
- dirty = true ;
222
+ this . setTransformDirty ( ) ;
203
223
}
204
- if ( 'visible' in properties ) {
205
- this . _visible = properties . visible ;
224
+ }
225
+
226
+ /**
227
+ * Update visibility if it is different. Marks the convex hull as dirty.
228
+ * @param {boolean } visible A new visibility state.
229
+ */
230
+ updateVisible ( visible ) {
231
+ if ( this . _visible !== visible ) {
232
+ this . _visible = visible ;
206
233
this . setConvexHullDirty ( ) ;
207
234
}
208
- if ( dirty ) {
209
- this . setTransformDirty ( ) ;
235
+ }
236
+
237
+ /**
238
+ * Update an effect. Marks the convex hull as dirty if the effect changes shape.
239
+ * @param {string } effectName The name of the effect.
240
+ * @param {number } rawValue A new effect value.
241
+ */
242
+ updateEffect ( effectName , rawValue ) {
243
+ const effectInfo = ShaderManager . EFFECT_INFO [ effectName ] ;
244
+ if ( rawValue ) {
245
+ this . _effectBits |= effectInfo . mask ;
246
+ } else {
247
+ this . _effectBits &= ~ effectInfo . mask ;
248
+ }
249
+ const converter = effectInfo . converter ;
250
+ this . _uniforms [ effectInfo . uniformName ] = converter ( rawValue ) ;
251
+ if ( effectInfo . shapeChanges ) {
252
+ this . setConvexHullDirty ( ) ;
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Update the position, direction, scale, or effect properties of this Drawable.
258
+ * @deprecated Use specific update* methods instead.
259
+ * @param {object.<string,*> } properties The new property values to set.
260
+ */
261
+ updateProperties ( properties ) {
262
+ if ( 'position' in properties ) {
263
+ this . updatePosition ( properties . position ) ;
264
+ }
265
+ if ( 'direction' in properties ) {
266
+ this . updateDirection ( properties . direction ) ;
267
+ }
268
+ if ( 'scale' in properties ) {
269
+ this . updateScale ( properties . scale ) ;
270
+ }
271
+ if ( 'visible' in properties ) {
272
+ this . updateVisible ( properties . visible ) ;
210
273
}
211
274
const numEffects = ShaderManager . EFFECTS . length ;
212
275
for ( let index = 0 ; index < numEffects ; ++ index ) {
213
276
const effectName = ShaderManager . EFFECTS [ index ] ;
214
277
if ( effectName in properties ) {
215
- const rawValue = properties [ effectName ] ;
216
- const effectInfo = ShaderManager . EFFECT_INFO [ effectName ] ;
217
- if ( rawValue ) {
218
- this . _effectBits |= effectInfo . mask ;
219
- } else {
220
- this . _effectBits &= ~ effectInfo . mask ;
221
- }
222
- const converter = effectInfo . converter ;
223
- this . _uniforms [ effectInfo . uniformName ] = converter ( rawValue ) ;
224
- if ( effectInfo . shapeChanges ) {
225
- this . setConvexHullDirty ( ) ;
226
- }
278
+ this . updateEffect ( effectName , properties [ effectName ] ) ;
227
279
}
228
280
}
229
281
}
@@ -424,6 +476,16 @@ class Drawable {
424
476
return true ;
425
477
}
426
478
479
+ // If the effect bits for mosaic, pixelate, whirl, or fisheye are set, use linear
480
+ if ( ( this . _effectBits & (
481
+ ShaderManager . EFFECT_INFO . fisheye . mask |
482
+ ShaderManager . EFFECT_INFO . whirl . mask |
483
+ ShaderManager . EFFECT_INFO . pixelate . mask |
484
+ ShaderManager . EFFECT_INFO . mosaic . mask
485
+ ) ) !== 0 ) {
486
+ return false ;
487
+ }
488
+
427
489
// We can't use nearest neighbor unless we are a multiple of 90 rotation
428
490
if ( this . _direction % 90 !== 0 ) {
429
491
return false ;
@@ -511,7 +573,6 @@ class Drawable {
511
573
* @return {!Rectangle } Bounds for the Drawable.
512
574
*/
513
575
getFastBounds ( result ) {
514
- this . updateMatrix ( ) ;
515
576
if ( ! this . needsConvexHullPoints ( ) ) {
516
577
return this . getBounds ( result ) ;
517
578
}
0 commit comments