Skip to content

Commit

Permalink
Feature #683: duplicate layer when SHIFT+CLICK on create layer button
Browse files Browse the repository at this point in the history
  • Loading branch information
juliandescottes committed May 11, 2017
1 parent f2b733c commit c2ad4b4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/js/controller/LayersListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
this.rootEl.addEventListener('click', this.onClick_.bind(this));
this.toggleLayerPreviewEl.addEventListener('click', this.toggleLayerPreview_.bind(this));

this.initCreateLayerButton_();
this.initToggleLayerPreview_();

this.renderLayerList_();
Expand Down Expand Up @@ -45,6 +46,14 @@
}
};

ns.LayersListController.prototype.initCreateLayerButton_ = function () {
var tooltip = pskl.utils.TooltipFormatter.format('Create a layer', null, [
{key : 'shift', description : 'Clone current layer'}
]);
var addButton = this.rootEl.querySelector('[data-action="add"]');
addButton.setAttribute('title', tooltip);
};

ns.LayersListController.prototype.initToggleLayerPreview_ = function () {
var descriptors = [{description : 'Opacity defined in PREFERENCES'}];
var helpText = 'Preview all layers';
Expand Down Expand Up @@ -117,7 +126,7 @@
var el = evt.target || evt.srcElement;
var index;
if (el.classList.contains('button')) {
this.onButtonClick_(el);
this.onButtonClick_(el, evt);
} else if (el.classList.contains('layer-name')) {
index = pskl.utils.Dom.getData(el, 'layerIndex');
this.piskelController.setCurrentLayerIndex(parseInt(index, 10));
Expand Down Expand Up @@ -145,14 +154,18 @@
this.renderLayerList_();
};

ns.LayersListController.prototype.onButtonClick_ = function (button) {
ns.LayersListController.prototype.onButtonClick_ = function (button, evt) {
var action = button.getAttribute('data-action');
if (action == 'up') {
this.piskelController.moveLayerUp();
} else if (action == 'down') {
this.piskelController.moveLayerDown();
} else if (action == 'add') {
this.piskelController.createLayer();
if (evt.shiftKey) {
this.piskelController.duplicateCurrentLayer();
} else {
this.piskelController.createLayer();
}
} else if (action == 'delete') {
this.piskelController.removeCurrentLayer();
} else if (action == 'merge') {
Expand Down
7 changes: 7 additions & 0 deletions src/js/controller/piskel/PiskelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@
return name;
};

ns.PiskelController.prototype.duplicateCurrentLayer = function () {
var layer = this.getCurrentLayer();
var clone = pskl.utils.LayerUtils.clone(layer);
this.piskel.addLayer(clone);
this.setCurrentLayerIndex(this.piskel.getLayers().length - 1);
},

ns.PiskelController.prototype.createLayer = function (name) {
if (!name) {
name = this.generateLayerName_();
Expand Down
1 change: 1 addition & 0 deletions src/js/controller/piskel/PublicPiskelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
this.saveWrap_('duplicateFrameAt', true);
this.saveWrap_('moveFrame', true);
this.saveWrap_('createLayer', true);
this.saveWrap_('duplicateCurrentLayer', true);
this.saveWrap_('mergeDownLayerAt', true);
this.saveWrap_('moveLayerUp', true);
this.saveWrap_('moveLayerDown', true);
Expand Down
7 changes: 7 additions & 0 deletions src/js/utils/LayerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
var ns = $.namespace('pskl.utils');

ns.LayerUtils = {
clone : function (layer) {
var clonedFrames = layer.getFrames().map(function (frame) {
return frame.clone();
});
return pskl.model.Layer.fromFrames(layer.getName() + " (clone)", clonedFrames);
},

mergeLayers : function (layerA, layerB) {
var framesA = layerA.getFrames();
var framesB = layerB.getFrames();
Expand Down
2 changes: 1 addition & 1 deletion src/templates/layers-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h3 class="toolbox-title layers-title">Layers
<div class="toolbox-buttons">
<button data-action="add"
class="button layers-button piskel-icon-plus"
title="Create a new layer" rel="tooltip" data-placement="top" ></button>
rel="tooltip" data-placement="top" ></button>

<button data-action="up"
class="button layers-button piskel-icon-arrow-up-fat"
Expand Down
26 changes: 26 additions & 0 deletions test/js/utils/LayerUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,30 @@ describe("LayerUtils test", function() {
['rgba(255,0,0,0.5)', B]
]);
});

it("clones a layer", function() {
var grid1 = [
[B, T],
[T, B]
];

var grid2 = [
[R, B],
[B, R]
];

// when
var layer = new pskl.model.Layer('l1');
layer.addFrame(pskl.model.Frame.fromPixelGrid(grid1));
layer.addFrame(pskl.model.Frame.fromPixelGrid(grid2));

// then
var clone = pskl.utils.LayerUtils.clone(layer);
var clonedFrame = clone.getFrameAt(0);

// verify
frameEqualsGrid(clone.getFrameAt(0), grid1);
frameEqualsGrid(clone.getFrameAt(1), grid2);
expect(clone.getName()).toBe('l1 (clone)');
});
});

0 comments on commit c2ad4b4

Please sign in to comment.