diff --git a/web/app/sketcher/commands.js b/web/app/sketcher/commands.js new file mode 100644 index 00000000..139597f9 --- /dev/null +++ b/web/app/sketcher/commands.js @@ -0,0 +1,2 @@ + + diff --git a/web/app/sketcher/shapes/origin.js b/web/app/sketcher/shapes/origin.js index fdf34a11..f3873d7a 100644 --- a/web/app/sketcher/shapes/origin.js +++ b/web/app/sketcher/shapes/origin.js @@ -31,3 +31,42 @@ ReferencePointTool.prototype.mousedown = function(e) { ReferencePointTool.prototype.mousewheel = function(e) { }; + +ReferencePointTool.prototype.hint = function(e) { + return "specify point" +}; + +ReferencePointTool.prototype.processCommand = function(command) { + var referencePoint = this.viewer.referencePoint; + let point = ParseVector(referencePoint, command); + referencePoint.x += point.x; + referencePoint.y += point.y; + this.viewer.refresh(); +}; + +const VECTOR_PATTERNS = /^(@)?(.+)(,|<)(.+)$/; + +function ParseVector(referencePoint, command) { + command = str.replace(/\s+/g, ''); + + const match = command.match(VECTOR_PATTERNS); + if (match) { + const ref = !match[1]; + let x = parseFloat(match[2]); + if (isNaN(x)) return "wrong input for number: " + match[2]; + const polar = match[3] == '<'; + let y = parseFloat(match[4]); + if (isNaN(y)) return "wrong input for number: " + match[4]; + if (polar) { + y = y * Math.sin(x); + x = x * Math.cos(x); + } + if (ref) { + x += referencePoint.x; + y += referencePoint.y; + } + return {x, y}; + } + + return "wrong input, point is expected: x,y | @x,y | r this.handleTerminalInput(command)); + this.bindToolsToTerminal(); + + this.winManager.registerResize(dockEl, ui.DIRECTIONS.EAST, function() {$('body').trigger('layout'); }); $('body').on('layout', this.viewer.onWindowResize); - this.registerAction = function(id, desc, action) { - app.actions[id] = {id: id, desc: desc, action: action}; + this.registerAction = function(id, desc, action, command) { + app.actions[id] = {id, desc, action}; + app.commands[command] = id; app._actionsOrder.push(id); }; @@ -79,7 +82,7 @@ function App2D() { this.registerAction('exportDXF', "Export To DXF", function () { IO.exportTextData(app.viewer.io.dxfExport(), app.getSketchId() + ".dxf"); }); - + this.registerAction('undo', "Undo", function () { app.viewer.historyManager.undo(); }); @@ -94,7 +97,7 @@ function App2D() { this.registerAction('referencePoint', "Set Reference Point", function () { app.viewer.toolManager.takeControl(new ReferencePointTool(app.viewer)); - }); + }, "origin"); this.registerAction('addPoint', "Add Point", function () { app.viewer.toolManager.takeControl(new AddPointTool(app.viewer)); @@ -367,6 +370,37 @@ App2D.prototype.getSketchId = function() { return App2D.STORAGE_PREFIX + id; }; +App2D.prototype.bindToolsToTerminal = function() { +}; + +App2D.STATIC_COMMANDS = { + "time" : () => new Date(), + "help" : (app) => app.getAllCommandList().join(", ") + +}; + +App2D.prototype.getAllCommandList = function() { + const commands = this.commands.slice(); + commands.push.apply(commands, App2D.STATIC_COMMANDS); + return commands; +}; + +App2D.prototype.handleTerminalInput = function(commandStr) { + commandStr = commandStr.trim(); + if (this.terminalHandeler != null) { + this.terminalHandeler(commandStr); + } else { + let cmd = App2D.STATIC_COMMANDS[commandStr]; + if (cmd) { + cmd(this); + } + let actionDef = this.commands[cmd]; + if (actionDef) { + actionDef(); + } + } +}; + App2D.STORAGE_PREFIX = "TCAD.projects."; -export default App2D; \ No newline at end of file +export default App2D;- \ No newline at end of file diff --git a/web/app/sketcher/viewer2d.js b/web/app/sketcher/viewer2d.js index bd3ce18e..b1de0e41 100644 --- a/web/app/sketcher/viewer2d.js +++ b/web/app/sketcher/viewer2d.js @@ -813,6 +813,7 @@ ReferencePoint.prototype.draw = function(ctx, scale) { function ToolManager(viewer, defaultTool) { this.defaultTool = defaultTool; this.tool = defaultTool; + this.viewer = viewer; var canvas = viewer.canvas; var tm = this; canvas.addEventListener('mousemove', function (e) { @@ -866,6 +867,7 @@ function ToolManager(viewer, defaultTool) { ToolManager.prototype.takeControl = function(tool) { this.tool = tool; + this.viewer.bus.notify("tool-state"); }; ToolManager.prototype.releaseControl = function() {