Skip to content

Commit

Permalink
DragControls: Add mouseButtons
Browse files Browse the repository at this point in the history
  • Loading branch information
puxiao committed Aug 6, 2024
1 parent 364f90e commit 66a3704
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/examples/en/controls/DragControls.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ <h3>hoveroff</h3>

<h2>Properties</h2>

<h3>[property:Array mouseButtons]</h3>
<p>
The array of allowed MouseEvent.button values for drag interactions.
<br>
Default is [ THREE.MOUSE.LEFT, THREE.MOUSE.RIGHT, THREE.MOUSE.MIDDLE ].
</p>

<h3>[property:Boolean enabled]</h3>
<p>
Whether or not the controls are enabled.
Expand Down
11 changes: 9 additions & 2 deletions docs/examples/zh/controls/DragControls.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ <h3>hoveroff</h3>

<h2>属性</h2>

<h3>[property:Array mouseButtons]</h3>
<p>
鼠标事件中允许响应的按键值数组。
<br>
默认值为 [ THREE.MOUSE.LEFT, THREE.MOUSE.RIGHT, THREE.MOUSE.MIDDLE ]。
</p>

<h3>[property:Boolean enabled]</h3>
<p>
是否启用控制器。
Expand All @@ -109,7 +116,7 @@ <h3>[property:Boolean transformGroup]</h3>

<h3>[property:String mode]</h3>
<p>
The current transformation mode. Possible values are `translate`, and `rotate`. Default is `translate`.
当前的变换模式,该值可以是 `translate` `rotate`。 默认值为 `translate`
</p>

<h2>方法</h2>
Expand Down Expand Up @@ -143,7 +150,7 @@ <h3>[method:Raycaster getRaycaster] ()</h3>

<h3>[method:undefined setObjects] ( [param:Array objects] )</h3>
<p>
Sets an array of draggable objects by overwriting the existing one.
通过覆盖现有数组来设置可拖动对象数组。
</p>

<h2>源代码</h2>
Expand Down
30 changes: 30 additions & 0 deletions examples/jsm/controls/DragControls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
EventDispatcher,
Matrix4,
MOUSE,
Plane,
Raycaster,
Vector2,
Expand All @@ -21,6 +22,9 @@ const _inverseMatrix = new Matrix4();
const _up = new Vector3();
const _right = new Vector3();

const _mouseButtons = [ MOUSE.LEFT, MOUSE.RIGHT, MOUSE.MIDDLE ];
const _mouseMoveButtons = [ 0, 1, 2, 4 ];

class DragControls extends EventDispatcher {

constructor( _objects, _camera, _domElement ) {
Expand Down Expand Up @@ -89,6 +93,8 @@ class DragControls extends EventDispatcher {

if ( scope.enabled === false ) return;

if ( event.pointerType === 'mouse' && _mouseMoveButtons.includes( event.buttons ) === false ) return;

updatePointer( event );

_raycaster.setFromCamera( _pointer, _camera );
Expand Down Expand Up @@ -175,6 +181,8 @@ class DragControls extends EventDispatcher {

if ( scope.enabled === false ) return;

if ( event.pointerType === 'mouse' && _mouseButtons.includes( event.button ) === false ) return;

updatePointer( event );

_intersections.length = 0;
Expand Down Expand Up @@ -277,6 +285,28 @@ class DragControls extends EventDispatcher {

}

get mouseButtons() {

return _mouseButtons;

}

set mouseButtons( arr ) {

_mouseButtons.length = 0;
arr.forEach( item => _mouseButtons.push( item ) );

_mouseMoveButtons.length = 0;
_mouseMoveButtons.push( 0 );

if ( _mouseButtons.includes( MOUSE.LEFT ) ) _mouseMoveButtons.push( 1 );

if ( _mouseButtons.includes( MOUSE.RIGHT ) ) _mouseMoveButtons.push( 2 );

if ( _mouseButtons.includes( MOUSE.MIDDLE ) ) _mouseMoveButtons.push( 4 );

}

}

export { DragControls };

0 comments on commit 66a3704

Please sign in to comment.