From 4e7fa2f04e6e643407cafc8beaa51ee2a5e9caff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 14 Feb 2016 21:34:35 +0100 Subject: [PATCH] Implement PaperScope#settings.insertItems Controlling whether newly created items are automatically inserted into the scene graph. --- src/core/PaperScope.js | 15 +++++--- src/item/Item.js | 19 +++++----- src/item/Project.js | 2 +- test/tests/Item.js | 82 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 102 insertions(+), 16 deletions(-) diff --git a/src/core/PaperScope.js b/src/core/PaperScope.js index cb9c451b..343cf0e0 100644 --- a/src/core/PaperScope.js +++ b/src/core/PaperScope.js @@ -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 */ /** diff --git a/src/item/Item.js b/src/item/Item.js index 65e2ac24..bad56abd 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -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--; } } diff --git a/src/item/Project.js b/src/item/Project.js index 564b293f..bd115c00 100644 --- a/src/item/Project.js +++ b/src/item/Project.js @@ -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 }); }, /** diff --git a/test/tests/Item.js b/test/tests/Item.js index 71fd63d3..3f40c46b 100644 --- a/test/tests/Item.js +++ b/test/tests/Item.js @@ -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'); });