readability

This commit is contained in:
Jörn-Michael Miehe 2023-01-17 23:50:25 +00:00
parent c1da30cc3e
commit b81446e99d
2 changed files with 19 additions and 17 deletions

View file

@ -34,7 +34,7 @@ function get_event_thous(event: MouseEvent): Vector2D {
return new Vector2D(); return new Vector2D();
} }
let target = event.currentTarget as Element; const target = event.currentTarget as Element;
return new Vector2D( return new Vector2D(
Math.round((event.offsetX / target.clientWidth) * 1000), Math.round((event.offsetX / target.clientWidth) * 1000),
@ -69,20 +69,13 @@ export default class RectPad extends Vue {
} }
private pop_rectangle(point: Vector2D): Rectangle | undefined { private pop_rectangle(point: Vector2D): Rectangle | undefined {
const idx = this.rectangles.findIndex( const idx = this.rectangles.findIndex((rect) => rect.contains(point));
(rect) =>
point.x >= rect.left &&
point.y >= rect.top &&
point.x <= rect.left + rect.width &&
point.y <= rect.top + rect.height
);
if (idx === -1) { if (idx === -1) {
return; return;
} }
const rects = this.rectangles.splice(idx, 1); return this.rectangles.splice(idx, 1)[0];
return rects[0];
} }
private draw_start(event: MouseEvent) { private draw_start(event: MouseEvent) {
@ -102,9 +95,11 @@ export default class RectPad extends Vue {
this.drawing = false; this.drawing = false;
if (this.preview_rectangle.area >= this.min_rect_area) { if (this.preview_rectangle.area < this.min_rect_area) {
this.rectangles.push(this.preview_rectangle); return;
} }
this.rectangles.push(this.preview_rectangle);
} }
private drag_start(event: MouseEvent) { private drag_start(event: MouseEvent) {
@ -112,7 +107,7 @@ export default class RectPad extends Vue {
return; return;
} }
let point = get_event_thous(event); const point = get_event_thous(event);
this.drag_rect = this.pop_rectangle(point); this.drag_rect = this.pop_rectangle(point);
if (this.drag_rect === undefined) { if (this.drag_rect === undefined) {
@ -136,12 +131,10 @@ export default class RectPad extends Vue {
} }
private on_mousemove(event: MouseEvent) { private on_mousemove(event: MouseEvent) {
let point = get_event_thous(event);
if (this.drawing) { if (this.drawing) {
this.preview_corner2 = point; this.preview_corner2 = get_event_thous(event);
} else if (this.dragging && this.drag_rect) { } 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_corner1 = this.drag_rect.origin.plus(movement);
this.preview_corner2 = this.drag_rect.corner.plus(movement); this.preview_corner2 = this.drag_rect.corner.plus(movement);

View file

@ -64,6 +64,15 @@ export class Rectangle {
return this.width * this.height; 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 { public normalize(): Rectangle {
let left = this.left; let left = this.left;
let top = this.top; let top = this.top;