git @ Cat's Eye Technologies yoob.js / ab550dc
Begin fixing up yoob.TextTerminal for the new Playfield layout. Chris Pressey 9 years ago
2 changed file(s) with 42 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
33 * version 0.11-PRE
44
55 `yoob.Playfield`, its Views (`yoob.PlayfieldCanvasView`,
6 `yoob.PlayfieldHTMLView`), and `yoob.Cursor` all take configuration
7 dictionaries as their single argument to `init()`.
6 `yoob.PlayfieldHTMLView`), `yoob.Cursor`, and `yoob.TextTerminal`
7 all take configuration dictionaries as their single argument to `init()`.
88
99 `yoob.Cursor`s are now attached to `yoob.Playfield` and `yoob.Tape`
1010 objects, and are no longer attached to the PlatfieldViews.
00 /*
1 * This file is part of yoob.js version 0.4
1 * This file is part of yoob.js version 0.11-PRE
22 * Available from https://github.com/catseye/yoob.js/
33 * This file is in the public domain. See http://unlicense.org/ for details.
44 */
66
77 /*
88 * 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.
1110 *
1211 * The yoob.TextTerminal has no concern for display; in MVC terms it is a
1312 * "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.
1818 *
1919 * You can also call t.setColors() to set the text and background colors of
2020 * the next text to be written, between calls to t.write(). You can call
2121 * t.reset() to clear the simulated screen (to the selected backgroundColor.)
2222 *
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.
2625 *
2726 * Note that this is not a completely "dumb" or "raw" console. Some methods do
2827 * understand terminal control codes. But you don't have to use them.
5150 };
5251 };
5352
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 });
5660 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 });
6365 this.reset();
6466 return this;
6567 };
6668
6769 // 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;
7479 view.getLowerX = function() { return 0; };
7580 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; };
7983 return view;
8084 };
8185
136140 * make the cursor visible(?), and home it.
137141 */
138142 this.reset = function() {
139 this.cursor.x = 0;
140 this.cursor.y = 0;
143 this.cursor.setX(0).setY(0);
141144 this.pf.clear();
142145 };
143146
146149 * TextTerminal display if necessary.
147150 */
148151 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) {
152154 this.pf.scrollRectangleY(-1, 0, 0, this.cols-1, this.rows-1);
153155 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);
155157 }
156158 };
157159
160162 * next row if necessary.
161163 */
162164 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) {
165167 this.advanceRow();
166168 }
167169 };
199201 if (c === '\n') {
200202 this.advanceRow();
201203 } 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);
204206 }
205207 } else {
206208 this.writeChar(c);
213215 * (0-based) and y is the row number (also 0-based.)
214216 */
215217 this.gotoxy = function(x, y) {
216 this.cursor.x = x;
217 this.cursor.y = y;
218 this.cursor.setX(x).setY(y);
218219 };
219220 };