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

134 lines
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 model"
:key="`door-${index}`"
:door="door"
force_visible
2023-09-07 01:17:14 +00:00
/>
<SVGRect
v-if="preview_visible"
variant="success"
:rectangle="preview"
visible
/>
</ThouCanvas>
2023-01-17 18:25:56 +00:00
</template>
<script setup lang="ts">
2024-08-23 16:38:04 +00:00
import { Door } from "@/lib/rects/door";
import { Rectangle } from "@/lib/rects/rectangle";
import { Vector2D } from "@/lib/rects/vector2d";
import { computed, ref } from "vue";
2023-09-07 02:08:56 +00:00
import { VueLike } from "@/lib/helpers";
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
type CanvasState =
| { kind: "idle" }
| { kind: "drawing" }
| { kind: "dragging"; door: VueLike<Door>; origin: Vector2D };
2023-01-31 14:21:06 +00:00
const model = defineModel<VueLike<Door>[]>({ required: true });
2023-01-19 00:27:38 +00:00
const MIN_RECT_AREA = 300;
const state = ref<CanvasState>({ kind: "idle" });
const preview = ref(new Rectangle());
2023-01-17 23:42:07 +00:00
const preview_visible = computed(() => state.value.kind !== "idle");
function pop_door(point: Vector2D): VueLike<Door> | undefined {
const idx = model.value.findIndex((rect) => rect.position.contains(point));
2023-01-17 23:42:07 +00:00
if (idx === -1) {
return;
2023-01-17 23:34:42 +00:00
}
return model.value.splice(idx, 1)[0];
}
2025-12-28 16:35:10 +00:00
function draw_start(event: MouseEvent, point: Vector2D): void {
if (preview_visible.value) {
return;
2023-01-17 18:25:56 +00:00
}
preview.value = new Rectangle(point, point);
state.value = { kind: "drawing" };
}
2023-01-17 22:42:21 +00:00
2025-12-28 16:35:10 +00:00
function draw_finish(): void {
if (state.value.kind !== "drawing") {
return;
}
if (preview.value.area >= MIN_RECT_AREA) {
model.value.push(new Door(preview.value));
}
2023-01-17 18:25:56 +00:00
state.value = { kind: "idle" };
}
2023-01-17 23:50:25 +00:00
2025-12-28 16:35:10 +00:00
function drag_start(event: MouseEvent, point: Vector2D): void {
if (preview_visible.value) {
return;
2023-01-17 18:25:56 +00:00
}
const drag_door = pop_door(point);
2023-01-17 23:34:42 +00:00
if (drag_door === undefined) {
return;
}
2023-01-17 23:34:42 +00:00
preview.value = drag_door.position;
2023-01-17 23:34:42 +00:00
state.value = { kind: "dragging", door: drag_door, origin: point };
}
2023-01-17 23:34:42 +00:00
2025-12-28 16:35:10 +00:00
function drag_finish(): void {
if (state.value.kind !== "dragging") {
return;
2023-01-17 23:34:42 +00:00
}
model.value.push(new Door(preview.value, state.value.door.day));
2023-01-17 23:34:42 +00:00
state.value = { kind: "idle" };
}
2023-01-17 23:34:42 +00:00
2025-12-28 16:35:10 +00:00
function on_mousemove(event: MouseEvent, point: Vector2D): void {
if (state.value.kind === "drawing") {
preview.value = preview.value.update(undefined, point);
} else if (state.value.kind === "dragging") {
const movement = point.minus(state.value.origin);
preview.value = state.value.door.position.move(movement);
2023-01-17 23:34:42 +00:00
}
}
2023-01-17 23:34:42 +00:00
2025-12-28 16:35:10 +00:00
function remove_rect(event: MouseEvent, point: Vector2D): void {
if (preview_visible.value) {
return;
2023-01-17 18:25:56 +00:00
}
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>