asciiblaster

- draw irc art in your web browser
git clone git://git.acid.vegas/asciiblaster.git
Log | Files | Refs | Archive | README

lex.js (3479B)

      1 function Lex (x,y) {
      2   if (typeof x == "number") {
      3     this.y = y
      4     this.x = x
      5     this.span = document.createElement("span")
      6   }
      7   else {
      8     this.span = x
      9   }
     10   this.fg = colors.white
     11   this.bg = colors.black
     12   this.char = " "
     13   this.opacity = 1
     14   this.focused = false
     15 }
     16 Lex.prototype.build = function(){
     17   if (isNaN(this.bg) || this.bg == Infinity || this.bg == -Infinity) this.bg = colors.black
     18   if (isNaN(this.fg) || this.fg == Infinity || this.fg == -Infinity) this.fg = colors.black
     19   this.span.className = this.css()
     20   this.span.innerHTML = this.html()
     21 }
     22 Lex.prototype.css = function(){
     23    return (
     24     this.focused ?
     25       "focused " : ""
     26   ) + (
     27     this.opacity === 0 ?
     28       "transparent f" + color_alphabet[modi(this.fg,16)] :
     29       "f" + color_alphabet[modi(this.fg,16)] + " b" + color_alphabet[modi(this.bg,16)]
     30   )
     31 }
     32 Lex.prototype.html = function(){
     33   return this.char == " " ? " " : this.char || " "
     34 }
     35 Lex.prototype.read = function(){
     36   this.char = this.span.innerHTML
     37   return this.char
     38 }
     39 Lex.prototype.ascii = function(){
     40   return this.char || " "
     41 }
     42 Lex.prototype.sanitize = function(){
     43   switch (this.char) {
     44 //    case "%": return "%"
     45     case undefined:
     46     case "":  return " "
     47     default:  return this.char
     48   }
     49 }
     50 var fgOnly = false
     51 Lex.prototype.mirc = function(){
     52   var char = this.char || " "
     53   if (fgOnly) {
     54     return "\x03" + (this.fg&15) + char
     55   }
     56   if ((this.bg&15) < 10 && ! isNaN(parseInt(char))) {
     57     return "\x03" + (this.fg&15) + ",0" + (this.bg&15) + char
     58   }
     59   else {
     60     return "\x03" + (this.fg&15) + "," + (this.bg&15) + char
     61   }
     62 }
     63 Lex.prototype.ansi = function(){
     64   var fg = ansi_fg[ this.fg&15 ]
     65   var bg = ansi_bg[ this.bg&15 ]
     66   var c = this.sanitize()
     67   if (c == "\\") c = "\\\\"
     68   if (c == '"') c = '\\"'
     69   return "\\e[" + fg + ";" + bg + "m" + c
     70 }
     71 Lex.prototype.assign = function (lex){
     72   this.fg = lex.fg
     73   this.bg = lex.bg
     74   this.char = lex.char
     75   this.opacity = lex.opacity
     76   this.build()
     77 }
     78 Lex.prototype.stamp = function (lex, brush){
     79   if (brush.draw_fg) this.fg = lex.fg
     80   if (brush.draw_bg && lex.opacity > 0) this.bg = lex.bg
     81   if (brush.draw_char) this.char = lex.char
     82   this.opacity = 1
     83   this.build()
     84 }
     85 Lex.prototype.clone = function () {
     86   var lex = new Lex (0,0)
     87   lex.assign(this)
     88   return lex
     89 }
     90 Lex.prototype.erase = function (){
     91   this.fg = fillColor
     92   this.bg = fillColor
     93   this.char = " "
     94   this.opacity = 1
     95   this.build()
     96 }
     97 Lex.prototype.eq = function(lex){
     98   return lex && this.fg == lex.fg && this.bg == lex.bg && this.char == lex.char
     99 }
    100 Lex.prototype.eqColor = function(lex){
    101   return lex && this.fg == lex.fg && this.bg == lex.bg
    102 }
    103 Lex.prototype.ne = function(lex){
    104   return ! this.eq(lex)
    105 }
    106 Lex.prototype.clear = function(){
    107   this.bg = colors.black
    108   this.fg = 0
    109   this.char = " "
    110   this.opacity = 0
    111   this.build()
    112 }
    113 Lex.prototype.isClear = function(){
    114   return this.bg == 1 && this.fg == 0 && this.char == " "
    115 }
    116 Lex.prototype.focus = function(){
    117   if (focused) focused.blur()
    118   this.span.classList.add('focused')
    119   this.focused = true
    120   focused = this
    121 }
    122 Lex.prototype.blur = function(){
    123   focused = null
    124   this.span && this.span.classList.remove('focused')
    125   this.focused = false
    126   this.onBlur && this.onBlur()
    127 }
    128 Lex.prototype.demolish = function(){
    129   if (this.span.parentNode) { this.span.parentNode.removeChild(this.span) }
    130   this.span = null
    131 }
    132 Lex.prototype.key = function(char, keyCode) {
    133   if (! char) { return }
    134   this.char = char
    135   this.fg = brush.fg
    136   this.build()
    137   return true
    138 }