function Lex (x,y) { if (typeof x == "number") { this.x = x this.y = y this.span = document.createElement("span") } else { this.span = x } this.fg = colors.white this.bg = colors.black this.char = " " } Lex.prototype.build = function(){ this.span.className = this.css() this.span.innerHTML = this.html() } Lex.prototype.css = function(){ return "f" + letters[this.fg&15] + "b" + letters[this.bg&15] } Lex.prototype.html = function(){ return this.char == " " ? " " : this.char || " " } Lex.prototype.read = function(){ this.char = this.span.innerHTML return this.char } Lex.prototype.irc = function(){ var char = this.char == "%" ? '%%' : this.char || " " if (this.bg == 1 && this.fg == 0) { return char } else { return "\\x03" + (this.fg&15) + "," + (this.bg&15) + char } } Lex.prototype.clone = function (lex){ if (lex.isClear()) return this.fg = lex.fg this.bg = lex.bg this.char = lex.char this.build() } Lex.prototype.erase = function (lex){ if (lex.isClear()) return this.fg = colors.white this.bg = colors.black this.char = " " this.build() } Lex.prototype.fill = function(fg,bg){ this.fg = fg this.bg = bg this.build() } Lex.prototype.eq = function(lex){ return lex && this.fg == lex.fg && this.bg == lex.fg && this.char == lex.char } Lex.prototype.clear = function(){ this.bg = 1 this.fg = 0 this.char = " " this.build() } Lex.prototype.isClear = function(){ return this.bg == 1 && this.fg == 0 && this.char == " " } Lex.prototype.focus = function(){ if (focused) focused.blur() this.span.classList.add('focused') focused = this } Lex.prototype.blur = function(){ this.span.classList.remove('focused') focused = null } Lex.prototype.key = function(char, keyCode) { console.log(keyCode, this.y, this.x) switch (keyCode) { case 8: canvas.focusLex(this.y-1, this.x) focused.char = " " focused.build() return case 13: // return canvas.focusLex(0, this.x+1) break case 38: // up canvas.focusLex(this.y, this.x-1) break case 40: // down canvas.focusLex(this.y, this.x+1) break case 37: // left canvas.focusLex(this.y-1, this.x) break case 39: // right canvas.focusLex(this.y+1, this.x) break default: this.char = char this.fg = brush.bg this.build() this.blur() if (this.y < canvas.w-1) { canvas.aa[this.x][this.y+1].focus() } else { canvas.focusLex(0, this.x+1) } } } Lex.prototype.demolish = function(){ this.span.parentNode.removeChild(this.span) this.span = null }