0 | 0 |
/*
|
1 | |
* This file is part of yoob.js version 0.4
|
|
1 |
* This file is part of yoob.js version 0.11-PRE
|
2 | 2 |
* Available from https://github.com/catseye/yoob.js/
|
3 | 3 |
* This file is in the public domain. See http://unlicense.org/ for details.
|
4 | 4 |
*/
|
|
6 | 6 |
|
7 | 7 |
/*
|
8 | 8 |
* A text-based-console simulation in Javascript. It is, in actuality, a
|
9 | |
* facade for a yoob.Playfield and a yoob.Cursor. (And soon, perhaps, a
|
10 | |
* yoob.Playfield*View also.)
|
|
9 |
* facade for a yoob.Playfield and a yoob.Cursor.
|
11 | 10 |
*
|
12 | 11 |
* The yoob.TextTerminal has no concern for display; in MVC terms it is a
|
13 | 12 |
* "model" and you will need to use a "view" such as yoob.PlayfieldCanvasView
|
14 | |
* or yoob.PlayfieldHTMLView to display it.
|
15 | |
*
|
16 | |
* Create a new yoob.TextTerminal object t, then call t.init(80, 25), then call
|
17 | |
* t.write() to write text to the console.
|
|
13 |
* or yoob.PlayfieldHTMLView to display it. (There is a convenience method on
|
|
14 |
* to create such an object.)
|
|
15 |
*
|
|
16 |
* Create a new yoob.TextTerminal object t, then call t.init({ columns: 80, rows: 25}),
|
|
17 |
* then call t.write() to write text to the console.
|
18 | 18 |
*
|
19 | 19 |
* You can also call t.setColors() to set the text and background colors of
|
20 | 20 |
* the next text to be written, between calls to t.write(). You can call
|
21 | 21 |
* t.reset() to clear the simulated screen (to the selected backgroundColor.)
|
22 | 22 |
*
|
23 | |
* Note there is no overStrike mode anymore. But also, because the TextTerminal
|
24 | |
* is backed by a Playfield, you can read characters and colors at any position
|
25 | |
* on the console. Also, cursor doesn't blink anymore; will be addressed.
|
|
23 |
* Because the TextTerminal is backed by a Playfield, you can read characters and
|
|
24 |
* colors at any position on the console.
|
26 | 25 |
*
|
27 | 26 |
* Note that this is not a completely "dumb" or "raw" console. Some methods do
|
28 | 27 |
* understand terminal control codes. But you don't have to use them.
|
|
51 | 50 |
};
|
52 | 51 |
};
|
53 | 52 |
|
54 | |
this.init = function(cols, rows) {
|
55 | |
this.pf = new yoob.Playfield();
|
|
53 |
this.init = function(cfg) {
|
|
54 |
cfg = cfg || {};
|
|
55 |
this.rows = cfg.rows || 25;
|
|
56 |
this.cols = cfg.columns || 80;
|
|
57 |
this.textColor = cfg.textColor || "green";
|
|
58 |
this.backgroundColor = cfg.backgroundColor || "black";
|
|
59 |
this.cursor = new yoob.Cursor().init({ dx: 1 });
|
56 | 60 |
this.defaultCell = new ConsoleCell().init(' ', 'green', 'black');
|
57 | |
this.pf.setDefault(this.defaultCell);
|
58 | |
this.cursor = new yoob.Cursor(0, 0, 1, 0);
|
59 | |
this.rows = rows;
|
60 | |
this.cols = cols;
|
61 | |
this.textColor = "green";
|
62 | |
this.backgroundColor = "black";
|
|
61 |
this.pf = new yoob.Playfield(),init({
|
|
62 |
defaultValue: this.defaultCell,
|
|
63 |
cursors: [this.cursor]
|
|
64 |
});
|
63 | 65 |
this.reset();
|
64 | 66 |
return this;
|
65 | 67 |
};
|
66 | 68 |
|
67 | 69 |
// convenience function
|
68 | |
this.createPlayfieldCanvasView = function(element, cellWidth, cellHeight) {
|
69 | |
var view = new yoob.PlayfieldCanvasView();
|
70 | |
view.init(this.pf, element);
|
71 | |
view.setCursors([this.cursor]);
|
72 | |
view.setCellDimensions(cellWidth, cellHeight);
|
73 | |
var self = this;
|
|
70 |
this.createPlayfieldCanvasView = function(canvas, cellWidth, cellHeight) {
|
|
71 |
var view = new yoob.PlayfieldCanvasView().init({
|
|
72 |
playfield: this.pf,
|
|
73 |
canvas: canvas,
|
|
74 |
cellWidth: cellWidth,
|
|
75 |
cellHeight: cellHeight,
|
|
76 |
fixedPosition: true
|
|
77 |
});
|
|
78 |
var $this = this;
|
74 | 79 |
view.getLowerX = function() { return 0; };
|
75 | 80 |
view.getLowerY = function() { return 0; };
|
76 | |
view.getUpperX = function() { return self.cols - 1; };
|
77 | |
view.getUpperY = function() { return self.rows - 1; };
|
78 | |
view.fixedPosition = true;
|
|
81 |
view.getUpperX = function() { return $this.cols - 1; };
|
|
82 |
view.getUpperY = function() { return $this.rows - 1; };
|
79 | 83 |
return view;
|
80 | 84 |
};
|
81 | 85 |
|
|
136 | 140 |
* make the cursor visible(?), and home it.
|
137 | 141 |
*/
|
138 | 142 |
this.reset = function() {
|
139 | |
this.cursor.x = 0;
|
140 | |
this.cursor.y = 0;
|
|
143 |
this.cursor.setX(0).setY(0);
|
141 | 144 |
this.pf.clear();
|
142 | 145 |
};
|
143 | 146 |
|
|
146 | 149 |
* TextTerminal display if necessary.
|
147 | 150 |
*/
|
148 | 151 |
this.advanceRow = function() {
|
149 | |
this.cursor.x = 0;
|
150 | |
this.cursor.y += 1;
|
151 | |
while (this.cursor.y >= this.rows) {
|
|
152 |
this.cursor.setX(0).setY(this.cursor.getY() + 1);
|
|
153 |
while (this.cursor.getY() >= this.rows) {
|
152 | 154 |
this.pf.scrollRectangleY(-1, 0, 0, this.cols-1, this.rows-1);
|
153 | 155 |
this.pf.clearRectangle(0, this.rows-1, this.cols-1, this.rows-1);
|
154 | |
this.cursor.y -= 1;
|
|
156 |
this.cursor.setY(this.cursor.getY() - 1);
|
155 | 157 |
}
|
156 | 158 |
};
|
157 | 159 |
|
|
160 | 162 |
* next row if necessary.
|
161 | 163 |
*/
|
162 | 164 |
this.advanceCol = function() {
|
163 | |
this.cursor.x += 1;
|
164 | |
if (this.cursor.x >= this.cols) {
|
|
165 |
this.cursor.setX(this.cursor.getX() + 1);
|
|
166 |
if (this.cursor.getX() >= this.cols) {
|
165 | 167 |
this.advanceRow();
|
166 | 168 |
}
|
167 | 169 |
};
|
|
199 | 201 |
if (c === '\n') {
|
200 | 202 |
this.advanceRow();
|
201 | 203 |
} else if (c === '\b') {
|
202 | |
if (this.cursor.x > 0) {
|
203 | |
this.cursor.x--;
|
|
204 |
if (this.cursor.getX() > 0) {
|
|
205 |
this.cursor.setX(this.cursor.getX() - 1);
|
204 | 206 |
}
|
205 | 207 |
} else {
|
206 | 208 |
this.writeChar(c);
|
|
213 | 215 |
* (0-based) and y is the row number (also 0-based.)
|
214 | 216 |
*/
|
215 | 217 |
this.gotoxy = function(x, y) {
|
216 | |
this.cursor.x = x;
|
217 | |
this.cursor.y = y;
|
|
218 |
this.cursor.setX(x).setY(y);
|
218 | 219 |
};
|
219 | 220 |
};
|