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.keypress = function(e) {};
|
||||||
ReferencePointTool.prototype.keyup = 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) {
|
ReferencePointTool.prototype.cleanup = function(e) {
|
||||||
this.viewer.cleanSnap();
|
this.viewer.cleanSnap();
|
||||||
};
|
};
|
||||||
|
|
@ -25,6 +33,7 @@ ReferencePointTool.prototype.mousedown = function(e) {
|
||||||
let p = needSnap ? this.viewer.snapped.pop() : this.viewer.screenToModel(e);
|
let p = needSnap ? this.viewer.snapped.pop() : this.viewer.screenToModel(e);
|
||||||
this.viewer.referencePoint.x = p.x;
|
this.viewer.referencePoint.x = p.x;
|
||||||
this.viewer.referencePoint.y = p.y;
|
this.viewer.referencePoint.y = p.y;
|
||||||
|
this.sendMessage(p.x + ', ' + p.y);
|
||||||
this.viewer.refresh();
|
this.viewer.refresh();
|
||||||
this.viewer.toolManager.releaseControl();
|
this.viewer.toolManager.releaseControl();
|
||||||
};
|
};
|
||||||
|
|
@ -32,30 +41,31 @@ ReferencePointTool.prototype.mousedown = function(e) {
|
||||||
ReferencePointTool.prototype.mousewheel = function(e) {
|
ReferencePointTool.prototype.mousewheel = function(e) {
|
||||||
};
|
};
|
||||||
|
|
||||||
ReferencePointTool.prototype.hint = function(e) {
|
|
||||||
return "specify point"
|
|
||||||
};
|
|
||||||
|
|
||||||
ReferencePointTool.prototype.processCommand = function(command) {
|
ReferencePointTool.prototype.processCommand = function(command) {
|
||||||
var referencePoint = this.viewer.referencePoint;
|
var referencePoint = this.viewer.referencePoint;
|
||||||
let point = ParseVector(referencePoint, command);
|
let result = ParseVector(referencePoint, command);
|
||||||
referencePoint.x += point.x;
|
if(typeof result === 'string') {
|
||||||
referencePoint.y += point.y;
|
return result;
|
||||||
|
}
|
||||||
|
referencePoint.x += result.x;
|
||||||
|
referencePoint.y += result.y;
|
||||||
this.viewer.refresh();
|
this.viewer.refresh();
|
||||||
|
this.viewer.toolManager.releaseControl();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const VECTOR_PATTERNS = /^(@)?(.+)(,|<)(.+)$/;
|
const VECTOR_PATTERNS = /^(@)?(.+)(,|<)(.+)$/;
|
||||||
|
|
||||||
function ParseVector(referencePoint, command) {
|
function ParseVector(referencePoint, command) {
|
||||||
command = str.replace(/\s+/g, '');
|
command = command.replace(/\s+/g, '');
|
||||||
|
|
||||||
const match = command.match(VECTOR_PATTERNS);
|
const match = command.match(VECTOR_PATTERNS);
|
||||||
if (match) {
|
if (match) {
|
||||||
const ref = !match[1];
|
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];
|
if (isNaN(x)) return "wrong input for number: " + match[2];
|
||||||
const polar = match[3] == '<';
|
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 (isNaN(y)) return "wrong input for number: " + match[4];
|
||||||
if (polar) {
|
if (polar) {
|
||||||
y = y * Math.sin(x);
|
y = y * Math.sin(x);
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ function App2D() {
|
||||||
let coord = this.viewer.screenToModel(e);
|
let coord = this.viewer.screenToModel(e);
|
||||||
$('.coordinates-info').text(coord.x.toFixed(3) + " : " + coord.y.toFixed(3));
|
$('.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.terminal = new Terminal(this.commandsWin, (command) => this.handleTerminalInput(command), () => this.getAllCommandList());
|
||||||
this.bindToolsToTerminal();
|
this.bindToolsToTerminal();
|
||||||
|
|
||||||
|
|
@ -368,6 +368,13 @@ App2D.prototype.getSketchId = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
App2D.prototype.bindToolsToTerminal = 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 = {
|
App2D.STATIC_COMMANDS = {
|
||||||
|
|
@ -384,8 +391,8 @@ App2D.prototype.getAllCommandList = function() {
|
||||||
|
|
||||||
App2D.prototype.handleTerminalInput = function(commandStr) {
|
App2D.prototype.handleTerminalInput = function(commandStr) {
|
||||||
commandStr = commandStr.trim();
|
commandStr = commandStr.trim();
|
||||||
if (this.terminalHandeler != null) {
|
if (this.terminalHandler) {
|
||||||
return this.terminalHandeler(commandStr);
|
return this.terminalHandler(commandStr);
|
||||||
} else {
|
} else {
|
||||||
let cmd = App2D.STATIC_COMMANDS[commandStr];
|
let cmd = App2D.STATIC_COMMANDS[commandStr];
|
||||||
if (cmd) {
|
if (cmd) {
|
||||||
|
|
|
||||||
|
|
@ -868,11 +868,15 @@ function ToolManager(viewer, defaultTool) {
|
||||||
ToolManager.prototype.takeControl = function(tool) {
|
ToolManager.prototype.takeControl = function(tool) {
|
||||||
this.tool = tool;
|
this.tool = tool;
|
||||||
this.viewer.bus.notify("tool-state");
|
this.viewer.bus.notify("tool-state");
|
||||||
|
if (this.tool.restart) {
|
||||||
|
this.tool.restart();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ToolManager.prototype.releaseControl = function() {
|
ToolManager.prototype.releaseControl = function() {
|
||||||
this.tool.cleanup();
|
this.tool.cleanup();
|
||||||
this.tool = this.defaultTool;
|
this.tool = this.defaultTool;
|
||||||
|
this.viewer.bus.notify("tool-state");
|
||||||
};
|
};
|
||||||
|
|
||||||
ToolManager.prototype.getTool = function() {
|
ToolManager.prototype.getTool = function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue