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

Transform controls: undo positioning upon failure #610

Merged
merged 4 commits into from
Nov 29, 2024
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
12 changes: 8 additions & 4 deletions packages/base/src/3dview/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface IMeshGroupMetadata {
[key: string]: any;
}

export interface IMeshGroup extends THREE.Group {
userData: IMeshGroupMetadata;
}

export function projectVector(options: {
vector: THREE.Vector3;
camera: THREE.Camera;
Expand Down Expand Up @@ -162,7 +166,7 @@ export function buildShape(options: {
isWireframe: boolean;
objColor?: THREE.Color | string | number;
}): {
meshGroup: THREE.Group;
meshGroup: IMeshGroup;
mainMesh: THREE.Mesh<THREE.BufferGeometry, THREE.MeshStandardMaterial>;
edgesMeshes: LineSegments2[];
} | null {
Expand Down Expand Up @@ -240,11 +244,12 @@ export function buildShape(options: {
geometry.computeBoundsTree();
}

const meshGroup = new THREE.Group();
const meshGroup = new THREE.Group() as IMeshGroup;
meshGroup.name = `${objName}-group`;
meshGroup.visible = visible;
meshGroup.userData = {
jcObject
jcObject,
type: 'shape'
};

// We only build the stencil logic for solid meshes
Expand Down Expand Up @@ -345,7 +350,6 @@ export function buildShape(options: {
boundingBox.visible = false;
boundingBox.name = SELECTION_BOUNDING_BOX;
meshGroup.add(boundingBox);
meshGroup.userData.type = 'shape';

meshGroup.add(mainMesh);

Expand Down
43 changes: 33 additions & 10 deletions packages/base/src/3dview/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ import {
computeExplodedState,
projectVector,
IMouseDrag,
IMeshGroupMetadata
IMeshGroupMetadata,
getQuaternion
} from './helpers';
import { MainViewModel } from './mainviewmodel';
import { Spinner } from './spinner';
Expand Down Expand Up @@ -417,8 +418,9 @@ export class MainView extends React.Component<IProps, IStates> {
this._controls.enabled = !event.value;
});
// Update the currently transformed object in the shared model once finished moving
this._transformControls.addEventListener('mouseUp', () => {
this._transformControls.addEventListener('mouseUp', async () => {
const updatedObject = this._selectedMeshes[0];

const objectName = updatedObject.name;

const updatedPosition = new THREE.Vector3();
Expand Down Expand Up @@ -451,15 +453,36 @@ export class MainView extends React.Component<IProps, IStates> {
updatedPosition.z
];

this._mainViewModel.maybeUpdateObjectParameters(objectName, {
...obj.parameters,
Placement: {
...obj.parameters.Placement,
Position: newPosition,
Axis: updatedRotation[0],
Angle: updatedRotation[1]
const done = await this._mainViewModel.maybeUpdateObjectParameters(
objectName,
{
...obj.parameters,
Placement: {
...obj.parameters.Placement,
Position: newPosition,
Axis: updatedRotation[0],
Angle: updatedRotation[1]
}
}
});
);
// If the dry run failed, we bring back the object to its original position
if (!done && updatedObject.parent) {
const origPosition = obj.parameters.Placement.Position;

// Undo positioning
updatedObject.parent.position.copy(new THREE.Vector3(0, 0, 0));
updatedObject.parent.applyQuaternion(updatedQuaternion.invert());

// Redo original positioning
updatedObject.parent.applyQuaternion(getQuaternion(obj));
updatedObject.parent.position.copy(
new THREE.Vector3(
origPosition[0],
origPosition[1],
origPosition[2]
)
);
}
}
});
this._scene.add(this._transformControls);
Expand Down
6 changes: 4 additions & 2 deletions packages/base/src/3dview/mainviewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class MainViewModel implements IDisposable {
async maybeUpdateObjectParameters(
name: string,
properties: { [key: string]: any }
): Promise<void> {
): Promise<boolean> {
// getContent already returns a deep copy of the content, we can change it safely here
const updatedContent = this.jcadModel.getContent();
for (const object of updatedContent.objects) {
Expand All @@ -197,7 +197,7 @@ export class MainViewModel implements IDisposable {
'Failed to update the desired shape',
'The tool was unable to update the desired shape due to invalid parameter values. The values you entered may not be compatible with the dimensions of your piece.'
);
return;
return false;
}

// Dry run was successful, ready to apply the update now
Expand All @@ -215,6 +215,8 @@ export class MainViewModel implements IDisposable {
meta
});
}

return true;
}

/**
Expand Down
Loading