summaryrefslogtreecommitdiff
path: root/js/matrix.js
blob: ccd90b2e38668f3c264afacad09e64ce1e5b5166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function Matrix (w,h,f){
	this.x = 0
	this.y = 0
	this.w = w
	this.h = h
	this.f = f
	this.initialize()
}
Matrix.prototype.initialize = function(){
	var w = this.w || 1, h = this.h || 1, f = this.f
	var aa = new Array (h)
	for (var y = 0; y < h; y++) {
		aa[y] = new Array (w)
		for (var x = 0; x < w; x++) {
			aa[y][x] = f(x,y)
		}
	}
	this.aa = aa
}
Matrix.prototype.rebuild = function (){
	this.demolish()
	this.initialize()
	this.append()
	this.bind()
	this.generate && this.generate()
}
Matrix.prototype.bind = function () {}
Matrix.prototype.demolish = function (){
	this.forEach(function(lex){
		lex.demolish()
	})
	while (this.rapper.firstChild) {
    this.rapper.removeChild(this.rapper.firstChild);
	}
	this.aa.forEach(function(row){
		row.length = 0
	})
	this.aa.length = 0
}
Matrix.prototype.forEach = function(f){
	this.aa.forEach(function(row, y){
		row.forEach(function(lex, x){
			f(lex, x, y)
		})
	})
}
Matrix.prototype.focusLex = function(y,x){
	this.aa[mod(y,this.h)][mod(x,this.w)].focus()
}
Matrix.prototype.clear = function(){
	this.forEach(function(lex,x,y){ lex.clear() })
}
Matrix.prototype.fill = function(fg, bg){
	this.fg = fg
	this.bg = bg
	this.forEach(function(lex,x,y){
		lex.fill(fg,bg)
		lex.build()
	})
}
Matrix.prototype.build = function(){
	this.forEach(function(lex,x,y){
		lex.build()
	})
}
Matrix.prototype.append = function(rapper){
	rapper = this.rapper = rapper || this.rapper
  this.aa.forEach(function(row, y){
  	var div = document.createElement("div")
  	row.forEach(function(lex, x) {
			div.appendChild(lex.span)
		})
		rapper.appendChild( div )
	})
}
Matrix.prototype.region = function(w,h,x,y) {
	w = w || 1
	h = h || 1
	x = x || 0
	y = y || 0
	var parent = this
	var mat = new Matrix(w, h, function(x,y){
		return parent.aa[y][x]
	})
	mat.f = this.f
}
Matrix.prototype.ascii = function () {
  var lines = this.aa.map(function(row, y){
    var last, line = ""
    row.forEach(function(lex, x) {
			line += lex.ascii()
		})
    return line.replace(/\s+$/,"")
	})
  var txt = lines.join("\n")
  return txt
}
Matrix.prototype.mirc = function () {
  var lines = this.aa.map(function(row, y){
    var last, line = ""
    row.forEach(function(lex, x) {
			if (lex.eq(last)) {
				line += lex.sanitize()
			}
			else {
				if (x > 0 && last && (last.bg != 1 || last.fg != 0)) line += "\x03"
				line += lex.mirc()
				last = lex
			}
		})
		if (last && ! last.isClear()) { line += "\x03" }
    return line
	}).filter(function(line){ return line.length > 0 })
	return lines.join("\n")
}
Matrix.prototype.irssi = function(){
	var txt = this.mirc()
                .replace(/\%/g, '%%')
                .replace(/\\/g, '\\\\')
                .replace(/\"/g, '\\\"')
                .replace(/\'/g, '\\\'')
                .replace(/\`/g, '\\\`')
                .replace(/\s+\n/g, '\n')
                .replace(/\s+$/g, '\n')
                .replace(/^\n+/, '')
                .replace(/\n/g, '\\n')
                .replace(/\x03/g, '\\x03');
	// console.log(txt)
  return '/exec -out printf "' + 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)
}