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

Interaction handler keyboard handlers fix #3881

Merged
merged 9 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions cvat-canvas/src/typescript/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface Canvas {
cancel(): void;
configure(configuration: Configuration): void;
isAbleToChangeFrame(): boolean;
destroy(): void;

readonly geometry: Geometry;
}
Expand Down Expand Up @@ -163,6 +164,10 @@ class CanvasImpl implements Canvas {
public get geometry(): Geometry {
return this.model.geometry;
}

public destroy(): void {
this.view.destroy();
}
}

export type InteractionData = _InteractionData;
Expand Down
13 changes: 13 additions & 0 deletions cvat-canvas/src/typescript/canvasView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {

export interface CanvasView {
html(): HTMLDivElement;
destroy(): void;
}

export class CanvasViewImpl implements CanvasView, Listener {
Expand Down Expand Up @@ -1369,6 +1370,18 @@ export class CanvasViewImpl implements CanvasView, Listener {
return this.canvas;
}

public destroy(): void {
this.canvas.dispatchEvent(
new CustomEvent('canvas.destroy', {
bubbles: false,
cancelable: true,
}),
);
// We can't call namespaced svgjs event
// see - https://svgjs.dev/docs/2.7/events/
bsekachev marked this conversation as resolved.
Show resolved Hide resolved
this.adoptedContent.fire('destroy');
}

private redrawBitmap(): void {
const width = +this.background.style.width.slice(0, -2);
const height = +this.background.style.height.slice(0, -2);
Expand Down
44 changes: 26 additions & 18 deletions cvat-canvas/src/typescript/interactionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,27 @@ export class InteractionHandlerImpl implements InteractionHandler {
return false;
}

private onKeyUp = (e:KeyboardEvent):void => {
klakhov marked this conversation as resolved.
Show resolved Hide resolved
if (this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keyup');
}
if (this.shouldRaiseEvent(false)) {
// 17 is ctrl
this.onInteraction(this.prepareResult(), true, false);
}
}
};

private onKeyDown = (e:KeyboardEvent):void => {
klakhov marked this conversation as resolved.
Show resolved Hide resolved
if (!e.repeat && this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keydown');
}
this.thresholdWasModified = false;
}
};

public constructor(
onInteraction: (
shapes: InteractionResult[] | null,
Expand Down Expand Up @@ -452,25 +473,12 @@ export class InteractionHandlerImpl implements InteractionHandler {
}
});

window.addEventListener('keyup', (e: KeyboardEvent): void => {
if (this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keyup');
}
if (this.shouldRaiseEvent(false)) {
// 17 is ctrl
this.onInteraction(this.prepareResult(), true, false);
}
}
});
window.addEventListener('keyup', this.onKeyUp);
window.addEventListener('keydown', this.onKeyDown);

window.addEventListener('keydown', (e: KeyboardEvent): void => {
if (!e.repeat && this.interactionData.enabled && e.keyCode === 17) {
if (this.interactionData.onChangeToolsBlockerState && !this.thresholdWasModified) {
this.interactionData.onChangeToolsBlockerState('keydown');
}
this.thresholdWasModified = false;
}
this.canvas.on('destroy.canvas', ():void => {
window.removeEventListener('keyup', this.onKeyUp);
window.removeEventListener('keydown', this.onKeyDown);
});
}

Expand Down
5 changes: 5 additions & 0 deletions cvat-canvas3d/src/typescript/canvas3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface Canvas3d {
fitCanvas(): void;
fit(): void;
group(groupData: GroupData): void;
destroy(): void;
}

class Canvas3dImpl implements Canvas3d {
Expand Down Expand Up @@ -104,6 +105,10 @@ class Canvas3dImpl implements Canvas3d {
public fitCanvas(): void {
this.model.fit();
}

public destroy(): void {
this.view.destroy();
}
}

export {
Expand Down
55 changes: 32 additions & 23 deletions cvat-canvas3d/src/typescript/canvas3dView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Canvas3dView {
html(): ViewsDOM;
render(): void;
keyControls(keys: KeyboardEvent): void;
destroy(): void;
}

export enum CameraAction {
Expand Down Expand Up @@ -287,6 +288,7 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
(_state: any): boolean => _state.clientID === Number(intersects[0].object.name),
);
if (item.length !== 0) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.model.data.groupData.grouped = this.model.data.groupData.grouped.filter(
(_state: any): boolean => _state.clientID !== Number(intersects[0].object.name),
Expand Down Expand Up @@ -543,9 +545,9 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
this.action.rotation.screenInit = { x: diffX, y: diffY };
this.action.rotation.screenMove = { x: diffX, y: diffY };
if (
this.model.data.selected
&& !this.model.data.selected.perspective.userData.lock
&& !this.model.data.selected.perspective.userData.hidden
this.model.data.selected &&
!this.model.data.selected.perspective.userData.lock &&
!this.model.data.selected.perspective.userData.hidden
) {
this.action.scan = view;
this.model.mode = Mode.EDIT;
Expand Down Expand Up @@ -698,8 +700,8 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
cuboid.setOpacity(opacity);

if (
this.model.data.activeElement.clientID === clientID
&& ![Mode.DRAG_CANVAS, Mode.GROUP].includes(this.mode)
this.model.data.activeElement.clientID === clientID &&
![Mode.DRAG_CANVAS, Mode.GROUP].includes(this.mode)
) {
cuboid.setOpacity(selectedOpacity);
if (!object.lock) {
Expand Down Expand Up @@ -964,12 +966,12 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
const sphereCenter = points.geometry.boundingSphere.center;
const { radius } = points.geometry.boundingSphere;
if (!this.views.perspective.camera) return;
const xRange = -radius / 2 < this.views.perspective.camera.position.x - sphereCenter.x
&& radius / 2 > this.views.perspective.camera.position.x - sphereCenter.x;
const yRange = -radius / 2 < this.views.perspective.camera.position.y - sphereCenter.y
&& radius / 2 > this.views.perspective.camera.position.y - sphereCenter.y;
const zRange = -radius / 2 < this.views.perspective.camera.position.z - sphereCenter.z
&& radius / 2 > this.views.perspective.camera.position.z - sphereCenter.z;
const xRange = -radius / 2 < this.views.perspective.camera.position.x - sphereCenter.x &&
radius / 2 > this.views.perspective.camera.position.x - sphereCenter.x;
const yRange = -radius / 2 < this.views.perspective.camera.position.y - sphereCenter.y &&
radius / 2 > this.views.perspective.camera.position.y - sphereCenter.y;
const zRange = -radius / 2 < this.views.perspective.camera.position.z - sphereCenter.z &&
radius / 2 > this.views.perspective.camera.position.z - sphereCenter.z;
let newX = 0;
let newY = 0;
let newZ = 0;
Expand Down Expand Up @@ -1085,10 +1087,10 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {

private positionAllViews(x: number, y: number, z: number, animation: boolean): void {
if (
this.views.perspective.controls
&& this.views.top.controls
&& this.views.side.controls
&& this.views.front.controls
this.views.perspective.controls &&
this.views.top.controls &&
this.views.side.controls &&
this.views.front.controls
) {
this.views.perspective.controls.setLookAt(x - 8, y - 8, z + 3, x, y, z, animation);
this.views.top.camera.position.set(x, y, z + 8);
Expand Down Expand Up @@ -1266,8 +1268,8 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {

private renderTranslateAction(view: ViewType, viewType: any): void {
if (
this.action.translation.helper.x === this.views[view].rayCaster.mouseVector.x
&& this.action.translation.helper.y === this.views[view].rayCaster.mouseVector.y
this.action.translation.helper.x === this.views[view].rayCaster.mouseVector.x &&
this.action.translation.helper.y === this.views[view].rayCaster.mouseVector.y
) {
return;
}
Expand Down Expand Up @@ -1332,8 +1334,8 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
}

if (
this.action.resize.recentMouseVector.x === currentPosX
&& this.action.resize.recentMouseVector.y === currentPosY
this.action.resize.recentMouseVector.x === currentPosX &&
this.action.resize.recentMouseVector.y === currentPosY
) {
return;
}
Expand Down Expand Up @@ -1736,15 +1738,15 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
y: canvas.offsetTop + canvas.offsetHeight / 2,
};
if (
this.action.rotation.screenInit.x === this.action.rotation.screenMove.x
&& this.action.rotation.screenInit.y === this.action.rotation.screenMove.y
this.action.rotation.screenInit.x === this.action.rotation.screenMove.x &&
this.action.rotation.screenInit.y === this.action.rotation.screenMove.y
) {
return;
}

if (
this.action.rotation.recentMouseVector.x === this.views[view].rayCaster.mouseVector.x
&& this.action.rotation.recentMouseVector.y === this.views[view].rayCaster.mouseVector.y
this.action.rotation.recentMouseVector.x === this.views[view].rayCaster.mouseVector.x &&
this.action.rotation.recentMouseVector.y === this.views[view].rayCaster.mouseVector.y
) {
return;
}
Expand Down Expand Up @@ -1875,4 +1877,11 @@ export class Canvas3dViewImpl implements Canvas3dView, Listener {
front: this.views.front.renderer.domElement,
};
}

public destroy(): void {
this.dispatchEvent(new CustomEvent('canvas.destroy', {
klakhov marked this conversation as resolved.
Show resolved Hide resolved
bubbles: false,
cancelable: true,
}));
}
}
2 changes: 2 additions & 0 deletions cvat-ui/src/reducers/annotation-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export default (state = defaultState, action: AnyAction): AnnotationState => {
activeShapeType = ShapeType.CUBOID;
}

state.canvas.instance.destroy();
bsekachev marked this conversation as resolved.
Show resolved Hide resolved

return {
...state,
job: {
Expand Down