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

add drag and drop for rect #155

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added public/ico/scale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/data/enums/CustomCursorStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export enum CustomCursorStyle {
CANCEL = "CANCEL",
CLOSE = "CLOSE",
GRAB = "GRAB",
GRABBING = "GRABBING"
GRABBING = "GRABBING",
DRAG = "DRAG"
}
3 changes: 3 additions & 0 deletions src/logic/actions/LabelActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export class LabelActions {
case LabelType.POLYGON:
LabelActions.deletePolygonLabelById(imageId, labelId);
break;
case LabelType.LINE:
LabelActions.deleteLineLabelById(imageId, labelId);
break;
}
}

Expand Down
58 changes: 54 additions & 4 deletions src/logic/render/RectRenderEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export class RectRenderEngine extends BaseRenderEngine {

private startCreateRectPoint: IPoint;
private startResizeRectAnchor: RectAnchor;
private startDragRectPoint: IPoint;
private DraggingRect: IRect;

public constructor(canvas: HTMLCanvasElement) {
super(canvas);
Expand All @@ -58,8 +60,7 @@ export class RectRenderEngine extends BaseRenderEngine {
} else {
if (!!LabelsSelector.getHighlightedLabelId())
store.dispatch(updateActiveLabelId(LabelsSelector.getHighlightedLabelId()));
else
this.startRectCreation(data.mousePositionOnViewPortContent);
this.startDragRect(data.mousePositionOnViewPortContent, RectUtil.translate(rect, data.viewPortContentImageRect))
}
} else if (isMouseOverImage) {

Expand Down Expand Up @@ -105,6 +106,31 @@ export class RectRenderEngine extends BaseRenderEngine {
});
store.dispatch(updateImageDataById(imageData.id, imageData));
}

if (!!this.startDragRectPoint && !!this.DraggingRect && !!activeLabelRect) {
const rect: IRect = this.calculateRectRelativeToActiveImage(activeLabelRect.rect, data);
const startPosition: IPoint = {
x: this.startDragRectPoint.x,
y: this.startDragRectPoint.y
};
const delta: IPoint = PointUtil.subtract(mousePositionSnapped, startPosition);
const removeRect: IRect = RectUtil.moveRect(rect, delta);
const scale: number = RenderEngineUtil.calculateImageScale(data);
const scaledRect: IRect = RectUtil.scaleRect(removeRect, scale);
const imageData = LabelsSelector.getActiveImageData();
imageData.labelRects = imageData.labelRects.map((labelRect: LabelRect) => {
if (labelRect.id === activeLabelRect.id) {
return {
...labelRect,
rect: scaledRect
};
}
return labelRect;
});
store.dispatch(updateImageDataById(imageData.id, imageData));
}


}
this.endRectTransformation()
};
Expand Down Expand Up @@ -157,6 +183,17 @@ export class RectRenderEngine extends BaseRenderEngine {
};
const activeRectBetweenPixels = RenderEngineUtil.setRectBetweenPixels(activeRect);
DrawUtil.drawRect(this.canvas, activeRectBetweenPixels, this.config.lineActiveColor, this.config.lineThickness);
}
if (!!this.startDragRectPoint && !!this.DraggingRect) {
const mousePositionSnapped: IPoint = RectUtil.snapPointToRect(mousePosition, imageRect);
const activeRect: IRect = {
x: this.DraggingRect.x + (mousePositionSnapped.x - this.startDragRectPoint.x),
y: this.DraggingRect.y + (mousePositionSnapped.y - this.startDragRectPoint.y),
width: this.DraggingRect.width,
height: this.DraggingRect.height
};
const activeRectBetweenPixels = RenderEngineUtil.setRectBetweenPixels(activeRect);
DrawUtil.drawRect(this.canvas, activeRectBetweenPixels, this.config.lineActiveColor, this.config.lineThickness);
}
}

Expand Down Expand Up @@ -197,10 +234,15 @@ export class RectRenderEngine extends BaseRenderEngine {
if (!!this.canvas && !!data.mousePositionOnViewPortContent && !GeneralSelector.getImageDragModeStatus()) {
const rectUnderMouse: LabelRect = this.getRectUnderMouse(data);
const rectAnchorUnderMouse: RectAnchor = this.getAnchorUnderMouse(data);
if ((!!rectAnchorUnderMouse && rectUnderMouse && rectUnderMouse.status === LabelStatus.ACCEPTED) || !!this.startResizeRectAnchor) {
store.dispatch(updateCustomCursorStyle(CustomCursorStyle.MOVE));
if(rectUnderMouse){
if ((!!rectAnchorUnderMouse && rectUnderMouse.status === LabelStatus.ACCEPTED) || !!this.startResizeRectAnchor) {
store.dispatch(updateCustomCursorStyle(CustomCursorStyle.MOVE));
}else if(rectUnderMouse.status === LabelStatus.ACCEPTED || !!this.startDragRectPoint) {
store.dispatch(updateCustomCursorStyle(CustomCursorStyle.DRAG));
}
return;
}

else if (RenderEngineUtil.isMouseOverCanvas(data)) {
if (!RenderEngineUtil.isMouseOverImage(data) && !!this.startCreateRectPoint)
store.dispatch(updateCustomCursorStyle(CustomCursorStyle.MOVE));
Expand Down Expand Up @@ -303,9 +345,17 @@ export class RectRenderEngine extends BaseRenderEngine {
EditorActions.setViewPortActionsDisabledStatus(true);
}

private startDragRect(mousePosition: IPoint, rect: IRect) {
this.startDragRectPoint = mousePosition;
this.DraggingRect = rect
EditorActions.setViewPortActionsDisabledStatus(true);
}

private endRectTransformation() {
this.startCreateRectPoint = null;
this.startResizeRectAnchor = null;
this.startDragRectPoint = null;
this.DraggingRect = null;
EditorActions.setViewPortActionsDisabledStatus(false);
}
}
5 changes: 4 additions & 1 deletion src/utils/EditorUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class EditorUtil {
return "ico/close.png";
case CustomCursorStyle.MOVE:
return "ico/move.png";
case CustomCursorStyle.DRAG:
return "ico/move.png";
case CustomCursorStyle.CANCEL:
return "ico/cancel.png";
case CustomCursorStyle.GRAB:
Expand All @@ -32,7 +34,8 @@ export class EditorUtil {
"close": cursorStyle === CustomCursorStyle.CLOSE,
"cancel": cursorStyle === CustomCursorStyle.CANCEL,
"grab": cursorStyle === CustomCursorStyle.GRAB,
"grabbing": cursorStyle === CustomCursorStyle.GRABBING
"grabbing": cursorStyle === CustomCursorStyle.GRABBING,
"drag": cursorStyle === CustomCursorStyle.DRAG
}
);
};
Expand Down
9 changes: 9 additions & 0 deletions src/utils/RectUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export class RectUtil {
return rect;
}

public static moveRect(inputRect: IRect, delta): IRect {
const rect: IRect = { ...inputRect };
return {
...rect,
x: rect.x + delta.x,
y: rect.y + delta.y
}
}

public static translate(rect: IRect, delta: IPoint): IRect {
return {
...rect,
Expand Down
2 changes: 1 addition & 1 deletion src/views/EditorView/Editor/Editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
border: 2px solid transparent;
}

&.move, &.add, &.resize, &.close, &.cancel, &.grab, &.grabbing {
&.drag, &.move, &.add, &.resize, &.close, &.cancel, &.grab, &.grabbing {
> img {
display: block;
}
Expand Down