Skip to content

Commit

Permalink
Add tiles control events
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Jan 12, 2024
1 parent 8f8ca54 commit e538562
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions example/src/controls/TileControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const _deltaPointer = new Vector2();
const _centerPoint = new Vector2();
const _originalCenterPoint = new Vector2();

const _changeEvent = { type: 'change' };
const _startEvent = { type: 'start' };
const _endEvent = { type: 'end' };

export class TileControls {

constructor( scene, camera, domElement ) {
Expand All @@ -43,6 +47,7 @@ export class TileControls {

// settings
this.state = NONE;
this.pinchState = NONE;
this.cameraRadius = 5;
this.rotationSpeed = 5;
this.minAltitude = 0;
Expand Down Expand Up @@ -111,7 +116,6 @@ export class TileControls {
this.domElement = domElement;
domElement.style.touchAction = 'none';

let pinchAction = NONE;
let shiftClicked = false;

const contextMenuCallback = e => {
Expand Down Expand Up @@ -168,7 +172,6 @@ export class TileControls {
} else if ( pointerTracker.getPointerCount() > 2 ) {

this.resetState();
pinchAction = NONE;
return;

}
Expand All @@ -192,7 +195,7 @@ export class TileControls {
pointerTracker.isLeftClicked() && shiftClicked
) {

this.state = ROTATE;
this.setState( ROTATE );
this.rotationPoint.copy( hit.point );
this.rotationPointSet = true;

Expand All @@ -205,7 +208,7 @@ export class TileControls {
// if the clicked point is coming from below the plane then don't perform the drag
if ( raycaster.ray.direction.dot( up ) < 0 ) {

this.state = DRAG;
this.setState( DRAG );
this.dragPoint.copy( hit.point );
this.startDragPoint.copy( hit.point );
this.dragPointSet = true;
Expand Down Expand Up @@ -264,7 +267,7 @@ export class TileControls {
const previousDist = pointerTracker.getPreviousPointerDistance();
const pointerDist = pointerTracker.getPointerDistance();
const separateDelta = pointerDist - previousDist;
if ( pinchAction === NONE ) {
if ( this.pinchState === NONE ) {

// check which direction was moved in first - if the pointers are pinching then
// it's a zoom. But if they move in parallel it's a rotation
Expand All @@ -276,25 +279,26 @@ export class TileControls {

if ( Math.abs( separateDelta ) > parallelDelta ) {

this.resetState();
pinchAction = ZOOM;
this.setState( NONE, ZOOM );
this.zoomDirectionSet = false;

} else {

pinchAction = ROTATE;
this.pinchState = ROTATE;
this.setState( NONE, ROTATE );


}

}

}

if ( pinchAction === ZOOM ) {
if ( this.pinchState === ZOOM ) {

this._updateZoom( separateDelta );

} else if ( pinchAction === ROTATE ) {
} else if ( this.pinchState === ROTATE ) {

this._updateRotation();
this.pivotMesh.visible = true;
Expand Down Expand Up @@ -328,7 +332,6 @@ export class TileControls {
const { pointerTracker } = this;

pointerTracker.deletePointer( e );
pinchAction = NONE;

if (
pointerTracker.getPointerType() === 'touch' &&
Expand Down Expand Up @@ -408,14 +411,43 @@ export class TileControls {

resetState() {

if ( this.state !== NONE && this.pinchState !== NONE ) {

this.dispatchEvent( _endEvent );

}

this.state = NONE;
this.pinchState = NONE;
this.dragPointSet = false;
this.rotationPointSet = false;
this.scene.remove( this.pivotMesh );
this.pivotMesh.visible = true;

}

setState( state = null, pinchState = null ) {

if ( this.state === NONE && this.pinchState === NONE ) {

this.dispatchEvent( _startEvent );

}

if ( state !== null ) {

this.state = state;

}

if ( pinchState !== null ) {

this.pinchState = pinchState;

}

}

update() {

const {
Expand Down Expand Up @@ -547,6 +579,8 @@ export class TileControls {

}

this.dispatchEvent( _changeEvent );

}

// update the point being zoomed in to based on the zoom direction
Expand Down Expand Up @@ -672,6 +706,8 @@ export class TileControls {

}

this.dispatchEvent( _changeEvent );

}

_updateRotation() {
Expand Down Expand Up @@ -745,6 +781,8 @@ export class TileControls {
// update the transform members
camera.matrixWorld.decompose( camera.position, camera.quaternion, _vec );

this.dispatchEvent( _changeEvent );

}

// sets the "up" axis for the current surface of the tile set
Expand Down

0 comments on commit e538562

Please sign in to comment.