diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-12-09 16:15:37 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-12-09 16:15:37 -0500 |
| commit | da3b3feedfe3a928dec6ac615579538a9ab5b582 (patch) | |
| tree | 2b176b394b301e2d633372d70f6bc8743e317feb /js | |
| parent | 6a16ad9c408fb84dd27c618312f3111563ca2ad5 (diff) | |
less-destructive resize; stub out selection
Diffstat (limited to 'js')
| -rw-r--r-- | js/lex.js | 2 | ||||
| -rw-r--r-- | js/matrix.js | 65 | ||||
| -rw-r--r-- | js/ui/brush.js | 26 | ||||
| -rw-r--r-- | js/ui/canvas.js | 3 | ||||
| -rw-r--r-- | js/ui/controls.js | 14 | ||||
| -rw-r--r-- | js/ui/keys.js | 7 | ||||
| -rw-r--r-- | js/ui/selection.js | 21 |
7 files changed, 102 insertions, 36 deletions
@@ -93,7 +93,7 @@ Lex.prototype.blur = function(){ focused = null } Lex.prototype.demolish = function(){ - this.span.parentNode.removeChild(this.span) + if (this.span.parentNode) { this.span.parentNode.removeChild(this.span) } this.span = null } Lex.prototype.key = function(char, keyCode) { diff --git a/js/matrix.js b/js/matrix.js index fd4ee4f..79f3d9f 100644 --- a/js/matrix.js +++ b/js/matrix.js @@ -98,6 +98,59 @@ Matrix.prototype.getCell = function(x,y){ if (this.aa[y] && this.aa[y][x]) return this.aa[y][x] else return null } +Matrix.prototype.resize = function(w,h){ + var div, row, lex + var f = this.f, old_h = this.h, old_w = this.w + var rapper = this.rapper + w = max(w, 1) + h = max(h, 1) + if (h < old_h) { + for (var y = old_h; y > h; y--) { + row = this.aa.pop() + div = row[0].span.parentNode + row.forEach(function(lex, x){ + lex.demolish() + }) + div.parentNode.removeChild(div) + } + } + else if (h > old_h) { + for (var y = old_h; y < h; y++) { + div = document.createElement("div") + rapper.appendChild( div ) + this.aa[y] = new Array (w) + for (var x = 0; x < w; x++) { + lex = this.aa[y][x] = f(x,y) + div.appendChild(lex.span) + } + } + } + + if (w < old_w) { + this.aa.forEach(function(row, y){ + while (row.length > w) { + lex = row.pop() + lex.demolish() + } + }) + } + else if (w > old_w) { + this.aa.forEach(function(row, y){ + div = row[0].span.parentNode + for (var x = row.length; x < w; x++) { + lex = row[x] = f(x,y) + div.appendChild(lex.span) + } + }) + } + + this.w = w + this.h = h + this.bind && this.bind() +} + +// + Matrix.prototype.ascii = function () { var lines = this.aa.map(function(row, y){ var last, line = "" @@ -160,15 +213,3 @@ Matrix.prototype.irssi = function(){ } return '/exec -out printf "' + escaped_txt + '"\n' } -Matrix.prototype.expand = function(i){ - var w = this.w = clamp(this.w+i, 1, 9), h = this.h = clamp(this.h+i, 1, 9) - console.log(w,h) - controls.width.char = ""+w - controls.width.build() - controls.height.char = ""+h - controls.height.build() - this.rebuild() -} -Matrix.prototype.contract = function(i){ - this.expand(-i) -} diff --git a/js/ui/brush.js b/js/ui/brush.js index a055a97..c7cf583 100644 --- a/js/ui/brush.js +++ b/js/ui/brush.js @@ -7,20 +7,6 @@ var brush = (function(){ }) brush.modified = false - - brush.bind = function(){ - brush.forEach(function(lex, x, y){ - if (lex.bound) return - lex.bound = true - - lex.span.addEventListener('mousedown', function(e){ - e.preventDefault() - dragging = true - // lex.fill(lex.fg, lex.bg) - }) - - }) - } brush.bind = function(){ @@ -70,6 +56,18 @@ var brush = (function(){ }) } + brush.expand = function(i){ + var w = this.w = clamp(this.w+i, 1, 9), h = this.h = clamp(this.h+i, 1, 9) + controls.width.char = ""+w + controls.width.build() + controls.height.char = ""+h + controls.height.build() + this.rebuild() + } + brush.contract = function(i){ + this.expand(-i) + } + brush.fg = 0 brush.bg = 1 diff --git a/js/ui/canvas.js b/js/ui/canvas.js index d354c62..e36f1b5 100644 --- a/js/ui/canvas.js +++ b/js/ui/canvas.js @@ -2,6 +2,7 @@ var canvas = current_canvas = (function(){ var cols = 80 var rows = 24 + var last_point = [0,0] var exports = new Matrix (cols, rows, function(x,y){ var lex = new Lex (x,y) @@ -11,8 +12,6 @@ var canvas = current_canvas = (function(){ exports.bind = function(){ - var last_point = [0,0] - exports.forEach(function(lex, x, y){ if (lex.bound) return diff --git a/js/ui/controls.js b/js/ui/controls.js index d575725..aadd3c9 100644 --- a/js/ui/controls.js +++ b/js/ui/controls.js @@ -154,9 +154,10 @@ var controls = (function(){ } controls.canvas_width.char = ""+n controls.canvas_width.build() - canvas.w = n - canvas.rebuild() - canvas.build() + canvas.resize(n, canvas.h) + // canvas.w = n + // canvas.rebuild() + // canvas.build() }) controls.canvas_height.key = int_key(function(n, keyCode){ controls.canvas_height.read() @@ -165,9 +166,10 @@ var controls = (function(){ } controls.canvas_height.char = ""+n controls.canvas_height.build() - canvas.h = n - canvas.rebuild() - canvas.build() + canvas.resize(canvas.w, n) + // canvas.h = n + // canvas.rebuild() + // canvas.build() }) } diff --git a/js/ui/keys.js b/js/ui/keys.js index 7711e9d..d1badf6 100644 --- a/js/ui/keys.js +++ b/js/ui/keys.js @@ -74,7 +74,12 @@ var keys = (function(){ if (focused && char) { var y = focused.y, x = focused.x focused.key(char, e.keyCode) - current_canvas.focusLex(y + direction[0], focused.x + direction[1]) + if (! ('y' in focused && 'x' in focused) ) { + return + } + console.log(focused) + console.log(y, direction[0], x, direction[1]) + current_canvas.focusLex(y + direction[0], x + direction[1]) } }) } diff --git a/js/ui/selection.js b/js/ui/selection.js new file mode 100644 index 0000000..cbeb051 --- /dev/null +++ b/js/ui/selection.js @@ -0,0 +1,21 @@ +var selection = (function(){ + + var selection = new Matrix (1, 1, function(x,y){ + var lex = new Lex (x,y) + lex.build() + return lex + }) + + // in selection mode.. + // - we start by clicking the canvas. this positions the selection, and copies + // the character + // - then we drag down and to the right. this resizes the selection and pushes new + // rows and columns. each of these copies the character underneath. + // - on mouseup, the selection is locked. then.. + // - drag the selection to move it -- this "cuts" it and leaves a blank space on the canvas. + // - shift-drag the selection to copy it + + + return selection + +})() |
