git @ Cat's Eye Technologies yoob.js / be0d018
Several only-marginally-helpful edits to yoob.TextTerminal. catseye 12 years ago
2 changed file(s) with 31 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
3838 */
3939
4040 t.write("Click me!\n");
41
42 t.setCharAt(30, 30, '?'); // why no work?
43
4144 var z = 0;
4245 element.onclick = function(e) {
4346 t.writeRaw("Hello, world! (" + z + ") ");
6265 };
6366 ib.onenter = function(str) {
6467 t.write("You typed '" + str + "'\n");
68 t.write("And now I\bU see\n");
6569 view.draw();
6670 };
6771
2424 * is backed by a Playfield, you can read characters and colors at any position
2525 * on the console. Also, cursor doesn't blink anymore; will be addressed.
2626 *
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.
3029 */
3130 yoob.TextTerminal = function() {
3231
4342 ctx.fillStyle = this.backgroundColor;
4443 ctx.fillRect(x, y, cellWidth, cellHeight);
4544 ctx.fillStyle = this.textColor;
46 ctx.fillText(this.character.toString(), x, y);
45 if (this.character !== ' ') {
46 ctx.fillText(this.character, x, y);
47 }
4748 };
4849 };
4950
7273 // convenience function
7374 this.createPlayfieldCanvasView = function(element, cellWidth, cellHeight) {
7475 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]);
7778 view.setCellDimensions(cellWidth, cellHeight);
7879 var self = this;
7980 view.getLowerX = function() { return 0; };
8384 return view;
8485 };
8586
86 this.getPlayfield = function() {
87 return this.pf;
88 };
89
90 this.getCursor = function() {
91 return this.cursor;
92 };
93
9487 this.setColors = function(textColor, backgroundColor) {
9588 if (textColor !== undefined) {
9689 this.textColor = textColor;
114107 };
115108
116109 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;
118116 return this;
119117 };
120118
121119 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 }
122125 this.pf.get(x, y).textColor = style;
123126 return this;
124127 };
125128
126129 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 }
127135 this.pf.get(x, y).backgroundColor = style;
128136 return this;
129137 };
195203 var c = string.charAt(i);
196204 if (c === '\n') {
197205 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 }
200210 } else {
201211 this.writeChar(c);
202212 }