Skip to content
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

Fix Gizmo Release Drag #13237

Merged
merged 4 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
// If behavior is disabled before releaseDrag is ever called, call it now.
if (this._attachedToElement) {
this.releaseDrag();
this._activeDragButton = -1;
}

return;
Expand Down Expand Up @@ -266,7 +265,6 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
this._activeDragButton === pointerInfo.event.button
) {
this.releaseDrag();
this._activeDragButton = -1;
}
} else if (pointerInfo.type == PointerEventTypes.POINTERMOVE) {
const pointerId = (<IPointerEvent>pointerInfo.event).pointerId;
Expand Down Expand Up @@ -329,6 +327,7 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
}

this.currentDraggingPointerId = -1;
this._activeDragButton = -1;
this._moving = false;

// Reattach camera controls
Expand Down Expand Up @@ -561,6 +560,5 @@ export class PointerDragBehavior implements Behavior<AbstractMesh> {
this._dragPlane.dispose();
}
this.releaseDrag();
this._activeDragButton = -1;
}
}
16 changes: 14 additions & 2 deletions packages/dev/core/src/Gizmos/gizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class Gizmo implements IGizmo {
}

protected _updateGizmoRotationToMatchAttachedMesh = true;
protected _updateGizmoPositionToMatchAttachedMesh = true;

/**
* If set the gizmo's rotation will be updated to match the attached mesh each frame (Default: true)
Expand All @@ -188,11 +189,22 @@ export class Gizmo implements IGizmo {
/**
* If set the gizmo's position will be updated to match the attached mesh each frame (Default: true)
*/
public updateGizmoPositionToMatchAttachedMesh = true;
public set updateGizmoPositionToMatchAttachedMesh(value: boolean) {
this._updateGizmoPositionToMatchAttachedMesh = value;
}
public get updateGizmoPositionToMatchAttachedMesh() {
return this._updateGizmoPositionToMatchAttachedMesh;
}
/**
* When set, the gizmo will always appear the same size no matter where the camera is (default: true)
*/
public updateScale = true;
protected _updateScale = true;
public set updateScale(value: boolean) {
this._updateScale = value;
}
public get updateScale() {
return this._updateScale;
}
protected _interactionsEnabled = true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected _attachedNodeChanged(value: Nullable<Node>) {}
Expand Down
8 changes: 5 additions & 3 deletions packages/dev/core/src/Gizmos/planeRotationGizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ export class PlaneRotationGizmo extends Gizmo implements IPlaneRotationGizmo {
const nodeQuaternion = new Quaternion(0, 0, 0, 1);
const nodeTranslation = new Vector3(0, 0, 0);
this._handlePivot();

this.attachedNode.getWorldMatrix().decompose(nodeScale, nodeQuaternion, nodeTranslation);

const newVector = event.dragPlanePoint.subtract(nodeTranslation).normalize();
const originalVector = lastDragPosition.subtract(nodeTranslation).normalize();
const nodeTranslationForOperation = this.updateGizmoPositionToMatchAttachedMesh ? nodeTranslation : this._rootMesh.absolutePosition;
const newVector = event.dragPlanePoint.subtract(nodeTranslationForOperation).normalize();
const originalVector = lastDragPosition.subtract(nodeTranslationForOperation).normalize();
const cross = Vector3.Cross(newVector, originalVector);
const dot = Vector3.Dot(newVector, originalVector);
let angle = Math.atan2(cross.length(), dot);
Expand All @@ -252,7 +254,7 @@ export class PlaneRotationGizmo extends Gizmo implements IPlaneRotationGizmo {
// Flip up vector depending on which side the camera is on
let cameraFlipped = false;
if (gizmoLayer.utilityLayerScene.activeCamera) {
const camVec = gizmoLayer.utilityLayerScene.activeCamera.position.subtract(nodeTranslation).normalize();
const camVec = gizmoLayer.utilityLayerScene.activeCamera.position.subtract(nodeTranslationForOperation).normalize();
if (Vector3.Dot(camVec, localPlaneNormalTowardsCamera) > 0) {
planeNormalTowardsCamera.scaleInPlace(-1);
localPlaneNormalTowardsCamera.scaleInPlace(-1);
Expand Down
22 changes: 22 additions & 0 deletions packages/dev/core/src/Gizmos/positionGizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,28 @@ export class PositionGizmo extends Gizmo implements IPositionGizmo {
return this._updateGizmoRotationToMatchAttachedMesh;
}

public set updateGizmoPositionToMatchAttachedMesh(value: boolean) {
this._updateGizmoPositionToMatchAttachedMesh = value;
[this.xGizmo, this.yGizmo, this.zGizmo, this.xPlaneGizmo, this.yPlaneGizmo, this.zPlaneGizmo].forEach((gizmo) => {
if (gizmo) {
gizmo.updateGizmoPositionToMatchAttachedMesh = value;
}
});
}
public get updateGizmoPositionToMatchAttachedMesh() {
return this._updateGizmoPositionToMatchAttachedMesh;
}

public set updateScale(value: boolean) {
if (this.xGizmo) {
this.xGizmo.updateScale = value;
this.yGizmo.updateScale = value;
this.zGizmo.updateScale = value;
}
}
public get updateScale() {
return this.xGizmo.updateScale;
}
/**
* Drag distance in babylon units that the gizmo will snap to when dragged (Default: 0)
*/
Expand Down
21 changes: 21 additions & 0 deletions packages/dev/core/src/Gizmos/rotationGizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,27 @@ export class RotationGizmo extends Gizmo implements IRotationGizmo {
return this.xGizmo.updateGizmoRotationToMatchAttachedMesh;
}

public set updateGizmoPositionToMatchAttachedMesh(value: boolean) {
if (this.xGizmo) {
this.xGizmo.updateGizmoPositionToMatchAttachedMesh = value;
this.yGizmo.updateGizmoPositionToMatchAttachedMesh = value;
this.zGizmo.updateGizmoPositionToMatchAttachedMesh = value;
}
}
public get updateGizmoPositionToMatchAttachedMesh() {
return this.xGizmo.updateGizmoPositionToMatchAttachedMesh;
}

public set updateScale(value: boolean) {
if (this.xGizmo) {
this.xGizmo.updateScale = value;
this.yGizmo.updateScale = value;
this.zGizmo.updateScale = value;
}
}
public get updateScale() {
return this.xGizmo.updateScale;
}
/**
* Drag distance in babylon units that the gizmo will snap to when dragged (Default: 0)
*/
Expand Down
10 changes: 10 additions & 0 deletions packages/dev/core/src/Gizmos/scaleGizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ export class ScaleGizmo extends Gizmo implements IScaleGizmo {
});
}

public set updateScale(value: boolean) {
if (this.xGizmo) {
this.xGizmo.updateScale = value;
this.yGizmo.updateScale = value;
this.zGizmo.updateScale = value;
}
}
public get updateScale() {
return this.xGizmo.updateScale;
}
/**
* True when the mouse pointer is hovering a gizmo mesh
*/
Expand Down