19 | 19 |
* simulated screen (to the selected backgroundColor.) You can also set
|
20 | 20 |
* or clear overStrike mode.
|
21 | 21 |
*
|
22 | |
* TODO:
|
23 | |
* - make this completely dumb, with no control codes whatsoever, even
|
24 | |
* newline. then make a text-terminal which inherits from it, but
|
25 | |
* which knows about control codes.
|
26 | |
* - add onDrawCharacter() callback, which can draw the character
|
27 | |
* itself and return false, or return true to allow the console to
|
28 | |
* draw it.
|
|
22 |
* Note, this console is completely "dumb": it does not understand any
|
|
23 |
* control codes whatsoever, not even newline. For a subclass of this
|
|
24 |
* which does understand (some) control codes, use text-terminal.js.
|
29 | 25 |
*/
|
30 | 26 |
yoob.TextConsole = function() {
|
31 | 27 |
this.canvas = undefined;
|
|
132 | 128 |
};
|
133 | 129 |
|
134 | 130 |
/*
|
135 | |
* Advance the cursor to the next line, scrolling the
|
|
131 |
* Advance the cursor to the next row, scrolling the
|
136 | 132 |
* TextConsole display if necessary.
|
137 | 133 |
*/
|
138 | |
this.advance = function() {
|
|
134 |
this.advanceRow = function() {
|
|
135 |
this._stopCursor();
|
139 | 136 |
this.col = 0;
|
140 | 137 |
this.row += 1;
|
141 | 138 |
var ctx = this.canvas.getContext('2d');
|
|
150 | 147 |
);
|
151 | 148 |
this.row -= 1;
|
152 | 149 |
}
|
153 | |
};
|
154 | |
|
155 | |
/*
|
156 | |
* Write a string to the TextConsole. Line feeds will cause a
|
157 | |
* new line, and backspaces will move the cursor left if it is not
|
158 | |
* already at the left edge.
|
|
150 |
this._startCursor();
|
|
151 |
};
|
|
152 |
|
|
153 |
/*
|
|
154 |
* Advance the cursor to the next column, advancing to the
|
|
155 |
* next row if necessary.
|
|
156 |
*/
|
|
157 |
this.advanceCol = function() {
|
|
158 |
this._stopCursor();
|
|
159 |
this.col += 1;
|
|
160 |
if (this.col >= this.cols) {
|
|
161 |
this.advanceRow();
|
|
162 |
}
|
|
163 |
this._startCursor();
|
|
164 |
};
|
|
165 |
|
|
166 |
/*
|
|
167 |
* Called when a character is written to the console. This
|
|
168 |
* may be overridden by subclasses. If it returns false, the
|
|
169 |
* character is written with the default logic. If it returns
|
|
170 |
* true, it is not, and neither is the cursor advanced. (A
|
|
171 |
* subclass which overrides this may write the character and/or
|
|
172 |
* advance the cursor itself.
|
|
173 |
*/
|
|
174 |
this.onWriteChar = function(character, ctx) {
|
|
175 |
return false;
|
|
176 |
};
|
|
177 |
|
|
178 |
/*
|
|
179 |
* Write a character to the console.
|
|
180 |
*/
|
|
181 |
this.writeChar = function(c) {
|
|
182 |
// Inefficient!
|
|
183 |
var ctx = this.canvas.getContext('2d');
|
|
184 |
ctx.textBaseline = "top";
|
|
185 |
ctx.font = this.charHeight + "px monospace";
|
|
186 |
ctx.fillStyle = this.textColor;
|
|
187 |
|
|
188 |
if (this.onWriteChar(c))
|
|
189 |
return;
|
|
190 |
if (c >= ' ') { // && c != DEL ?
|
|
191 |
ctx.fillText(c, this.col * this.charWidth, this.row * this.charHeight);
|
|
192 |
this.advanceCol();
|
|
193 |
}
|
|
194 |
};
|
|
195 |
|
|
196 |
/*
|
|
197 |
* Write a string to the TextConsole. Control characters are not heeded.
|
159 | 198 |
*/
|
160 | 199 |
this.write = function(string) {
|
161 | 200 |
var i = 0;
|
|
165 | 204 |
this._stopCursor();
|
166 | 205 |
while (i < string.length) {
|
167 | 206 |
var c = string.charAt(i);
|
168 | |
if (c === '\n') {
|
169 | |
this.advance();
|
170 | |
} else if (c === '\b' && this.col > 0) {
|
171 | |
this.col--;
|
172 | |
} else if (c >= ' ') {
|
173 | |
if (!this.overStrike) {
|
174 | |
ctx.fillStyle = this.backgroundColor;
|
175 | |
ctx.fillRect(this.col * this.charWidth, this.row * this.charHeight,
|
176 | |
this.charWidth, this.charHeight);
|
177 | |
}
|
178 | |
ctx.fillStyle = this.textColor;
|
179 | |
ctx.fillText(c, this.col * this.charWidth, this.row * this.charHeight);
|
180 | |
this.col += 1;
|
181 | |
if (this.col >= this.cols) {
|
182 | |
this.advance();
|
183 | |
}
|
|
207 |
if (!this.overStrike) {
|
|
208 |
ctx.fillStyle = this.backgroundColor;
|
|
209 |
ctx.fillRect(this.col * this.charWidth, this.row * this.charHeight,
|
|
210 |
this.charWidth, this.charHeight);
|
184 | 211 |
}
|
|
212 |
this.writeChar(c);
|
185 | 213 |
i++;
|
186 | 214 |
};
|
187 | 215 |
this._startCursor();
|