diff options
Diffstat (limited to 'public/assets/javascripts/rectangles')
| -rw-r--r-- | public/assets/javascripts/rectangles/models/rect.js | 4 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/models/vec2.js | 3 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/util/undo.js | 42 |
3 files changed, 49 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 500ee6d..ec32ab7 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -110,6 +110,10 @@ Rect.prototype.fits = function(v){ return this.x.length() >= v.a && this.y.length() >= v.b } + 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 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 + } + +})() |
