mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2024-12-29 09:22:22 -05:00
Implement PaperScope#settings.insertItems
Controlling whether newly created items are automatically inserted into the scene graph.
This commit is contained in:
parent
56dd636f22
commit
4e7fa2f04e
4 changed files with 102 additions and 16 deletions
|
@ -50,6 +50,7 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
|
|||
// Default configurable settings.
|
||||
this.settings = new Base({
|
||||
applyMatrix: true,
|
||||
insertItems: true,
|
||||
handleSize: 4,
|
||||
hitTolerance: 0
|
||||
});
|
||||
|
@ -115,16 +116,22 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
|
|||
*/
|
||||
version: /*#=*/__options.version,
|
||||
|
||||
// DOCS: PaperScope#settings
|
||||
/**
|
||||
* Gives access to paper's configurable settings.
|
||||
*
|
||||
* @name PaperScope#settings
|
||||
* @type Object
|
||||
*
|
||||
* @option [settings.applyMatrix=true] {Boolean}
|
||||
* @option [settings.handleSize=4] {Number}
|
||||
* @option [settings.hitTolerance=0] {Number}
|
||||
* @option [settings.insertItems=true] {Boolean} controls whether newly
|
||||
* created items are automatically inserted into the scene graph, by
|
||||
* adding them to {@link Project#getActiveLayer()}
|
||||
* @option [settings.applyMatrix=true] {Boolean} controls what value newly
|
||||
* created items have their {@link Item#getApplyMatrix()} property set
|
||||
* to (Note that not all items can set this to `false`)
|
||||
* @option [settings.handleSize=4] {Number} the size of the curve handles
|
||||
* when drawing selections
|
||||
* @option [settings.hitTolerance=0] {Number} the default tolerance for hit-
|
||||
* tests, when no value is specified
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
|
@ -127,20 +127,22 @@ new function() { // Injection scope for various item event handlers
|
|||
internal = hasProps && props.internal === true,
|
||||
matrix = this._matrix = new Matrix(),
|
||||
// Allow setting another project than the currently active one.
|
||||
project = hasProps && props.project || paper.project;
|
||||
project = hasProps && props.project || paper.project,
|
||||
settings = paper.settings;
|
||||
this._id = internal ? null : UID.get();
|
||||
this._parent = this._index = null;
|
||||
// Inherit the applyMatrix setting from paper.settings.applyMatrix
|
||||
this._applyMatrix = this._canApplyMatrix && paper.settings.applyMatrix;
|
||||
// Inherit the applyMatrix setting from settings.applyMatrix
|
||||
this._applyMatrix = this._canApplyMatrix && settings.applyMatrix;
|
||||
// Handle matrix before everything else, to avoid issues with
|
||||
// #addChild() calling _changed() and accessing _matrix already.
|
||||
if (point)
|
||||
matrix.translate(point);
|
||||
matrix._owner = this;
|
||||
this._style = new Style(project._currentStyle, this, project);
|
||||
// Do not add to the project if it's an internal path, if props.insert
|
||||
// is false, or if the props are setting a different parent anyway.
|
||||
if (internal || hasProps && props.insert === false) {
|
||||
// Do not add to the project if it's an internal path, or if
|
||||
// props.insert or settings.isnertItems is false.
|
||||
if (internal || hasProps && props.insert === false
|
||||
|| !settings.insertItems && !(hasProps && props.insert === true)) {
|
||||
this._setProject(project);
|
||||
} else {
|
||||
(hasProps && props.parent || project)
|
||||
|
@ -2327,10 +2329,11 @@ new function() { // Injection scope for hit-test functions shared with project
|
|||
} else {
|
||||
// If the item is removed and inserted it again further
|
||||
/// above, the index needs to be adjusted accordingly.
|
||||
var shift = item._parent === this && item._index < index;
|
||||
var parent = item._parent,
|
||||
shift = parent === this && item._index < index;
|
||||
// Notify parent of change. Don't notify item itself yet,
|
||||
// as we're doing so when adding it to the new parent below.
|
||||
if (item._remove(false, true) && shift)
|
||||
if (parent && item._remove(false, true) && shift)
|
||||
index--;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -238,7 +238,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
* @type Layer
|
||||
*/
|
||||
getActiveLayer: function() {
|
||||
return this._activeLayer || new Layer({ project: this });
|
||||
return this._activeLayer || new Layer({ project: this, insert: true });
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -693,6 +693,81 @@ test('Item#blendMode in a transformed Group', function() {
|
|||
'Middle center pixel should be yellow:');
|
||||
});
|
||||
|
||||
test('Item#applyMatrix', function() {
|
||||
equals(function() {
|
||||
return new Path({ applyMatrix: true }).applyMatrix;
|
||||
}, true);
|
||||
equals(function() {
|
||||
return new Path({ applyMatrix: false }).applyMatrix;
|
||||
}, false);
|
||||
equals(function() {
|
||||
return new Raster({ applyMatrix: false }).applyMatrix;
|
||||
}, false);
|
||||
equals(function() {
|
||||
return new Raster({ applyMatrix: true }).applyMatrix;
|
||||
}, false);
|
||||
|
||||
var applyMatrix = paper.settings.applyMatrix;
|
||||
paper.settings.applyMatrix = true;
|
||||
equals(function() {
|
||||
return new Path().applyMatrix;
|
||||
}, true);
|
||||
equals(function() {
|
||||
return new Raster().applyMatrix;
|
||||
}, false);
|
||||
paper.settings.applyMatrix = false;
|
||||
equals(function() {
|
||||
return new Path().applyMatrix;
|
||||
}, false);
|
||||
equals(function() {
|
||||
return new Raster().applyMatrix;
|
||||
}, false);
|
||||
paper.settings.applyMatrix = applyMatrix;
|
||||
|
||||
var path = new Path.Rectangle({
|
||||
size: [100, 100],
|
||||
position: [0, 0],
|
||||
applyMatrix: false
|
||||
});
|
||||
|
||||
equals(path.matrix, new Matrix(),
|
||||
'path.matrix before scaling');
|
||||
equals(path.bounds, new Rectangle(-50, -50, 100, 100),
|
||||
'path.bounds before scaling');
|
||||
equals(path.segments[0].point, new Point(-50, 50),
|
||||
'path.segments[0].point before scaling');
|
||||
|
||||
path.scale(1, 2);
|
||||
|
||||
equals(path.matrix, new Matrix().scale(1, 2),
|
||||
'path.matrix after scaling');
|
||||
equals(path.bounds, new Rectangle(-50, -100, 100, 200),
|
||||
'path.bounds after scaling');
|
||||
equals(path.segments[0].point, new Point(-50, 50),
|
||||
'path.segments[0].point after scaling');
|
||||
|
||||
path.applyMatrix = true;
|
||||
|
||||
equals(path.matrix, new Matrix(),
|
||||
'path.matrix after setting path.applyMatrix = true;');
|
||||
equals(path.bounds, new Rectangle(-50, -100, 100, 200),
|
||||
'path.bounds after setting path.applyMatrix = true;');
|
||||
equals(path.segments[0].point, new Point(-50, 100),
|
||||
'path.segments[0].point after setting path.applyMatrix = true;');
|
||||
});
|
||||
|
||||
test('PaperScope#settings.insertItems', function() {
|
||||
var insertItems = paper.settings.insertItems;
|
||||
paper.settings.insertItems = true;
|
||||
equals(function() {
|
||||
return new Path().parent === project.activeLayer;
|
||||
}, true);
|
||||
paper.settings.insertItems = false;
|
||||
equals(function() {
|
||||
return new Path().parent === null;
|
||||
}, true);
|
||||
paper.settings.insertItems = insertItems;
|
||||
});
|
||||
|
||||
test('Item#pivot', function() {
|
||||
var path1 = new Path.Rectangle({
|
||||
|
@ -713,11 +788,12 @@ test('Item#pivot', function() {
|
|||
|
||||
path1.pivot = pivot;
|
||||
path1.position = [200, 200];
|
||||
equals(path1.pivot, pivot, 'Changing position of an item with applyMatrix = false should not change pivot');
|
||||
equals(path1.pivot, pivot,
|
||||
'Changing position of an item with applyMatrix = false should not change pivot');
|
||||
|
||||
var difference = new Point(100, 100);
|
||||
path2.pivot = pivot;
|
||||
path2.position = path2.position.add(difference);
|
||||
equals(path2.pivot, pivot.add(difference), 'Changing position of an item with applyMatrix = true should change pivot');
|
||||
|
||||
equals(path2.pivot, pivot.add(difference),
|
||||
'Changing position of an item with applyMatrix = true should change pivot');
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue