Skip to content

Commit fe97b94

Browse files
committed
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 paperjs#27
1 parent 2d57885 commit fe97b94

6 files changed

Lines changed: 139 additions & 64 deletions

File tree

src/core/PaperScopeItem.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Paper.js
3+
*
4+
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
5+
* based on Scriptographer.org and designed to be largely API compatible.
6+
* http://paperjs.org/
7+
* http://scriptographer.org/
8+
*
9+
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
10+
* http://lehni.org/ & http://jonathanpuckey.com/
11+
*
12+
* Distributed under the MIT license. See LICENSE file for details.
13+
*
14+
* All rights reserved.
15+
*/
16+
17+
/**
18+
* @name PaperScopeItem
19+
*
20+
* @class A private base class for all classes that have lists and references in
21+
* the {@link PaperScope} ({@link Project}, {@link View}, {@link Tool}), so
22+
* functionality can be shared.
23+
*
24+
* @private
25+
*/
26+
var PaperScopeItem = Base.extend(/** @lends PaperScopeItem# */{
27+
28+
/**
29+
* Creates a PaperScopeItem object.
30+
*/
31+
initialize: function(activate) {
32+
// Store reference to the currently active global paper scope:
33+
this._scope = paper;
34+
// Push it onto this._scope.projects and set index:
35+
this._index = this._scope[this._list].push(this) - 1;
36+
// If the project has no active reference, activate this one
37+
if (activate || !this._scope[this._reference])
38+
this.activate();
39+
},
40+
41+
activate: function() {
42+
if (!this._scope)
43+
return false;
44+
this._scope[this._reference] = this;
45+
return true;
46+
},
47+
48+
remove: function() {
49+
if (this._index == null)
50+
return false;
51+
Base.splice(this._scope[this._list], null, this._index, 1);
52+
// Clear the active tool reference if it was pointint to this.
53+
if (this._scope[this._reference] == this)
54+
this._scope[this._reference] = null;
55+
this._scope = null;
56+
return true;
57+
}
58+
});

src/core/PaperScript.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ var PaperScript = this.PaperScript = new function() {
156156
// Set currently active scope.
157157
paper = scope;
158158
var view = scope.view,
159-
// TODO: Add support for multiple tools
160-
tool = scope.tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
159+
tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
161160
&& new Tool(),
162161
res;
163162
// Define variables for potential handlers, so eval() calls below to

src/paper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var paper = new function() {
5353

5454
/*#*/ include('core/Base.js');
5555
/*#*/ include('core/PaperScope.js');
56+
/*#*/ include('core/PaperScopeItem.js');
5657

5758
// Include Paper classes, which are later injected into PaperScope by setting
5859
// them on the 'this' object, e.g.:

src/project/Project.js

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
* An array of all open projects is accessible through the
3535
* {@link PaperScope#projects} variable.
3636
*/
37-
var Project = this.Project = Base.extend(/** @lends Project# */{
37+
var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
38+
_list: 'projects',
39+
_reference: 'project',
40+
3841
// TODO: Add arguments to define pages
3942
/**
4043
* Creates a Paper.js project.
@@ -43,16 +46,12 @@ var Project = this.Project = Base.extend(/** @lends Project# */{
4346
* and the {@link PaperScope#project} variable points to it.
4447
*/
4548
initialize: function() {
46-
// Store reference to the currently active global paper scope:
47-
this._scope = paper;
48-
// Push it onto this._scope.projects and set index:
49-
this._index = this._scope.projects.push(this) - 1;
49+
// Activate straight away so paper.project is set, as required by
50+
// Layer and DoumentView constructors.
51+
this.base(true);
5052
this._currentStyle = new PathStyle();
5153
this._selectedItems = {};
5254
this._selectedItemCount = 0;
53-
// Activate straight away so paper.project is set, as required by
54-
// Layer and DoumentView constructors.
55-
this.activate();
5655
this.layers = [];
5756
this.symbols = [];
5857
this.activeLayer = new Layer();
@@ -63,6 +62,21 @@ var Project = this.Project = Base.extend(/** @lends Project# */{
6362
this._scope._needsRedraw();
6463
},
6564

65+
/**
66+
* Activates this project, so all newly created items will be placed
67+
* in it.
68+
*
69+
* @name Project#activate
70+
* @function
71+
*/
72+
73+
/**
74+
* Removes this project from the {@link PaperScope#projects} list.
75+
*
76+
* @name Project#remove
77+
* @function
78+
*/
79+
6680
/**
6781
* The currently active path style. All selected items and newly
6882
* created items will be styled with this style.
@@ -98,33 +112,6 @@ var Project = this.Project = Base.extend(/** @lends Project# */{
98112
this._currentStyle.initialize(style);
99113
},
100114

101-
/**
102-
* Activates this project, so all newly created items will be placed
103-
* in it.
104-
*/
105-
activate: function() {
106-
if (this._scope) {
107-
this._scope.project = this;
108-
return true;
109-
}
110-
return false;
111-
},
112-
113-
/**
114-
* Removes this project from the {@link PaperScope#projects} list.
115-
*/
116-
remove: function() {
117-
if (this._scope) {
118-
Base.splice(this._scope.projects, null, this._index, 1);
119-
// Clear the active project reference if it was pointint to this.
120-
if (this._scope.project == this)
121-
this._scope.project = null;
122-
this._scope = null;
123-
return true;
124-
}
125-
return false;
126-
},
127-
128115
/**
129116
* The index of the project in the {@link PaperScope#projects} list.
130117
*

src/tool/Tool.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,33 @@
4646
* path.add(event.point);
4747
* }
4848
*/
49-
var Tool = this.Tool = Base.extend(/** @lends Tool# */{
49+
var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{
50+
_list: 'tools',
51+
_reference: 'tool',
52+
5053
// DOCS: rewrite Tool constructor explanation
5154
initialize: function() {
52-
// Store reference to the currently active global paper scope:
53-
this._scope = paper;
55+
this.base();
5456
this._firstMove = true;
5557
this._count = 0;
5658
this._downCount = 0;
5759
},
5860

61+
/**
62+
* Activates this tool, meaning {@link PaperScope#tool} will
63+
* point to it and it will be the one that recieves mouse events.
64+
*
65+
* @name Tool#activate
66+
* @function
67+
*/
68+
69+
/**
70+
* Removes this tool from the {@link PaperScope#tools} list.
71+
*
72+
* @name Tool#remove
73+
* @function
74+
*/
75+
5976
/**
6077
* The fixed time delay in milliseconds between each call to the
6178
* {@link #onMouseDrag} event. Setting this to an interval means the

src/ui/View.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
* center, both useful for constructing artwork that should appear centered on
2424
* screen.
2525
*/
26-
var View = this.View = Base.extend(/** @lends View# */{
26+
var View = this.View = PaperScopeItem.extend(/** @lends View# */{
27+
_list: 'views',
28+
_reference: 'view',
29+
2730
/**
2831
* Creates a view object
2932
* @param {HTMLCanvasElement|String} canvas The canvas object that this
3033
* view should wrap, or the String id that represents it
3134
*/
3235
initialize: function(canvas) {
33-
// Associate this view with the active paper scope.
34-
this._scope = paper;
35-
// Push it onto project.views and set index:
36-
this._index = this._scope.views.push(this) - 1;
36+
this.base();
3737
// Handle canvas argument
3838
var size;
3939
if (typeof canvas === 'string')
@@ -109,6 +109,32 @@ var View = this.View = Base.extend(/** @lends View# */{
109109
this._scope._redrawNotified = false;
110110
},
111111

112+
/**
113+
* Makes this view the active one, meaning {@link PaperScope#view} will
114+
* point to it.
115+
*
116+
* @name View#activate
117+
* @function
118+
*/
119+
120+
/**
121+
* Removes thsi view from the {@link PaperScope#views} list and frees the
122+
* associated canvas.
123+
*/
124+
remove: function() {
125+
if (!this.base())
126+
return false;
127+
// Clear focus if removed view had it
128+
if (View._focused == this)
129+
View._focused = null;
130+
delete View._views[this._id];
131+
// Uninstall event handlers again for this view.
132+
DomEvent.remove(this._canvas, this._events);
133+
// Clearing _onFrame makes the frame handler stop automatically.
134+
this._canvas = this._events = this._onFrame = null;
135+
return true;
136+
},
137+
112138
/**
113139
* The underlying native canvas element.
114140
*
@@ -239,6 +265,12 @@ var View = this.View = Base.extend(/** @lends View# */{
239265
this._inverse = null;
240266
},
241267

268+
/**
269+
* Draws the view.
270+
*
271+
* @name View#draw
272+
* @function
273+
*/
242274
draw: function(checkRedraw) {
243275
if (checkRedraw && !this._redrawNeeded)
244276
return false;
@@ -264,25 +296,6 @@ var View = this.View = Base.extend(/** @lends View# */{
264296
return true;
265297
},
266298

267-
activate: function() {
268-
this._scope.view = this;
269-
},
270-
271-
remove: function() {
272-
if (this._index == null)
273-
return false;
274-
// Clear focus if removed view had it
275-
if (View._focused == this)
276-
View._focused = null;
277-
delete View._views[this._id];
278-
Base.splice(this._scope.views, null, this._index, 1);
279-
// Uninstall event handlers again for this view.
280-
DomEvent.remove(this._canvas, this._events);
281-
// Clearing _onFrame makes the frame handler stop automatically.
282-
this._scope = this._canvas = this._events = this._onFrame = null;
283-
return true;
284-
},
285-
286299
// TODO: getInvalidBounds
287300
// TODO: invalidate(rect)
288301
// TODO: style: artwork / preview / raster / opaque / ink

0 commit comments

Comments
 (0)