|
@@ -16,7 +16,10 @@ import "../../Meshes/Builders/planeBuilder";
|
|
*/
|
|
*/
|
|
export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
private static _AnyMouseID = -2;
|
|
private static _AnyMouseID = -2;
|
|
- private _attachedNode: AbstractMesh;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Abstract mesh the behavior is set on
|
|
|
|
+ */
|
|
|
|
+ public attachedNode: AbstractMesh;
|
|
private _dragPlane: Mesh;
|
|
private _dragPlane: Mesh;
|
|
private _scene: Scene;
|
|
private _scene: Scene;
|
|
private _pointerObserver: Nullable<Observer<PointerInfo>>;
|
|
private _pointerObserver: Nullable<Observer<PointerInfo>>;
|
|
@@ -137,7 +140,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
*/
|
|
*/
|
|
public attach(ownerNode: AbstractMesh): void {
|
|
public attach(ownerNode: AbstractMesh): void {
|
|
this._scene = ownerNode.getScene();
|
|
this._scene = ownerNode.getScene();
|
|
- this._attachedNode = ownerNode;
|
|
|
|
|
|
+ this.attachedNode = ownerNode;
|
|
|
|
|
|
// Initialize drag plane to not interfere with existing scene
|
|
// Initialize drag plane to not interfere with existing scene
|
|
if (!PointerDragBehavior._planeScene) {
|
|
if (!PointerDragBehavior._planeScene) {
|
|
@@ -158,7 +161,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
this.lastDragPosition = new Vector3(0, 0, 0);
|
|
this.lastDragPosition = new Vector3(0, 0, 0);
|
|
|
|
|
|
var pickPredicate = (m: AbstractMesh) => {
|
|
var pickPredicate = (m: AbstractMesh) => {
|
|
- return this._attachedNode == m || m.isDescendantOf(this._attachedNode);
|
|
|
|
|
|
+ return this.attachedNode == m || m.isDescendantOf(this.attachedNode);
|
|
};
|
|
};
|
|
|
|
|
|
this._pointerObserver = this._scene.onPointerObservable.add((pointerInfo, eventState) => {
|
|
this._pointerObserver = this._scene.onPointerObservable.add((pointerInfo, eventState) => {
|
|
@@ -204,15 +207,15 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
|
|
|
|
this._beforeRenderObserver = this._scene.onBeforeRenderObservable.add(() => {
|
|
this._beforeRenderObserver = this._scene.onBeforeRenderObservable.add(() => {
|
|
if (this._moving && this.moveAttached) {
|
|
if (this._moving && this.moveAttached) {
|
|
- PivotTools._RemoveAndStorePivotPoint(this._attachedNode);
|
|
|
|
|
|
+ PivotTools._RemoveAndStorePivotPoint(this.attachedNode);
|
|
// Slowly move mesh to avoid jitter
|
|
// Slowly move mesh to avoid jitter
|
|
- this._targetPosition.subtractToRef((this._attachedNode).absolutePosition, this._tmpVector);
|
|
|
|
|
|
+ this._targetPosition.subtractToRef((this.attachedNode).absolutePosition, this._tmpVector);
|
|
this._tmpVector.scaleInPlace(this.dragDeltaRatio);
|
|
this._tmpVector.scaleInPlace(this.dragDeltaRatio);
|
|
- (this._attachedNode).getAbsolutePosition().addToRef(this._tmpVector, this._tmpVector);
|
|
|
|
|
|
+ (this.attachedNode).getAbsolutePosition().addToRef(this._tmpVector, this._tmpVector);
|
|
if (this.validateDrag(this._tmpVector)) {
|
|
if (this.validateDrag(this._tmpVector)) {
|
|
- (this._attachedNode).setAbsolutePosition(this._tmpVector);
|
|
|
|
|
|
+ (this.attachedNode).setAbsolutePosition(this._tmpVector);
|
|
}
|
|
}
|
|
- PivotTools._RestorePivotPoint(this._attachedNode);
|
|
|
|
|
|
+ PivotTools._RestorePivotPoint(this.attachedNode);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -255,18 +258,18 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
}
|
|
}
|
|
|
|
|
|
private _startDrag(pointerId: number, fromRay?: Ray, startPickedPoint?: Vector3) {
|
|
private _startDrag(pointerId: number, fromRay?: Ray, startPickedPoint?: Vector3) {
|
|
- if (!this._scene.activeCamera || this.dragging || !this._attachedNode) {
|
|
|
|
|
|
+ if (!this._scene.activeCamera || this.dragging || !this.attachedNode) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- PivotTools._RemoveAndStorePivotPoint(this._attachedNode);
|
|
|
|
|
|
+ PivotTools._RemoveAndStorePivotPoint(this.attachedNode);
|
|
// Create start ray from the camera to the object
|
|
// Create start ray from the camera to the object
|
|
if (fromRay) {
|
|
if (fromRay) {
|
|
this._startDragRay.direction.copyFrom(fromRay.direction);
|
|
this._startDragRay.direction.copyFrom(fromRay.direction);
|
|
this._startDragRay.origin.copyFrom(fromRay.origin);
|
|
this._startDragRay.origin.copyFrom(fromRay.origin);
|
|
} else {
|
|
} else {
|
|
this._startDragRay.origin.copyFrom(this._scene.activeCamera.position);
|
|
this._startDragRay.origin.copyFrom(this._scene.activeCamera.position);
|
|
- this._attachedNode.getWorldMatrix().getTranslationToRef(this._tmpVector);
|
|
|
|
|
|
+ this.attachedNode.getWorldMatrix().getTranslationToRef(this._tmpVector);
|
|
this._tmpVector.subtractToRef(this._scene.activeCamera.position, this._startDragRay.direction);
|
|
this._tmpVector.subtractToRef(this._scene.activeCamera.position, this._startDragRay.direction);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -278,7 +281,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
this.currentDraggingPointerID = pointerId;
|
|
this.currentDraggingPointerID = pointerId;
|
|
this.lastDragPosition.copyFrom(pickedPoint);
|
|
this.lastDragPosition.copyFrom(pickedPoint);
|
|
this.onDragStartObservable.notifyObservers({ dragPlanePoint: pickedPoint, pointerId: this.currentDraggingPointerID });
|
|
this.onDragStartObservable.notifyObservers({ dragPlanePoint: pickedPoint, pointerId: this.currentDraggingPointerID });
|
|
- this._targetPosition.copyFrom((this._attachedNode).absolutePosition);
|
|
|
|
|
|
+ this._targetPosition.copyFrom((this.attachedNode).absolutePosition);
|
|
|
|
|
|
// Detatch camera controls
|
|
// Detatch camera controls
|
|
if (this.detachCameraControls && this._scene.activeCamera && !this._scene.activeCamera.leftCamera) {
|
|
if (this.detachCameraControls && this._scene.activeCamera && !this._scene.activeCamera.leftCamera) {
|
|
@@ -290,7 +293,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- PivotTools._RestorePivotPoint(this._attachedNode);
|
|
|
|
|
|
+ PivotTools._RestorePivotPoint(this.attachedNode);
|
|
}
|
|
}
|
|
|
|
|
|
private _dragDelta = new Vector3();
|
|
private _dragDelta = new Vector3();
|
|
@@ -307,7 +310,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
// depending on the drag mode option drag accordingly
|
|
// depending on the drag mode option drag accordingly
|
|
if (this._options.dragAxis) {
|
|
if (this._options.dragAxis) {
|
|
// Convert local drag axis to world
|
|
// Convert local drag axis to world
|
|
- Vector3.TransformCoordinatesToRef(this._options.dragAxis, this._attachedNode.getWorldMatrix().getRotationMatrix(), this._worldDragAxis);
|
|
|
|
|
|
+ Vector3.TransformCoordinatesToRef(this._options.dragAxis, this.attachedNode.getWorldMatrix().getRotationMatrix(), this._worldDragAxis);
|
|
|
|
|
|
// Project delta drag from the drag plane onto the drag axis
|
|
// Project delta drag from the drag plane onto the drag axis
|
|
pickedPoint.subtractToRef(this.lastDragPosition, this._tmpVector);
|
|
pickedPoint.subtractToRef(this.lastDragPosition, this._tmpVector);
|
|
@@ -340,7 +343,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
if (this._useAlternatePickedPointAboveMaxDragAngle) {
|
|
if (this._useAlternatePickedPointAboveMaxDragAngle) {
|
|
// Invert ray direction along the towards object axis
|
|
// Invert ray direction along the towards object axis
|
|
this._tmpVector.copyFrom(ray.direction);
|
|
this._tmpVector.copyFrom(ray.direction);
|
|
- (this._attachedNode).absolutePosition.subtractToRef(ray.origin, this._alternatePickedPoint);
|
|
|
|
|
|
+ (this.attachedNode).absolutePosition.subtractToRef(ray.origin, this._alternatePickedPoint);
|
|
this._alternatePickedPoint.normalize();
|
|
this._alternatePickedPoint.normalize();
|
|
this._alternatePickedPoint.scaleInPlace(this._useAlternatePickedPointAboveMaxDragAngleDragSpeed * Vector3.Dot(this._alternatePickedPoint, this._tmpVector));
|
|
this._alternatePickedPoint.scaleInPlace(this._useAlternatePickedPointAboveMaxDragAngleDragSpeed * Vector3.Dot(this._alternatePickedPoint, this._tmpVector));
|
|
this._tmpVector.addInPlace(this._alternatePickedPoint);
|
|
this._tmpVector.addInPlace(this._alternatePickedPoint);
|
|
@@ -349,7 +352,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
var dot = Vector3.Dot(this._dragPlane.forward, this._tmpVector);
|
|
var dot = Vector3.Dot(this._dragPlane.forward, this._tmpVector);
|
|
this._dragPlane.forward.scaleToRef(-dot, this._alternatePickedPoint);
|
|
this._dragPlane.forward.scaleToRef(-dot, this._alternatePickedPoint);
|
|
this._alternatePickedPoint.addInPlace(this._tmpVector);
|
|
this._alternatePickedPoint.addInPlace(this._tmpVector);
|
|
- this._alternatePickedPoint.addInPlace((this._attachedNode).absolutePosition);
|
|
|
|
|
|
+ this._alternatePickedPoint.addInPlace((this.attachedNode).absolutePosition);
|
|
return this._alternatePickedPoint;
|
|
return this._alternatePickedPoint;
|
|
} else {
|
|
} else {
|
|
return null;
|
|
return null;
|
|
@@ -376,7 +379,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
private _updateDragPlanePosition(ray: Ray, dragPlanePosition: Vector3) {
|
|
private _updateDragPlanePosition(ray: Ray, dragPlanePosition: Vector3) {
|
|
this._pointA.copyFrom(dragPlanePosition);
|
|
this._pointA.copyFrom(dragPlanePosition);
|
|
if (this._options.dragAxis) {
|
|
if (this._options.dragAxis) {
|
|
- this.useObjectOrienationForDragging ? Vector3.TransformCoordinatesToRef(this._options.dragAxis, this._attachedNode.getWorldMatrix().getRotationMatrix(), this._localAxis) : this._localAxis.copyFrom(this._options.dragAxis);
|
|
|
|
|
|
+ this.useObjectOrienationForDragging ? Vector3.TransformCoordinatesToRef(this._options.dragAxis, this.attachedNode.getWorldMatrix().getRotationMatrix(), this._localAxis) : this._localAxis.copyFrom(this._options.dragAxis);
|
|
|
|
|
|
// Calculate plane normal in direction of camera but perpendicular to drag axis
|
|
// Calculate plane normal in direction of camera but perpendicular to drag axis
|
|
this._pointA.addToRef(this._localAxis, this._pointB); // towards drag axis
|
|
this._pointA.addToRef(this._localAxis, this._pointB); // towards drag axis
|
|
@@ -394,7 +397,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
this._pointA.addToRef(this._lookAt, this._lookAt);
|
|
this._pointA.addToRef(this._lookAt, this._lookAt);
|
|
this._dragPlane.lookAt(this._lookAt);
|
|
this._dragPlane.lookAt(this._lookAt);
|
|
} else if (this._options.dragPlaneNormal) {
|
|
} else if (this._options.dragPlaneNormal) {
|
|
- this.useObjectOrienationForDragging ? Vector3.TransformCoordinatesToRef(this._options.dragPlaneNormal, this._attachedNode.getWorldMatrix().getRotationMatrix(), this._localAxis) : this._localAxis.copyFrom(this._options.dragPlaneNormal);
|
|
|
|
|
|
+ this.useObjectOrienationForDragging ? Vector3.TransformCoordinatesToRef(this._options.dragPlaneNormal, this.attachedNode.getWorldMatrix().getRotationMatrix(), this._localAxis) : this._localAxis.copyFrom(this._options.dragPlaneNormal);
|
|
this._dragPlane.position.copyFrom(this._pointA);
|
|
this._dragPlane.position.copyFrom(this._pointA);
|
|
this._pointA.addToRef(this._localAxis, this._lookAt);
|
|
this._pointA.addToRef(this._localAxis, this._lookAt);
|
|
this._dragPlane.lookAt(this._lookAt);
|
|
this._dragPlane.lookAt(this._lookAt);
|
|
@@ -403,7 +406,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
|
|
this._dragPlane.lookAt(ray.origin);
|
|
this._dragPlane.lookAt(ray.origin);
|
|
}
|
|
}
|
|
// Update the position of the drag plane so it doesn't get out of sync with the node (eg. when moving back and forth quickly)
|
|
// Update the position of the drag plane so it doesn't get out of sync with the node (eg. when moving back and forth quickly)
|
|
- this._dragPlane.position.copyFrom(this._attachedNode.absolutePosition);
|
|
|
|
|
|
+ this._dragPlane.position.copyFrom(this.attachedNode.absolutePosition);
|
|
|
|
|
|
this._dragPlane.computeWorldMatrix(true);
|
|
this._dragPlane.computeWorldMatrix(true);
|
|
}
|
|
}
|