浏览代码

PropertyBinding: Convert to es6 class (#20016)

* PropertyBinding.Composite: Convert to es6 class

* PropertyBinding: Convert to es6 class

* PropertyBinding: Clean up state initialization

* PropertyBinding: Avoid Object.assign
Ian Purvis 4 年之前
父节点
当前提交
2fd49ac747
共有 1 个文件被更改,包括 174 次插入155 次删除
  1. 174 155
      src/animation/PropertyBinding.js

+ 174 - 155
src/animation/PropertyBinding.js

@@ -34,18 +34,18 @@ const _trackRe = new RegExp( ''
 
 
 const _supportedObjectNames = [ 'material', 'materials', 'bones' ];
 const _supportedObjectNames = [ 'material', 'materials', 'bones' ];
 
 
-function Composite( targetGroup, path, optionalParsedPath ) {
+class Composite {
 
 
-	const parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path );
+	constructor( targetGroup, path, optionalParsedPath ) {
 
 
-	this._targetGroup = targetGroup;
-	this._bindings = targetGroup.subscribe_( path, parsedPath );
+		const parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path );
 
 
-}
+		this._targetGroup = targetGroup;
+		this._bindings = targetGroup.subscribe_( path, parsedPath );
 
 
-Object.assign( Composite.prototype, {
+	}
 
 
-	getValue: function ( array, offset ) {
+	getValue( array, offset ) {
 
 
 		this.bind(); // bind all binding
 		this.bind(); // bind all binding
 
 
@@ -55,9 +55,9 @@ Object.assign( Composite.prototype, {
 		// and only call .getValue on the first
 		// and only call .getValue on the first
 		if ( binding !== undefined ) binding.getValue( array, offset );
 		if ( binding !== undefined ) binding.getValue( array, offset );
 
 
-	},
+	}
 
 
-	setValue: function ( array, offset ) {
+	setValue( array, offset ) {
 
 
 		const bindings = this._bindings;
 		const bindings = this._bindings;
 
 
@@ -67,9 +67,9 @@ Object.assign( Composite.prototype, {
 
 
 		}
 		}
 
 
-	},
+	}
 
 
-	bind: function () {
+	bind() {
 
 
 		const bindings = this._bindings;
 		const bindings = this._bindings;
 
 
@@ -79,9 +79,9 @@ Object.assign( Composite.prototype, {
 
 
 		}
 		}
 
 
-	},
+	}
 
 
-	unbind: function () {
+	unbind() {
 
 
 		const bindings = this._bindings;
 		const bindings = this._bindings;
 
 
@@ -93,25 +93,32 @@ Object.assign( Composite.prototype, {
 
 
 	}
 	}
 
 
-} );
+}
 
 
+// Note: This class uses a State pattern on a per-method basis:
+// 'bind' sets 'this.getValue' / 'setValue' and shadows the
+// prototype version of these methods with one that represents
+// the bound state. When the property is not found, the methods
+// become no-ops.
+class PropertyBinding {
 
 
-function PropertyBinding( rootNode, path, parsedPath ) {
+	constructor( rootNode, path, parsedPath ) {
 
 
-	this.path = path;
-	this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path );
+		this.path = path;
+		this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path );
 
 
-	this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName ) || rootNode;
+		this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName ) || rootNode;
 
 
-	this.rootNode = rootNode;
+		this.rootNode = rootNode;
 
 
-}
+		// initial state of these methods that calls 'bind'
+		this.getValue = this._getValue_unbound;
+		this.setValue = this._setValue_unbound;
 
 
-Object.assign( PropertyBinding, {
+	}
 
 
-	Composite: Composite,
 
 
-	create: function ( root, path, parsedPath ) {
+	static create( root, path, parsedPath ) {
 
 
 		if ( ! ( root && root.isAnimationObjectGroup ) ) {
 		if ( ! ( root && root.isAnimationObjectGroup ) ) {
 
 
@@ -123,7 +130,7 @@ Object.assign( PropertyBinding, {
 
 
 		}
 		}
 
 
-	},
+	}
 
 
 	/**
 	/**
 	 * Replaces spaces with underscores and removes unsupported characters from
 	 * Replaces spaces with underscores and removes unsupported characters from
@@ -132,13 +139,13 @@ Object.assign( PropertyBinding, {
 	 * @param {string} name Node name to be sanitized.
 	 * @param {string} name Node name to be sanitized.
 	 * @return {string}
 	 * @return {string}
 	 */
 	 */
-	sanitizeNodeName: function ( name ) {
+	static sanitizeNodeName( name ) {
 
 
 		return name.replace( /\s/g, '_' ).replace( _reservedRe, '' );
 		return name.replace( /\s/g, '_' ).replace( _reservedRe, '' );
 
 
-	},
+	}
 
 
-	parseTrackName: function ( trackName ) {
+	static parseTrackName( trackName ) {
 
 
 		const matches = _trackRe.exec( trackName );
 		const matches = _trackRe.exec( trackName );
 
 
@@ -184,9 +191,9 @@ Object.assign( PropertyBinding, {
 
 
 		return results;
 		return results;
 
 
-	},
+	}
 
 
-	findNode: function ( root, nodeName ) {
+	static findNode( root, nodeName ) {
 
 
 		if ( ! nodeName || nodeName === '' || nodeName === '.' || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) {
 		if ( ! nodeName || nodeName === '' || nodeName === '.' || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) {
 
 
@@ -246,204 +253,166 @@ Object.assign( PropertyBinding, {
 
 
 	}
 	}
 
 
-} );
-
-Object.assign( PropertyBinding.prototype, { // prototype, continued
-
 	// these are used to "bind" a nonexistent property
 	// these are used to "bind" a nonexistent property
-	_getValue_unavailable: function () {},
-	_setValue_unavailable: function () {},
-
-	BindingType: {
-		Direct: 0,
-		EntireArray: 1,
-		ArrayElement: 2,
-		HasFromToArray: 3
-	},
-
-	Versioning: {
-		None: 0,
-		NeedsUpdate: 1,
-		MatrixWorldNeedsUpdate: 2
-	},
-
-	GetterByBindingType: [
+	_getValue_unavailable() {}
+	_setValue_unavailable() {}
 
 
-		function getValue_direct( buffer, offset ) {
+	// Getters
 
 
-			buffer[ offset ] = this.node[ this.propertyName ];
+	_getValue_direct( buffer, offset ) {
 
 
-		},
+		buffer[ offset ] = this.node[ this.propertyName ];
 
 
-		function getValue_array( buffer, offset ) {
-
-			const source = this.resolvedProperty;
-
-			for ( let i = 0, n = source.length; i !== n; ++ i ) {
-
-				buffer[ offset ++ ] = source[ i ];
-
-			}
-
-		},
-
-		function getValue_arrayElement( buffer, offset ) {
+	}
 
 
-			buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ];
+	_getValue_array( buffer, offset ) {
 
 
-		},
+		const source = this.resolvedProperty;
 
 
-		function getValue_toArray( buffer, offset ) {
+		for ( let i = 0, n = source.length; i !== n; ++ i ) {
 
 
-			this.resolvedProperty.toArray( buffer, offset );
+			buffer[ offset ++ ] = source[ i ];
 
 
 		}
 		}
 
 
-	],
+	}
 
 
-	SetterByBindingTypeAndVersioning: [
+	_getValue_arrayElement( buffer, offset ) {
 
 
-		[
-			// Direct
+		buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ];
 
 
-			function setValue_direct( buffer, offset ) {
+	}
 
 
-				this.targetObject[ this.propertyName ] = buffer[ offset ];
+	_getValue_toArray( buffer, offset ) {
 
 
-			},
+		this.resolvedProperty.toArray( buffer, offset );
 
 
-			function setValue_direct_setNeedsUpdate( buffer, offset ) {
+	}
 
 
-				this.targetObject[ this.propertyName ] = buffer[ offset ];
-				this.targetObject.needsUpdate = true;
+	// Direct
 
 
-			},
+	_setValue_direct( buffer, offset ) {
 
 
-			function setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) {
+		this.targetObject[ this.propertyName ] = buffer[ offset ];
 
 
-				this.targetObject[ this.propertyName ] = buffer[ offset ];
-				this.targetObject.matrixWorldNeedsUpdate = true;
+	}
 
 
-			}
+	_setValue_direct_setNeedsUpdate( buffer, offset ) {
 
 
-		], [
+		this.targetObject[ this.propertyName ] = buffer[ offset ];
+		this.targetObject.needsUpdate = true;
 
 
-			// EntireArray
+	}
 
 
-			function setValue_array( buffer, offset ) {
+	_setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
 
-				const dest = this.resolvedProperty;
+		this.targetObject[ this.propertyName ] = buffer[ offset ];
+		this.targetObject.matrixWorldNeedsUpdate = true;
 
 
-				for ( let i = 0, n = dest.length; i !== n; ++ i ) {
+	}
 
 
-					dest[ i ] = buffer[ offset ++ ];
+	// EntireArray
 
 
-				}
+	_setValue_array( buffer, offset ) {
 
 
-			},
+		const dest = this.resolvedProperty;
 
 
-			function setValue_array_setNeedsUpdate( buffer, offset ) {
+		for ( let i = 0, n = dest.length; i !== n; ++ i ) {
 
 
-				const dest = this.resolvedProperty;
+			dest[ i ] = buffer[ offset ++ ];
 
 
-				for ( let i = 0, n = dest.length; i !== n; ++ i ) {
+		}
 
 
-					dest[ i ] = buffer[ offset ++ ];
+	}
 
 
-				}
+	_setValue_array_setNeedsUpdate( buffer, offset ) {
 
 
-				this.targetObject.needsUpdate = true;
+		const dest = this.resolvedProperty;
 
 
-			},
+		for ( let i = 0, n = dest.length; i !== n; ++ i ) {
 
 
-			function setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) {
+			dest[ i ] = buffer[ offset ++ ];
 
 
-				const dest = this.resolvedProperty;
+		}
 
 
-				for ( let i = 0, n = dest.length; i !== n; ++ i ) {
+		this.targetObject.needsUpdate = true;
 
 
-					dest[ i ] = buffer[ offset ++ ];
+	}
 
 
-				}
+	_setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
 
-				this.targetObject.matrixWorldNeedsUpdate = true;
+		const dest = this.resolvedProperty;
 
 
-			}
+		for ( let i = 0, n = dest.length; i !== n; ++ i ) {
 
 
-		], [
+			dest[ i ] = buffer[ offset ++ ];
 
 
-			// ArrayElement
+		}
 
 
-			function setValue_arrayElement( buffer, offset ) {
+		this.targetObject.matrixWorldNeedsUpdate = true;
 
 
-				this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
+	}
 
 
-			},
+	// ArrayElement
 
 
-			function setValue_arrayElement_setNeedsUpdate( buffer, offset ) {
+	_setValue_arrayElement( buffer, offset ) {
 
 
-				this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
-				this.targetObject.needsUpdate = true;
+		this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
 
 
-			},
+	}
 
 
-			function setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) {
+	_setValue_arrayElement_setNeedsUpdate( buffer, offset ) {
 
 
-				this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
-				this.targetObject.matrixWorldNeedsUpdate = true;
+		this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
+		this.targetObject.needsUpdate = true;
 
 
-			}
+	}
 
 
-		], [
+	_setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
 
-			// HasToFromArray
+		this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
+		this.targetObject.matrixWorldNeedsUpdate = true;
 
 
-			function setValue_fromArray( buffer, offset ) {
+	}
 
 
-				this.resolvedProperty.fromArray( buffer, offset );
+	// HasToFromArray
 
 
-			},
+	_setValue_fromArray( buffer, offset ) {
 
 
-			function setValue_fromArray_setNeedsUpdate( buffer, offset ) {
+		this.resolvedProperty.fromArray( buffer, offset );
 
 
-				this.resolvedProperty.fromArray( buffer, offset );
-				this.targetObject.needsUpdate = true;
+	}
 
 
-			},
+	_setValue_fromArray_setNeedsUpdate( buffer, offset ) {
 
 
-			function setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) {
+		this.resolvedProperty.fromArray( buffer, offset );
+		this.targetObject.needsUpdate = true;
 
 
-				this.resolvedProperty.fromArray( buffer, offset );
-				this.targetObject.matrixWorldNeedsUpdate = true;
+	}
 
 
-			}
+	_setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
 
-		]
+		this.resolvedProperty.fromArray( buffer, offset );
+		this.targetObject.matrixWorldNeedsUpdate = true;
 
 
-	],
+	}
 
 
-	getValue: function getValue_unbound( targetArray, offset ) {
+	_getValue_unbound( targetArray, offset ) {
 
 
 		this.bind();
 		this.bind();
 		this.getValue( targetArray, offset );
 		this.getValue( targetArray, offset );
 
 
-		// Note: This class uses a State pattern on a per-method basis:
-		// 'bind' sets 'this.getValue' / 'setValue' and shadows the
-		// prototype version of these methods with one that represents
-		// the bound state. When the property is not found, the methods
-		// become no-ops.
-
-	},
+	}
 
 
-	setValue: function getValue_unbound( sourceArray, offset ) {
+	_setValue_unbound( sourceArray, offset ) {
 
 
 		this.bind();
 		this.bind();
 		this.setValue( sourceArray, offset );
 		this.setValue( sourceArray, offset );
 
 
-	},
+	}
 
 
 	// create getter / setter pair for a property in the scene graph
 	// create getter / setter pair for a property in the scene graph
-	bind: function () {
+	bind() {
 
 
 		let targetObject = this.node;
 		let targetObject = this.node;
 		const parsedPath = this.parsedPath;
 		const parsedPath = this.parsedPath;
@@ -657,9 +626,9 @@ Object.assign( PropertyBinding.prototype, { // prototype, continued
 		this.getValue = this.GetterByBindingType[ bindingType ];
 		this.getValue = this.GetterByBindingType[ bindingType ];
 		this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ];
 		this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ];
 
 
-	},
+	}
 
 
-	unbind: function () {
+	unbind() {
 
 
 		this.node = null;
 		this.node = null;
 
 
@@ -670,15 +639,65 @@ Object.assign( PropertyBinding.prototype, { // prototype, continued
 
 
 	}
 	}
 
 
-} );
+}
+
+PropertyBinding.Composite = Composite;
+
+PropertyBinding.prototype.BindingType = {
+	Direct: 0,
+	EntireArray: 1,
+	ArrayElement: 2,
+	HasFromToArray: 3
+};
+
+PropertyBinding.prototype.Versioning = {
+	None: 0,
+	NeedsUpdate: 1,
+	MatrixWorldNeedsUpdate: 2
+};
+
+PropertyBinding.prototype.GetterByBindingType = [
+
+	PropertyBinding.prototype._getValue_direct,
+	PropertyBinding.prototype._getValue_array,
+	PropertyBinding.prototype._getValue_arrayElement,
+	PropertyBinding.prototype._getValue_toArray,
+
+];
+
+PropertyBinding.prototype.SetterByBindingTypeAndVersioning = [
+
+	[
+		// Direct
+		PropertyBinding.prototype._setValue_direct,
+		PropertyBinding.prototype._setValue_direct_setNeedsUpdate,
+		PropertyBinding.prototype._setValue_direct_setMatrixWorldNeedsUpdate,
+
+	], [
+
+		// EntireArray
+
+		PropertyBinding.prototype._setValue_array,
+		PropertyBinding.prototype._setValue_array_setNeedsUpdate,
+		PropertyBinding.prototype._setValue_array_setMatrixWorldNeedsUpdate,
+
+	], [
+
+		// ArrayElement
+		PropertyBinding.prototype._setValue_arrayElement,
+		PropertyBinding.prototype._setValue_arrayElement_setNeedsUpdate,
+		PropertyBinding.prototype._setValue_arrayElement_setMatrixWorldNeedsUpdate,
+
+	], [
+
+		// HasToFromArray
+		PropertyBinding.prototype._setValue_fromArray,
+		PropertyBinding.prototype._setValue_fromArray_setNeedsUpdate,
+		PropertyBinding.prototype._setValue_fromArray_setMatrixWorldNeedsUpdate,
 
 
-// DECLARE ALIAS AFTER assign prototype
-Object.assign( PropertyBinding.prototype, {
+	]
 
 
-	// initial state of these methods that calls 'bind'
-	_getValue_unbound: PropertyBinding.prototype.getValue,
-	_setValue_unbound: PropertyBinding.prototype.setValue,
+];
 
 
-} );
 
 
 export { PropertyBinding };
 export { PropertyBinding };