Skip to content

Commit 8328c77

Browse files
Fix selection logic (#569)
* Selection logic improvements * Rename * Simplify + rename + detach transform control upon selecting an edge * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3879356 commit 8328c77

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

packages/base/src/3dview/mainview.tsx

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ interface IProps {
5959
const CAMERA_NEAR = 1e-6;
6060
const CAMERA_FAR = 1e27;
6161

62+
// The amount of pixels a mouse move can do until we stop considering it's a click
63+
const CLICK_THRESHOLD = 5;
64+
6265
interface IStates {
6366
id: string; // ID of the component, it is used to identify which component
6467
//is the source of awareness updates.
@@ -265,13 +268,6 @@ export class MainView extends React.Component<IProps, IStates> {
265268
'pointermove',
266269
this._onPointerMove.bind(this)
267270
);
268-
this._renderer.domElement.addEventListener('mouseup', e => {
269-
if (!this._disabledNextClick) {
270-
this._onClick(e);
271-
}
272-
273-
this._disabledNextClick = false;
274-
});
275271

276272
this._renderer.domElement.addEventListener('contextmenu', e => {
277273
e.preventDefault();
@@ -296,42 +292,25 @@ export class MainView extends React.Component<IProps, IStates> {
296292
this._controls.enableDamping = true;
297293
this._controls.dampingFactor = 0.15;
298294

299-
const startMousePosition = new THREE.Vector2();
300-
const endMousePosition = new THREE.Vector2();
301-
const clickThreshold = 5;
302-
303-
this._controls.addEventListener('start', () => {
304-
this._hasOrbited = false;
305-
});
306-
this._controls.addEventListener('end', () => {
307-
// This "change" event here happens before the "mouseup" event on the renderer,
308-
// we need to disable that next "mouseup" event that's coming to not deselect
309-
// any currently selected mesh un-intentionally
310-
if (this._hasOrbited) {
311-
this._disabledNextClick = true;
312-
}
313-
});
314-
315-
this._controls.addEventListener('change', () => {
316-
this._hasOrbited = true;
317-
this._updateAnnotation();
318-
});
319-
320295
this._renderer.domElement.addEventListener('mousedown', e => {
321-
startMousePosition.set(e.clientX, e.clientY);
296+
this._startMousePosition.set(e.clientX, e.clientY);
322297
});
323298

324299
this._renderer.domElement.addEventListener('mouseup', e => {
325-
endMousePosition.set(e.clientX, e.clientY);
326-
const distance = endMousePosition.distanceTo(startMousePosition);
300+
this._endMousePosition.set(e.clientX, e.clientY);
301+
const distance = this._endMousePosition.distanceTo(
302+
this._startMousePosition
303+
);
327304

328-
if (distance !== 0 && distance <= clickThreshold) {
305+
if (distance <= CLICK_THRESHOLD) {
329306
this._onClick(e);
330-
} else if (this._disabledNextClick) {
331-
this._disabledNextClick = false;
332307
}
333308
});
334309

310+
this._controls.addEventListener('change', () => {
311+
this._updateAnnotation();
312+
});
313+
335314
this._controls.addEventListener(
336315
'change',
337316
throttle(() => {
@@ -818,6 +797,13 @@ export class MainView extends React.Component<IProps, IStates> {
818797
}
819798

820799
if (selected) {
800+
const boundingBox = meshGroup?.getObjectByName(
801+
SELECTION_BOUNDING_BOX
802+
) as THREE.Mesh;
803+
if (boundingBox) {
804+
boundingBox.visible = true;
805+
}
806+
821807
this._selectedMeshes.push(mainMesh);
822808
}
823809
edgesMeshes.forEach(el => {
@@ -857,6 +843,8 @@ export class MainView extends React.Component<IProps, IStates> {
857843
});
858844
this._meshGroup?.add(meshGroup);
859845
}
846+
847+
this._updateTransformControls(selectedNames);
860848
});
861849

862850
// Update the reflength.
@@ -1132,9 +1120,6 @@ export class MainView extends React.Component<IProps, IStates> {
11321120
if (material?.linewidth) {
11331121
material.linewidth = DEFAULT_LINEWIDTH;
11341122
}
1135-
1136-
// Detach TransformControls from the previous selection
1137-
this._transformControls.detach();
11381123
}
11391124

11401125
// Set new selection
@@ -1185,8 +1170,15 @@ export class MainView extends React.Component<IProps, IStates> {
11851170
}
11861171
}
11871172

1188-
if (selectedNames.length === 1) {
1189-
const selectedMeshName = selectedNames[0];
1173+
this._updateTransformControls(selectedNames);
1174+
}
1175+
1176+
/*
1177+
* Attach the transform controls to the current selection, or detach it
1178+
*/
1179+
private _updateTransformControls(selection: string[]) {
1180+
if (selection.length === 1) {
1181+
const selectedMeshName = selection[0];
11901182
const matchingChild = this._meshGroup?.children.find(child =>
11911183
child.name.startsWith(selectedMeshName)
11921184
);
@@ -1208,8 +1200,16 @@ export class MainView extends React.Component<IProps, IStates> {
12081200

12091201
this._transformControls.visible = true;
12101202
this._transformControls.enabled = true;
1203+
1204+
return;
12111205
}
12121206
}
1207+
1208+
// Detach TransformControls from the previous selection
1209+
this._transformControls.detach();
1210+
1211+
this._transformControls.visible = false;
1212+
this._transformControls.enabled = false;
12131213
}
12141214

12151215
private _onSharedMetadataChanged = (
@@ -1712,9 +1712,9 @@ export class MainView extends React.Component<IProps, IStates> {
17121712
private _geometry: THREE.BufferGeometry; // Threejs BufferGeometry
17131713
private _refLength: number | null = null; // Length of bounding box of current object
17141714
private _sceneAxe: THREE.Object3D | null; // Array of X, Y and Z axe
1715-
private _controls: OrbitControls; // Mouse controls
1716-
private _hasOrbited = false; // Whether the last orbit control run has actually orbited
1717-
private _disabledNextClick = false; // We set this when we stop orbiting, to prevent the next click event
1715+
private _controls: OrbitControls; // Camera controls
1716+
private _startMousePosition = new THREE.Vector2(); // Start mouse position when dragging the camera controls
1717+
private _endMousePosition = new THREE.Vector2(); // End mouse position when dragging the camera controls
17181718
private _clipPlaneTransformControls: TransformControls; // Clip plane position/rotation controls
17191719
private _transformControls: TransformControls; // Mesh position controls
17201720
private _pointer3D: IPointer | null = null;

0 commit comments

Comments
 (0)