tools.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. 3D Modeler - drawing tools
  3. What the pointer does on the sheet while a drawing command is active
  4. (editor.js hands it over). Each tool is
  5. { name, prompt, move(p), down(p, event), key(event) -> handled,
  6. overlay() -> { items, edges, marks, frames }, cancel(rightClick) }
  7. with p a sheet point (mm).
  8. The Dimension tool works like a desktop CAD general dimension: what is
  9. clicked decides what is measured.
  10. a circle its diameter (an arc: its radius); R / D switch
  11. a straight edge its length, or click a second straight edge:
  12. the angle between them, or the distance when
  13. they are parallel
  14. a point (an end, a middle, a centre) and a second point
  15. or edge: the distance between them
  16. then a last click places the text. A linear dimension is horizontal,
  17. vertical or aligned by where the pointer is; Space cycles through them.
  18. The Radius, Diameter, Angle, Hole Note and Center Mark buttons start the
  19. same tool limited to what they measure.
  20. ctx (from drawing.js):
  21. editor, data(), projections(), newId(prefix), commit(label, fn),
  22. setStatus(text), setPrompt(text), estimate(view) (a local box),
  23. editText(draft) (opens the text panel for a new note or text),
  24. done() (the tool ended by itself)
  25. */
  26. import { annotationItems, scaleOf, toSheet, toLocal, frameOf } from './layout.js';
  27. import { projectedFrame, sideFor, orientationKey, ORIENTATIONS } from './sheet.js';
  28. import * as G2 from './geometry2d.js';
  29. const NAMES = {
  30. linear: 'Dimension', radius: 'Radius', diameter: 'Diameter', angle: 'Angle', hole: 'Hole Note',
  31. center: 'Center Mark', note: 'Leader Note', text: 'Text'
  32. };
  33. function viewById(data, id) {
  34. return data.views.filter(function (v) {
  35. return v.id === id;
  36. })[0] || null;
  37. }
  38. function sheetPoint(data, viewId, local) {
  39. const v = viewById(data, viewId);
  40. return v ? toSheet(v, scaleOf(data, v), local) : local;
  41. }
  42. function edgeOnSheet(data, viewId, edge) {
  43. const v = viewById(data, viewId);
  44. if (!v) {
  45. return [];
  46. }
  47. const s = scaleOf(data, v);
  48. if (edge.type === 'circle' && edge.closed) {
  49. const pts = [];
  50. for (let i = 0; i <= 64; i++) {
  51. const t = i / 64 * 2 * Math.PI;
  52. pts.push(toSheet(v, s, [edge.c[0] + edge.r * Math.cos(t), edge.c[1] + edge.r * Math.sin(t)]));
  53. }
  54. return pts;
  55. }
  56. const pts = [];
  57. for (let i = 0; i < edge.pts.length; i += 2) {
  58. pts.push(toSheet(v, s, [edge.pts[i], edge.pts[i + 1]]));
  59. }
  60. return pts;
  61. }
  62. // What a pointer hit is, as something to dimension
  63. function pickOf(hit) {
  64. if (!hit) {
  65. return null;
  66. }
  67. if (hit.snap) {
  68. return { kind: 'point', view: hit.view, p: hit.snap.p, snap: hit.snap.kind };
  69. }
  70. if (hit.edge) {
  71. return { kind: hit.edge.type === 'circle' ? 'circle' : hit.edge.type === 'line' ? 'line' : 'curve', view: hit.view, edge: hit.edge, local: hit.local };
  72. }
  73. return { kind: 'free', view: hit.view, p: hit.local };
  74. }
  75. function isHorizontal(e) {
  76. return Math.abs(e.a[1] - e.b[1]) < 1e-6;
  77. }
  78. function isVertical(e) {
  79. return Math.abs(e.a[0] - e.b[0]) < 1e-6;
  80. }
  81. // The orientation measuring square to a line: across a horizontal line is vertical
  82. function across(e) {
  83. return isHorizontal(e) ? 'v' : isVertical(e) ? 'h' : 'a';
  84. }
  85. function along(e) {
  86. return isHorizontal(e) ? 'h' : isVertical(e) ? 'v' : 'a';
  87. }
  88. // Horizontal, vertical or aligned, by where the pointer is around two points
  89. function orientFor(p1, p2, q) {
  90. if (Math.abs(p1[1] - p2[1]) < 1e-9) {
  91. return 'h';
  92. }
  93. if (Math.abs(p1[0] - p2[0]) < 1e-9) {
  94. return 'v';
  95. }
  96. const insideX = q[0] > Math.min(p1[0], p2[0]) && q[0] < Math.max(p1[0], p2[0]);
  97. const insideY = q[1] > Math.min(p1[1], p2[1]) && q[1] < Math.max(p1[1], p2[1]);
  98. if (insideX && !insideY) {
  99. return 'h';
  100. }
  101. if (insideY && !insideX) {
  102. return 'v';
  103. }
  104. return 'a';
  105. }
  106. // The foot of p on the (endless) line through edge e
  107. function foot(p, e) {
  108. const d = [e.b[0] - e.a[0], e.b[1] - e.a[1]];
  109. const l2 = d[0] * d[0] + d[1] * d[1];
  110. const t = l2 ? ((p[0] - e.a[0]) * d[0] + (p[1] - e.a[1]) * d[1]) / l2 : 0;
  111. return [e.a[0] + t * d[0], e.a[1] + t * d[1]];
  112. }
  113. function parallel(e1, e2) {
  114. const d1 = [e1.b[0] - e1.a[0], e1.b[1] - e1.a[1]];
  115. const d2 = [e2.b[0] - e2.a[0], e2.b[1] - e2.a[1]];
  116. const l = Math.hypot(d1[0], d1[1]) * Math.hypot(d2[0], d2[1]);
  117. return !l || Math.abs(d1[0] * d2[1] - d1[1] * d2[0]) / l < 1e-6;
  118. }
  119. // The point of an edge nearest to p (view coordinates)
  120. function nearestOnEdge(edge, p) {
  121. if (edge.type === 'circle' && edge.closed) {
  122. const d = [p[0] - edge.c[0], p[1] - edge.c[1]];
  123. const l = Math.hypot(d[0], d[1]) || 1;
  124. return [edge.c[0] + d[0] / l * edge.r, edge.c[1] + d[1] / l * edge.r];
  125. }
  126. let best = null;
  127. for (let i = 2; i < edge.pts.length; i += 2) {
  128. const n = G2.nearestOnSegment(p, [edge.pts[i - 2], edge.pts[i - 1]], [edge.pts[i], edge.pts[i + 1]]);
  129. if (!best || n.d < best.d) {
  130. best = n;
  131. }
  132. }
  133. return best ? best.point : p;
  134. }
  135. export function createDrawingTools(ctx) {
  136. const editor = ctx.editor;
  137. function hoverMarks(pick) {
  138. const data = ctx.data();
  139. const out = { marks: [], edges: [] };
  140. if (!pick) {
  141. return out;
  142. }
  143. if (pick.kind === 'point') {
  144. out.marks.push({ p: sheetPoint(data, pick.view, pick.p), kind: pick.snap || 'end' });
  145. } else if (pick.edge) {
  146. out.edges.push(edgeOnSheet(data, pick.view, pick.edge));
  147. if (pick.kind === 'circle') {
  148. out.marks.push({ p: sheetPoint(data, pick.view, pick.edge.c), kind: 'center' });
  149. }
  150. }
  151. return out;
  152. }
  153. /* ================= dimensions ================= */
  154. function dimension(mode) {
  155. let picks = [];
  156. let hovered = null;
  157. let draft = null;
  158. let forced = null; // a linear orientation chosen with Space
  159. let circleKind = null; // radius or diameter chosen with R / D
  160. const accepts = function (pick) {
  161. if (!pick) {
  162. return false;
  163. }
  164. if (mode === 'angle') {
  165. return pick.kind === 'line';
  166. }
  167. if (mode === 'radius' || mode === 'diameter' || mode === 'hole' || mode === 'center') {
  168. return pick.kind === 'circle';
  169. }
  170. return pick.kind === 'point' || pick.kind === 'line' || pick.kind === 'circle';
  171. };
  172. const firstPrompt = {
  173. auto: 'Dimension: click an edge, a circle or a point of a view',
  174. radius: 'Radius: click a circle or an arc',
  175. diameter: 'Diameter: click a circle or an arc',
  176. hole: 'Hole Note: click a hole (a circle)',
  177. angle: 'Angle: click two straight edges',
  178. center: 'Center Mark: click circles to mark their centres'
  179. }[mode];
  180. function prompt() {
  181. if (!picks.length) {
  182. ctx.setPrompt(firstPrompt);
  183. return;
  184. }
  185. const first = picks[0];
  186. if (first.kind === 'circle' || picks.length === 2) {
  187. ctx.setPrompt('Click to place the text' + (first.kind === 'circle' && mode === 'auto' ? ' (R: radius, D: diameter)' : '') + '. Esc starts again.');
  188. } else if (first.kind === 'line') {
  189. ctx.setPrompt(mode === 'angle' ? 'Angle: click the second straight edge' : 'Click to place the length, or a second edge (angle or distance), or a point');
  190. } else {
  191. ctx.setPrompt('Click a second point, edge or circle');
  192. }
  193. }
  194. function linearDraft(view, p1, p2, at, natural) {
  195. return { type: 'linear', view: view, p1: p1.slice(), p2: p2.slice(), orient: forced || natural || orientFor(p1, p2, at), at: at };
  196. }
  197. function twoLines(view, e1, e2, at) {
  198. if (parallel(e1, e2)) {
  199. const mid = [(e1.a[0] + e1.b[0]) / 2, (e1.a[1] + e1.b[1]) / 2];
  200. return linearDraft(view, mid, foot(mid, e2), at, across(e1));
  201. }
  202. return { type: 'angle', view: view, l1: [e1.a.slice(), e1.b.slice()], l2: [e2.a.slice(), e2.b.slice()], at: at };
  203. }
  204. function makeDraft(p) {
  205. if (!picks.length) {
  206. return null;
  207. }
  208. const data = ctx.data();
  209. const first = picks[0];
  210. const v = viewById(data, first.view);
  211. if (!v) {
  212. return null;
  213. }
  214. const at = toLocal(v, scaleOf(data, v), p);
  215. if (first.kind === 'circle') {
  216. const type = mode === 'auto' ? (circleKind || (first.edge.closed ? 'diameter' : 'radius')) : mode;
  217. const out = { type: type, view: v.id, c: first.edge.c.slice(), r: first.edge.r, at: at };
  218. if (type === 'hole') {
  219. const proj = ctx.projections()[v.id];
  220. out.count = proj ? Math.max(1, G2.countCircles(proj.edges, first.edge.r)) : 1;
  221. }
  222. return out;
  223. }
  224. const second = picks[1] || (hovered && hovered.view === first.view ? hovered : null);
  225. if (first.kind === 'line') {
  226. if (second && second.kind === 'line' && second.edge !== first.edge) {
  227. return twoLines(v.id, first.edge, second.edge, at);
  228. }
  229. if (second && second.kind === 'point' && picks[1]) {
  230. return linearDraft(v.id, second.p, foot(second.p, first.edge), at, across(first.edge));
  231. }
  232. return mode === 'angle' ? null : linearDraft(v.id, first.edge.a, first.edge.b, at, along(first.edge));
  233. }
  234. if (first.kind === 'point' && second) {
  235. if (second.kind === 'point' && G2.dist(second.p, first.p) > 1e-9) {
  236. return linearDraft(v.id, first.p, second.p, at, null);
  237. }
  238. if (second.kind === 'line' && picks[1]) {
  239. return linearDraft(v.id, first.p, foot(first.p, second.edge), at, across(second.edge));
  240. }
  241. }
  242. return null;
  243. }
  244. function reset() {
  245. picks = [];
  246. draft = null;
  247. forced = null;
  248. circleKind = null;
  249. prompt();
  250. }
  251. function commit(record) {
  252. if (!record) {
  253. return;
  254. }
  255. const out = Object.assign({ id: ctx.newId('d') }, record);
  256. ctx.commit('Add ' + NAMES[out.type === 'linear' ? 'linear' : out.type], function (d) {
  257. d.annotations.push(out);
  258. });
  259. reset();
  260. }
  261. const tool = {
  262. name: mode === 'auto' ? 'dimension' : mode,
  263. move: function (p) {
  264. const first = picks[0];
  265. const hit = editor.geometryAt(p, first ? function (v) {
  266. return v.id === first.view;
  267. } : null);
  268. hovered = pickOf(hit);
  269. draft = makeDraft(p);
  270. },
  271. down: function (p) {
  272. tool.move(p);
  273. const pick = hovered;
  274. if (!picks.length) {
  275. if (!accepts(pick)) {
  276. ctx.setStatus(mode === 'auto' ? 'Click a straight edge, a circle or a snap point of a view' : firstPrompt);
  277. return;
  278. }
  279. if (mode === 'center') {
  280. commit({ type: 'center', view: pick.view, c: pick.edge.c.slice(), r: pick.edge.r });
  281. return;
  282. }
  283. picks = [pick];
  284. draft = makeDraft(p);
  285. prompt();
  286. return;
  287. }
  288. const first = picks[0];
  289. const same = pick && pick.view === first.view;
  290. if (picks.length === 1 && first.kind === 'line') {
  291. if (same && pick.kind === 'line' && pick.edge !== first.edge) {
  292. picks.push(pick);
  293. draft = makeDraft(p);
  294. prompt();
  295. return;
  296. }
  297. if (same && pick.kind === 'point' && mode === 'auto') {
  298. picks.push(pick);
  299. draft = makeDraft(p);
  300. prompt();
  301. return;
  302. }
  303. if (mode === 'angle') {
  304. ctx.setStatus('Click a second straight edge of the same view');
  305. return;
  306. }
  307. }
  308. if (picks.length === 1 && first.kind === 'point') {
  309. if (same && pick.kind === 'point' && G2.dist(pick.p, first.p) > 1e-9) {
  310. picks.push(pick);
  311. } else if (same && pick.kind === 'circle') {
  312. picks.push({ kind: 'point', view: pick.view, p: pick.edge.c, snap: 'center' });
  313. } else if (same && pick.kind === 'line') {
  314. picks.push(pick);
  315. } else {
  316. ctx.setStatus('Click a second point, a straight edge or a circle of the same view');
  317. return;
  318. }
  319. draft = makeDraft(p);
  320. prompt();
  321. return;
  322. }
  323. commit(draft);
  324. },
  325. key: function (event) {
  326. const k = event.key.toLowerCase();
  327. if (k === 'escape' && picks.length) {
  328. reset();
  329. return true;
  330. }
  331. if (k === ' ' && draft && draft.type === 'linear') {
  332. const order = [null, 'h', 'v', 'a'];
  333. forced = order[(order.indexOf(forced) + 1) % order.length];
  334. ctx.setStatus(forced ? 'Dimension: ' + { h: 'horizontal', v: 'vertical', a: 'aligned' }[forced] : 'Dimension: by the pointer');
  335. const pointer = editor.pointer();
  336. if (pointer) {
  337. draft = makeDraft(pointer);
  338. }
  339. return true;
  340. }
  341. if ((k === 'r' || k === 'd') && mode === 'auto' && picks.length && picks[0].kind === 'circle') {
  342. circleKind = k === 'r' ? 'radius' : 'diameter';
  343. const pointer = editor.pointer();
  344. if (pointer) {
  345. draft = makeDraft(pointer);
  346. }
  347. return true;
  348. }
  349. return false;
  350. },
  351. overlay: function () {
  352. const data = ctx.data();
  353. const out = hoverMarks(hovered);
  354. picks.forEach(function (pick) {
  355. const m = hoverMarks(pick);
  356. out.marks = out.marks.concat(m.marks);
  357. out.edges = out.edges.concat(m.edges);
  358. });
  359. out.items = draft ? annotationItems(draft, data) : [];
  360. // A rubber band from a first point while nothing can be measured yet
  361. if (!draft && picks.length === 1 && picks[0].kind === 'point' && editor.pointer()) {
  362. out.items.push({ t: 'line', a: sheetPoint(data, picks[0].view, picks[0].p), b: editor.pointer(), layer: 'thin' });
  363. }
  364. return out;
  365. },
  366. cancel: function () {
  367. if (picks.length) {
  368. reset();
  369. } else {
  370. ctx.done();
  371. }
  372. },
  373. start: prompt
  374. };
  375. return tool;
  376. }
  377. /* ================= leader notes and text ================= */
  378. function note() {
  379. let target = null;
  380. let hovered = null;
  381. let draft = null;
  382. const tool = {
  383. name: 'note',
  384. move: function (p) {
  385. const data = ctx.data();
  386. if (!target) {
  387. hovered = pickOf(editor.geometryAt(p));
  388. draft = null;
  389. return;
  390. }
  391. const v = target.view ? viewById(data, target.view) : null;
  392. draft = { type: 'note', view: target.view, p: target.p, at: v ? toLocal(v, scaleOf(data, v), p) : p, text: 'Note', end: target.end };
  393. },
  394. down: function (p) {
  395. if (!target) {
  396. const pick = pickOf(editor.geometryAt(p));
  397. if (pick && pick.kind === 'point') {
  398. target = { view: pick.view, p: pick.p.slice(), end: 'arrow' };
  399. } else if (pick && pick.edge) {
  400. target = { view: pick.view, p: nearestOnEdge(pick.edge, pick.local), end: 'arrow' };
  401. } else if (pick) {
  402. // Inside a view but on no edge: a face; ISO marks surfaces with a dot
  403. target = { view: pick.view, p: pick.p.slice(), end: 'dot' };
  404. } else {
  405. target = { view: null, p: p, end: 'arrow' };
  406. }
  407. ctx.setPrompt('Leader Note: click where the text goes');
  408. return;
  409. }
  410. tool.move(p);
  411. const made = draft;
  412. target = null;
  413. draft = null;
  414. ctx.setPrompt('Leader Note: click what the note points at (an edge, a point, or a face inside a view)');
  415. ctx.editText(made);
  416. },
  417. key: function (event) {
  418. if (event.key === 'Escape' && target) {
  419. target = null;
  420. draft = null;
  421. ctx.setPrompt('Leader Note: click what the note points at (an edge, a point, or a face inside a view)');
  422. return true;
  423. }
  424. return false;
  425. },
  426. overlay: function () {
  427. const out = target ? { marks: [], edges: [] } : hoverMarks(hovered);
  428. out.items = draft ? annotationItems(draft, ctx.data()) : [];
  429. return out;
  430. },
  431. cancel: function () {
  432. if (target) {
  433. target = null;
  434. draft = null;
  435. } else {
  436. ctx.done();
  437. }
  438. },
  439. start: function () {
  440. ctx.setPrompt('Leader Note: click what the note points at (an edge, a point, or a face inside a view)');
  441. }
  442. };
  443. return tool;
  444. }
  445. function freeText() {
  446. return {
  447. name: 'text',
  448. move: function () {},
  449. down: function (p) {
  450. ctx.editText({ type: 'text', view: null, at: p, text: '' });
  451. },
  452. key: function () {
  453. return false;
  454. },
  455. overlay: function () {
  456. const p = editor.pointer();
  457. return p ? { marks: [{ p: p, kind: 'end' }] } : null;
  458. },
  459. cancel: function () {
  460. ctx.done();
  461. },
  462. start: function () {
  463. ctx.setPrompt('Text: click where the text starts');
  464. }
  465. };
  466. }
  467. /* ================= projected views ================= */
  468. // Where a projected view would go for a pointer at p: { rel, x, y, box, label }
  469. function projectedGhost(parentId, p) {
  470. const data = ctx.data();
  471. const parent = viewById(data, parentId);
  472. const pbox = parent && editor.viewBox(parentId);
  473. if (!pbox) {
  474. return null;
  475. }
  476. const s = scaleOf(data, parent);
  477. const rel = sideFor(p[0] - (pbox[0] + pbox[2]) / 2, p[1] - (pbox[1] + pbox[3]) / 2);
  478. if (!rel) {
  479. return null;
  480. }
  481. const frame = projectedFrame(frameOf(data, parent), rel, data.sheet.projection);
  482. const local = ctx.estimate({ source: parent.source, dir: frame.dir, xdir: frame.xdir });
  483. if (!local) {
  484. return null;
  485. }
  486. const mx = (local[0] + local[2]) / 2;
  487. const my = (local[1] + local[3]) / 2;
  488. let x = p[0] - s * mx;
  489. let y = p[1] - s * my;
  490. if (rel === 'right' || rel === 'left') {
  491. y = parent.y;
  492. } else if (rel === 'top' || rel === 'bottom') {
  493. x = parent.x;
  494. }
  495. const key = orientationKey(frame);
  496. const label = rel.length === 2 ? 'Isometric view' : key ? ORIENTATIONS[key].label + ' view' : 'Projected view';
  497. return { rel: rel, x: x, y: y, box: [x + s * local[0], y + s * local[1], x + s * local[2], y + s * local[3]], label: label };
  498. }
  499. function projected(parentId) {
  500. let parent = parentId || null;
  501. let ghost = null;
  502. let hoverView = null;
  503. let made = 0;
  504. const tool = {
  505. name: 'projected',
  506. move: function (p) {
  507. if (!parent) {
  508. hoverView = editor.viewAt(p);
  509. return;
  510. }
  511. ghost = projectedGhost(parent, p);
  512. },
  513. down: function (p) {
  514. if (!parent) {
  515. parent = editor.viewAt(p);
  516. if (parent) {
  517. tool.start();
  518. } else {
  519. ctx.setStatus('Click a view to project from');
  520. }
  521. return;
  522. }
  523. ghost = projectedGhost(parent, p);
  524. if (!ghost) {
  525. return;
  526. }
  527. const data = ctx.data();
  528. const pv = viewById(data, parent);
  529. const id = ctx.newId('v');
  530. const iso = ghost.rel.length === 2;
  531. const record = {
  532. id: id, name: ctx.newViewName(), source: pv.source, parent: parent, rel: ghost.rel,
  533. x: ghost.x, y: ghost.y, scale: null, hidden: iso ? false : !!pv.hidden, smooth: pv.smooth !== false,
  534. centers: pv.centers !== false, label: false, faces: pv.faces || null
  535. };
  536. ctx.commit('Add projected view', function (d) {
  537. d.views.push(record);
  538. });
  539. made++;
  540. ctx.setStatus(ghost.label + ' added. Move round the view for more; right click or Esc when done.');
  541. },
  542. key: function () {
  543. return false;
  544. },
  545. overlay: function () {
  546. if (!parent) {
  547. const box = hoverView && editor.viewBox(hoverView);
  548. return box ? { frames: [{ box: box, cls: 'ov-hot' }] } : null;
  549. }
  550. return ghost ? { frames: [{ box: ghost.box, cls: 'ov-ghost', label: ghost.label }] } : null;
  551. },
  552. cancel: function () {
  553. ctx.done();
  554. },
  555. start: function () {
  556. ctx.setPrompt(parent ? 'Projected View: move beside the view and click to place (right click or Esc when done)' : 'Projected View: click the view to project from');
  557. },
  558. count: function () {
  559. return made;
  560. }
  561. };
  562. return tool;
  563. }
  564. /* ================= placing a new view ================= */
  565. // spec: the view record without x, y; local: its estimated local box
  566. function place(spec, local, onPlaced, onCancel) {
  567. let ghost = null;
  568. const tool = {
  569. name: 'place',
  570. move: function (p) {
  571. const s = scaleOf(ctx.data(), spec);
  572. const mx = (local[0] + local[2]) / 2;
  573. const my = (local[1] + local[3]) / 2;
  574. const x = p[0] - s * mx;
  575. const y = p[1] - s * my;
  576. ghost = { x: x, y: y, box: [x + s * local[0], y + s * local[1], x + s * local[2], y + s * local[3]] };
  577. },
  578. down: function (p) {
  579. tool.move(p);
  580. onPlaced(ghost.x, ghost.y);
  581. },
  582. key: function () {
  583. return false;
  584. },
  585. overlay: function () {
  586. return ghost ? { frames: [{ box: ghost.box, cls: 'ov-ghost', label: spec.name }] } : null;
  587. },
  588. cancel: function () {
  589. if (onCancel) {
  590. onCancel();
  591. }
  592. ctx.done();
  593. },
  594. start: function () {
  595. ctx.setPrompt('Click on the sheet to place ' + spec.name + ' (Esc to cancel)');
  596. }
  597. };
  598. return tool;
  599. }
  600. return {
  601. dimension: dimension,
  602. note: note,
  603. text: freeText,
  604. projected: projected,
  605. place: place
  606. };
  607. }