-
-
Notifications
You must be signed in to change notification settings - Fork 266
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
Change mouse button config to a function (e: Event) => ACTION
#459
Open
jamesnakagawa
wants to merge
1
commit into
yomotsu:dev
Choose a base branch
from
jamesnakagawa:provide-modifier-key-access
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,23 +314,20 @@ export class CameraControls extends EventDispatcher { | |
// button configs | ||
/** | ||
* User's mouse input config. | ||
* @param MouseEvent or undefined. Can be a click event, wheel event, or contextmenu event. | ||
* @returns CameraControls.ACTION (ROTATE, TRUCK, OFFSET, DOLLY, ZOOM, NONE) | ||
* | ||
* | button to assign | behavior | | ||
* | --------------------- | -------- | | ||
* | `mouseButtons.left` | `CameraControls.ACTION.ROTATE`* \| `CameraControls.ACTION.TRUCK` \| `CameraControls.ACTION.OFFSET` \| `CameraControls.ACTION.DOLLY` \| `CameraControls.ACTION.ZOOM` \| `CameraControls.ACTION.NONE` | | ||
* | `mouseButtons.right` | `CameraControls.ACTION.ROTATE` \| `CameraControls.ACTION.TRUCK`* \| `CameraControls.ACTION.OFFSET` \| `CameraControls.ACTION.DOLLY` \| `CameraControls.ACTION.ZOOM` \| `CameraControls.ACTION.NONE` | | ||
* | `mouseButtons.wheel` ¹ | `CameraControls.ACTION.ROTATE` \| `CameraControls.ACTION.TRUCK` \| `CameraControls.ACTION.OFFSET` \| `CameraControls.ACTION.DOLLY` \| `CameraControls.ACTION.ZOOM` \| `CameraControls.ACTION.NONE` | | ||
* | `mouseButtons.middle` ² | `CameraControls.ACTION.ROTATE` \| `CameraControls.ACTION.TRUCK` \| `CameraControls.ACTION.OFFSET` \| `CameraControls.ACTION.DOLLY`* \| `CameraControls.ACTION.ZOOM` \| `CameraControls.ACTION.NONE` | | ||
* | ||
* 1. Mouse wheel event for scroll "up/down" on mac "up/down/left/right" | ||
* 2. Mouse click on wheel event "button" | ||
* - \* is the default. | ||
* - The default of `mouseButtons.wheel` is: | ||
* - The default of `WheelEvent` is: | ||
* - `DOLLY` for Perspective camera. | ||
* - `ZOOM` for Orthographic camera, and can't set `DOLLY`. | ||
* @category Properties | ||
* - The default for other `MouseEvent`s is: | ||
* - `ROTATE` if left button is pressed, otherwise | ||
* - `ZOOM` if middle button is pressed, otherwise | ||
* - `TRUCK` if right button is pressed | ||
* - The default is `ROTATE` if event is undefined | ||
* @category Methods | ||
*/ | ||
mouseButtons: MouseButtons; | ||
mouseEventToAction: ( e?: Event ) => ACTION; | ||
|
||
/** | ||
* User's touch input config. | ||
|
@@ -518,14 +515,40 @@ export class CameraControls extends EventDispatcher { | |
this._dollyControlCoord = new THREE.Vector2(); | ||
|
||
// configs | ||
this.mouseButtons = { | ||
left: ACTION.ROTATE, | ||
middle: ACTION.DOLLY, | ||
right: ACTION.TRUCK, | ||
wheel: | ||
isPerspectiveCamera( this._camera ) ? ACTION.DOLLY : | ||
isOrthographicCamera( this._camera ) ? ACTION.ZOOM : | ||
ACTION.NONE, | ||
this.mouseEventToAction = ( event: Event | undefined ) => { | ||
|
||
if ( event instanceof WheelEvent ) { | ||
|
||
return isPerspectiveCamera( this._camera ) ? ACTION.DOLLY : | ||
isOrthographicCamera( this._camera ) ? ACTION.ZOOM : | ||
ACTION.NONE; | ||
|
||
} | ||
|
||
if ( event instanceof MouseEvent ) { | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.LEFT ) === MOUSE_BUTTON.LEFT ) { | ||
|
||
return ACTION.ROTATE; | ||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.MIDDLE ) === MOUSE_BUTTON.MIDDLE ) { | ||
|
||
return ACTION.DOLLY; | ||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.RIGHT ) === MOUSE_BUTTON.RIGHT ) { | ||
|
||
return ACTION.TRUCK; | ||
|
||
} | ||
|
||
} | ||
|
||
return ACTION.ROTATE; | ||
|
||
}; | ||
|
||
this.touches = { | ||
|
@@ -714,22 +737,9 @@ export class CameraControls extends EventDispatcher { | |
|
||
if ( | ||
( ! this._isDragging && this._lockedPointer ) || | ||
this._isDragging && ( event.buttons & MOUSE_BUTTON.LEFT ) === MOUSE_BUTTON.LEFT | ||
) { | ||
|
||
this._state = this._state | this.mouseButtons.left; | ||
|
||
} | ||
|
||
if ( this._isDragging && ( event.buttons & MOUSE_BUTTON.MIDDLE ) === MOUSE_BUTTON.MIDDLE ) { | ||
this._isDragging && ( event.buttons & MOUSE_BUTTON.ALL ) > 0 ) { | ||
|
||
this._state = this._state | this.mouseButtons.middle; | ||
|
||
} | ||
|
||
if ( this._isDragging && ( event.buttons & MOUSE_BUTTON.RIGHT ) === MOUSE_BUTTON.RIGHT ) { | ||
|
||
this._state = this._state | this.mouseButtons.right; | ||
this._state = this._state | this.mouseEventToAction( event ); | ||
|
||
} | ||
|
||
|
@@ -754,22 +764,10 @@ export class CameraControls extends EventDispatcher { | |
|
||
if ( | ||
this._lockedPointer || | ||
( event.buttons & MOUSE_BUTTON.LEFT ) === MOUSE_BUTTON.LEFT | ||
( event.buttons & MOUSE_BUTTON.ALL ) > 0 | ||
) { | ||
|
||
this._state = this._state | this.mouseButtons.left; | ||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.MIDDLE ) === MOUSE_BUTTON.MIDDLE ) { | ||
|
||
this._state = this._state | this.mouseButtons.middle; | ||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.RIGHT ) === MOUSE_BUTTON.RIGHT ) { | ||
|
||
this._state = this._state | this.mouseButtons.right; | ||
this._state = this._state | this.mouseEventToAction( event ); | ||
|
||
} | ||
|
||
|
@@ -839,8 +837,10 @@ export class CameraControls extends EventDispatcher { | |
|
||
const onMouseWheel = ( event: WheelEvent ): void => { | ||
|
||
const action = this.mouseEventToAction( event ); | ||
|
||
if ( ! this._domElement ) return; | ||
if ( ! this._enabled || this.mouseButtons.wheel === ACTION.NONE ) return; | ||
if ( ! this._enabled || action === ACTION.NONE ) return; | ||
|
||
if ( | ||
this._interactiveArea.left !== 0 || | ||
|
@@ -867,8 +867,8 @@ export class CameraControls extends EventDispatcher { | |
|
||
if ( | ||
this.dollyToCursor || | ||
this.mouseButtons.wheel === ACTION.ROTATE || | ||
this.mouseButtons.wheel === ACTION.TRUCK | ||
action === ACTION.ROTATE || | ||
action === ACTION.TRUCK | ||
) { | ||
|
||
const now = performance.now(); | ||
|
@@ -885,7 +885,7 @@ export class CameraControls extends EventDispatcher { | |
const x = this.dollyToCursor ? ( event.clientX - this._elementRect.x ) / this._elementRect.width * 2 - 1 : 0; | ||
const y = this.dollyToCursor ? ( event.clientY - this._elementRect.y ) / this._elementRect.height * - 2 + 1 : 0; | ||
|
||
switch ( this.mouseButtons.wheel ) { | ||
switch ( action ) { | ||
|
||
case ACTION.ROTATE: { | ||
|
||
|
@@ -939,7 +939,7 @@ export class CameraControls extends EventDispatcher { | |
|
||
// contextmenu event is fired right after pointerdown/mousedown. | ||
// remove attached handlers and active pointer, if interrupted by contextmenu. | ||
if ( this.mouseButtons.right === CameraControls.ACTION.NONE ) { | ||
if ( this.mouseEventToAction( event ) === CameraControls.ACTION.NONE ) { | ||
|
||
const pointerId = | ||
event instanceof PointerEvent ? event.pointerId : | ||
|
@@ -996,7 +996,7 @@ export class CameraControls extends EventDispatcher { | |
|
||
if ( ! event ) { | ||
|
||
if ( this._lockedPointer ) this._state = this._state | this.mouseButtons.left; | ||
if ( this._lockedPointer ) this._state = this._state | this.mouseEventToAction( event ); | ||
|
||
} else if ( 'pointerType' in event && event.pointerType === 'touch' ) { | ||
|
||
|
@@ -1019,25 +1019,11 @@ export class CameraControls extends EventDispatcher { | |
|
||
} | ||
|
||
} else { | ||
|
||
if ( ! this._lockedPointer && ( event.buttons & MOUSE_BUTTON.LEFT ) === MOUSE_BUTTON.LEFT ) { | ||
|
||
this._state = this._state | this.mouseButtons.left; | ||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.MIDDLE ) === MOUSE_BUTTON.MIDDLE ) { | ||
|
||
this._state = this._state | this.mouseButtons.middle; | ||
} else if ( ! this._lockedPointer && ( event.buttons & MOUSE_BUTTON.LEFT ) === MOUSE_BUTTON.LEFT || | ||
( event.buttons & MOUSE_BUTTON.MIDDLE ) === MOUSE_BUTTON.MIDDLE || | ||
( event.buttons & MOUSE_BUTTON.RIGHT ) === MOUSE_BUTTON.RIGHT ) { | ||
Comment on lines
+1022
to
+1024
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not use |
||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.RIGHT ) === MOUSE_BUTTON.RIGHT ) { | ||
|
||
this._state = this._state | this.mouseButtons.right; | ||
|
||
} | ||
this._state = this._state | this.mouseEventToAction( event ); | ||
|
||
} | ||
|
||
|
@@ -1557,6 +1543,48 @@ export class CameraControls extends EventDispatcher { | |
|
||
} | ||
|
||
/** | ||
* Convert mouse configuration object into computed configuration function which accepts | ||
* an Event parameter. | ||
*/ | ||
set mouseButtons( config: MouseButtons ) { | ||
|
||
this.mouseEventToAction = ( event: Event | undefined ) => { | ||
|
||
if ( event instanceof WheelEvent ) { | ||
|
||
return config.wheel; | ||
|
||
} | ||
|
||
if ( event instanceof MouseEvent ) { | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.LEFT ) === MOUSE_BUTTON.LEFT ) { | ||
|
||
return config.left; | ||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.MIDDLE ) === MOUSE_BUTTON.MIDDLE ) { | ||
|
||
return config.middle; | ||
|
||
} | ||
|
||
if ( ( event.buttons & MOUSE_BUTTON.RIGHT ) === MOUSE_BUTTON.RIGHT ) { | ||
|
||
return config.right; | ||
|
||
} | ||
|
||
} | ||
|
||
return ACTION.ROTATE; | ||
|
||
}; | ||
|
||
} | ||
|
||
/** | ||
* Adds the specified event listener. | ||
* Applicable event types (which is `K`) are: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to simplify this conditional because of this change.
( event.buttons & MOUSE_BUTTON.ALL ) > 0
looks a little weird, and technically it would excludeevent.buttons === 8 || event.buttons === 16
, which is not a common use case. I wanted the result of the function to match previous behaviour exactly.