Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Object3D add and remove #11459

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,11 @@ Object.assign( Object3D.prototype, EventDispatcher.prototype, {
}

object.parent = this;
object.dispatchEvent( { type: 'added' } );

this.children.push( object );

object.dispatchEvent( { type: 'added' } );
this.dispatchEvent( { type: 'childAdded', child: object } );

} else {

console.error( "THREE.Object3D.add: object not an instance of THREE.Object3D.", object );
Expand Down Expand Up @@ -363,10 +364,10 @@ Object.assign( Object3D.prototype, EventDispatcher.prototype, {
if ( index !== - 1 ) {

object.parent = null;
this.children.splice( index, 1 );

object.dispatchEvent( { type: 'removed' } );

this.children.splice( index, 1 );
this.dispatchEvent( { type: 'childRemoved', child: object } );

}

Expand Down
50 changes: 50 additions & 0 deletions test/unit/src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,53 @@ QUnit.test( "getWorldRotation" , function( assert ) {
obj.lookAt(new THREE.Vector3(1, 0, 0));
assert.numEqual( obj.getWorldRotation().y * RadToDeg, 90, "y is equal" );
});

QUnit.test( "add" , function( assert ) {
assert.expect( 16 );

var parent = new THREE.Object3D();
var child = new THREE.Object3D();

function parentAdd( evt ) {
assert.success( "parent dispatches 'childAdded' event when adding a child" );
assert.strictEqual( evt.child, child, "childAdded event passes child object" );
assert.strictEqual( parent.children[0], child, "parent has child when 'add' event dispatched" );
assert.strictEqual( parent, child.parent, "child has parent when 'add' event dispatched" );
}

function parentRemove( evt ) {
assert.success( "parent dispatches 'childRemoved' event when removing a child" );
assert.strictEqual( evt.child, child, "childRemoved event passes child object" );
assert.strictEqual( parent.children.length, 0, "child removed from parent when 'remove' event dispatched" );
assert.strictEqual( child.parent, null, "child has no parent when 'remove' event dispatched" );
}

function childAdd() {
assert.success( "child dispatches 'added' event when adding to a parent object" );
assert.strictEqual( parent.children[0], child, "parent has child when 'added' event dispatched" );
assert.strictEqual( parent, child.parent, "child has parent when 'added' event dispatched" );
}

function childRemove() {
assert.success( "child dispatches 'removed' event when removing from parent" );
assert.strictEqual( parent.children.length, 0, "child removed from parent when 'removed' event dispatched" );
assert.strictEqual( child.parent, null, "child has no parent when 'removed' event dispatched" );
}

parent.addEventListener( "childAdded", parentAdd );
parent.addEventListener( "childRemoved", parentRemove );
child.addEventListener( "added", childAdd );
child.addEventListener( "removed", childRemove );

parent.add( child );
assert.strictEqual( parent.children[0], child, "parent has child after parent.add( child )" );
assert.strictEqual( parent, child.parent, "child has parent after parent.add( child )" );

parent.remove( child );

// clean up
parent.removeEventListener( "childAdded", parentAdd );
parent.removeEventListener( "childRemoved", parentRemove );
child.removeEventListener( "add", childAdd );
child.removeEventListener( "removed", childRemove );
});