From a3a75b6096ab449e39bf177c635f22104f5b896d Mon Sep 17 00:00:00 2001 From: jdescottes Date: Tue, 6 Oct 2015 01:22:43 +0200 Subject: [PATCH] Issue #315 : custom replay for Swap Color --- src/js/tools/Tool.js | 8 ++++ src/js/tools/drawing/BaseTool.js | 8 ---- src/js/tools/drawing/ColorSwap.js | 46 +++++++++++-------- src/js/tools/drawing/Lighten.js | 2 +- .../tools/transform/AbstractTransformTool.js | 11 +---- test/drawing/DrawingTests.browser.js | 4 +- test/drawing/DrawingTests.casper.js | 5 +- ...r.alllayers.allframes.twice.undo.once.json | 1 + .../tests/swapcolor.twice.undo.once.json | 1 + 9 files changed, 46 insertions(+), 40 deletions(-) create mode 100644 test/drawing/tests/swapcolor.alllayers.allframes.twice.undo.once.json create mode 100644 test/drawing/tests/swapcolor.twice.undo.once.json diff --git a/src/js/tools/Tool.js b/src/js/tools/Tool.js index afd4df91..fb9c6f3e 100644 --- a/src/js/tools/Tool.js +++ b/src/js/tools/Tool.js @@ -14,4 +14,12 @@ ns.Tool.prototype.getId = function() { return this.toolId; }; + + ns.Tool.prototype.raiseSaveStateEvent = function (replayData) { + $.publish(Events.PISKEL_SAVE_STATE, { + type : pskl.service.HistoryService.REPLAY, + scope : this, + replay : replayData + }); + }; })(); diff --git a/src/js/tools/drawing/BaseTool.js b/src/js/tools/drawing/BaseTool.js index e89c75b5..2c335457 100644 --- a/src/js/tools/drawing/BaseTool.js +++ b/src/js/tools/drawing/BaseTool.js @@ -76,14 +76,6 @@ } }; - ns.BaseTool.prototype.raiseSaveStateEvent = function (replayData) { - $.publish(Events.PISKEL_SAVE_STATE, { - type : pskl.service.HistoryService.REPLAY, - scope : this, - replay : replayData - }); - }; - ns.BaseTool.prototype.releaseToolAt = function (col, row, frame, overlay, event) {}; /** diff --git a/src/js/tools/drawing/ColorSwap.js b/src/js/tools/drawing/ColorSwap.js index fc8cd3b3..cdf7b5e9 100644 --- a/src/js/tools/drawing/ColorSwap.js +++ b/src/js/tools/drawing/ColorSwap.js @@ -23,36 +23,44 @@ */ ns.ColorSwap.prototype.applyToolAt = function(col, row, frame, overlay, event) { if (frame.containsPixel(col, row)) { - var sampledColor = frame.getPixel(col, row); + var oldColor = frame.getPixel(col, row); + var newColor = this.getToolColor(); var allLayers = pskl.utils.UserAgent.isMac ? event.metaKey : event.ctrlKey; var allFrames = event.shiftKey; - this.swapColors(sampledColor, this.getToolColor(), allLayers, allFrames); + this.swapColors_(oldColor, newColor, allLayers, allFrames); - $.publish(Events.PISKEL_SAVE_STATE, { - type : pskl.service.HistoryService.SNAPSHOT + this.raiseSaveStateEvent({ + allLayers : allLayers, + allFrames : allFrames, + oldColor : oldColor, + newColor : newColor }); } }; - ns.ColorSwap.prototype.swapColors = function(oldColor, newColor, allLayers, allFrames) { - var swapPixelColor = function (pixelColor, x, y, frame) { - if (pixelColor == oldColor) { - frame.pixels[x][y] = newColor; - } - }; - var currentLayer = pskl.app.piskelController.getCurrentLayer(); + ns.ColorSwap.prototype.replay = function (frame, replayData) { + this.swapColors_(replayData.oldColor, replayData.newColor, replayData.allLayers, replayData.allFrames); + }; + + ns.ColorSwap.prototype.swapColors_ = function(oldColor, newColor, allLayers, allFrames) { var currentFrameIndex = pskl.app.piskelController.getCurrentFrameIndex(); - pskl.app.piskelController.getPiskel().getLayers().forEach(function (l) { - if (allLayers || l === currentLayer) { - l.getFrames().forEach(function (f, frameIndex) { - if (allFrames || frameIndex === currentFrameIndex) { - f.forEachPixel(swapPixelColor); - f.version++; - } - }); + var layers = allLayers ? pskl.app.piskelController.getLayers() : [pskl.app.piskelController.getCurrentLayer()]; + layers.forEach(function (layer) { + var frames = allFrames ? layer.getFrames() : [layer.getFrameAt(currentFrameIndex)]; + frames.forEach(function (frame) { + this.applyToolOnFrame_(frame, oldColor, newColor); + }.bind(this)); + }.bind(this)); + }; + + ns.ColorSwap.prototype.applyToolOnFrame_ = function (frame, oldColor, newColor) { + frame.forEachPixel(function (color, col, row) { + if (color == oldColor) { + frame.pixels[col][row] = newColor; } }); + frame.version++; }; })(); diff --git a/src/js/tools/drawing/Lighten.js b/src/js/tools/drawing/Lighten.js index 3e90dc80..0c3b9a53 100644 --- a/src/js/tools/drawing/Lighten.js +++ b/src/js/tools/drawing/Lighten.js @@ -41,7 +41,7 @@ /** * @Override */ - ns.Lighten.prototype.applyToolAt = function(col, row, frame, overlay, event, mouseButton) { + ns.Lighten.prototype.applyToolAt = function(col, row, frame, overlay, event) { var modifiedColor = this.getModifiedColor_(col, row, frame, overlay, event); this.draw(modifiedColor, col, row, frame, overlay); }; diff --git a/src/js/tools/transform/AbstractTransformTool.js b/src/js/tools/transform/AbstractTransformTool.js index edb4d7a5..98c06be0 100644 --- a/src/js/tools/transform/AbstractTransformTool.js +++ b/src/js/tools/transform/AbstractTransformTool.js @@ -12,7 +12,8 @@ this.applyTool_(evt.altKey, allFrames, allLayers); $.publish(Events.PISKEL_RESET); - this.raiseSaveStateEvent_({ + + this.raiseSaveStateEvent({ altKey : evt.altKey, allFrames : allFrames, allLayers : allLayers @@ -30,14 +31,6 @@ }.bind(this)); }; - ns.AbstractTransformTool.prototype.raiseSaveStateEvent_ = function (replayData) { - $.publish(Events.PISKEL_SAVE_STATE, { - type : pskl.service.HistoryService.REPLAY, - scope : this, - replay : replayData - }); - }; - ns.AbstractTransformTool.prototype.replay = function (frame, replayData) { this.applyTool_(replayData.altKey, replayData.allFrames, replayData.allLayers); }; diff --git a/test/drawing/DrawingTests.browser.js b/test/drawing/DrawingTests.browser.js index 276ff1c3..a0a4e2dd 100644 --- a/test/drawing/DrawingTests.browser.js +++ b/test/drawing/DrawingTests.browser.js @@ -23,5 +23,7 @@ "transform.flip.once.alt.json", "transform.flip.twice.undo.once.json", "transform.flip.thrice.undo.all.redo.all.json", - "selection.lasso.json" + "selection.lasso.json", + "swapcolor.twice.undo.once.json", + "swapcolor.alllayers.allframes.twice.undo.once.json" ]} \ No newline at end of file diff --git a/test/drawing/DrawingTests.casper.js b/test/drawing/DrawingTests.casper.js index 22002fdf..1e5af607 100644 --- a/test/drawing/DrawingTests.casper.js +++ b/test/drawing/DrawingTests.casper.js @@ -5,7 +5,6 @@ "history.basic.json", "layers.fun.json", "layers.merge.json", - "lighten.darken.json", "move.json", "move-alllayers-allframes.json", "pen.secondary.color.json", @@ -22,5 +21,7 @@ "transform.flip.once.alt.json", "transform.flip.twice.undo.once.json", "transform.flip.thrice.undo.all.redo.all.json", - "selection.lasso.json" + "selection.lasso.json", + "swapcolor.twice.undo.once.json", + "swapcolor.alllayers.allframes.twice.undo.once.json" ]; \ No newline at end of file diff --git a/test/drawing/tests/swapcolor.alllayers.allframes.twice.undo.once.json b/test/drawing/tests/swapcolor.alllayers.allframes.twice.undo.once.json new file mode 100644 index 00000000..4fa20132 --- /dev/null +++ b/test/drawing/tests/swapcolor.alllayers.allframes.twice.undo.once.json @@ -0,0 +1 @@ +{"events":[{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":1},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":1},"type":"mouse-event"},{"type":"instrumented-event","methodName":"addFrame","args":[]},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":1},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":1},"type":"mouse-event"},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":0},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":0},"type":"mouse-event"},{"type":"instrumented-event","methodName":"createLayer","args":[]},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":0},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":0},"type":"mouse-event"},{"type":"keyboard-event","event":{"which":38,"shiftKey":false,"altKey":false,"ctrlKey":false,"target":{"nodeName":"BODY"}}},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":1},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":1},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":1},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":1},"type":"mouse-event"},{"type":"color-event","color":"#ff0000","isPrimary":true},{"type":"tool-event","toolId":"tool-colorswap"},{"event":{"type":"mousedown","button":0,"shiftKey":true,"altKey":false,"ctrlKey":true},"coords":{"x":0,"y":1},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":true,"altKey":false,"ctrlKey":true},"coords":{"x":0,"y":1},"type":"mouse-event"},{"type":"color-event","color":"#1aff00","isPrimary":true},{"event":{"type":"mousedown","button":0,"shiftKey":true,"altKey":false,"ctrlKey":true},"coords":{"x":1,"y":1},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":true,"altKey":false,"ctrlKey":true},"coords":{"x":1,"y":1},"type":"mouse-event"},{"type":"keyboard-event","event":{"which":90,"shiftKey":false,"altKey":false,"ctrlKey":true,"target":{"nodeName":"BODY"}}}],"initialState":{"size":{"width":2,"height":2},"primaryColor":"#000000","secondaryColor":"rgba(0, 0, 0, 0)","selectedTool":"tool-pen"},"png":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAYAAAB/qH1jAAAAGklEQVQIW2P8z8Dwn5GBgRFEMzAgMcAcIAIAiwYGAbu53sQAAAAASUVORK5CYII="} \ No newline at end of file diff --git a/test/drawing/tests/swapcolor.twice.undo.once.json b/test/drawing/tests/swapcolor.twice.undo.once.json new file mode 100644 index 00000000..008a43b7 --- /dev/null +++ b/test/drawing/tests/swapcolor.twice.undo.once.json @@ -0,0 +1 @@ +{"events":[{"type":"color-event","color":"#000000","isPrimary":true},{"type":"tool-event","toolId":"tool-pen"},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":1},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":1},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":1,"y":1},"type":"mouse-event"},{"type":"color-event","color":"rgb(255, 0, 0)","isPrimary":true},{"type":"tool-event","toolId":"tool-colorswap"},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":0},"type":"mouse-event"},{"type":"color-event","color":"rgb(0, 255, 255)","isPrimary":true},{"event":{"type":"mousedown","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":1},"type":"mouse-event"},{"event":{"type":"mousemove","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":1},"type":"mouse-event"},{"event":{"type":"mouseup","button":0,"shiftKey":false,"altKey":false,"ctrlKey":false},"coords":{"x":0,"y":1},"type":"mouse-event"},{"type":"keyboard-event","event":{"which":90,"shiftKey":false,"altKey":false,"ctrlKey":true,"target":{"nodeName":"BODY"}}}],"initialState":{"size":{"width":2,"height":2},"primaryColor":"#00ffff","secondaryColor":"rgba(0, 0, 0, 0)","selectedTool":"tool-colorswap"},"png":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAGElEQVQIW2P8z8Dwn5GBgZGRgYGBAcQBACQgBAEgNsbbAAAAAElFTkSuQmCC"} \ No newline at end of file