From 43af2bebd1de70f0e2f5627815812dbfd01a5c9a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 5 Aug 2014 15:30:07 -0400 Subject: cutting out stuff for mobile --- public/assets/javascripts/rectangles/models/rect.js | 4 ++++ public/assets/javascripts/rectangles/models/vec2.js | 3 +++ 2 files changed, 7 insertions(+) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 590440a..58469bc 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -107,6 +107,10 @@ Rect.prototype.eq = function(r){ return this.x.eq(r.x) && this.y.eq(r.y) } + Rect.prototype.zero = function(){ + this.a.zero() + this.b.zero() + } Rect.prototype.nearEdge = function (x, y, r) { var edges = 0 if (x < this.x.a+r) { diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 2bf286b..104c6f7 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -55,6 +55,9 @@ this.b /= n return this } + vec2.prototype.zero = function(){ + this.a = this.b = 0 + } vec2.prototype.setPosition = function(n){ var len = this.length() this.a = n -- cgit v1.2.3-70-g09d2 From 9cd88d59e45b530e483490804503e6b47030fd4d Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 8 Aug 2014 18:56:41 -0400 Subject: undo stack --- public/assets/javascripts/rectangles/util/undo.js | 42 ++++++ test/00-setup.js | 1 - test/09-test-undo.js | 163 ++++++++++++++++++++++ 3 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 public/assets/javascripts/rectangles/util/undo.js create mode 100644 test/09-test-undo.js (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/util/undo.js b/public/assets/javascripts/rectangles/util/undo.js new file mode 100644 index 0000000..3700817 --- /dev/null +++ b/public/assets/javascripts/rectangles/util/undo.js @@ -0,0 +1,42 @@ +(function(){ + + var UndoStack = function(){ + this.stack = [] + this.types = {} + this.pointer = -1 + } + UndoStack.prototype.push = function(action){ + this.pointer++ + this.stack[this.pointer] = action + this.purge() + } + UndoStack.prototype.purge = function(){ + if (this.stack.length-1 == this.pointer) return + this.stack.length = this.pointer+1 + } + UndoStack.prototype.undo = function(){ + if (this.pointer == -1) return false + var action = this.stack[this.pointer] + this.types[ action.type ].undo(action) + this.pointer-- + return this.pointer > -1 + } + UndoStack.prototype.redo = function(){ + if (this.pointer == this.stack.length-1) return false + this.pointer++ + var action = this.stack[this.pointer] + this.types[ action.type ].redo(action) + return this.pointer < this.stack.length-1 + } + UndoStack.prototype.register = function(actionType){ + this.types[ actionType.type ] = actionType + } + + if ('window' in this) { + window.UndoStack = new UndoStack + } + else { + module.exports = new UndoStack + } + +})() diff --git a/test/00-setup.js b/test/00-setup.js index 78ad2c4..20f9d66 100644 --- a/test/00-setup.js +++ b/test/00-setup.js @@ -1,2 +1 @@ Error.stackTraceLimit = 5 - diff --git a/test/09-test-undo.js b/test/09-test-undo.js new file mode 100644 index 0000000..84b5d09 --- /dev/null +++ b/test/09-test-undo.js @@ -0,0 +1,163 @@ +var assert = require("assert") +var UndoStack = require("../public/assets/javascripts/rectangles/util/undo.js") + +describe('undo', function(){ + + var state = "zero" + + describe('#register()', function(){ + + UndoStack.register({ + type: "demo", + undo: function(action){ + state = action.prev + }, + redo: function(action){ + state = action.next + }, + }) + + it('registers undoable actions', function(){ + assert( UndoStack.types.hasOwnProperty("demo") ) + }) + }) + + describe('#push()', function(){ + + it('starts empty', function(){ + assert.equal(0, UndoStack.stack.length) + assert.equal(-1, UndoStack.pointer) + }) + + it('pushes some actions', function(){ + + UndoStack.push({ + type: "demo", + prev: state, + next: "one" + }) + state = "one" + + UndoStack.push({ + type: "demo", + prev: state, + next: "two" + }) + state = "two" + + UndoStack.push({ + type: "demo", + prev: state, + next: "three" + }) + state = "three" + + assert.equal(3, UndoStack.stack.length) + assert.equal(2, UndoStack.pointer) + }) + }) + + describe('#undo()', function(){ + + it('retrieves old state', function(){ + assert.equal("three", state) + UndoStack.undo() + assert.equal("two", state) + assert.equal(1, UndoStack.pointer) + }) + + it('can only undo so far', function(){ + var canUndo + + canUndo = UndoStack.undo() + assert.equal("one", state) + assert.equal(0, UndoStack.pointer) + assert.equal(true, canUndo) + + canUndo = UndoStack.undo() + assert.equal("zero", state) + assert.equal(-1, UndoStack.pointer) + assert.equal(false, canUndo) + + canUndo = UndoStack.undo() + assert.equal("zero", state) + assert.equal(-1, UndoStack.pointer) + assert.equal(false, canUndo) + }) + + }) + + describe('#redo()', function(){ + + it('reassigns new state', function(){ + UndoStack.redo() + assert.equal("one", state) + }) + + it('can only redo so far', function(){ + var canRedo + + canRedo = UndoStack.redo() + assert.equal("two", state) + assert.equal(true, canRedo) + + canRedo = UndoStack.redo() + assert.equal("three", state) + assert.equal(false, canRedo) + + canRedo = UndoStack.redo() + assert.equal("three", state) + assert.equal(false, canRedo) + }) + + it('clobbers old state if we undo then do something else', function(){ + UndoStack.undo() + assert.equal("two", state) + assert.equal(1, UndoStack.pointer) + + UndoStack.undo() + assert.equal("one", state) + assert.equal(0, UndoStack.pointer) + assert.equal(3, UndoStack.stack.length) + + UndoStack.push({ + type: "demo", + prev: state, + next: "four" + }) + state = "four" + + assert.equal(1, UndoStack.pointer) + assert.equal(2, UndoStack.stack.length) + + UndoStack.undo() + assert.equal("one", state) + assert.equal(0, UndoStack.pointer) + assert.equal(2, UndoStack.stack.length) + + UndoStack.redo() + assert.equal("four", state) + assert.equal(1, UndoStack.pointer) + assert.equal(2, UndoStack.stack.length) + }) + }) + +}) + +// 1) push action +// 2) push action +// 3) push action +// undo 3 +// undo 2 +// undo 1 +// undo * can't undo anymore +// redo 1 +// redo 2 +// redo 3 +// redo * can't redo anymore +// undo 3 +// 4) push action (clobbers action 3) +// undo 4 +// undo 2 +// redo 2 +// redo 4 -- cgit v1.2.3-70-g09d2