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
197
198
199
|
// Rulers, guides and snapping.
// - Drag from a ruler to create a guide; with the Move tool, drag a guide to move it,
// or back onto a ruler to delete it.
// - Tools call snapPoint() / snapMove() so points and moved content snap to guides,
// the grid, and the document's edges and center.
// Guides live in doc.guideLines ([{axis: 'x' | 'y', pos}]) and are part of undo state.
import { RULER, drawRulers, drawGuides, SNAP_COLOR } from '../render/rulers.js';
const HIT = 4; // screen px for grabbing a guide
const SNAP = 8; // screen px snapping distance
export class GuideController {
constructor(app) {
this.app = app;
this.drag = null; // { axis, pos, index, before }
this.snapped = null; // { x, y } lines to highlight while dragging
}
get prefs() { return this.app.prefs; }
get rulers() { return this.prefs.get('rulers', false); }
get visible() { return this.prefs.get('guides', true); }
get snapOn() { return this.prefs.get('snap', true); }
get locked() { return this.prefs.get('lockGuides', false); }
/** Ruler band under a screen point: 'x' (top ruler → horizontal guide) or 'y'. */
rulerAt(s) {
if (!this.rulers) return null;
if (s.y < RULER && s.x >= RULER) return 'top';
if (s.x < RULER && s.y >= RULER) return 'left';
if (s.x < RULER && s.y < RULER) return 'corner';
return null;
}
guideAt(s) {
const doc = this.app.doc;
if (!doc || !this.visible) return -1;
const { zoom, panX, panY } = doc.view;
let best = -1, bestD = HIT + 1;
doc.guideLines.forEach((g, i) => {
const d = g.axis === 'x' ? Math.abs(g.pos * zoom + panX - s.x) : Math.abs(g.pos * zoom + panY - s.y);
if (d < bestD) { best = i; bestD = d; }
});
return best;
}
/** Cursor for hovering at screen point s, or null to use the tool's. */
hoverCursor(s) {
const r = this.rulerAt(s);
if (r === 'top') return 'row-resize';
if (r === 'left') return 'col-resize';
if (r === 'corner') return 'default';
if (this.app.tool?.id === 'move' && !this.locked) {
const i = this.guideAt(s);
if (i >= 0) return this.app.doc.guideLines[i].axis === 'x' ? 'col-resize' : 'row-resize';
}
return null;
}
/** Returns true if this controller takes the pointer gesture. */
pointerDown(e, s, p) {
const doc = this.app.doc;
const r = this.rulerAt(s);
if (r === 'corner') return true;
if (r) {
this.prefs.set('guides', true);
const axis = r === 'top' ? 'y' : 'x';
this.drag = { axis, pos: Math.round(axis === 'x' ? p.x : p.y), index: -1, before: doc.captureState(), orig: doc.guideLines };
return true;
}
if (this.app.tool?.id === 'move' && !this.locked) {
const i = this.guideAt(s);
if (i >= 0) {
const g = doc.guideLines[i];
this.drag = { axis: g.axis, pos: g.pos, index: i, before: doc.captureState(), orig: doc.guideLines };
doc.guideLines = doc.guideLines.filter((_, k) => k !== i);
return true;
}
}
return false;
}
pointerMove(e, s, p) {
const d = this.drag;
if (!d) return false;
let pos = d.axis === 'x' ? p.x : p.y;
pos = e.shiftKey ? pos : this.snapValue(pos, d.axis, { guides: false });
d.pos = Math.round(pos);
d.moved = true;
d.overRuler = this.rulerAt(s) !== null;
this.app.setStatusHint(d.overRuler ? 'Release to delete the guide' : `${d.axis === 'x' ? 'X' : 'Y'} ${d.pos} px`);
this.app.renderer.request();
return true;
}
pointerUp() {
const d = this.drag;
if (!d) return false;
this.drag = null;
const doc = this.app.doc;
if (!d.moved || (d.overRuler && d.index < 0)) {
// a click, or a new guide dropped back on the ruler: nothing changes
doc.guideLines = d.orig;
doc.markDirty();
} else if (d.overRuler) {
doc.commitChange('Delete guide', d.before);
} else {
doc.guideLines = [...doc.guideLines, { axis: d.axis, pos: d.pos }];
doc.commitChange(d.index >= 0 ? 'Move guide' : 'New guide', d.before);
}
this.app.setStatusHint(this.app.tool?.hint || '');
this.app.renderer.request();
return true;
}
cancel() {
if (!this.drag) return;
this.app.doc.guideLines = this.drag.orig;
this.drag = null;
this.app.renderer.request();
}
// ------------------------------------------------------------ snapping
/** Snap targets on one axis (doc px). */
targets(axis, { guides = true } = {}) {
const doc = this.app.doc;
const size = axis === 'x' ? doc.width : doc.height;
const t = [0, size, size / 2];
if (guides && this.visible) for (const g of doc.guideLines) if (g.axis === axis) t.push(g.pos);
return t;
}
/** Nearest target to v within the snapping distance, considering the grid too; else v. */
snapValue(v, axis, opts = {}) {
const doc = this.app.doc;
if (!doc || !this.snapOn) return v;
const thr = SNAP / doc.view.zoom;
let best = v, bestD = thr;
for (const t of this.targets(axis, opts)) {
const d = Math.abs(t - v);
if (d <= bestD) { best = t; bestD = d; }
}
if (doc.guides.show) {
const t = Math.round(v / doc.guides.grid) * doc.guides.grid;
if (Math.abs(t - v) < bestD) { best = t; bestD = Math.abs(t - v); }
}
return best;
}
/** Snap a document point (e.g. a marquee corner). Records feedback lines. */
snapPoint(p) {
if (!this.app.doc || !this.snapOn) return p;
const x = this.snapValue(p.x, 'x'), y = this.snapValue(p.y, 'y');
this.snapped = { x: x !== p.x ? x : null, y: y !== p.y ? y : null };
return { x, y };
}
/** Adjust a move (dx, dy) of `rect` so one of its edges or its center lands on a target. */
snapMove(rect, dx, dy) {
const doc = this.app.doc;
if (!doc || !this.snapOn || !rect) return { dx, dy };
const thr = SNAP / doc.view.zoom;
const axis = (pos, len, delta, ax) => {
const edges = [pos, pos + len / 2, pos + len].map((e) => e + delta);
const targets = this.targets(ax);
if (doc.guides.show) for (const e of edges) targets.push(Math.round(e / doc.guides.grid) * doc.guides.grid);
let best = null, bestD = thr;
for (const e of edges) {
for (const t of targets) {
const d = Math.abs(t - e);
if (d <= bestD) { bestD = d; best = { shift: t - e, line: t }; }
}
}
return best;
};
const sx = axis(rect.x, rect.w, dx, 'x'), sy = axis(rect.y, rect.h, dy, 'y');
this.snapped = { x: sx ? sx.line : null, y: sy ? sy.line : null };
return { dx: dx + (sx ? Math.round(sx.shift) : 0), dy: dy + (sy ? Math.round(sy.shift) : 0) };
}
clearSnap() {
if (this.snapped) { this.snapped = null; this.app.renderer.request(); }
}
// ------------------------------------------------------------ drawing
drawGuides(ctx, renderer, doc) {
if (this.visible && doc.guideLines.length) drawGuides(ctx, renderer, doc, doc.guideLines);
if (this.drag) drawGuides(ctx, renderer, doc, [this.drag], { dash: this.drag.overRuler ? [3, 3] : null });
}
drawOverlay(ctx, renderer, doc) {
if (this.snapped && this.app.pointerDown) {
const lines = [];
if (this.snapped.x != null) lines.push({ axis: 'x', pos: this.snapped.x });
if (this.snapped.y != null) lines.push({ axis: 'y', pos: this.snapped.y });
drawGuides(ctx, renderer, doc, lines, { color: SNAP_COLOR, dash: [4, 3] });
}
if (this.rulers) drawRulers(ctx, renderer, doc, renderer.pointer);
}
}
|