Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify annotations update #146

Merged
merged 1 commit into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 28 additions & 42 deletions packages/jupytercad-extension/src/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,8 @@ export class MainView extends React.Component<IProps, IStates> {
this._onPointerMove.bind(this)
);
this._renderer.domElement.addEventListener('mouseup', e => {
this._updateAnnotation({ updateDisplay: 1, updatePosition: true });
this._onClick.bind(this)(e);
});
this._renderer.domElement.addEventListener('mousedown', e => {
this._updateAnnotation({ updateDisplay: 0 });
});

this._renderer.domElement.addEventListener('contextmenu', e => {
e.preventDefault();
Expand All @@ -292,11 +288,12 @@ export class MainView extends React.Component<IProps, IStates> {
);
this._controls = controls;

this._controls.addEventListener('change', () => {
this._updateAnnotation();
});
this._controls.addEventListener(
'change',
throttle(() => {
this._updateAnnotation({ updatePosition: true });

// Not syncing camera state if following someone else
if (this._model.localState?.remoteUser) {
return;
Expand Down Expand Up @@ -406,52 +403,41 @@ export class MainView extends React.Component<IProps, IStates> {
);
};

private _updateAnnotation = (options: {
updatePosition?: boolean;
updateDisplay?: number;
}) => {
private _updateAnnotation() {
Object.keys(this.state.annotations).forEach(key => {
const el = document.getElementById(key);
if (el) {
if (
options.updatePosition &&
(el.style.opacity !== '0' || options.updateDisplay !== undefined)
) {
const annotation = this._model.annotationModel?.getAnnotation(key);
let screenPosition = new THREE.Vector2();

if (annotation) {
const parent = this._meshGroup?.getObjectByName(
annotation.parent
) as BasicMesh;
const position = new THREE.Vector3(
annotation.position[0],
annotation.position[1],
annotation.position[2]
);
const annotation = this._model.annotationModel?.getAnnotation(key);
let screenPosition = new THREE.Vector2();

// If in exploded view, we explode the annotation position as well
if (this._explodedView.enabled && parent) {
const explodedState = this._computeExplodedState(parent);
const explodeVector = explodedState.vector.multiplyScalar(
explodedState.distance
);
if (annotation) {
const parent = this._meshGroup?.getObjectByName(
annotation.parent
) as BasicMesh;
const position = new THREE.Vector3(
annotation.position[0],
annotation.position[1],
annotation.position[2]
);

position.add(explodeVector);
}
// If in exploded view, we explode the annotation position as well
if (this._explodedView.enabled && parent) {
const explodedState = this._computeExplodedState(parent);
const explodeVector = explodedState.vector.multiplyScalar(
explodedState.distance
);

screenPosition = this._projectVector(position);
position.add(explodeVector);
}

el.style.left = `${screenPosition.x}px`;
el.style.top = `${screenPosition.y}px`;
}
if (options.updateDisplay !== undefined) {
el.style.opacity = options.updateDisplay.toString();
screenPosition = this._projectVector(position);
}

el.style.left = `${Math.round(screenPosition.x)}px`;
el.style.top = `${Math.round(screenPosition.y)}px`;
}
});
};
}

private _onPointerMove(e: MouseEvent) {
const rect = this._renderer.domElement.getBoundingClientRect();
Expand Down Expand Up @@ -1065,7 +1051,7 @@ export class MainView extends React.Component<IProps, IStates> {

private _handleWindowResize = (): void => {
this.resizeCanvasToDisplaySize();
this._updateAnnotation({ updatePosition: true, updateDisplay: 1 });
this._updateAnnotation();
};

render(): JSX.Element {
Expand Down