/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2026, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2026 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import { Intersection, Object3D, Vector2, Vector3 } from "three"; import type { IComponent } from "@inweb/viewer-core"; import type { Viewer } from "../Viewer"; import type { IModelImpl } from "../models/ModelImpl.types"; import { Snapper } from "../measurement/Snapper"; export class SelectionComponent implements IComponent { protected viewer: Viewer; protected downPosition: Vector2; constructor(viewer: Viewer) { this.viewer = viewer; this.downPosition = new Vector2(); this.viewer.canvas.addEventListener("pointerdown", this.onPointerDown); this.viewer.canvas.addEventListener("pointerup", this.onPointerUp); this.viewer.canvas.addEventListener("dblclick", this.onDoubleClick); } dispose() { this.viewer.canvas.removeEventListener("pointerdown", this.onPointerDown); this.viewer.canvas.removeEventListener("pointerup", this.onPointerUp); this.viewer.canvas.removeEventListener("dblclick", this.onDoubleClick); } onPointerDown = (event: PointerEvent) => { if (!event.isPrimary || event.button !== 0) return; this.getMousePosition(event, this.downPosition); }; onPointerUp = (event: PointerEvent) => { if (!event.isPrimary || event.button !== 0) return; const upPosition = this.getMousePosition(event, new Vector2()); if (upPosition.distanceTo(this.downPosition) !== 0) return; const extentsSize = this.viewer.extents.getSize(new Vector3()).length() || 1; const snapper = new Snapper(this.viewer.camera, this.viewer.clippingPlanes, this.viewer.canvas); snapper.threshold = extentsSize / 10000; type Intersection2 = Intersection & { model: IModelImpl }; let intersections: Intersection2[] = []; this.viewer.models.forEach((model) => { const objects = model.getVisibleObjects(); const intersects = snapper.getPointerIntersects(upPosition, objects); if (intersects.length > 0) intersections.push({ ...intersects[0], model }); }); intersections = intersections.sort((a, b) => a.distance - b.distance); if (intersections.length > 0) { const model = intersections[0].model; const handles = model.getHandlesByObjects([intersections[0].object]); if (!event.shiftKey) this.viewer.setSelected(handles); else this.viewer.toggleSelection(handles); } else { if (!event.shiftKey) this.viewer.clearSelected(); } }; onDoubleClick = (event: MouseEvent) => { if (event.button !== 0) return; this.viewer.zoomToSelected(); }; getMousePosition(event: MouseEvent, target: Vector2): Vector2 { return target.set(event.clientX, event.clientY); } }