24 | 24 |
* is backed by a Playfield, you can read characters and colors at any position
|
25 | 25 |
* on the console. Also, cursor doesn't blink anymore; will be addressed.
|
26 | 26 |
*
|
27 | |
* Note, this console is completely "dumb": it does not understand any
|
28 | |
* control codes whatsoever, not even newline. For a subclass of this
|
29 | |
* which does understand (some) control codes, use text-terminal.js.
|
|
27 |
* Note that this is not a completely "dumb" or "raw" console. Some methods do
|
|
28 |
* understand terminal control codes. But you don't have to use them.
|
30 | 29 |
*/
|
31 | 30 |
yoob.TextTerminal = function() {
|
32 | 31 |
|
|
43 | 42 |
ctx.fillStyle = this.backgroundColor;
|
44 | 43 |
ctx.fillRect(x, y, cellWidth, cellHeight);
|
45 | 44 |
ctx.fillStyle = this.textColor;
|
46 | |
ctx.fillText(this.character.toString(), x, y);
|
|
45 |
if (this.character !== ' ') {
|
|
46 |
ctx.fillText(this.character, x, y);
|
|
47 |
}
|
47 | 48 |
};
|
48 | 49 |
};
|
49 | 50 |
|
|
72 | 73 |
// convenience function
|
73 | 74 |
this.createPlayfieldCanvasView = function(element, cellWidth, cellHeight) {
|
74 | 75 |
var view = new yoob.PlayfieldCanvasView();
|
75 | |
view.init(this.getPlayfield(), element);
|
76 | |
view.setCursors([this.getCursor()]);
|
|
76 |
view.init(this.pf, element);
|
|
77 |
view.setCursors([this.cursor]);
|
77 | 78 |
view.setCellDimensions(cellWidth, cellHeight);
|
78 | 79 |
var self = this;
|
79 | 80 |
view.getLowerX = function() { return 0; };
|
|
83 | 84 |
return view;
|
84 | 85 |
};
|
85 | 86 |
|
86 | |
this.getPlayfield = function() {
|
87 | |
return this.pf;
|
88 | |
};
|
89 | |
|
90 | |
this.getCursor = function() {
|
91 | |
return this.cursor;
|
92 | |
};
|
93 | |
|
94 | 87 |
this.setColors = function(textColor, backgroundColor) {
|
95 | 88 |
if (textColor !== undefined) {
|
96 | 89 |
this.textColor = textColor;
|
|
114 | 107 |
};
|
115 | 108 |
|
116 | 109 |
this.setCharAt = function(x, y, c) {
|
117 | |
this.pf.get(x, y).character = c;
|
|
110 |
var cell = this.pf.get(x, y);
|
|
111 |
if (cell === this.defaultCell) {
|
|
112 |
cell = new ConsoleCell().init(' ', 'green', 'black');
|
|
113 |
this.pf.put(x, y, cell);
|
|
114 |
}
|
|
115 |
cell.character = c;
|
118 | 116 |
return this;
|
119 | 117 |
};
|
120 | 118 |
|
121 | 119 |
this.setTextColorAt = function(x, y, style) {
|
|
120 |
var cell = this.pf.get(x, y);
|
|
121 |
if (cell === this.defaultCell) {
|
|
122 |
cell = new ConsoleCell().init(' ', 'green', 'black');
|
|
123 |
this.pf.put(x, y, cell);
|
|
124 |
}
|
122 | 125 |
this.pf.get(x, y).textColor = style;
|
123 | 126 |
return this;
|
124 | 127 |
};
|
125 | 128 |
|
126 | 129 |
this.setBackgroundColorAt = function(x, y, style) {
|
|
130 |
var cell = this.pf.get(x, y);
|
|
131 |
if (cell === this.defaultCell) {
|
|
132 |
cell = new ConsoleCell().init(' ', 'green', 'black');
|
|
133 |
this.pf.put(x, y, cell);
|
|
134 |
}
|
127 | 135 |
this.pf.get(x, y).backgroundColor = style;
|
128 | 136 |
return this;
|
129 | 137 |
};
|
|
195 | 203 |
var c = string.charAt(i);
|
196 | 204 |
if (c === '\n') {
|
197 | 205 |
this.advanceRow();
|
198 | |
} else if (c === '\b' && this.getCursor().x > 0) {
|
199 | |
this.getCursor().x--;
|
|
206 |
} else if (c === '\b') {
|
|
207 |
if (this.cursor.x > 0) {
|
|
208 |
this.cursor.x--;
|
|
209 |
}
|
200 | 210 |
} else {
|
201 | 211 |
this.writeChar(c);
|
202 | 212 |
}
|