Define PaperScopeItem as a private base class for all classes that have lists and references in the PaperScope (Project, View, Tool), so they can share functionality (#initialize(), #activate(), #remove()), and add support for multiple tools. Closes #27

This commit is contained in:
Jürg Lehni 2011-08-02 10:08:08 +01:00
parent 2d5788540d
commit fe97b94340
6 changed files with 139 additions and 64 deletions

View file

@ -0,0 +1,58 @@
/*
* Paper.js
*
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
* based on Scriptographer.org and designed to be largely API compatible.
* http://paperjs.org/
* http://scriptographer.org/
*
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name PaperScopeItem
*
* @class A private base class for all classes that have lists and references in
* the {@link PaperScope} ({@link Project}, {@link View}, {@link Tool}), so
* functionality can be shared.
*
* @private
*/
var PaperScopeItem = Base.extend(/** @lends PaperScopeItem# */{
/**
* Creates a PaperScopeItem object.
*/
initialize: function(activate) {
// Store reference to the currently active global paper scope:
this._scope = paper;
// Push it onto this._scope.projects and set index:
this._index = this._scope[this._list].push(this) - 1;
// If the project has no active reference, activate this one
if (activate || !this._scope[this._reference])
this.activate();
},
activate: function() {
if (!this._scope)
return false;
this._scope[this._reference] = this;
return true;
},
remove: function() {
if (this._index == null)
return false;
Base.splice(this._scope[this._list], null, this._index, 1);
// Clear the active tool reference if it was pointint to this.
if (this._scope[this._reference] == this)
this._scope[this._reference] = null;
this._scope = null;
return true;
}
});

View file

@ -156,8 +156,7 @@ var PaperScript = this.PaperScript = new function() {
// Set currently active scope.
paper = scope;
var view = scope.view,
// TODO: Add support for multiple tools
tool = scope.tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
&& new Tool(),
res;
// Define variables for potential handlers, so eval() calls below to

View file

@ -53,6 +53,7 @@ var paper = new function() {
/*#*/ include('core/Base.js');
/*#*/ include('core/PaperScope.js');
/*#*/ include('core/PaperScopeItem.js');
// Include Paper classes, which are later injected into PaperScope by setting
// them on the 'this' object, e.g.:

View file

@ -34,7 +34,10 @@
* An array of all open projects is accessible through the
* {@link PaperScope#projects} variable.
*/
var Project = this.Project = Base.extend(/** @lends Project# */{
var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
_list: 'projects',
_reference: 'project',
// TODO: Add arguments to define pages
/**
* Creates a Paper.js project.
@ -43,16 +46,12 @@ var Project = this.Project = Base.extend(/** @lends Project# */{
* and the {@link PaperScope#project} variable points to it.
*/
initialize: function() {
// Store reference to the currently active global paper scope:
this._scope = paper;
// Push it onto this._scope.projects and set index:
this._index = this._scope.projects.push(this) - 1;
// Activate straight away so paper.project is set, as required by
// Layer and DoumentView constructors.
this.base(true);
this._currentStyle = new PathStyle();
this._selectedItems = {};
this._selectedItemCount = 0;
// Activate straight away so paper.project is set, as required by
// Layer and DoumentView constructors.
this.activate();
this.layers = [];
this.symbols = [];
this.activeLayer = new Layer();
@ -63,6 +62,21 @@ var Project = this.Project = Base.extend(/** @lends Project# */{
this._scope._needsRedraw();
},
/**
* Activates this project, so all newly created items will be placed
* in it.
*
* @name Project#activate
* @function
*/
/**
* Removes this project from the {@link PaperScope#projects} list.
*
* @name Project#remove
* @function
*/
/**
* The currently active path style. All selected items and newly
* created items will be styled with this style.
@ -98,33 +112,6 @@ var Project = this.Project = Base.extend(/** @lends Project# */{
this._currentStyle.initialize(style);
},
/**
* Activates this project, so all newly created items will be placed
* in it.
*/
activate: function() {
if (this._scope) {
this._scope.project = this;
return true;
}
return false;
},
/**
* Removes this project from the {@link PaperScope#projects} list.
*/
remove: function() {
if (this._scope) {
Base.splice(this._scope.projects, null, this._index, 1);
// Clear the active project reference if it was pointint to this.
if (this._scope.project == this)
this._scope.project = null;
this._scope = null;
return true;
}
return false;
},
/**
* The index of the project in the {@link PaperScope#projects} list.
*

View file

@ -46,16 +46,33 @@
* path.add(event.point);
* }
*/
var Tool = this.Tool = Base.extend(/** @lends Tool# */{
var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{
_list: 'tools',
_reference: 'tool',
// DOCS: rewrite Tool constructor explanation
initialize: function() {
// Store reference to the currently active global paper scope:
this._scope = paper;
this.base();
this._firstMove = true;
this._count = 0;
this._downCount = 0;
},
/**
* Activates this tool, meaning {@link PaperScope#tool} will
* point to it and it will be the one that recieves mouse events.
*
* @name Tool#activate
* @function
*/
/**
* Removes this tool from the {@link PaperScope#tools} list.
*
* @name Tool#remove
* @function
*/
/**
* The fixed time delay in milliseconds between each call to the
* {@link #onMouseDrag} event. Setting this to an interval means the

View file

@ -23,17 +23,17 @@
* center, both useful for constructing artwork that should appear centered on
* screen.
*/
var View = this.View = Base.extend(/** @lends View# */{
var View = this.View = PaperScopeItem.extend(/** @lends View# */{
_list: 'views',
_reference: 'view',
/**
* Creates a view object
* @param {HTMLCanvasElement|String} canvas The canvas object that this
* view should wrap, or the String id that represents it
*/
initialize: function(canvas) {
// Associate this view with the active paper scope.
this._scope = paper;
// Push it onto project.views and set index:
this._index = this._scope.views.push(this) - 1;
this.base();
// Handle canvas argument
var size;
if (typeof canvas === 'string')
@ -109,6 +109,32 @@ var View = this.View = Base.extend(/** @lends View# */{
this._scope._redrawNotified = false;
},
/**
* Makes this view the active one, meaning {@link PaperScope#view} will
* point to it.
*
* @name View#activate
* @function
*/
/**
* Removes thsi view from the {@link PaperScope#views} list and frees the
* associated canvas.
*/
remove: function() {
if (!this.base())
return false;
// Clear focus if removed view had it
if (View._focused == this)
View._focused = null;
delete View._views[this._id];
// Uninstall event handlers again for this view.
DomEvent.remove(this._canvas, this._events);
// Clearing _onFrame makes the frame handler stop automatically.
this._canvas = this._events = this._onFrame = null;
return true;
},
/**
* The underlying native canvas element.
*
@ -239,6 +265,12 @@ var View = this.View = Base.extend(/** @lends View# */{
this._inverse = null;
},
/**
* Draws the view.
*
* @name View#draw
* @function
*/
draw: function(checkRedraw) {
if (checkRedraw && !this._redrawNeeded)
return false;
@ -264,25 +296,6 @@ var View = this.View = Base.extend(/** @lends View# */{
return true;
},
activate: function() {
this._scope.view = this;
},
remove: function() {
if (this._index == null)
return false;
// Clear focus if removed view had it
if (View._focused == this)
View._focused = null;
delete View._views[this._id];
Base.splice(this._scope.views, null, this._index, 1);
// Uninstall event handlers again for this view.
DomEvent.remove(this._canvas, this._events);
// Clearing _onFrame makes the frame handler stop automatically.
this._scope = this._canvas = this._events = this._onFrame = null;
return true;
},
// TODO: getInvalidBounds
// TODO: invalidate(rect)
// TODO: style: artwork / preview / raster / opaque / ink