changeset 37:9e7a743ccac5

Added very, very buggy support for the top window.
author Atul Varma <varmaa@toolness.com>
date Thu, 15 May 2008 00:03:40 -0700
parents afa332a44721
children 696fef276111
files console.js engine-runner.js gnusto.css gnusto.html remedial.js tests/test_engine_runner.js trivial-zui.js
diffstat 7 files changed, 238 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/console.js	Thu May 15 00:03:40 2008 -0700
@@ -0,0 +1,75 @@
+function Console(width, height, element) {
+  this._width = width;
+  this._height = height;
+  this._element = element;
+  this._pos = [0, 0];
+  this.clear();
+}
+
+Console.prototype = {
+  clear: function() {
+    this._characters = [];
+    for (var y = 0; y < this._height; y++) {
+      var row = [];
+      for (var x = 0; x < this._width; x++) {
+        row.push("&nbsp;");
+      }
+      this._characters.push(row);
+    }
+    this.render();
+  },
+
+  moveTo: function(x, y) {
+    this._pos = [x, y];
+  },
+
+  write: function(string) {
+    var x = this._pos[0];
+    var y = this._pos[1];
+    for (var i = 0; i < string.length; i++) {
+      var character = null;
+
+      if (string[i] == " ")
+        character = "&nbsp;";
+      else if (string[i] == "\n") {
+        x = 0;
+        y += 1;
+      } else
+        character = string[i].entityify();
+
+      if (character != null) {
+        //console.log("writing out "+character.quote());
+        this._characters[y][x] = character;
+        x += 1;
+      }
+    }
+    //this._pos = [x, y];
+    this.render();
+  },
+
+  render: function() {
+    var string = "";
+    for (var y = 0; y < this._height; y++) {
+      for (var x = 0; x < this._width; x++) {
+        string += this._characters[y][x];
+      }
+      string += "<br/>";
+    }
+    this._element.innerHTML = string;
+  },
+
+  close: function() {
+    this._element.innerHTML = "";
+  }
+}
+
+var con = null;
+
+function testConsole(element) {
+  console.log("creating");
+  con = new Console(60, 3, element);
+  console.log("writing");
+  con.write("blargy blarg");
+  console.log("rendering");
+  con.render();
+}
--- a/engine-runner.js	Tue May 13 22:56:06 2008 -0700
+++ b/engine-runner.js	Thu May 15 00:03:40 2008 -0700
@@ -28,15 +28,20 @@
     onSetStyle: function(textStyle, foreground, background) {
     },
 
+    // From the Z-Machine spec for split_window: Splits the screen so
+    // that the upper window has the given number of lines: or, if
+    // this is zero, unsplits the screen again.
+
+    onSplitWindow: function(numLines) {
+    },
+    onSetWindow: function(window) {
+    },
+
     // From the Z-Machine spec for erase_window: Erases window with
     // given number (to background colour); or if -1 it unsplits the
     // screen and clears the lot; or if -2 it clears the screen
     // without unsplitting it.
 
-    onSplitWindow: function(window) {
-    },
-    onSetWindow: function(window) {
-    },
     onEraseWindow: function(window) {
     },
     onEraseLine: function() {
@@ -123,7 +128,10 @@
             var logString = "[ " + engine.effect(0);
 
             for (var i = 1; engine.effect(i) != undefined; i++) {
-                logString += ", " + engine.effect(i);
+                var value = engine.effect(i);
+                if (typeof value == "string")
+                    value = value.quote();
+                logString += ", " + value;
             }
 
             self._log(logString + " ]");
--- a/gnusto.css	Tue May 13 22:56:06 2008 -0700
+++ b/gnusto.css	Thu May 15 00:03:40 2008 -0700
@@ -3,6 +3,19 @@
     text-align: center;
 }
 
+#top-window {
+    width: 640px;
+    text-align: center;
+    margin: 0 auto;
+    font-family: monaco;
+    font-size: 10px;
+    color: #ffffff;
+    background: #000000;
+    top: 0px;
+    left: 0px;
+    position: fixed;
+}
+
 #content {
     width: 640px;
     text-align: left;
--- a/gnusto.html	Tue May 13 22:56:06 2008 -0700
+++ b/gnusto.html	Thu May 15 00:03:40 2008 -0700
@@ -8,12 +8,15 @@
   <title></title>
 </head>
 <body>
+<div id="top-window"></div>
 <div id="content"></div>
 </body>
 <script type="text/javascript" src="jquery-1.2.3.min.js"></script>
+<script type="text/javascript" src="remedial.js"></script>
 <script type="text/javascript" src="base64.js"></script>
 <script type="text/javascript" src="troll.js"></script>
 <script type="text/javascript" src="gnusto-engine.min.js"></script>
 <script type="text/javascript" src="engine-runner.js"></script>
+<script type="text/javascript" src="console.js"></script>
 <script type="text/javascript" src="trivial-zui.js"></script>
 </html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/remedial.js	Thu May 15 00:03:40 2008 -0700
@@ -0,0 +1,86 @@
+// Taken from "Remedial Javascript" by Douglas Crockford:
+// http://javascript.crockford.com/remedial.html
+
+function typeOf(value) {
+    var s = typeof value;
+    if (s === 'object') {
+        if (value) {
+            if (typeof value.length === 'number' &&
+                    !(value.propertyIsEnumerable('length')) &&
+                    typeof value.splice === 'function') {
+                s = 'array';
+            }
+        } else {
+            s = 'null';
+        }
+    }
+    return s;
+}
+
+
+function isEmpty(o) {
+    var i, v;
+    if (typeOf(o) === 'object') {
+        for (i in o) {
+            v = o[i];
+            if (v !== undefined && typeOf(v) !== 'function') {
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+String.prototype.entityify = function () {
+    return this.replace(/&/g, "&amp;").replace(/</g,
+        "&lt;").replace(/>/g, "&gt;");
+};
+
+String.prototype.quote = function () {
+    var c, i, l = this.length, o = '"';
+    for (i = 0; i < l; i += 1) {
+        c = this.charAt(i);
+        if (c >= ' ') {
+            if (c === '\\' || c === '"') {
+                o += '\\';
+            }
+            o += c;
+        } else {
+            switch (c) {
+            case '\b':
+                o += '\\b';
+                break;
+            case '\f':
+                o += '\\f';
+                break;
+            case '\n':
+                o += '\\n';
+                break;
+            case '\r':
+                o += '\\r';
+                break;
+            case '\t':
+                o += '\\t';
+                break;
+            default:
+                c = c.charCodeAt();
+                o += '\\u00' + Math.floor(c / 16).toString(16) +
+                    (c % 16).toString(16);
+            }
+        }
+    }
+    return o + '"';
+};
+
+String.prototype.supplant = function (o) {
+    return this.replace(/{([^{}]*)}/g,
+        function (a, b) {
+            var r = o[b];
+            return typeof r === 'string' || typeof r === 'number' ? r : a;
+        }
+    );
+};
+
+String.prototype.trim = function () {
+    return this.replace(/^\s+|\s+$/g, "");
+};
--- a/tests/test_engine_runner.js	Tue May 13 22:56:06 2008 -0700
+++ b/tests/test_engine_runner.js	Thu May 15 00:03:40 2008 -0700
@@ -1,8 +1,10 @@
+load("remedial.js");
 load("gnusto-engine.min.js");
 load("engine-runner.js");
 
 function FakeEngine() {
     this._step = 0;
+    this.m_memory = new Array(5000);
     var self = this;
 
     this.__proto__ = {
@@ -47,6 +49,10 @@
     var self = this;
 
     this.__proto__ = {
+        getSize: function() {
+            return [60, 255];
+        },
+
         onQuit: function() {
             print("onQuit() received");
         },
--- a/trivial-zui.js	Tue May 13 22:56:06 2008 -0700
+++ b/trivial-zui.js	Thu May 15 00:03:40 2008 -0700
@@ -2,6 +2,11 @@
 var RETURN_KEYCODE = 13;
 
 function TrivialZui() {
+    var contentLeft = $("#content").get(0).offsetLeft + "px";
+    $("#top-window").get(0).style.left = contentLeft;
+
+    this._size = [70, 255];
+    this._console = null;
     this._activeWindow = 0;
     this._inputString = "";
     this._currentCallback = null;
@@ -80,8 +85,12 @@
             }
         },
 
+        _eraseBottomWindow: function() {
+            $("#content").html("");
+        },
+
         getSize: function() {
-            return [60, 255];
+            return self._size;
         },
 
         onLineInput: function(callback) {
@@ -102,27 +111,52 @@
         },
 
         onSetWindow: function(window) {
+            // From the Z-Spec, section 8.7.2.
+            if (window == 1)
+                self._console.moveTo(0, 0);
+
             self._activeWindow = window;
         },
 
         onEraseWindow: function(window) {
-            if (window <= 0) {
-                // TODO: Implement this better.
-                self.onPrint("\n\n\n\n\n\n\n\n\n\n");
+            if (window == -2) {
+                self._console.clear();
+                self._eraseBottomWindow();
+            } else if (window == -1) {
+                // From the Z-Spec, section 8.7.3.3.
+                self.onSplitWindow(0);
+
+                // TODO: Depending on the Z-Machine version, we want
+                // to move the cursor to the bottom-left or top-left.
+                self._eraseBottomWindow();
+            } else if (window == 0) {
+                self._eraseBottomWindow();
+            } else if (window == 1) {
+                self._console.clear();
             }
         },
 
         onSetCursor: function(x, y) {
+            self._console.moveTo(x - 1, y - 1);
         },
 
-        onSplitWindow: function(window) {
+        onSplitWindow: function(numlines) {
+            if (numlines == 0) {
+                self._console.close();
+                self._console = null;
+            } else
+                self._console = new Console(self._size[0],
+                                            numlines,
+                                            $("#top-window").get(0));
         },
 
         onPrint: function(output) {
-            output = output.replace('\n', '<br/>', 'g');
             if (self._activeWindow == 0) {
+                output = output.replace('\n', '<br/>', 'g');
                 $("#content").append(output);
                 window.scroll(0, document.body.scrollHeight);
+            } else {
+                self._console.write(output);
             }
         }
     };
@@ -140,6 +174,8 @@
 
     $(window).keyup(suppressionFunc);
     $(window).keydown(suppressionFunc);
+
+    self._eraseBottomWindow();
 }
 
 $(document).ready( function() {