Skip to content

Commit

Permalink
DragControls: Add support for multiple groups. (#27791)
Browse files Browse the repository at this point in the history
* Update DragControls.js

Fixed Group drag and drop issue.
Existing code can drag only  one group now we can drag multiple group in scene.

* Added new updateObject function for dynamically update objects

* function name updated as per review comment

* Update function name
  • Loading branch information
narenjoriona committed Feb 26, 2024
1 parent 5085620 commit c825051
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions examples/jsm/controls/DragControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DragControls extends EventDispatcher {
_domElement.style.touchAction = 'none'; // disable touch scroll

let _selected = null, _hovered = null;

const _intersections = [];

this.mode = 'translate';
Expand Down Expand Up @@ -73,6 +73,10 @@ class DragControls extends EventDispatcher {

}

function setObjects(object){ //array of object
_objects = object
}

function getRaycaster() {

return _raycaster;
Expand Down Expand Up @@ -178,7 +182,17 @@ class DragControls extends EventDispatcher {

if ( _intersections.length > 0 ) {

_selected = ( scope.transformGroup === true ) ? _objects[ 0 ] : _intersections[ 0 ].object;
if ( scope.transformGroup === true ) {

// look for the outermost group in the object's upper hierarchy

_selected = findGroup( _intersections[ 0 ].object );

} else {

_selected = _intersections[ 0 ].object;

}

_plane.setFromNormalAndCoplanarPoint( _camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );

Expand Down Expand Up @@ -234,6 +248,16 @@ class DragControls extends EventDispatcher {

}

function findGroup( obj, group = null ) {

if ( obj.isGroup ) group = obj;

if ( obj.parent === null ) return group;

return findGroup( obj.parent, group );

}

activate();

// API
Expand All @@ -247,9 +271,11 @@ class DragControls extends EventDispatcher {
this.dispose = dispose;
this.getObjects = getObjects;
this.getRaycaster = getRaycaster;
this.setObjects = setObjects;

}

}


export { DragControls };

0 comments on commit c825051

Please sign in to comment.