mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-25 14:02:59 +00:00
drag rectangles
This commit is contained in:
parent
7650fb16a5
commit
7160cfeff7
2 changed files with 108 additions and 29 deletions
|
@ -4,20 +4,23 @@
|
||||||
viewBox="0 0 1000 1000"
|
viewBox="0 0 1000 1000"
|
||||||
preserveAspectRatio="none"
|
preserveAspectRatio="none"
|
||||||
@mousedown.left="draw_start"
|
@mousedown.left="draw_start"
|
||||||
@mousemove="on_mousemove"
|
|
||||||
@mouseup.left="draw_finish"
|
@mouseup.left="draw_finish"
|
||||||
@contextmenu.prevent=""
|
@contextmenu.prevent
|
||||||
|
@mousedown.right="drag_start"
|
||||||
|
@mouseup.right="drag_finish"
|
||||||
|
@mousemove="on_mousemove"
|
||||||
|
@dblclick.left="remove_rect"
|
||||||
>
|
>
|
||||||
<Rect
|
|
||||||
v-if="preview_state.visible"
|
|
||||||
:highlighted="true"
|
|
||||||
:rectangle="preview_rectangle"
|
|
||||||
/>
|
|
||||||
<Rect
|
<Rect
|
||||||
v-for="(rect, index) in rectangles"
|
v-for="(rect, index) in rectangles"
|
||||||
:key="'rect' + index"
|
:key="'rect' + index"
|
||||||
:rectangle="rect"
|
:rectangle="rect"
|
||||||
/>
|
/>
|
||||||
|
<Rect
|
||||||
|
v-if="preview_visible"
|
||||||
|
:highlighted="true"
|
||||||
|
:rectangle="preview_rectangle"
|
||||||
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -45,44 +48,116 @@ function get_event_thous(event: MouseEvent): Vector2D {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
export default class RectPad extends Vue {
|
export default class RectPad extends Vue {
|
||||||
private preview_state = {
|
|
||||||
visible: false,
|
|
||||||
corner1: new Vector2D(),
|
|
||||||
corner2: new Vector2D(),
|
|
||||||
};
|
|
||||||
private readonly min_rect_area = 4;
|
private readonly min_rect_area = 4;
|
||||||
|
private drawing = false;
|
||||||
|
private preview_corner1 = new Vector2D();
|
||||||
|
private preview_corner2 = new Vector2D();
|
||||||
|
private dragging = false;
|
||||||
|
private drag_index = -1;
|
||||||
|
private drag_origin = new Vector2D();
|
||||||
private rectangles: Rectangle[] = [];
|
private rectangles: Rectangle[] = [];
|
||||||
|
|
||||||
|
private get preview_visible(): boolean {
|
||||||
|
return this.drawing || this.dragging;
|
||||||
|
}
|
||||||
|
|
||||||
|
private get preview_rectangle(): Rectangle {
|
||||||
|
return new Rectangle(
|
||||||
|
this.preview_corner1,
|
||||||
|
this.preview_corner2
|
||||||
|
).normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private find_rectangle(point: Vector2D) {
|
||||||
|
return this.rectangles.findIndex(
|
||||||
|
(rect) =>
|
||||||
|
point.x >= rect.left &&
|
||||||
|
point.y >= rect.top &&
|
||||||
|
point.x <= rect.left + rect.width &&
|
||||||
|
point.y <= rect.top + rect.height
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private draw_start(event: MouseEvent) {
|
private draw_start(event: MouseEvent) {
|
||||||
if (this.preview_state.visible) {
|
if (this.preview_visible) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.preview_state.visible = true;
|
this.drawing = true;
|
||||||
this.preview_state.corner1 = get_event_thous(event);
|
this.preview_corner1 = get_event_thous(event);
|
||||||
this.preview_state.corner2 = get_event_thous(event);
|
this.preview_corner2 = get_event_thous(event);
|
||||||
}
|
|
||||||
|
|
||||||
private on_mousemove(event: MouseEvent) {
|
|
||||||
if (!this.preview_state.visible) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.preview_state.corner2 = get_event_thous(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private draw_finish() {
|
private draw_finish() {
|
||||||
this.preview_state.visible = false;
|
if (!this.drawing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
this.rectangles.push(this.preview_rectangle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private get preview_rectangle(): Rectangle {
|
private drag_start(event: MouseEvent) {
|
||||||
return new Rectangle(
|
if (this.preview_visible) {
|
||||||
this.preview_state.corner1,
|
return;
|
||||||
this.preview_state.corner2
|
}
|
||||||
).normalize();
|
|
||||||
|
let point = get_event_thous(event);
|
||||||
|
this.drag_index = this.find_rectangle(point);
|
||||||
|
|
||||||
|
if (this.drag_index === -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dragging = true;
|
||||||
|
this.drag_origin = point;
|
||||||
|
|
||||||
|
this.preview_corner1 = this.rectangles[this.drag_index].origin;
|
||||||
|
this.preview_corner2 = this.rectangles[this.drag_index].corner;
|
||||||
|
|
||||||
|
console.log("drag_start", point);
|
||||||
|
}
|
||||||
|
|
||||||
|
private drag_finish(event: MouseEvent) {
|
||||||
|
if (!this.dragging) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dragging = false;
|
||||||
|
this.rectangles[this.drag_index] = this.preview_rectangle;
|
||||||
|
|
||||||
|
console.log("drag_finish", get_event_thous(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
private on_mousemove(event: MouseEvent) {
|
||||||
|
let point = get_event_thous(event);
|
||||||
|
|
||||||
|
if (this.drawing) {
|
||||||
|
this.preview_corner2 = point;
|
||||||
|
} else if (this.dragging) {
|
||||||
|
let movement = point.minus(this.drag_origin);
|
||||||
|
|
||||||
|
this.preview_corner1 =
|
||||||
|
this.rectangles[this.drag_index].origin.plus(movement);
|
||||||
|
this.preview_corner2 =
|
||||||
|
this.rectangles[this.drag_index].corner.plus(movement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private remove_rect(event: MouseEvent) {
|
||||||
|
let point = get_event_thous(event);
|
||||||
|
let rect_index = this.find_rectangle(point);
|
||||||
|
|
||||||
|
if (rect_index === -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.rectangles.splice(rect_index, 1);
|
||||||
|
|
||||||
|
console.log("remove_rect", get_event_thous(event));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -44,6 +44,10 @@ export class Rectangle {
|
||||||
return this.origin.y;
|
return this.origin.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get corner(): Vector2D {
|
||||||
|
return this._corner_2;
|
||||||
|
}
|
||||||
|
|
||||||
public get size(): Vector2D {
|
public get size(): Vector2D {
|
||||||
return this._corner_2.minus(this._corner_1);
|
return this._corner_2.minus(this._corner_1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue