Fix duplicator functionality.

This commit is contained in:
Michael Mainguy 2024-05-30 11:29:07 -05:00
parent 63d1e627ad
commit fbafd747d3
2 changed files with 52 additions and 35 deletions

View File

@ -24,52 +24,57 @@ import {getMeshType} from "./functions/getMeshType";
const CLICK_TIME = 300; const CLICK_TIME = 300;
export class Base { export class Base {
static stickVector = Vector3.Zero(); static stickVector = Vector3.Zero();
protected readonly scene: Scene;
protected readonly xr: WebXRDefaultExperience;
protected readonly diagramManager: DiagramManager;
protected xrInputSource: WebXRInputSource; protected xrInputSource: WebXRInputSource;
protected speedFactor = 4; protected speedFactor = 4;
protected readonly scene: Scene;
protected grabbedObject: DiagramObject = null; protected grabbedObject: DiagramObject = null;
protected grabbedMesh: AbstractMesh = null; protected grabbedMesh: AbstractMesh = null;
protected grabbedMeshType: MeshTypeEnum = null; protected grabbedMeshType: MeshTypeEnum = null;
private clickStart: number = 0;
protected readonly xr: WebXRDefaultExperience;
protected readonly diagramManager: DiagramManager;
protected controllers: Controllers; protected controllers: Controllers;
private clickMenu: ClickMenu;
private pickPoint: Vector3 = new Vector3(); private readonly _logger = log.getLogger('Base');
private meshUnderPointer: AbstractMesh; private _clickStart: number = 0;
private logger = log.getLogger('Base'); private _clickMenu: ClickMenu;
private _pickPoint: Vector3 = new Vector3();
private _meshUnderPointer: AbstractMesh;
constructor(controller: WebXRInputSource, constructor(controller: WebXRInputSource,
xr: WebXRDefaultExperience, xr: WebXRDefaultExperience,
diagramManager: DiagramManager) { diagramManager: DiagramManager) {
this.logger.debug('Base Controller Constructor called'); this._logger.debug('Base Controller Constructor called');
this.xrInputSource = controller; this.xrInputSource = controller;
this.controllers = diagramManager.controllers; this.controllers = diagramManager.controllers;
this.scene = DefaultScene.Scene; this.scene = DefaultScene.Scene;
this.xr = xr; this.xr = xr;
this.scene.onPointerObservable.add((pointerInfo) => { this.scene.onPointerObservable.add((pointerInfo) => {
if (pointerInfo?.pickInfo?.gripTransform?.id == this.xrInputSource?.grip?.id) {
if (pointerInfo.pickInfo.pickedMesh) { if (pointerInfo.pickInfo.pickedMesh) {
this.meshUnderPointer = pointerInfo.pickInfo.pickedMesh; this._pickPoint.copyFrom(pointerInfo.pickInfo.pickedPoint);
if (this.diagramManager.getDiagramObject(pointerInfo.pickInfo.pickedMesh.id)) { this._meshUnderPointer = pointerInfo.pickInfo.pickedMesh;
this.pickPoint.copyFrom(pointerInfo.pickInfo.pickedPoint); } else {
this._meshUnderPointer = null;
} }
} }
}); });
this.diagramManager = diagramManager; this.diagramManager = diagramManager;
//@TODO THis works, but it uses initGrip, not sure if this is the best idea //@TODO THis works, but it uses initGrip, not sure if this is the best idea
this.xrInputSource.onMotionControllerInitObservable.add(motionControllerObserver, -1, false, this); this.xrInputSource.onMotionControllerInitObservable.add(motionControllerObserver, -1, false, this);
this.controllers.controllerObservable.add((event) => { this.controllers.controllerObservable.add((event) => {
this.logger.debug(event); this._logger.debug(event);
switch (event.type) { switch (event.type) {
case ControllerEventType.PULSE: case ControllerEventType.PULSE:
if (event.gripId == this?.xrInputSource?.grip?.id) { if (event.gripId == this?.xrInputSource?.grip?.id) {
this.xrInputSource?.motionController?.pulse(.35, 50) this.xrInputSource?.motionController?.pulse(.35, 50)
.then(() => { .then(() => {
this.logger.debug("pulse done"); this._logger.debug("pulse done");
}); });
} }
break; break;
@ -96,31 +101,34 @@ export class Base {
} }
protected initClicker(trigger: WebXRControllerComponent) { protected initClicker(trigger: WebXRControllerComponent) {
this.logger.debug("initTrigger"); this._logger.debug("initTrigger");
trigger.onButtonStateChangedObservable.add(() => { trigger.onButtonStateChangedObservable.add(() => {
if (trigger.changes.pressed) { if (trigger.changes.pressed) {
if (trigger.pressed) { if (trigger.pressed) {
if (this.clickStart == 0) { if (this._clickStart == 0) {
this.clickStart = Date.now(); this._clickStart = Date.now();
window.setTimeout(() => { window.setTimeout(() => {
if (this.clickStart > 0) { if (this._clickStart > 0) {
this.logger.debug("grabbing and cloning"); this._logger.debug("grabbing and cloning");
const clone = grabAndClone(this.diagramManager, this.meshUnderPointer, this.xrInputSource.motionController.rootMesh); const clone = grabAndClone(this.diagramManager, this._meshUnderPointer, this.xrInputSource.motionController.rootMesh);
this.grabbedObject = clone; this.grabbedObject = clone;
this.grabbedMesh = clone.mesh; this.grabbedMesh = clone.mesh;
this.grabbedMeshType = getMeshType(clone.mesh, this.diagramManager);
this._meshUnderPointer = clone.mesh;
} }
}, 300, this); }, 300, this);
} }
} else { } else {
const clickEnd = Date.now(); const clickEnd = Date.now();
if (this.clickStart > 0 && (clickEnd - this.clickStart) < CLICK_TIME) { if (this._clickStart > 0 && (clickEnd - this._clickStart) < CLICK_TIME) {
this.click(); this.click();
} else { } else {
if (this.grabbedObject || this.grabbedMesh) { if (this.grabbedObject || this.grabbedMesh) {
this.drop(); this.drop();
} }
} }
this.clickStart = 0; this._clickStart = 0;
} }
} }
}, -1, false, this); }, -1, false, this);
@ -128,7 +136,7 @@ export class Base {
private grab() { private grab() {
let mesh = this.xr.pointerSelection.getMeshUnderPointer(this.xrInputSource.uniqueId); let mesh = this._meshUnderPointer
if (!mesh) { if (!mesh) {
return; return;
} }
@ -165,7 +173,7 @@ export class Base {
case MeshTypeEnum.ENTITY: case MeshTypeEnum.ENTITY:
if (diagramObject) { if (diagramObject) {
diagramObject.baseTransform.setParent(null); diagramObject.baseTransform.setParent(null);
snapAll(this.grabbedObject.baseTransform, this.diagramManager.config, this.pickPoint); snapAll(this.grabbedObject.baseTransform, this.diagramManager.config, this._pickPoint);
diagramObject.mesh.computeWorldMatrix(true); diagramObject.mesh.computeWorldMatrix(true);
const event: DiagramEvent = const event: DiagramEvent =
{ {
@ -182,7 +190,7 @@ export class Base {
break; break;
case MeshTypeEnum.TOOL: case MeshTypeEnum.TOOL:
this.grabbedObject.baseTransform.setParent(null); this.grabbedObject.baseTransform.setParent(null);
snapAll(this.grabbedObject.baseTransform, this.diagramManager.config, this.pickPoint); snapAll(this.grabbedObject.baseTransform, this.diagramManager.config, this._pickPoint);
diagramObject.mesh.computeWorldMatrix(true); diagramObject.mesh.computeWorldMatrix(true);
const event: DiagramEvent = const event: DiagramEvent =
{ {
@ -212,18 +220,18 @@ export class Base {
private click() { private click() {
let mesh = this.xr.pointerSelection.getMeshUnderPointer(this.xrInputSource.uniqueId); let mesh = this.xr.pointerSelection.getMeshUnderPointer(this.xrInputSource.uniqueId);
if (this.diagramManager.isDiagramObject(mesh)) { if (this.diagramManager.isDiagramObject(mesh)) {
this.logger.debug("click on " + mesh.id); this._logger.debug("click on " + mesh.id);
if (this.diagramManager.diagramMenuManager.connectionPreview) { if (this.diagramManager.diagramMenuManager.connectionPreview) {
this.diagramManager.diagramMenuManager.connect(mesh); this.diagramManager.diagramMenuManager.connect(mesh);
} else { } else {
if (this.clickMenu) { if (this._clickMenu) {
this.clickMenu.dispose(); this._clickMenu.dispose();
} }
this.clickMenu = this.diagramManager.diagramMenuManager.createClickMenu(mesh, this.xrInputSource); this._clickMenu = this.diagramManager.diagramMenuManager.createClickMenu(mesh, this.xrInputSource);
} }
} else { } else {
this.logger.debug("click on nothing"); this._logger.debug("click on nothing");
} }
} }

View File

@ -109,9 +109,18 @@ export class DiagramObject {
public clone(): DiagramObject { public clone(): DiagramObject {
const clone = new DiagramObject(this._scene, this._eventObservable, {actionManager: this._mesh.actionManager}); const clone = new DiagramObject(this._scene, this._eventObservable, {actionManager: this._mesh.actionManager});
const newEntity: DiagramEntity = {...this._diagramEntity}; const oldEntity = this._diagramEntity;
newEntity.id = 'id' + uuidv4(); const newEntity: DiagramEntity = {
clone.fromDiagramEntity(this._diagramEntity); id: 'id' + uuidv4(),
position: oldEntity.position,
rotation: oldEntity.rotation,
scale: oldEntity.scale,
template: oldEntity.template,
color: oldEntity.color,
text: oldEntity.text
};
clone.fromDiagramEntity(newEntity);
this._logger.debug('DiagramObject clone called', clone, this._diagramEntity, newEntity); this._logger.debug('DiagramObject clone called', clone, this._diagramEntity, newEntity);
return clone; return clone;
} }