changeset 54:7dc2c7bded08

Added implementation of the restart opcode.
author Atul Varma <varmaa@toolness.com>
date Fri, 16 May 2008 11:11:17 -0700
parents 0abb2dbc2dd1
children d9fa2605b044
files console.js engine-runner.js web-zui.js
diffstat 3 files changed, 60 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/console.js	Fri May 16 10:07:38 2008 -0700
+++ b/console.js	Fri May 16 11:11:17 2008 -0700
@@ -76,5 +76,6 @@
 
   close: function() {
     this._element.innerHTML = "";
+    this._observer.onConsoleRender();
   }
 }
--- a/engine-runner.js	Fri May 16 10:07:38 2008 -0700
+++ b/engine-runner.js	Fri May 16 11:11:17 2008 -0700
@@ -171,6 +171,9 @@
         self._zui.onQuit();
         break;
       case GNUSTO_EFFECT_RESTART:
+        self.stop();
+        self._zui.onRestart();
+        break;
       case GNUSTO_EFFECT_WIMP_OUT:
       case GNUSTO_EFFECT_BREAKPOINT:
       case GNUSTO_EFFECT_FLAGS_CHANGED:
--- a/web-zui.js	Fri May 16 10:07:38 2008 -0700
+++ b/web-zui.js	Fri May 16 11:11:17 2008 -0700
@@ -16,6 +16,44 @@
       $("#content").get(0).style.padding = "" + height + "px 0 0 0";
     },
 
+    _finalize: function() {
+      if (self._console) {
+        self._console.close();
+        self._console = null;
+      }
+      $("#content").html("");
+      self._unbindEventHandlers();
+    },
+
+    _bindEventHandlers: function() {
+      $(window).keypress(self._windowKeypress);
+      $(window).resize(self._windowResize);
+      $(window).keyup(self._windowKeyup);
+      $(window).keydown(self._windowKeydown);
+    },
+
+    _unbindEventHandlers: function() {
+      $(window).unbind("keypress", self._windowKeypress);
+      $(window).unbind("resize", self._windowResize);
+      $(window).unbind("keyup", self._windowKeyup);
+      $(window).unbind("keydown", self._windowKeydown);
+    },
+
+    // We want to make sure that all key events don't bubble up, so
+    // that anything listening in--such as Firefox's "Search for text
+    // when I start typing" feature--doesn't think that we're not
+    // doing anything with the keypresses.  If we don't do this, such
+    // listeners may think that they can intervene and capture
+    // keystrokes before they get to us in the future.
+
+    _windowKeyup: function(event) {
+      return false;
+    },
+
+    _windowKeydown: function(event) {
+      return false;
+    },
+
     _windowKeypress: function(event) {
       if ($("#current-input").length == 0) {
         // We're not waiting for a line of input, but we may
@@ -111,6 +149,18 @@
     },
 
     onQuit: function() {
+      self._finalize();
+    },
+
+    onRestart: function() {
+      self._finalize();
+
+      // TODO: It's not high-priority, but according to the Z-Machine
+      // spec documentation for the restart opcode, we need to
+      // preserve the "transcribing to printer" bit and the "use
+      // fixed-pitch font" bit when restarting.
+
+      window.setTimeout(_webZuiStartup, 0);
     },
 
     onSetStyle: function(textStyle, foreground, background) {
@@ -209,26 +259,11 @@
   };
 
   self._windowResize();
-
-  $(window).keypress(self._windowKeypress);
-  $(window).resize(self._windowResize);
-
-  // We want to make sure that all key events don't bubble up, so
-  // that anything listening in--such as Firefox's "Search for text
-  // when I start typing" feature--doesn't think that we're not
-  // doing anything with the keypresses.  If we don't do this, such
-  // listeners may think that they can intervene and capture
-  // keystrokes before they get to us in the future.
-
-  var suppressionFunc = function(evt) { return false; };
-
-  $(window).keyup(suppressionFunc);
-  $(window).keydown(suppressionFunc);
-
+  self._bindEventHandlers();
   self._eraseBottomWindow();
 }
 
-$(document).ready(function() {
+function _webZuiStartup() {
   var logfunc = undefined;
 
   if (window.console) {
@@ -239,6 +274,8 @@
   var zui = new WebZui();
   var runner = new EngineRunner(engine, zui, logfunc);
 
-  engine.loadStory(troll_z5);
+  engine.loadStory(troll_z5.slice());
   runner.run();
-});
+}
+
+$(document).ready(_webZuiStartup);