@@ -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
/**
@@ -103,6 +104,8 @@ class Drawable {
103
104
/** @todo move convex hull functionality, maybe bounds functionality overall, to Skin classes */
104
105
this . _convexHullPoints = null ;
105
106
this . _convexHullDirty = true ;
107
+
108
+ this . _skinWasAltered = this . _skinWasAltered . bind ( this ) ;
106
109
}
107
110
108
111
/**
@@ -141,7 +144,13 @@ class Drawable {
141
144
*/
142
145
set skin ( newSkin ) {
143
146
if ( this . _skin !== newSkin ) {
147
+ if ( this . _skin ) {
148
+ this . _skin . removeListener ( Skin . Events . WasAltered , this . _skinWasAltered ) ;
149
+ }
144
150
this . _skin = newSkin ;
151
+ if ( this . _skin ) {
152
+ this . _skin . addListener ( Skin . Events . WasAltered , this . _skinWasAltered ) ;
153
+ }
145
154
this . _skinWasAltered ( ) ;
146
155
}
147
156
}
@@ -178,55 +187,98 @@ class Drawable {
178
187
}
179
188
180
189
/**
181
- * Update the position, direction, scale, or effect properties of this Drawable .
182
- * @param {object.<string,* > } properties The new property values to set .
190
+ * Update the position if it is different. Marks the transform as dirty .
191
+ * @param {Array.<number > } position A new position .
183
192
*/
184
- updateProperties ( properties ) {
185
- let dirty = false ;
186
- if ( 'position' in properties && (
187
- this . _position [ 0 ] !== properties . position [ 0 ] ||
188
- this . _position [ 1 ] !== properties . position [ 1 ] ) ) {
189
- this . _position [ 0 ] = Math . round ( properties . position [ 0 ] ) ;
190
- this . _position [ 1 ] = Math . round ( properties . position [ 1 ] ) ;
191
- dirty = true ;
192
- }
193
- if ( 'direction' in properties && this . _direction !== properties . direction ) {
194
- this . _direction = properties . direction ;
193
+ updatePosition ( position ) {
194
+ if ( this . _position [ 0 ] !== position [ 0 ] ||
195
+ this . _position [ 1 ] !== position [ 1 ] ) {
196
+ this . _position [ 0 ] = Math . round ( position [ 0 ] ) ;
197
+ this . _position [ 1 ] = Math . round ( position [ 1 ] ) ;
198
+ this . setTransformDirty ( ) ;
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Update the direction if it is different. Marks the transform as dirty.
204
+ * @param {number } direction A new direction.
205
+ */
206
+ updateDirection ( direction ) {
207
+ if ( this . _direction !== direction ) {
208
+ this . _direction = direction ;
195
209
this . _rotationTransformDirty = true ;
196
- dirty = true ;
210
+ this . setTransformDirty ( ) ;
197
211
}
198
- if ( 'scale' in properties && (
199
- this . _scale [ 0 ] !== properties . scale [ 0 ] ||
200
- this . _scale [ 1 ] !== properties . scale [ 1 ] ) ) {
201
- this . _scale [ 0 ] = properties . scale [ 0 ] ;
202
- this . _scale [ 1 ] = properties . scale [ 1 ] ;
212
+ }
213
+
214
+ /**
215
+ * Update the scale if it is different. Marks the transform as dirty.
216
+ * @param {Array.<number> } scale A new scale.
217
+ */
218
+ updateScale ( scale ) {
219
+ if ( this . _scale [ 0 ] !== scale [ 0 ] ||
220
+ this . _scale [ 1 ] !== scale [ 1 ] ) {
221
+ this . _scale [ 0 ] = scale [ 0 ] ;
222
+ this . _scale [ 1 ] = scale [ 1 ] ;
203
223
this . _rotationCenterDirty = true ;
204
224
this . _skinScaleDirty = true ;
205
- dirty = true ;
225
+ this . setTransformDirty ( ) ;
206
226
}
207
- if ( 'visible' in properties ) {
208
- this . _visible = properties . visible ;
227
+ }
228
+
229
+ /**
230
+ * Update visibility if it is different. Marks the convex hull as dirty.
231
+ * @param {boolean } visible A new visibility state.
232
+ */
233
+ updateVisible ( visible ) {
234
+ if ( this . _visible !== visible ) {
235
+ this . _visible = visible ;
209
236
this . setConvexHullDirty ( ) ;
210
237
}
211
- if ( dirty ) {
212
- this . setTransformDirty ( ) ;
238
+ }
239
+
240
+ /**
241
+ * Update an effect. Marks the convex hull as dirty if the effect changes shape.
242
+ * @param {string } effectName The name of the effect.
243
+ * @param {number } rawValue A new effect value.
244
+ */
245
+ updateEffect ( effectName , rawValue ) {
246
+ const effectInfo = ShaderManager . EFFECT_INFO [ effectName ] ;
247
+ if ( rawValue ) {
248
+ this . _effectBits |= effectInfo . mask ;
249
+ } else {
250
+ this . _effectBits &= ~ effectInfo . mask ;
251
+ }
252
+ const converter = effectInfo . converter ;
253
+ this . _uniforms [ effectInfo . uniformName ] = converter ( rawValue ) ;
254
+ if ( effectInfo . shapeChanges ) {
255
+ this . setConvexHullDirty ( ) ;
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Update the position, direction, scale, or effect properties of this Drawable.
261
+ * @deprecated Use specific update* methods instead.
262
+ * @param {object.<string,*> } properties The new property values to set.
263
+ */
264
+ updateProperties ( properties ) {
265
+ if ( 'position' in properties ) {
266
+ this . updatePosition ( properties . position ) ;
267
+ }
268
+ if ( 'direction' in properties ) {
269
+ this . updateDirection ( properties . direction ) ;
270
+ }
271
+ if ( 'scale' in properties ) {
272
+ this . updateScale ( properties . scale ) ;
273
+ }
274
+ if ( 'visible' in properties ) {
275
+ this . updateVisible ( properties . visible ) ;
213
276
}
214
277
const numEffects = ShaderManager . EFFECTS . length ;
215
278
for ( let index = 0 ; index < numEffects ; ++ index ) {
216
279
const effectName = ShaderManager . EFFECTS [ index ] ;
217
280
if ( effectName in properties ) {
218
- const rawValue = properties [ effectName ] ;
219
- const effectInfo = ShaderManager . EFFECT_INFO [ effectName ] ;
220
- if ( rawValue ) {
221
- this . _effectBits |= effectInfo . mask ;
222
- } else {
223
- this . _effectBits &= ~ effectInfo . mask ;
224
- }
225
- const converter = effectInfo . converter ;
226
- this . _uniforms [ effectInfo . uniformName ] = converter ( rawValue ) ;
227
- if ( effectInfo . shapeChanges ) {
228
- this . setConvexHullDirty ( ) ;
229
- }
281
+ this . updateEffect ( effectName , properties [ effectName ] ) ;
230
282
}
231
283
}
232
284
}
@@ -412,29 +464,43 @@ class Drawable {
412
464
413
465
const localPosition = getLocalPosition ( this , vec ) ;
414
466
415
- if ( this . useNearest ) {
467
+ // We're not passing in a scale to useNearest, but that's okay because "touching" queries
468
+ // happen at the "native" size anyway.
469
+ if ( this . useNearest ( ) ) {
416
470
return this . skin . isTouchingNearest ( localPosition ) ;
417
471
}
418
472
return this . skin . isTouchingLinear ( localPosition ) ;
419
473
}
420
474
421
475
/**
422
476
* Should the drawable use NEAREST NEIGHBOR or LINEAR INTERPOLATION mode
477
+ * @param {?Array<Number> } scale Optionally, the screen-space scale of the drawable.
478
+ * @return {boolean } True if the drawable should use nearest-neighbor interpolation.
423
479
*/
424
- get useNearest ( ) {
480
+ useNearest ( scale = this . scale ) {
425
481
// Raster skins (bitmaps) should always prefer nearest neighbor
426
482
if ( this . skin . isRaster ) {
427
483
return true ;
428
484
}
429
485
486
+ // If the effect bits for mosaic, pixelate, whirl, or fisheye are set, use linear
487
+ if ( ( this . _effectBits & (
488
+ ShaderManager . EFFECT_INFO . fisheye . mask |
489
+ ShaderManager . EFFECT_INFO . whirl . mask |
490
+ ShaderManager . EFFECT_INFO . pixelate . mask |
491
+ ShaderManager . EFFECT_INFO . mosaic . mask
492
+ ) ) !== 0 ) {
493
+ return false ;
494
+ }
495
+
430
496
// We can't use nearest neighbor unless we are a multiple of 90 rotation
431
497
if ( this . _direction % 90 !== 0 ) {
432
498
return false ;
433
499
}
434
500
435
501
// If the scale of the skin is very close to 100 (0.99999 variance is okay I guess)
436
- if ( Math . abs ( this . scale [ 0 ] ) > 99 && Math . abs ( this . scale [ 0 ] ) < 101 &&
437
- Math . abs ( this . scale [ 1 ] ) > 99 && Math . abs ( this . scale [ 1 ] ) < 101 ) {
502
+ if ( Math . abs ( scale [ 0 ] ) > 99 && Math . abs ( scale [ 0 ] ) < 101 &&
503
+ Math . abs ( scale [ 1 ] ) > 99 && Math . abs ( scale [ 1 ] ) < 101 ) {
438
504
return true ;
439
505
}
440
506
return false ;
@@ -514,7 +580,6 @@ class Drawable {
514
580
* @return {!Rectangle } Bounds for the Drawable.
515
581
*/
516
582
getFastBounds ( result ) {
517
- this . updateMatrix ( ) ;
518
583
if ( ! this . needsConvexHullPoints ( ) ) {
519
584
return this . getBounds ( result ) ;
520
585
}
@@ -626,9 +691,11 @@ class Drawable {
626
691
return dst ;
627
692
}
628
693
const textColor =
629
- drawable . useNearest ?
630
- drawable . skin . _silhouette . colorAtNearest ( localPosition , dst ) :
631
- drawable . skin . _silhouette . colorAtLinear ( localPosition , dst ) ;
694
+ // commenting out to only use nearest for now
695
+ // drawable.useNearest() ?
696
+ drawable . skin . _silhouette . colorAtNearest ( localPosition , dst ) ;
697
+ // : drawable.skin._silhouette.colorAtLinear(localPosition, dst);
698
+
632
699
return EffectTransform . transformColor ( drawable , textColor , textColor ) ;
633
700
}
634
701
}
0 commit comments