commit:28f3032d95d5dd81862b30891b143b37ec4e93b6
author:Chip Black
committer:Chip Black
date:Tue Oct 20 01:10:32 2015 -0500
parents:681674186038002a222a3ea5c101f6b06b180887
Implement undo
diff --git a/src/Fill.js b/src/Fill.js
line changes: +1/-0
index 8d16a35..5c91a81
--- a/src/Fill.js
+++ b/src/Fill.js
@@ -29,6 +29,7 @@ export default class Fill extends Controller {
     }
 
     down(x, y) {
+        this.state.pixelEditor.pushState();
         let targetColor = this.state.pixelEditor.getPixel(x, y);
         if (targetColor == this.state.palette.color) {
             // Filling a color on top of itself is a no-op

diff --git a/src/Line.js b/src/Line.js
line changes: +0/-2
index e9e1424..952d936
--- a/src/Line.js
+++ b/src/Line.js
@@ -19,8 +19,6 @@ export default class Line extends Controller {
         }
     }
     up(x, y) {
-        this.state.pixelEditor.popState();
-        this.state.pixelEditor.drawLine(this.begin.x, this.begin.y, x, y);
         this.begin = null;
     }
 }

diff --git a/src/Pen.js b/src/Pen.js
line changes: +1/-0
index 2191ca2..4704898
--- a/src/Pen.js
+++ b/src/Pen.js
@@ -7,6 +7,7 @@ export default class Pen extends Controller {
     }
 
     down(x, y) {
+        this.state.pixelEditor.pushState();
         this.previous = {x, y};
         this.state.pixelEditor.drawPixel(x, y);
     }

diff --git a/src/Rect.js b/src/Rect.js
line changes: +0/-12
index a050cbf..565d4b8
--- a/src/Rect.js
+++ b/src/Rect.js
@@ -27,18 +27,6 @@ export default class Rect extends Controller {
         }
     }
     up(x, y) {
-        const x0 = x > this.begin.x ? this.begin.x : x;
-        const y0 = y > this.begin.y ? this.begin.y : y;
-        const w = Math.abs(x - this.begin.x) + 1;
-        const h = Math.abs(y - this.begin.y) + 1;
-
-        this.state.pixelEditor.popState();
-        this.state.pixelEditor.setHLine(x0, this.begin.y, w);
-        this.state.pixelEditor.setHLine(x0, y, w);
-        this.state.pixelEditor.setVLine(this.begin.x, y0 + 1, h - 2);
-        this.state.pixelEditor.setVLine(x, y0 + 1, h - 2);
-        this.state.pixelEditor.redraw();
-        this.state.pixelEditor.refresh();
         this.begin = null;
     }
 }

diff --git a/src/Snow.js b/src/Snow.js
line changes: +1/-0
index f5e1595..9a26ee0
--- a/src/Snow.js
+++ b/src/Snow.js
@@ -11,6 +11,7 @@ export default class Snow extends Controller {
 
     down(x, y) {
         this.penIsDown = true;
+        this.state.pixelEditor.pushState();
         this.state.pixelEditor.drawPixel(x, y, this.randomColor());
     }
     move(x, y) {

diff --git a/src/index.html b/src/index.html
line changes: +1/-0
index 20e3b90..fdbf05c
--- a/src/index.html
+++ b/src/index.html
@@ -16,6 +16,7 @@
       <div class="global-actions">
         <button onclick="savePNG()">Save</button>
         <button onclick="loadFile()">Load</button>
+        <button onclick="undo()">Undo</button>
         <button onclick="toggleGrid()" id="button-grid" class="active">Grid</button>
         <div id="size-flyout" class="size-flyout">
           <button onclick="selectSize(8)">8x8</button>

diff --git a/src/main.js b/src/main.js
line changes: +6/-0
index 3e6c195..e0f0f53
--- a/src/main.js
+++ b/src/main.js
@@ -39,6 +39,12 @@ window.loadFile = function loadFile() {
     loader.click();
 }
 
+window.undo = function() {
+    appState.pixelEditor.popState();
+    appState.pixelEditor.redraw();
+    appState.pixelEditor.refresh();
+}
+
 window.nextPalette = function() {
     appState.palette.nextPalette();
     appState.pixelEditor.paletteChanged();