mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-08 09:24:18 +01:00
implement command support for origin tool
This commit is contained in:
parent
f55ef264e4
commit
8e67c2cd39
3 changed files with 34 additions and 13 deletions
|
|
@ -7,6 +7,14 @@ ReferencePointTool.prototype.keydown = function(e) {};
|
|||
ReferencePointTool.prototype.keypress = function(e) {};
|
||||
ReferencePointTool.prototype.keyup = function(e) {};
|
||||
|
||||
ReferencePointTool.prototype.restart = function(e) {
|
||||
this.sendMessage('specify point');
|
||||
};
|
||||
|
||||
ReferencePointTool.prototype.sendMessage = function(text) {
|
||||
this.viewer.bus.notify('tool-message', text);
|
||||
};
|
||||
|
||||
ReferencePointTool.prototype.cleanup = function(e) {
|
||||
this.viewer.cleanSnap();
|
||||
};
|
||||
|
|
@ -25,6 +33,7 @@ ReferencePointTool.prototype.mousedown = function(e) {
|
|||
let p = needSnap ? this.viewer.snapped.pop() : this.viewer.screenToModel(e);
|
||||
this.viewer.referencePoint.x = p.x;
|
||||
this.viewer.referencePoint.y = p.y;
|
||||
this.sendMessage(p.x + ', ' + p.y);
|
||||
this.viewer.refresh();
|
||||
this.viewer.toolManager.releaseControl();
|
||||
};
|
||||
|
|
@ -32,30 +41,31 @@ 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;
|
||||
let result = ParseVector(referencePoint, command);
|
||||
if(typeof result === 'string') {
|
||||
return result;
|
||||
}
|
||||
referencePoint.x += result.x;
|
||||
referencePoint.y += result.y;
|
||||
this.viewer.refresh();
|
||||
this.viewer.toolManager.releaseControl();
|
||||
|
||||
};
|
||||
|
||||
const VECTOR_PATTERNS = /^(@)?(.+)(,|<)(.+)$/;
|
||||
|
||||
function ParseVector(referencePoint, command) {
|
||||
command = str.replace(/\s+/g, '');
|
||||
command = command.replace(/\s+/g, '');
|
||||
|
||||
const match = command.match(VECTOR_PATTERNS);
|
||||
if (match) {
|
||||
const ref = !match[1];
|
||||
let x = parseFloat(match[2]);
|
||||
let x = parseFloat(eval(match[2]));
|
||||
if (isNaN(x)) return "wrong input for number: " + match[2];
|
||||
const polar = match[3] == '<';
|
||||
let y = parseFloat(match[4]);
|
||||
let y = parseFloat(eval(match[4]));
|
||||
if (isNaN(y)) return "wrong input for number: " + match[4];
|
||||
if (polar) {
|
||||
y = y * Math.sin(x);
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ function App2D() {
|
|||
let coord = this.viewer.screenToModel(e);
|
||||
$('.coordinates-info').text(coord.x.toFixed(3) + " : " + coord.y.toFixed(3));
|
||||
});
|
||||
this.terminalHandeler = null;
|
||||
this.terminalHandler = undefined;
|
||||
this.terminal = new Terminal(this.commandsWin, (command) => this.handleTerminalInput(command), () => this.getAllCommandList());
|
||||
this.bindToolsToTerminal();
|
||||
|
||||
|
|
@ -368,6 +368,13 @@ App2D.prototype.getSketchId = function() {
|
|||
};
|
||||
|
||||
App2D.prototype.bindToolsToTerminal = function() {
|
||||
this.viewer.bus.subscribe('tool-state', () => {
|
||||
var tool = this.viewer.toolManager.tool;
|
||||
this.terminalHandler = tool.processCommand;
|
||||
});
|
||||
this.viewer.bus.subscribe('tool-message', (message) => {
|
||||
this.terminal.print(message);
|
||||
});
|
||||
};
|
||||
|
||||
App2D.STATIC_COMMANDS = {
|
||||
|
|
@ -384,8 +391,8 @@ App2D.prototype.getAllCommandList = function() {
|
|||
|
||||
App2D.prototype.handleTerminalInput = function(commandStr) {
|
||||
commandStr = commandStr.trim();
|
||||
if (this.terminalHandeler != null) {
|
||||
return this.terminalHandeler(commandStr);
|
||||
if (this.terminalHandler) {
|
||||
return this.terminalHandler(commandStr);
|
||||
} else {
|
||||
let cmd = App2D.STATIC_COMMANDS[commandStr];
|
||||
if (cmd) {
|
||||
|
|
|
|||
|
|
@ -868,11 +868,15 @@ function ToolManager(viewer, defaultTool) {
|
|||
ToolManager.prototype.takeControl = function(tool) {
|
||||
this.tool = tool;
|
||||
this.viewer.bus.notify("tool-state");
|
||||
if (this.tool.restart) {
|
||||
this.tool.restart();
|
||||
}
|
||||
};
|
||||
|
||||
ToolManager.prototype.releaseControl = function() {
|
||||
this.tool.cleanup();
|
||||
this.tool = this.defaultTool;
|
||||
this.viewer.bus.notify("tool-state");
|
||||
};
|
||||
|
||||
ToolManager.prototype.getTool = function() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue