git @ Cat's Eye Technologies yoob.js / 3b5d56b
yoob.TextTerminal is almost back in the running. catseye 12 years ago
5 changed file(s) with 106 addition(s) and 78 deletion(s). Raw diff Collapse all Expand all
3636 view.init(pf, element);
3737 */
3838
39 var view = new yoob.PlayfieldCanvasView();
40 view.init(console.getPlayfield(), element);
41 view.setCursors([console.getCursor()]);
42 view.setCellDimensions(12, 18);
43 view.getLowerX = function() { return 0; };
44 view.getLowerY = function() { return 0; };
45 view.getUpperX = function() { return 79; };
46 view.getUpperY = function() { return 24; };
39 var view = console.createPlayfieldCanvasView(element, 12, 18);
4740
4841 console.write("Click me!\n");
4942 var z = 0;
11 <head>
22 <meta charset="utf-8">
33 <title>yoob.TextTerminal Demo</title>
4 <script src="../src/yoob/text-console.js"></script>
5 <script src="../src/yoob/text-terminal.js"></script>
6 <script src="../src/yoob/line-input-buffer.js"></script>
4 <style>
5 #console {
6 background: black;
7 color: green;
8 }
9 </style>
710 </head>
811 <body>
912
1013 <h1>yoob.TextTerminal Demo</h1>
1114
12 <canvas id="canvas" width="400" height="400" tabindex="0">
15 <canvas id="console" width="400" height="400" tabindex="0">
1316 Your browser doesn't support displaying an HTML5 canvas.
1417 </canvas>
1518
1619 </body>
20 <script src="../src/yoob/playfield.js"></script>
21 <script src="../src/yoob/cursor.js"></script>
22 <script src="../src/yoob/playfield-canvas-view.js"></script>
23 <script src="../src/yoob/text-console.js"></script>
24 <script src="../src/yoob/text-terminal.js"></script>
25 <script src="../src/yoob/line-input-buffer.js"></script>
1726 <script>
18 var canvas = document.getElementById('canvas');
19 var t = new yoob.TextTerminal();
20 var z = 0;
21 t.init(canvas, 20, 80, 25);
27 var canvas = document.getElementById('console');
28 var t = new yoob.TextTerminal().init(80, 25);
29 var view = t.createPlayfieldCanvasView(canvas, 12, 18);
2230 t.write("Focus me and\ntype something. Try backspacing.\n\n");
23 var ib = new yoob.LineInputBuffer();
24 ib.init(canvas, t, function(str) {
31 view.draw();
32 var ib = new yoob.LineInputBuffer().init(canvas, t);
33 ib.onupdate = function(str) { view.draw(); }
34 ib.onenter = function(str) {
2535 alert(str);
26 });
36 view.draw();
37 };
2738 canvas.focus();
2839 </script>
1111 * Mostly for demonstration purposes so far.
1212 */
1313 yoob.LineInputBuffer = function() {
14 this.listenObject = undefined;
15 this.terminal = undefined;
16 this.callback = undefined;
17 this.text = undefined;
14 this.listenObject = undefined;
15 this.terminal = undefined;
16 this.callback = undefined;
17 this.text = undefined;
18 this.onupdate = undefined;
19 this.onenter = undefined;
20
21 this.init = function(listenObject, terminal) {
22 this.listenObject = listenObject;
23 this.terminal = terminal;
24 this.text = "";
1825
19 this.init = function(listenObject, terminal, callback) {
20 this.listenObject = listenObject;
21 this.terminal = terminal;
22 this.callback = callback;
23 this.text = "";
24
25 var me = this;
26 listenObject.addEventListener('keyup', function(e) {
27 //alert(e.keyCode);
28 switch (e.keyCode) {
29 case 8: /* Backspace */
26 var me = this;
27 listenObject.addEventListener('keyup', function(e) {
28 //alert(e.keyCode);
29 switch (e.keyCode) {
30 case 8: /* Backspace */
31 if (me.terminal !== undefined) {
32 me.terminal.write('\b \b');
33 }
34 if (me.text.length > 0) {
35 me.text = me.text.substring(0, me.text.length-2);
36 if (me.onupdate !== undefined) {
37 me.onupdate(me.text);
38 }
39 }
40 e.cancelBubble = true;
41 break;
42 case 13: /* Enter */
43 if (me.terminal !== undefined) {
44 me.terminal.write('\n');
45 }
46 me.text = me.text.substring(0, me.text.length-1);
47 if (me.onenter !== undefined) {
48 me.onenter(me.text);
49 }
50 me.text = "";
51 e.cancelBubble = true;
52 break;
53 case 38: /* Up arrow */
54 break;
55 case 40: /* Down arrow */
56 break;
57 case 37: /* Left arrow */
58 break;
59 case 39: /* Right arrow */
60 break;
61 }
62 }, true);
63
64 /* TODO support on more browsers, with keyup */
65 listenObject.addEventListener('keypress', function(e) {
66 if (e.altKey) {
67 //alert(e.charCode);
68 return;
69 }
70 var chr = String.fromCharCode(e.charCode);
3071 if (me.terminal !== undefined) {
31 me.terminal.write('\b \b');
72 me.terminal.write(chr);
3273 }
33 if (me.text.length > 0) {
34 me.text = me.text.substring(0, me.text.length-2);
74 me.text += chr;
75 if (me.onupdate !== undefined) {
76 me.onupdate(me.text);
3577 }
3678 e.cancelBubble = true;
37 break;
38 case 13: /* Enter */
39 if (me.terminal !== undefined) {
40 me.terminal.write('\n');
41 }
42 me.text = me.text.substring(0, me.text.length-1);
43 if (me.callback !== undefined) {
44 me.callback(me.text);
45 }
46 me.text = "";
47 e.cancelBubble = true;
48 break;
49 case 38: /* Up arrow */
50 break;
51 case 40: /* Down arrow */
52 break;
53 case 37: /* Left arrow */
54 break;
55 case 39: /* Right arrow */
56 break;
57 }
58 }, true);
59 /* TODO support on more browsers, with keyup */
60 listenObject.addEventListener('keypress', function(e) {
61 if (e.altKey) {
62 //alert(e.charCode);
63 return;
64 }
65 var chr = String.fromCharCode(e.charCode);
66 if (me.terminal !== undefined) {
67 me.terminal.write(chr);
68 }
69 me.text += chr;
70 e.cancelBubble = true;
71 }, true);
72 };
79 }, true);
80
81 return this;
82 };
7383 };
2121 *
2222 * Note there is no overStrike mode anymore. But also, because the TextConsole
2323 * is backed by a Playfield, you can read characters and colors at any position
24 * on the console.
24 * on the console. Also, cursor doesn't blink anymore; will be addressed.
2525 *
2626 * Note, this console is completely "dumb": it does not understand any
2727 * control codes whatsoever, not even newline. For a subclass of this
6868 return this;
6969 };
7070
71 // convenience function
72 this.createPlayfieldCanvasView = function(element, cellWidth, cellHeight) {
73 var view = new yoob.PlayfieldCanvasView();
74 view.init(this.getPlayfield(), element);
75 view.setCursors([this.getCursor()]);
76 view.setCellDimensions(cellWidth, cellHeight);
77 var self = this;
78 view.getLowerX = function() { return 0; };
79 view.getLowerY = function() { return 0; };
80 view.getUpperX = function() { return self.cols - 1; };
81 view.getUpperY = function() { return self.rows - 1; };
82 return view;
83 };
84
7185 this.getPlayfield = function() {
7286 return this.pf;
7387 };
168182 return;
169183 this.pf.put(
170184 this.cursor.x, this.cursor.y,
171 new ConsoleCell().init(c, this.textColor, this.backgroundColor)
185 new ConsoleCell().in it(c, this.textColor, this.backgroundColor)
172186 );
173187 this.advanceCol();
174188 };
00 /*
1 * This file is part of yoob.js version 0.3
1 * This file is part of yoob.js version 0.4-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 */
1414 if (c === '\n') {
1515 this.advanceRow();
1616 return true;
17 } else if (c === '\b' && this.col > 0) {
18 this.col--;
17 } else if (c === '\b' && this.getCursor().x > 0) {
18 this.getCursor().x--;
1919 return true;
2020 }
2121 return false;