// Move tool: drags the active layer, the selected pixels (floating), or a text layer. import { Tool } from './tool.js'; import { createCanvas, cloneCanvas, opaqueBounds } from '../core/util.js'; import { layoutText } from '../core/layer.js'; export class MoveTool extends Tool { static id = 'move'; static label = 'Move'; static icon = 'move'; static shortcut = 'V'; static schema = [ { key: 'autoSelect', label: 'Auto-select layer', type: 'checkbox', default: false }, ]; get cursor() { return 'move'; } get hint() { return 'Drag to move · Alt+drag: duplicate selection · arrows nudge (Shift ×10) · double-click text to edit'; } get hidesSelection() { return !!this.drag?.floating; } onPointerDown(e, p) { const doc = this.doc; if (this.options.autoSelect) { const hit = this.app.layerAt(p); if (hit) doc.setActiveLayer(hit.id); } const layer = doc.activeLayer; if (!layer) return; if (layer.locked) { this.app.toast('Layer is locked'); return; } this.drag = this.begin(layer, e.altKey); this.drag.start = p; } begin(layer, duplicate) { const doc = this.doc; const before = doc.captureState(); const key = doc.editTarget.key; if (layer.isText && key === 'canvas') { const box = layoutText(layer.text); return { before, layer, text: true, x0: layer.text.x, y0: layer.text.y, dx: 0, dy: 0, bounds: { x: box.x, y: box.y, w: box.w, h: box.h } }; } if (doc.selection) { const sel = doc.selection; const src = layer[key]; const floating = cloneCanvas(src); const fctx = floating.getContext('2d'); fctx.globalCompositeOperation = 'destination-in'; fctx.drawImage(sel.mask, 0, 0); const base = cloneCanvas(src); if (!duplicate) { const bctx = base.getContext('2d'); bctx.globalCompositeOperation = 'destination-out'; bctx.drawImage(sel.mask, 0, 0); bctx.globalCompositeOperation = 'source-over'; } layer[key] = base; if (key === 'canvas') doc.floating = { canvas: floating, layer, dx: 0, dy: 0 }; return { before, layer, key, floating, selection: sel, dx: 0, dy: 0, bounds: sel.bounds }; } return { before, layer, key, whole: true, dx: 0, dy: 0, get bounds() { return this._b === undefined ? (this._b = opaqueBounds(layer[key])) : this._b; } }; } onPointerMove(e, p) { const d = this.drag; if (!d) return; let dx = Math.round(p.x - d.start.x), dy = Math.round(p.y - d.start.y); const axis = e.shiftKey ? (Math.abs(dx) > Math.abs(dy) ? 'x' : 'y') : null; ({ dx, dy } = this.app.guides.snapMove(d.bounds, dx, dy)); if (axis === 'x') dy = 0; if (axis === 'y') dx = 0; this.applyOffset(d, dx, dy); this.app.setStatusHint(`Δ ${dx}, ${dy}`); } applyOffset(d, dx, dy) { d.dx = dx; d.dy = dy; const doc = this.doc; if (d.text) { d.layer.text.x = d.x0 + dx; d.layer.text.y = d.y0 + dy; d.layer.renderText(); } else if (d.floating) { if (doc.floating) { doc.floating.dx = dx; doc.floating.dy = dy; } else this.drawMaskFloating(d); } else if (d.key === 'canvas') { d.layer.dx = dx; d.layer.dy = dy; } else { this.drawMaskFloating(d); } doc.markDirty(); } /** Mask moves have no live compositor hook; redraw the mask canvas directly. */ drawMaskFloating(d) { const layer = d.layer; if (!d.maskBase) d.maskBase = d.floating ? layer.mask : cloneCanvas(layer.mask); const out = createCanvas(layer.mask.width, layer.mask.height); const ctx = out.getContext('2d'); // Masks stay opaque: areas uncovered by the move are white (visible). ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, out.width, out.height); if (d.whole) { ctx.drawImage(d.maskBase, d.dx, d.dy); } else { ctx.drawImage(d.maskBase, 0, 0); ctx.drawImage(d.floating, d.dx, d.dy); } layer.mask = out; layer.touchMask(); } onPointerUp() { const d = this.drag; this.drag = null; if (!d) return; this.finish(d); } finish(d) { const doc = this.doc; const { layer } = d; if (d.text) { if (d.dx || d.dy) doc.commitChange('Move text', d.before); return; } if (d.floating) { if (!d.dx && !d.dy) { // a click without a drag: put everything back, no history step doc.floating = null; doc.restoreState(d.before); return; } if (d.key === 'canvas') { const out = layer.canvas; // base (already a fresh copy) out.getContext('2d').drawImage(d.floating, d.dx, d.dy); doc.floating = null; } else { this.drawMaskFloating(d); } layer.touch(); layer.touchMask(); doc.selection = d.selection.translated(d.dx, d.dy); doc.commitChange('Move selection', d.before); return; } if (!d.dx && !d.dy) { layer.dx = layer.dy = 0; doc.markDirty(); return; } if (d.key === 'canvas') { const shifted = createCanvas(layer.canvas.width, layer.canvas.height); shifted.getContext('2d').drawImage(layer.canvas, d.dx, d.dy); layer.canvas = shifted; layer.dx = layer.dy = 0; if (layer.mask) { const m = createCanvas(layer.mask.width, layer.mask.height); const mctx = m.getContext('2d'); mctx.fillStyle = '#fff'; mctx.fillRect(0, 0, m.width, m.height); mctx.clearRect(d.dx, d.dy, m.width, m.height); mctx.drawImage(layer.mask, d.dx, d.dy); layer.mask = m; layer.touchMask(); } layer.touch(); } doc.commitChange('Move layer', d.before); } cancel() { const d = this.drag; this.drag = null; if (!d) return; this.doc.floating = null; d.layer.dx = d.layer.dy = 0; this.doc.restoreState(d.before); } onKeyDown(e) { if (!e.key.startsWith('Arrow') || e.ctrlKey || e.metaKey || this.drag) return false; const layer = this.doc.activeLayer; if (!layer || layer.locked) return false; const step = e.shiftKey ? 10 : 1; const dx = e.key === 'ArrowLeft' ? -step : e.key === 'ArrowRight' ? step : 0; const dy = e.key === 'ArrowUp' ? -step : e.key === 'ArrowDown' ? step : 0; const d = this.begin(layer, false); this.applyOffset(d, dx, dy); this.finish(d); return true; } onDoubleClick(e, p) { const layer = this.doc.activeLayer; if (layer?.isText) { const box = layoutText(layer.text); if (p.x >= box.x && p.y >= box.y && p.x <= box.x + box.w && p.y <= box.y + box.h) { this.app.setTool('text'); this.app.tool.editLayer(layer); } } } }