Skip to content

Commit

Permalink
update: disable selection of the Cell object
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Aug 8, 2023
1 parent 1e301e9 commit 8006ea6
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/components/ThreejsEditorModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,35 @@ export class ThreejsEditorModal extends ModalDialog {
false,
);

// on right click and hold disable orbit controls to allow pan
document.addEventListener("mousedown", (event) => {
// on right click and hold disable orbit controls to allow pan
if (event.button === THREE.MOUSE.RIGHT) {
this.editor.controls.enabled = false;
return;
}
});

// on a left click detect if the click was on the object and disable orbit controls in that case
// TODO: read thru and verify. Code was generated by LLM:
document.addEventListener("click", (event) => {
// on a left click detect if the click was on the object and disable orbit controls in that case
if (event.button === THREE.MOUSE.LEFT) {
const mouse = new THREE.Vector2();
// calculate mouse position in normalized device coordinates
// https://stackoverflow.com/questions/13055214/mouse-canvas-x-y-to-three-js-world-x-y-z
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(mouse, this.editor.camera);

const intersects = raycaster.intersectObjects(this.editor.scene.children, true);
if (intersects.length > 0) {
this.editor.controls.enabled = false;
} else {
this.editor.controls.enabled = true;
}

this.editor.controls.enabled = intersects.length === 0;
}
});

// Unselect the "Cell" object to access atoms inside it
this.editor.signals.objectSelected.add((selectedObject) => {
if (selectedObject && selectedObject.name === "Cell") {
this.editor.signals.objectSelected.dispatch(null);
}
console.log("controls.enabled:", this.editor.controls.enabled);
});

// on right click release enable orbit controls
Expand Down

0 comments on commit 8006ea6

Please sign in to comment.