mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 06:00:56 -05:00
Implement PaperScope#tool accessor.
For better handling of automatic tool creation in PaperScript.
This commit is contained in:
parent
7b03dbedb9
commit
6e3cef6eb4
3 changed files with 20 additions and 20 deletions
|
@ -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.
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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' ],
|
||||
|
|
Loading…
Reference in a new issue