Skip to content

Commit

Permalink
fix(PlanarControls): prevent triggering new movement when already moving
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie authored and gchoqueux committed Mar 4, 2021
1 parent 2914286 commit 66256bb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/Controls/PlanarControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const mouseButtons = {
MIDDLECLICK: THREE.MOUSE.MIDDLE,
RIGHTCLICK: THREE.MOUSE.RIGHT,
};
let currentPressedButton;

// starting camera position and orientation target
const startPosition = new THREE.Vector3();
Expand Down Expand Up @@ -950,9 +951,10 @@ class PlanarControls extends THREE.EventDispatcher {
onMouseDown(event) {
event.preventDefault();

if (STATE.TRAVEL === this.state) {
if (STATE.NONE !== this.state) {
return;
}
currentPressedButton = event.button;

this.updateMousePositionAndDelta(event);

Expand Down Expand Up @@ -992,7 +994,7 @@ class PlanarControls extends THREE.EventDispatcher {
onMouseUp(event) {
event.preventDefault();

if (STATE.TRAVEL !== this.state) {
if (STATE.TRAVEL !== this.state && currentPressedButton === event.button) {
this.state = STATE.NONE;
}

Expand Down Expand Up @@ -1023,7 +1025,7 @@ class PlanarControls extends THREE.EventDispatcher {
* @ignore
*/
onKeyDown(event) {
if (STATE.TRAVEL === this.state) {
if (STATE.NONE !== this.state) {
return;
}
switch (event.keyCode) {
Expand Down
15 changes: 11 additions & 4 deletions test/unit/planarControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('Planar Controls', function () {
event.button = THREE.MOUSE.LEFT;
controlsPerspective.onMouseDown(event);
assert.equal(STATE.DRAG, controlsPerspective.state);
controlsPerspective.state = STATE.NONE;
});

it('ctrl + left-click should switch controls to ROTATE state, unless rotation is disabled', function () {
Expand Down Expand Up @@ -131,26 +132,32 @@ describe('Planar Controls', function () {
controlsPerspective.state = STATE.TRAVEL;
controlsPerspective.onMouseDown(event);
assert.equal(controlsPerspective.state, STATE.TRAVEL);
controlsPerspective.state = STATE.NONE;
});

it('onMouseUp should switch controls to the relevant state', function () {
// STATE.DRAG
controlsPerspective.state = STATE.DRAG;
event.button = THREE.MOUSE.LEFT;
controlsPerspective.onMouseDown(event);
controlsPerspective.onMouseUp(event);
assert.equal(STATE.NONE, controlsPerspective.state);

// STATE.ROTATE
controlsPerspective.state = STATE.ROTATE;
event.ctrlKey = true;
controlsPerspective.onMouseDown(event);
controlsPerspective.onMouseUp(event);
assert.equal(STATE.NONE, controlsPerspective.state);
event.ctrlKey = false;

// STATE.PAN
controlsPerspective.state = STATE.PAN;
event.button = THREE.MOUSE.RIGHT;
controlsPerspective.onMouseDown(event);
controlsPerspective.onMouseUp(event);
assert.equal(STATE.NONE, controlsPerspective.state);

// STATE.TRAVEL
controlsPerspective.state = STATE.TRAVEL;
event.button = THREE.MOUSE.MIDDLE;
controlsPerspective.onMouseDown(event);
controlsPerspective.onMouseUp(event);
assert.equal(STATE.TRAVEL, controlsPerspective.state);
controlsPerspective.state = STATE.NONE;
Expand Down

0 comments on commit 66256bb

Please sign in to comment.