yoob.TextTerminal is almost back in the running.
catseye
12 years ago
36 | 36 | view.init(pf, element); |
37 | 37 | */ |
38 | 38 | |
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); | |
47 | 40 | |
48 | 41 | console.write("Click me!\n"); |
49 | 42 | var z = 0; |
1 | 1 | <head> |
2 | 2 | <meta charset="utf-8"> |
3 | 3 | <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> | |
7 | 10 | </head> |
8 | 11 | <body> |
9 | 12 | |
10 | 13 | <h1>yoob.TextTerminal Demo</h1> |
11 | 14 | |
12 | <canvas id="canvas" width="400" height="400" tabindex="0"> | |
15 | <canvas id="console" width="400" height="400" tabindex="0"> | |
13 | 16 | Your browser doesn't support displaying an HTML5 canvas. |
14 | 17 | </canvas> |
15 | 18 | |
16 | 19 | </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> | |
17 | 26 | <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); | |
22 | 30 | 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) { | |
25 | 35 | alert(str); |
26 | }); | |
36 | view.draw(); | |
37 | }; | |
27 | 38 | canvas.focus(); |
28 | 39 | </script> |
11 | 11 | * Mostly for demonstration purposes so far. |
12 | 12 | */ |
13 | 13 | 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 = ""; | |
18 | 25 | |
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); | |
30 | 71 | if (me.terminal !== undefined) { |
31 | me.terminal.write('\b \b'); | |
72 | me.terminal.write(chr); | |
32 | 73 | } |
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); | |
35 | 77 | } |
36 | 78 | 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 | }; | |
73 | 83 | }; |
21 | 21 | * |
22 | 22 | * Note there is no overStrike mode anymore. But also, because the TextConsole |
23 | 23 | * 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. | |
25 | 25 | * |
26 | 26 | * Note, this console is completely "dumb": it does not understand any |
27 | 27 | * control codes whatsoever, not even newline. For a subclass of this |
68 | 68 | return this; |
69 | 69 | }; |
70 | 70 | |
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 | ||
71 | 85 | this.getPlayfield = function() { |
72 | 86 | return this.pf; |
73 | 87 | }; |
168 | 182 | return; |
169 | 183 | this.pf.put( |
170 | 184 | 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) | |
172 | 186 | ); |
173 | 187 | this.advanceCol(); |
174 | 188 | }; |
0 | 0 | /* |
1 | * This file is part of yoob.js version 0.3 | |
1 | * This file is part of yoob.js version 0.4-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 | */ |
14 | 14 | if (c === '\n') { |
15 | 15 | this.advanceRow(); |
16 | 16 | 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--; | |
19 | 19 | return true; |
20 | 20 | } |
21 | 21 | return false; |