aboutsummaryrefslogtreecommitdiff
path: root/build/js/tools/move.js
blob: 73246b8645ea6ad523824c0490a0011b1ad2dec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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);
      }
    }
  }
}