summaryrefslogtreecommitdiff
path: root/js/lex.js
blob: 39a47cd06d631fc1bf068102b749f084fc205d92 (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
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.irc = function(){
	if (this.bg == 1 && this.fg == 0) {
// 		return "\\x03" + "," + (this.bg&15) + this.char
		return this.char
	}
	else {
		return "\\x03" + (this.fg&15) + "," + (this.bg&15) + this.char
	}
}
Lex.prototype.clone = function (lex){
	if (! erasing && lex.isClear()) return
	this.fg = lex.fg
	this.bg = lex.bg
	this.char = lex.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
}