mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-24 13:33:00 +00:00
readability
This commit is contained in:
parent
c1da30cc3e
commit
b81446e99d
2 changed files with 19 additions and 17 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue