advent22/ui/src/components/editor/DoorCanvas.vue

158 lines
3.3 KiB
Vue
Raw Normal View History

2023-01-17 18:25:56 +00:00
<template>
2023-09-07 01:17:14 +00:00
<ThouCanvas
@mousedown.left="draw_start"
@mouseup.left="draw_finish"
@mousedown.right="drag_start"
@mouseup.right="drag_finish"
@mousemove="on_mousemove"
@click.middle="remove_rect"
@dblclick.left="remove_rect"
>
<CalendarDoor
v-for="(door, index) in doors"
:key="`door-${index}`"
:door="door"
force_visible
2023-09-07 01:17:14 +00:00
/>
<SVGRect
v-if="preview_visible"
variant="success"
:rectangle="preview_rect"
visible
/>
</ThouCanvas>
2023-01-17 18:25:56 +00:00
</template>
<script lang="ts">
import { Door } from "@/lib/door";
2023-09-07 02:08:56 +00:00
import { Rectangle } from "@/lib/rectangle";
import { Vector2D } from "@/lib/vector2d";
2023-09-06 16:25:35 +00:00
import { Options, Vue } from "vue-class-component";
2023-09-07 02:08:56 +00:00
import CalendarDoor from "../calendar/CalendarDoor.vue";
2023-09-10 01:59:19 +00:00
import SVGRect from "../calendar/SVGRect.vue";
import ThouCanvas from "../calendar/ThouCanvas.vue";
2023-01-17 18:25:56 +00:00
2023-01-31 14:21:06 +00:00
enum CanvasState {
Idle,
Drawing,
Dragging,
}
2023-01-17 18:25:56 +00:00
@Options({
components: {
CalendarDoor,
2023-01-25 11:39:58 +00:00
SVGRect,
ThouCanvas,
2023-01-17 18:25:56 +00:00
},
2023-02-01 16:53:24 +00:00
props: {
doors: Array,
2023-02-01 16:53:24 +00:00
},
2023-01-17 18:25:56 +00:00
})
2023-01-24 23:19:25 +00:00
export default class extends Vue {
2023-01-18 00:04:43 +00:00
private readonly min_rect_area = 300;
2023-01-31 14:21:06 +00:00
private state = CanvasState.Idle;
2023-09-06 16:25:35 +00:00
public preview_rect = new Rectangle();
private drag_door?: Door;
2023-01-17 23:34:42 +00:00
private drag_origin = new Vector2D();
public doors!: Door[];
2023-01-17 18:25:56 +00:00
2023-09-06 16:25:35 +00:00
public get preview_visible(): boolean {
2023-01-31 14:21:06 +00:00
return this.state !== CanvasState.Idle;
2023-01-19 00:27:38 +00:00
}
private pop_door(point: Vector2D): Door | undefined {
const idx = this.doors.findIndex((rect) => rect.position.contains(point));
2023-01-17 23:42:07 +00:00
if (idx === -1) {
return;
}
return this.doors.splice(idx, 1)[0];
2023-01-17 23:34:42 +00:00
}
2023-09-06 16:25:35 +00:00
public draw_start(event: MouseEvent, point: Vector2D) {
2023-01-17 23:34:42 +00:00
if (this.preview_visible) {
return;
}
2023-01-31 14:21:06 +00:00
this.state = CanvasState.Drawing;
this.preview_rect = new Rectangle(point, point);
2023-01-17 18:25:56 +00:00
}
2023-09-06 16:25:35 +00:00
public draw_finish() {
2023-01-31 14:21:06 +00:00
if (this.state !== CanvasState.Drawing || this.preview_rect === undefined) {
2023-01-17 22:42:21 +00:00
return;
}
2023-01-31 14:21:06 +00:00
this.state = CanvasState.Idle;
2023-01-17 18:25:56 +00:00
2023-01-31 14:21:06 +00:00
if (this.preview_rect.area < this.min_rect_area) {
2023-01-17 23:50:25 +00:00
return;
2023-01-17 18:25:56 +00:00
}
2023-01-17 23:50:25 +00:00
this.doors.push(new Door(this.preview_rect));
2023-01-17 18:25:56 +00:00
}
2023-09-06 16:25:35 +00:00
public drag_start(event: MouseEvent, point: Vector2D) {
2023-01-17 23:34:42 +00:00
if (this.preview_visible) {
return;
}
this.drag_door = this.pop_door(point);
2023-01-17 23:34:42 +00:00
if (this.drag_door === undefined) {
2023-01-17 23:34:42 +00:00
return;
}
2023-01-31 14:21:06 +00:00
this.state = CanvasState.Dragging;
2023-01-17 23:34:42 +00:00
this.drag_origin = point;
this.preview_rect = this.drag_door.position;
2023-01-17 23:34:42 +00:00
}
2023-09-06 16:25:35 +00:00
public drag_finish() {
2023-01-31 14:21:06 +00:00
if (
this.state !== CanvasState.Dragging ||
this.preview_rect === undefined
) {
2023-01-17 23:34:42 +00:00
return;
}
2023-01-31 14:21:06 +00:00
this.state = CanvasState.Idle;
this.doors.push(new Door(this.preview_rect, this.drag_door!.day));
2023-01-17 23:34:42 +00:00
}
2023-09-06 16:25:35 +00:00
public on_mousemove(event: MouseEvent, point: Vector2D) {
2023-01-31 14:21:06 +00:00
if (this.preview_rect === undefined) {
return;
}
2023-01-17 23:34:42 +00:00
2023-01-31 14:21:06 +00:00
if (this.state === CanvasState.Drawing) {
this.preview_rect = this.preview_rect.update(undefined, point);
} else if (this.state === CanvasState.Dragging && this.drag_door) {
2023-01-31 14:21:06 +00:00
const movement = point.minus(this.drag_origin);
this.preview_rect = this.drag_door.position.move(movement);
2023-01-17 23:34:42 +00:00
}
}
2023-09-06 16:25:35 +00:00
public remove_rect(event: MouseEvent, point: Vector2D) {
2023-01-19 00:27:21 +00:00
if (this.preview_visible) {
return;
}
this.pop_door(point);
2023-01-17 18:25:56 +00:00
}
}
</script>
<style lang="scss" scoped>
svg {
2023-01-17 18:25:56 +00:00
cursor: crosshair;
* {
pointer-events: none;
}
2023-01-17 18:25:56 +00:00
}
2023-09-07 01:17:14 +00:00
</style>