Implement PaperScope#tool accessor.

For better handling of automatic tool creation in PaperScript.
This commit is contained in:
Jürg Lehni 2011-12-27 16:33:17 +01:00
parent 7b03dbedb9
commit 6e3cef6eb4
3 changed files with 20 additions and 20 deletions

View file

@ -51,7 +51,6 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
paper = this;
this.project = null;
this.projects = [];
this.tool = null;
this.tools = [];
// Assign an id to this canvas that's either extracted from the script
// or automatically generated.
@ -86,8 +85,8 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
/**
* The reference to the active project's view.
* @name PaperScope#view
* @type View
* @bean
*/
getView: function() {
return this.project.view;
@ -95,9 +94,16 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
/**
* The reference to the active tool.
* @name PaperScope#tool
* @type Tool
* @bean
*/
getTool: function() {
// If no tool exists yet but one is requested, produce it now on the fly
// so it can be used in PaperScript.
if (!this._tool)
this._tool = new Tool();
return this._tool;
},
/**
* The list of available tools.

View file

@ -156,8 +156,6 @@ var PaperScript = this.PaperScript = new function() {
// Set currently active scope.
paper = scope;
var view = scope.project.view,
tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
&& new Tool(),
res;
// Define variables for potential handlers, so eval() calls below to
// fetch their values do not require try-catch around them.
@ -171,20 +169,16 @@ var PaperScript = this.PaperScript = new function() {
onMouseDown, onMouseUp, onMouseDrag, onMouseMove,
onKeyDown, onKeyUp, onFrame, onResize;
res = eval(compile(code));
if (tool) {
// We could do this instead to avoid eval(), but it's longer
// tool.onEditOptions = onEditOptions;
// tool.onSelect = onSelect;
// tool.onDeselect = onDeselect;
// tool.onReselect = onReselect;
// tool.onMouseDown = onMouseDown;
// tool.onMouseUp = onMouseUp;
// tool.onMouseDrag = onMouseDrag;
// tool.onMouseMove = onMouseMove;
// tool.onKeyDown = onKeyDown;
// tool.onKeyUp = onKeyUp;
Base.each(tool._events, function(key) {
tool[key] = eval(key);
// Only look for tool handlers if something resembling their
// name is contained in the code.
if (/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)) {
Base.each(Tool.prototype._events, function(key) {
var value = eval(key);
if (value) {
// Use the getTool accessor that handles auto tool
// creation for us:
scope.getTool()[key] = value;
}
});
}
if (view) {

View file

@ -48,7 +48,7 @@
*/
var Tool = this.Tool = PaperScopeItem.extend(Callback, /** @lends Tool# */{
_list: 'tools',
_reference: 'tool',
_reference: '_tool', // PaperScope has accessor for #tool
_events: [ 'onEditOptions', 'onSelect', 'onDeselect', 'onReselect',
'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onMouseMove',
'onKeyDown', 'onKeyUp' ],