From b81446e99d1f8194b56e6b8d5bd9650e467e7525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 17 Jan 2023 23:50:25 +0000 Subject: [PATCH] readability --- ui/src/components/rects/RectPad.vue | 27 ++++++++++----------------- ui/src/components/rects/rectangles.ts | 9 +++++++++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ui/src/components/rects/RectPad.vue b/ui/src/components/rects/RectPad.vue index 2b99fbe..bb185c1 100644 --- a/ui/src/components/rects/RectPad.vue +++ b/ui/src/components/rects/RectPad.vue @@ -34,7 +34,7 @@ function get_event_thous(event: MouseEvent): Vector2D { return new Vector2D(); } - let target = event.currentTarget as Element; + const target = event.currentTarget as Element; return new Vector2D( Math.round((event.offsetX / target.clientWidth) * 1000), @@ -69,20 +69,13 @@ export default class RectPad extends Vue { } private pop_rectangle(point: Vector2D): Rectangle | undefined { - const idx = this.rectangles.findIndex( - (rect) => - point.x >= rect.left && - point.y >= rect.top && - point.x <= rect.left + rect.width && - point.y <= rect.top + rect.height - ); + const idx = this.rectangles.findIndex((rect) => rect.contains(point)); if (idx === -1) { return; } - const rects = this.rectangles.splice(idx, 1); - return rects[0]; + return this.rectangles.splice(idx, 1)[0]; } private draw_start(event: MouseEvent) { @@ -102,9 +95,11 @@ export default class RectPad extends Vue { this.drawing = false; - if (this.preview_rectangle.area >= this.min_rect_area) { - this.rectangles.push(this.preview_rectangle); + if (this.preview_rectangle.area < this.min_rect_area) { + return; } + + this.rectangles.push(this.preview_rectangle); } private drag_start(event: MouseEvent) { @@ -112,7 +107,7 @@ export default class RectPad extends Vue { return; } - let point = get_event_thous(event); + const point = get_event_thous(event); this.drag_rect = this.pop_rectangle(point); if (this.drag_rect === undefined) { @@ -136,12 +131,10 @@ export default class RectPad extends Vue { } private on_mousemove(event: MouseEvent) { - let point = get_event_thous(event); - if (this.drawing) { - this.preview_corner2 = point; + this.preview_corner2 = get_event_thous(event); } else if (this.dragging && this.drag_rect) { - let movement = point.minus(this.drag_origin); + const movement = get_event_thous(event).minus(this.drag_origin); this.preview_corner1 = this.drag_rect.origin.plus(movement); this.preview_corner2 = this.drag_rect.corner.plus(movement); diff --git a/ui/src/components/rects/rectangles.ts b/ui/src/components/rects/rectangles.ts index 329183b..60649a6 100644 --- a/ui/src/components/rects/rectangles.ts +++ b/ui/src/components/rects/rectangles.ts @@ -64,6 +64,15 @@ export class Rectangle { return this.width * this.height; } + public contains(point: Vector2D): boolean { + const test_rect = this.normalize(); + + return point.x >= test_rect._corner_1.x && + point.y >= test_rect._corner_1.y && + point.x <= test_rect._corner_2.x && + point.y <= test_rect._corner_2.y + } + public normalize(): Rectangle { let left = this.left; let top = this.top;