Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Jul 7, 2023
1 parent 8c7e481 commit bbb2a90
Show file tree
Hide file tree
Showing 35 changed files with 3,872 additions and 4,239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class AngleMeasurementsControl extends Component {
const touchEndCanvasPos = math.vec2();
const touchStartWorldPos = math.vec3();

this._onMouseHoverSurface = cameraControl.on("hoverSurface", event => {
this._onMouseHoverSurface = cameraControl.on("hoverOverEntitySurface", event => {
isMouseHoveringEntity = true;
mouseHoverEntity = event.entity;
mouseWorldPos.set(event.worldPos);
Expand Down Expand Up @@ -222,7 +222,7 @@ class AngleMeasurementsControl extends Component {
}
});

this._onHoverNothing = cameraControl.on("hoverOff", event => {
this._onHoverNothing = cameraControl.on("hoverNothing", event => {

isMouseHoveringEntity = false;
if (this._currentAngleMeasurement) {
Expand Down
177 changes: 96 additions & 81 deletions src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,14 @@ class DistanceMeasurementsControl extends Component {
const input = scene.input;
const startDot = this._touchStartDot;

const pickSurfacePrecisionEnabled = scene.pickSurfacePrecisionEnabled;

let mouseHoverEntity = null;
const mouseWorldPos = math.vec3();
const mouseCanvasPos = math.vec2();

const surfaceWorldPos = math.vec3();
const surfaceCanvasPos = math.vec2();

const snappedWorldPos = math.vec3();
const snappedCanvasPos = math.vec2();


let lastMouseCanvasX;
let lastMouseCanvasY;
Expand All @@ -171,13 +174,17 @@ class DistanceMeasurementsControl extends Component {
const touchEndCanvasPos = math.vec2();
const touchStartWorldPos = math.vec3();

let snapped = false;

this._onMouseHoverSurface = cameraControl.on("hoverOverEntitySurface", event => {

snapped = false;

// This gets fired for both mouse and touch input, but we don't care when handling touch
mouseHoverEntity = true;

mouseWorldPos.set(event.worldPos);
mouseCanvasPos.set(event.canvasPos);
surfaceWorldPos.set(event.worldPos);
surfaceCanvasPos.set(event.canvasPos);

if (touchState === FIRST_TOUCH_EXPECTED) {
this.markerDiv.style.marginLeft = `${event.canvasPos[0] - 5}px`;
Expand All @@ -201,18 +208,21 @@ class DistanceMeasurementsControl extends Component {
this._currentDistanceMeasurementByMouse.yAxisVisible = this._currentDistanceMeasurementByMouseInittouchState.yAxisVisible && this.plugin.defaultYAxisVisible;
this._currentDistanceMeasurementByMouse.zAxisVisible = this._currentDistanceMeasurementByMouseInittouchState.zAxisVisible && this.plugin.defaultZAxisVisible;
this._currentDistanceMeasurementByMouse.targetVisible = this._currentDistanceMeasurementByMouseInittouchState.targetVisible;
// this._currentDistanceMeasurementByMouse.target.entity = mouseHoverEntity;
this._currentDistanceMeasurementByMouse.target.worldPos = mouseWorldPos;
this._currentDistanceMeasurementByMouse.target.worldPos = surfaceWorldPos.slice();
}
});

this._onMouseHoverSurface = cameraControl.on("pickedVertex", event => {

console.log("pickedVertex");

snapped = true;

// This gets fired for both mouse and touch input, but we don't care when handling touch
mouseHoverEntity = true;

mouseWorldPos.set(event.snappedWorldPos);
mouseCanvasPos.set(event.snappedCanvasPos);
snappedWorldPos.set(event.snappedWorldPos);
snappedCanvasPos.set(event.snappedCanvasPos);

if (touchState === FIRST_TOUCH_EXPECTED) {
this.markerDiv.style.marginLeft = `${event.snappedCanvasPos[0] - 5}px`;
Expand All @@ -223,8 +233,8 @@ class DistanceMeasurementsControl extends Component {
}
// } else {
// if (event.worldPos !== null && event.canvasPos !== null) {
// mouseWorldPos.set(event.worldPos);
// mouseCanvasPos.set(event.canvasPos);
// surfaceWorldPos.set(event.worldPos);
// surfaceCanvasPos.set(event.canvasPos);
//
// if (touchState === FIRST_TOUCH_EXPECTED) {
// this.markerDiv.style.marginLeft = `${event.canvasPos[0]-5}px`;
Expand All @@ -250,25 +260,28 @@ class DistanceMeasurementsControl extends Component {
this._currentDistanceMeasurementByMouse.yAxisVisible = this._currentDistanceMeasurementByMouseInittouchState.yAxisVisible && this.plugin.defaultYAxisVisible;
this._currentDistanceMeasurementByMouse.zAxisVisible = this._currentDistanceMeasurementByMouseInittouchState.zAxisVisible && this.plugin.defaultZAxisVisible;
this._currentDistanceMeasurementByMouse.targetVisible = this._currentDistanceMeasurementByMouseInittouchState.targetVisible;
// this._currentDistanceMeasurementByMouse.target.entity = mouseHoverEntity;
this._currentDistanceMeasurementByMouse.target.worldPos = mouseWorldPos;
this._currentDistanceMeasurementByMouse.target.worldPos = surfaceWorldPos;
}
});

this._onInputMouseDown = input.on("mousedown", (coords) => {
console.log("mousedown");
lastMouseCanvasX = coords[0];
lastMouseCanvasY = coords[1];
});

this._onInputMouseUp = input.on("mouseup", (coords) => {
console.log("mouseup snapped = " + snapped);
if (coords[0] > lastMouseCanvasX + mouseCanvasClickTolerance ||
coords[0] < lastMouseCanvasX - mouseCanvasClickTolerance ||
coords[1] > lastMouseCanvasY + mouseCanvasClickTolerance ||
coords[1] < lastMouseCanvasY - mouseCanvasClickTolerance) {
return;
}
const worldPos = snapped ? snappedWorldPos.slice() : surfaceWorldPos.slice();
if (this._currentDistanceMeasurementByMouse) {
if (mouseHoverEntity) {
this._currentDistanceMeasurementByMouse.target.worldPos = worldPos;
this._currentDistanceMeasurementByMouse.clickable = true;
this.fire("measurementEnd", this._currentDistanceMeasurementByMouse);
this._currentDistanceMeasurementByMouse = null;
Expand All @@ -282,10 +295,10 @@ class DistanceMeasurementsControl extends Component {
this._currentDistanceMeasurementByMouse = plugin.createMeasurement({
id: math.createUUID(),
origin: {
worldPos: mouseWorldPos
worldPos: worldPos.slice()
},
target: {
worldPos: mouseWorldPos
worldPos: worldPos.slice()
},
approximate: true
});
Expand All @@ -304,8 +317,9 @@ class DistanceMeasurementsControl extends Component {
});

this._onMouseHoverOff = cameraControl.on("hoverNothing", event => {
console.log("hoverNothing");
mouseHoverEntity = null;

snapped = false;
this.markerDiv.style.marginLeft = `-100px`;
this.markerDiv.style.marginTop = `-100px`;

Expand All @@ -318,72 +332,73 @@ class DistanceMeasurementsControl extends Component {
});

this._onPickedNothing = cameraControl.on("pickedNothing", event => {
if (this._currentDistanceMeasurementByMouse) {
this.fire("measurementCancel", this._currentDistanceMeasurementByMouse);
this._currentDistanceMeasurementByMouse.destroy();
this._currentDistanceMeasurementByMouse = null;
}
startDot.setVisible(false);
touchState = FIRST_TOUCH_EXPECTED;
console.log("pickedNothing");
// if (this._currentDistanceMeasurementByMouse) {
// this.fire("measurementCancel", this._currentDistanceMeasurementByMouse);
// this._currentDistanceMeasurementByMouse.destroy();
// this._currentDistanceMeasurementByMouse = null;
// }
// startDot.setVisible(false);
// touchState = FIRST_TOUCH_EXPECTED;
});

canvas.addEventListener("touchstart", this._onCanvasTouchStart = (event) => {
const touches = event.touches;
const changedTouches = event.changedTouches;
if (touches.length === 1 && changedTouches.length === 1) {
getCanvasPosFromEvent(touches[0], touchStartCanvasPos);
}
}, {passive: true});

canvas.addEventListener("touchend", this._onCanvasTouchEnd = (event) => {
const touches = event.touches;
const changedTouches = event.changedTouches;
if (touches.length === 0 && changedTouches.length === 1) {
getCanvasPosFromEvent(changedTouches[0], touchEndCanvasPos);
if (touchEndCanvasPos[0] > touchStartCanvasPos[0] + touchCanvasClickTolerance ||
touchEndCanvasPos[0] < touchStartCanvasPos[0] - touchCanvasClickTolerance ||
touchEndCanvasPos[1] > touchStartCanvasPos[1] + touchCanvasClickTolerance ||
touchEndCanvasPos[1] < touchStartCanvasPos[1] - touchCanvasClickTolerance) {
return; // User is repositioning the camera or model
}
const pickResult = scene.pick({
canvasPos: touchEndCanvasPos,
pickSurface: true,
pickSurfacePrecision: pickSurfacePrecisionEnabled
});
if (pickResult && pickResult.worldPos) {
switch (touchState) {
case FIRST_TOUCH_EXPECTED:
startDot.setVisible(true);
this._touchStartMarker.worldPos = pickResult.worldPos;
touchStartWorldPos.set(pickResult.worldPos);
touchState = SECOND_TOUCH_EXPECTED;
break;
case SECOND_TOUCH_EXPECTED:
startDot.setVisible(false);
this._touchStartMarker.worldPos = pickResult.worldPos;
const measurement = plugin.createMeasurement({
id: math.createUUID(),
origin: {
worldPos: touchStartWorldPos
},
target: {
worldPos: pickResult.worldPos
},
approximate: (!pickSurfacePrecisionEnabled)
});
measurement.clickable = true;
touchState = FIRST_TOUCH_EXPECTED;
this.fire("measurementEnd", measurement);
break;
}
} else {
startDot.setVisible(false);
touchState = FIRST_TOUCH_EXPECTED;
}
}
// event.stopPropagation();
}, {passive: true});
// canvas.addEventListener("touchstart", this._onCanvasTouchStart = (event) => {
// const touches = event.touches;
// const changedTouches = event.changedTouches;
// if (touches.length === 1 && changedTouches.length === 1) {
// getCanvasPosFromEvent(touches[0], touchStartCanvasPos);
// }
// }, {passive: true});
//
// canvas.addEventListener("touchend", this._onCanvasTouchEnd = (event) => {
// const touches = event.touches;
// const changedTouches = event.changedTouches;
// if (touches.length === 0 && changedTouches.length === 1) {
// getCanvasPosFromEvent(changedTouches[0], touchEndCanvasPos);
// if (touchEndCanvasPos[0] > touchStartCanvasPos[0] + touchCanvasClickTolerance ||
// touchEndCanvasPos[0] < touchStartCanvasPos[0] - touchCanvasClickTolerance ||
// touchEndCanvasPos[1] > touchStartCanvasPos[1] + touchCanvasClickTolerance ||
// touchEndCanvasPos[1] < touchStartCanvasPos[1] - touchCanvasClickTolerance) {
// return; // User is repositioning the camera or model
// }
// const pickResult = scene.pick({
// canvasPos: touchEndCanvasPos,
// pickSurface: true,
// pickSurfacePrecision: pickSurfacePrecisionEnabled
// });
// if (pickResult && pickResult.worldPos) {
// switch (touchState) {
// case FIRST_TOUCH_EXPECTED:
// startDot.setVisible(true);
// this._touchStartMarker.worldPos = pickResult.worldPos;
// touchStartWorldPos.set(pickResult.worldPos);
// touchState = SECOND_TOUCH_EXPECTED;
// break;
// case SECOND_TOUCH_EXPECTED:
// startDot.setVisible(false);
// this._touchStartMarker.worldPos = pickResult.worldPos;
// const measurement = plugin.createMeasurement({
// id: math.createUUID(),
// origin: {
// worldPos: touchStartWorldPos
// },
// target: {
// worldPos: pickResult.worldPos
// },
// approximate: (!pickSurfacePrecisionEnabled)
// });
// measurement.clickable = true;
// touchState = FIRST_TOUCH_EXPECTED;
// this.fire("measurementEnd", measurement);
// break;
// }
// } else {
// startDot.setVisible(false);
// touchState = FIRST_TOUCH_EXPECTED;
// }
// }
// // event.stopPropagation();
// }, {passive: true});

this._active = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/SectionPlanesPlugin/Control.js
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ class Control {
grabbed = true;
});

this._onCameraControlHoverLeave = this._viewer.cameraControl.on("hoverOut", (hit) => {
this._onCameraControlHoverLeave = this._viewer.cameraControl.on("hoverOutEntity", (hit) => {
if (!this._visible) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/viewer/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class Viewer {
saoEnabled: cfg.saoEnabled,
alphaDepthMask: (cfg.alphaDepthMask !== false),
entityOffsetsEnabled: (!!cfg.entityOffsetsEnabled),
pickSurfacePrecisionEnabled: (!!cfg.pickSurfacePrecisionEnabled),
logarithmicDepthBufferEnabled: (!!cfg.logarithmicDepthBufferEnabled),
pbrEnabled: (!!cfg.pbrEnabled),
colorTextureEnabled: (cfg.colorTextureEnabled !== false)
Expand Down
3 changes: 0 additions & 3 deletions src/viewer/scene/CameraControl/lib/CameraUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,10 @@ class CameraUpdater {
updates.dollyDelta *= configs.dollyInertia;
}

pickController.fireEvents();

document.body.style.cursor = cursorType;
});
}


destroy() {
this._scene.off(this._onTick);
}
Expand Down
Loading

0 comments on commit bbb2a90

Please sign in to comment.