diff --git a/js-lib/common.js b/js-lib/common.js index d64d31b..6de8486 100644 --- a/js-lib/common.js +++ b/js-lib/common.js @@ -16,6 +16,16 @@ goog.provide('ascii'); /** @const */ var CHARACTER_PIXELS = 15; /** @const */ var RENDER_PADDING = 70; +/** @const */ var KEY_RETURN = ''; +/** @const */ var KEY_BACKSPACE = ''; +/** @const */ var KEY_COPY = ''; +/** @const */ var KEY_PASTE = ''; +/** @const */ var KEY_CUT = ''; +/** @const */ var KEY_UP = ''; +/** @const */ var KEY_DOWN = ''; +/** @const */ var KEY_LEFT = ''; +/** @const */ var KEY_RIGHT = ''; + /** * Stores a 2D vector. * diff --git a/js-lib/controller.js b/js-lib/controller.js index fb7abf3..b58f5c4 100644 --- a/js-lib/controller.js +++ b/js-lib/controller.js @@ -155,7 +155,11 @@ ascii.Controller.prototype.installBindings = function() { }.bind(this)); $(window).keypress(function(e) { - this.handleKeyPress(e.keyCode); + this.handleKeyPress(e); + }.bind(this)); + + $(window).keydown(function(e) { + this.handleKeyDown(e); }.bind(this)); }; @@ -193,15 +197,43 @@ ascii.Controller.prototype.updateButtons = function(id) { /** * Handles key presses. - * @param {number} keyCode + * @param {Object} event */ -ascii.Controller.prototype.handleKeyPress = function(keyCode) { - // TODO: Handle undo, redo, and ctrl+c and ctrl+v. - var stringValue = String.fromCharCode(keyCode); - // Override some special characters so that can be handled in one place. - if (keyCode == 13) { - stringValue = '\n'; +ascii.Controller.prototype.handleKeyPress = function(event) { + if (!event.ctrlKey && !event.metaKey) { + this.drawFunction.handleKey(String.fromCharCode(event.keyCode)); + this.view.dirty = true; } - this.drawFunction.handleKey(stringValue); - this.view.dirty = true; }; + +/** + * Handles key down events. + * @param {Object} event + */ +ascii.Controller.prototype.handleKeyDown = function(event) { + // Override some special characters so that they can be handled in one place. + var specialKeyCode = null; + + 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() } + // if (event.keyCode == 89) { this.state.redo(); } + if (event.keyCode == 88) { specialKeyCode = KEY_CUT; } + } + + if (event.keyCode == 8) { specialKeyCode = KEY_BACKSPACE; } + if (event.keyCode == 13) { specialKeyCode = KEY_RETURN; } + if (event.keyCode == 38) { specialKeyCode = KEY_UP; } + if (event.keyCode == 40) { specialKeyCode = KEY_DOWN; } + if (event.keyCode == 37) { specialKeyCode = KEY_LEFT; } + if (event.keyCode == 39) { specialKeyCode = KEY_RIGHT; } + + if (specialKeyCode != null) { + event.preventDefault(); + event.stopPropagation(); + this.drawFunction.handleKey(specialKeyCode); + this.view.dirty = true; + } +}; + diff --git a/js-lib/draw.js b/js-lib/draw.js index fe89b5d..d3b325d 100644 --- a/js-lib/draw.js +++ b/js-lib/draw.js @@ -166,22 +166,44 @@ ascii.DrawText.prototype.handleKey = function(value) { if (this.currentPosition == null) { return; } - - if (value == '\n') { - // Pressed return key, so clear this cell. + var nextPosition = this.currentPosition.add(new ascii.Vector(1, 0)); + + if (value == KEY_RETURN || this.state.getCell(nextPosition).isSpecial()) { + // Pressed return key or hit box, so clear this cell and new line. this.state.clearDraw(); - } else { - // Draw the value and commit it. + nextPosition = this.startPosition.add(new ascii.Vector(0, 1)); + this.startPosition = nextPosition; + } + if (value == KEY_BACKSPACE && this.startPosition.x <= nextPosition.x) { + // Pressed backspace key, so clear this cell and go back. + this.state.clearDraw(); + nextPosition = this.currentPosition.add(new ascii.Vector(-1, 0)); + this.state.drawValue(nextPosition, ' '); + this.state.commitDraw(); + } + if (value == KEY_UP) { + this.state.clearDraw(); + this.startPosition = nextPosition = this.currentPosition.add(new ascii.Vector(0, -1)) + } + if (value == KEY_LEFT) { + this.state.clearDraw(); + this.startPosition = nextPosition = this.currentPosition.add(new ascii.Vector(-1, 0)) + } + if (value == KEY_RIGHT) { + this.state.clearDraw(); + this.startPosition = nextPosition = this.currentPosition.add(new ascii.Vector(1, 0)) + } + if (value == KEY_DOWN) { + this.state.clearDraw(); + this.startPosition = nextPosition = this.currentPosition.add(new ascii.Vector(0, 1)) + } + + if (value.length == 1) { + // The value is not a special character, so draw the value and commit it. this.state.drawValue(this.currentPosition, value); this.state.commitDraw(); } - var nextPosition = this.currentPosition.add(new ascii.Vector(1, 0)); - // Check for hitting edge of box or return character, then line wrap. - if (value == '\n' || this.state.getCell(nextPosition).isSpecial()) { - nextPosition = this.startPosition.add(new ascii.Vector(0, 1)); - this.startPosition = nextPosition; - } // Highlight the next cell. this.currentPosition = nextPosition; var nextValue = this.state.getCell(nextPosition).getRawValue();