From 2b8368d63b4a5977867f1adc3a31ee78e59aae2d Mon Sep 17 00:00:00 2001 From: Lewis Hemens Date: Sun, 23 Feb 2014 17:13:22 +0000 Subject: [PATCH] Moved dirty bit into the state --- js-lib/controller.js | 10 +--------- js-lib/state.js | 15 ++++----------- js-lib/view.js | 3 ++- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/js-lib/controller.js b/js-lib/controller.js index 6326867..f858457 100644 --- a/js-lib/controller.js +++ b/js-lib/controller.js @@ -31,7 +31,6 @@ ascii.Controller.prototype.handlePress = function(position) { window.setTimeout(function() { if (this.dragOrigin == null && this.pressVector != null) { this.drawFunction.start(this.view.screenToCell(position)); - this.view.dirty = true; } // TODO: Skip this if release happens before timeout. }.bind(this), DRAG_LATENCY); @@ -62,7 +61,6 @@ ascii.Controller.prototype.handleMove = function(position) { !this.view.screenToCell(position) .equals(this.view.screenToCell(this.lastMoveCell)))) { this.drawFunction.move(this.view.screenToCell(position)); - this.view.dirty = true; this.lastMoveCell = position; } @@ -83,7 +81,6 @@ ascii.Controller.prototype.handleRelease = function(position) { if (this.dragOrigin == null && ($.now() - this.pressTimestamp) >= DRAG_LATENCY) { this.drawFunction.end(this.view.screenToCell(position)); - this.view.dirty = true; } this.pressVector = null; this.pressTimestamp = 0; @@ -156,7 +153,6 @@ ascii.Controller.prototype.installBindings = function() { $('#undo-button').click(function(e) { this.state.undo(); - this.view.dirty = true; }.bind(this)); $('#import-submit-button').click(function(e) { @@ -166,7 +162,6 @@ ascii.Controller.prototype.installBindings = function() { this.view.canvas.width / 4, this.view.canvas.height / 4))); $('#import-area').val(''); - this.view.dirty = true; }.bind(this)); $(window).keypress(function(e) { @@ -213,7 +208,6 @@ ascii.Controller.prototype.updateButtons = function(id) { } if (id == 'clear-button') { this.state.clear(); - this.view.dirty = true; } }; @@ -224,7 +218,6 @@ ascii.Controller.prototype.updateButtons = function(id) { ascii.Controller.prototype.handleKeyPress = function(event) { if (!event.ctrlKey && !event.metaKey && event.keyCode != 13) { this.drawFunction.handleKey(String.fromCharCode(event.keyCode)); - this.view.dirty = true; } }; @@ -239,7 +232,7 @@ ascii.Controller.prototype.handleKeyDown = function(event) { if (event.ctrlKey || event.metaKey) { if (event.keyCode == 67) { specialKeyCode = KEY_COPY; } if (event.keyCode == 86) { specialKeyCode = KEY_PASTE; } - if (event.keyCode == 90) { this.state.undo(); this.view.dirty = true; } + if (event.keyCode == 90) { this.state.undo(); } // if (event.keyCode == 89) { this.state.redo(); } if (event.keyCode == 88) { specialKeyCode = KEY_CUT; } } @@ -255,7 +248,6 @@ ascii.Controller.prototype.handleKeyDown = function(event) { //event.preventDefault(); //event.stopPropagation(); this.drawFunction.handleKey(specialKeyCode); - this.view.dirty = true; } }; diff --git a/js-lib/state.js b/js-lib/state.js index 4829d44..97436b3 100644 --- a/js-lib/state.js +++ b/js-lib/state.js @@ -9,6 +9,8 @@ ascii.State = function() { this.cells = new Array(MAX_GRID_WIDTH); /** @type {Array.} */ this.scratchCells = new Array(); + /** @type {boolean} */ + this.dirty = true; /** @type {Array.>} */ this.undoStates = new Array(); @@ -46,17 +48,6 @@ ascii.State.prototype.getCell = function(vector) { return this.cells[vector.x][vector.y]; }; -/** - * Sets the cells value at the given position. Probably shouldn't - * be used directly in many cases. Used drawValue instead. - * - * @param {ascii.Vector} position - * @param {?string} value - */ -ascii.State.prototype.setValue = function(position, value) { - this.getCell(position).value = value; -}; - /** * Sets the cells scratch (uncommitted) value at the given position. * @@ -67,6 +58,7 @@ ascii.State.prototype.drawValue = function(position, value) { var cell = this.getCell(position); this.scratchCells.push(new ascii.MappedCell(position, cell)); cell.scratchValue = value; + this.dirty = true; }; /** @@ -172,6 +164,7 @@ ascii.State.prototype.commitDraw = function(opt_skipSave) { } this.undoStates.push(oldValues); } + this.dirty = true; }; /** diff --git a/js-lib/view.js b/js-lib/view.js index 628d882..b8e30dd 100644 --- a/js-lib/view.js +++ b/js-lib/view.js @@ -30,8 +30,9 @@ ascii.View.prototype.resizeCanvas = function() { * Starts the animation loop for the canvas. Should only be called once. */ ascii.View.prototype.animate = function() { - if (this.dirty) { + if (this.dirty || this.state.dirty) { this.dirty = false; + this.state.dirty = false; this.render(); } var view = this;