import {EndPoint} from '../shapes/point' export class Tool { constructor(name, viewer) { this.name = name; this.viewer = viewer; } restart() {}; cleanup() {}; mousemove(e) {}; mousedown(e) {}; mouseup(e) {}; dblclick(e) {}; keydown(e) {}; keypress(e) {}; keyup(e) {}; sendMessage(text) { this.viewer.bus.dispatch('tool-message', text); }; sendHint(hint) { this.viewer.bus.dispatch('tool-hint', hint); }; sendSpecifyPointHint() { this.sendHint('specify point'); }; pointPicked(x, y) { this.sendMessage('picked: ' + this.viewer.roundToPrecision(x) + " : " + this.viewer.roundToPrecision(y)); this.viewer.referencePoint.x = x; this.viewer.referencePoint.y = y; }; snapIfNeed(p) { if (this.viewer.snapped != null) { const snapWith = this.viewer.snapped; this.viewer.cleanSnap(); p.setFromPoint(snapWith); this.viewer.parametricManager.linkObjects([p, snapWith]); this.viewer.parametricManager.refresh(); } } endpoint(e) { const ep = new EndPoint(0, 0); if (this.viewer.snapped != null) { this.snapIfNeed(ep); } else { ep.setFromPoint(this.viewer.screenToModel(e)) } return ep; } static dumbMode(e) { return e.ctrlKey || e.metaKey; } } Tool.ParseNumber = function(str) { let val; try { val = eval(str); } catch(e) { return e.toString(); } let valNumber = parseFloat(val); if (isNaN(valNumber)) return "wrong input for number: " + str; return valNumber; }; Tool.ParseNumberWithRef = function(str, ref) { const rel = str.startsWith('@'); if (rel) { str = str.substring(1); } let val = Tool.ParseNumber(str); if(typeof val === 'string') return val; if (rel) { val += ref; } return val; }; const VECTOR_PATTERN = /^(@)?(.+)(,|<)(.+)$/; Tool.ParseVector = function(referencePoint, command) { command = command.replace(/\s+/g, ''); const match = command.match(VECTOR_PATTERN); if (match) { const ref = match[1] !== undefined; let x = Tool.ParseNumber(match[2]); if(typeof x === 'string') return x; const polar = match[3] == '<'; let y = Tool.ParseNumber(match[4]); if(typeof y === 'string') return y; if (polar) { const angle = y / 180 * Math.PI; const radius = x; x = radius * Math.cos(angle); y = radius * Math.sin(angle); } if (ref) { x += referencePoint.x; y += referencePoint.y; } return {x, y}; } return "wrong input, point is expected: x,y | @x,y | r