From 1544b6ef091ffdb27a9c9181591574c46fce37c0 Mon Sep 17 00:00:00 2001 From: kts of kettek Date: Fri, 12 Apr 2024 23:15:41 -0700 Subject: [PATCH] Implement preview stack drag move --- frontend/src/sections/StackPreview.svelte | 59 +++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/frontend/src/sections/StackPreview.svelte b/frontend/src/sections/StackPreview.svelte index 59874ef..c4d0f2f 100644 --- a/frontend/src/sections/StackPreview.svelte +++ b/frontend/src/sections/StackPreview.svelte @@ -10,6 +10,14 @@ export let files: LoadedFile[] let shownFiles: Record = {} + let filePositions: Record = {} + $: { + for (let file of files) { + if (shownFiles[file.title] && !filePositions[file.title]) { + filePositions[file.title] = {x: 0, y: 0, z: 0} + } + } + } let rotation: number = 0 let zoom: number = 1 @@ -43,7 +51,8 @@ let x = canvas.width/2 let y = canvas.height/2 - for (let file of files) { + let sortedFiles = files.filter(file => shownFiles[file.title]).sort((a, b) => filePositions[a.title].z - filePositions[b.title].z) + for (let file of sortedFiles) { if (shownFiles[file.title]) { // For now, just get the first frame of the first animation. let done = false @@ -56,6 +65,7 @@ if (showBaseSizeOutline) { ctx.save() ctx.translate(x, y) + ctx.translate(filePositions[file.title].x, filePositions[file.title].y) ctx.scale(zoom, zoom) ctx.strokeStyle = baseSizeOutlineColor ctx.lineWidth = 1 / zoom @@ -65,6 +75,7 @@ if (showSizeOutline) { ctx.save() ctx.translate(x, y) + ctx.translate(filePositions[file.title].x, filePositions[file.title].y) ctx.scale(zoom, zoom) ctx.rotate(rotation * Math.PI / 180) ctx.strokeStyle = sizeOutlineColor @@ -75,6 +86,7 @@ } ctx.save() ctx.translate(x, y) + ctx.translate(filePositions[file.title].x, filePositions[file.title].y) ctx.scale(zoom, zoom) ctx.rotate(rotation * Math.PI / 180) @@ -93,7 +105,6 @@ } if (done) break } - x += file.data.width y = canvas.height/2 if (done) break } @@ -101,6 +112,48 @@ } } + function hitsFile(x: number, y: number): string { + x *= zoom + y *= zoom + for (let file of files) { + let name = file.title + if (!shownFiles[name]) continue + let px = canvas.width/2 + let py = canvas.height/2 + px += filePositions[name].x + py += filePositions[name].y + px -= file.data.width/2 + py -= file.data.height/2 + px *= zoom + py *= zoom + if (x >= px && x <= px + file.data.width*zoom && y >= py && y <= py + file.data.height*zoom) { + return name + } + } + return "" + } + + function mousedown(e: MouseEvent) { + let file = hitsFile(e.offsetX, e.offsetY) + if (file) { + filePositions[file].z = Object.values(filePositions).reduce((acc, val) => Math.max(acc, val.z), 0) + 1 + } + + let mouseup = (e: MouseEvent) => { + window.removeEventListener('mouseup', mouseup) + window.removeEventListener('mousemove', mousemove) + } + let mousemove = (e: MouseEvent) => { + let file = hitsFile(e.offsetX, e.offsetY) + if (file) { + filePositions[file].x = e.offsetX - canvas.width/2 + filePositions[file].y = e.offsetY - canvas.height/2 + } + } + window.addEventListener('mouseup', mouseup) + window.addEventListener('mousemove', mousemove) + } + onMount(()=>{ let frameID: number = 0 let frameDraw = () => { @@ -120,7 +173,7 @@ {/each} - +