Mercurial > web-gnusto
changeset 43:e45b81cdb4d0
Modified indentation to be 2 spaces instead of 4, as per Mozilla's JS style guidelines.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 15 May 2008 23:42:23 -0700 |
parents | 768813231e54 |
children | c628beacc40d |
files | engine-runner.js trivial-zui.js |
diffstat | 2 files changed, 326 insertions(+), 326 deletions(-) [+] |
line wrap: on
line diff
--- a/engine-runner.js Thu May 15 03:05:32 2008 -0700 +++ b/engine-runner.js Thu May 15 23:42:23 2008 -0700 @@ -3,196 +3,196 @@ Zui.prototype = { - // Returns a 2-element list containing the width and height of the - // screen, in characters. The width may be 255, which means - // "infinite". + // Returns a 2-element list containing the width and height of the + // screen, in characters. The width may be 255, which means + // "infinite". - getSize: function() { - }, - onLineInput: function(callback) { - }, - onCharacterInput: function(callback) { - }, - onSave: function(callback) { - }, - onRestore: function(callback) { - }, - onQuit: function() { - }, - onRestart: function(callback) { - }, - onWimpOut: function(callback) { - }, - onBreakpoint: function(callback) { - }, - onSetStyle: function(textStyle, foreground, background) { - }, + getSize: function() { + }, + onLineInput: function(callback) { + }, + onCharacterInput: function(callback) { + }, + onSave: function(callback) { + }, + onRestore: function(callback) { + }, + onQuit: function() { + }, + onRestart: function(callback) { + }, + onWimpOut: function(callback) { + }, + onBreakpoint: function(callback) { + }, + 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. + // 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) { - }, + 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. + // 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. - onEraseWindow: function(window) { - }, - onEraseLine: function() { - }, - onSetCursor: function(x, y) { - }, - onSetBufferMode: function() { - }, - onSetInputStream: function() { - }, - onGetCursor: function() { - }, - onPrint: function(output) { - }, - onPrintTable: function(lines) { - }, + onEraseWindow: function(window) { + }, + onEraseLine: function() { + }, + onSetCursor: function(x, y) { + }, + onSetBufferMode: function() { + }, + onSetInputStream: function() { + }, + onGetCursor: function() { + }, + onPrint: function(output) { + }, + onPrintTable: function(lines) { + }, }; function EngineRunner(engine, zui, logfunc) { - this._engine = engine; - this._zui = zui; - this._isRunning = false; - this._isInLoop = false; - this._isWaitingForCallback = false; + this._engine = engine; + this._zui = zui; + this._isRunning = false; + this._isInLoop = false; + this._isWaitingForCallback = false; - if (logfunc) { - this._log = logfunc; - } else { - this._log = function() {}; - } + if (logfunc) { + this._log = logfunc; + } else { + this._log = function() {}; + } - var self = this; + var self = this; - this.__proto__ = { - stop: function() { - self._isRunning = false; - }, + this.__proto__ = { + stop: function() { + self._isRunning = false; + }, - run: function() { - var size = self._zui.getSize(); + run: function() { + var size = self._zui.getSize(); - self._isRunning = true; - self._engine.m_memory[0x20] = size[1]; - self._engine.m_memory[0x21] = size[0]; - self._continueRunning(); - }, + self._isRunning = true; + self._engine.m_memory[0x20] = size[1]; + self._engine.m_memory[0x21] = size[0]; + self._continueRunning(); + }, - _continueRunning: function() { - while (self._isRunning && !self._isWaitingForCallback) { - self._loop(); - } - }, + _continueRunning: function() { + while (self._isRunning && !self._isWaitingForCallback) { + self._loop(); + } + }, - _receiveLineInput: function(input) { - self._isWaitingForCallback = false; - self._engine.answer(1, input); - if (!self._isInLoop) { - self._continueRunning(); - } else { - /* We're still inside _loop(), so just return. */ - } - }, + _receiveLineInput: function(input) { + self._isWaitingForCallback = false; + self._engine.answer(1, input); + if (!self._isInLoop) { + self._continueRunning(); + } else { + /* We're still inside _loop(), so just return. */ + } + }, - _receiveCharacterInput: function(input) { - self._isWaitingForCallback = false; - self._engine.answer(0, input); - if (!self._isInLoop) { - self._continueRunning(); - } else { - /* We're still inside _loop(), so just return. */ - } - }, + _receiveCharacterInput: function(input) { + self._isWaitingForCallback = false; + self._engine.answer(0, input); + if (!self._isInLoop) { + self._continueRunning(); + } else { + /* We're still inside _loop(), so just return. */ + } + }, - _loop: function() { - if (self._isInLoop) - throw Error("Already in loop!"); + _loop: function() { + if (self._isInLoop) + throw Error("Already in loop!"); - self._isInLoop = true; - var engine = self._engine; + self._isInLoop = true; + var engine = self._engine; - engine.run(); + engine.run(); - var text = engine.consoleText(); - if (text) - self._zui.onPrint(text); + var text = engine.consoleText(); + if (text) + self._zui.onPrint(text); - var effect = '"' + engine.effect(0) + '"'; + var effect = '"' + engine.effect(0) + '"'; - var logString = "[ " + engine.effect(0); + var logString = "[ " + engine.effect(0); - for (var i = 1; engine.effect(i) != undefined; i++) { - var value = engine.effect(i); - if (typeof value == "string") - value = value.quote(); - logString += ", " + value; - } + for (var i = 1; engine.effect(i) != undefined; i++) { + var value = engine.effect(i); + if (typeof value == "string") + value = value.quote(); + logString += ", " + value; + } - self._log(logString + " ]"); + self._log(logString + " ]"); - switch (effect) { - case GNUSTO_EFFECT_INPUT: - self._isWaitingForCallback = true; - self._zui.onLineInput(self._receiveLineInput); - break; - case GNUSTO_EFFECT_INPUT_CHAR: - self._isWaitingForCallback = true; - self._zui.onCharacterInput(self._receiveCharacterInput); - break; - case GNUSTO_EFFECT_SAVE: - case GNUSTO_EFFECT_RESTORE: - throw Error("Unimplemented effect: " + effect); - case GNUSTO_EFFECT_QUIT: - self.stop(); - self._zui.onQuit(); - break; - case GNUSTO_EFFECT_RESTART: - case GNUSTO_EFFECT_WIMP_OUT: - case GNUSTO_EFFECT_BREAKPOINT: - case GNUSTO_EFFECT_FLAGS_CHANGED: - case GNUSTO_EFFECT_PIRACY: - throw Error("Unimplemented effect: " + effect); - case GNUSTO_EFFECT_STYLE: - self._zui.onSetStyle(engine.effect(1), - engine.effect(2), - engine.effect(3)); - break; - case GNUSTO_EFFECT_SOUND: - throw Error("Unimplemented effect: " + effect); - case GNUSTO_EFFECT_SPLITWINDOW: - self._zui.onSplitWindow(engine.effect(1)); - break; - case GNUSTO_EFFECT_SETWINDOW: - self._zui.onSetWindow(engine.effect(1)); - break; - case GNUSTO_EFFECT_ERASEWINDOW: - self._zui.onEraseWindow(engine.effect(1)); - break; - case GNUSTO_EFFECT_ERASELINE: - throw Error("Unimplemented effect: " + effect); - case GNUSTO_EFFECT_SETCURSOR: - self._zui.onSetCursor(engine.effect(2), - engine.effect(1)); - break; - case GNUSTO_EFFECT_SETBUFFERMODE: - case GNUSTO_EFFECT_SETINPUTSTREAM: - case GNUSTO_EFFECT_GETCURSOR: - case GNUSTO_EFFECT_PRINTTABLE: - throw Error("Unimplemented effect: " + effect); - } + switch (effect) { + case GNUSTO_EFFECT_INPUT: + self._isWaitingForCallback = true; + self._zui.onLineInput(self._receiveLineInput); + break; + case GNUSTO_EFFECT_INPUT_CHAR: + self._isWaitingForCallback = true; + self._zui.onCharacterInput(self._receiveCharacterInput); + break; + case GNUSTO_EFFECT_SAVE: + case GNUSTO_EFFECT_RESTORE: + throw Error("Unimplemented effect: " + effect); + case GNUSTO_EFFECT_QUIT: + self.stop(); + self._zui.onQuit(); + break; + case GNUSTO_EFFECT_RESTART: + case GNUSTO_EFFECT_WIMP_OUT: + case GNUSTO_EFFECT_BREAKPOINT: + case GNUSTO_EFFECT_FLAGS_CHANGED: + case GNUSTO_EFFECT_PIRACY: + throw Error("Unimplemented effect: " + effect); + case GNUSTO_EFFECT_STYLE: + self._zui.onSetStyle(engine.effect(1), + engine.effect(2), + engine.effect(3)); + break; + case GNUSTO_EFFECT_SOUND: + throw Error("Unimplemented effect: " + effect); + case GNUSTO_EFFECT_SPLITWINDOW: + self._zui.onSplitWindow(engine.effect(1)); + break; + case GNUSTO_EFFECT_SETWINDOW: + self._zui.onSetWindow(engine.effect(1)); + break; + case GNUSTO_EFFECT_ERASEWINDOW: + self._zui.onEraseWindow(engine.effect(1)); + break; + case GNUSTO_EFFECT_ERASELINE: + throw Error("Unimplemented effect: " + effect); + case GNUSTO_EFFECT_SETCURSOR: + self._zui.onSetCursor(engine.effect(2), + engine.effect(1)); + break; + case GNUSTO_EFFECT_SETBUFFERMODE: + case GNUSTO_EFFECT_SETINPUTSTREAM: + case GNUSTO_EFFECT_GETCURSOR: + case GNUSTO_EFFECT_PRINTTABLE: + throw Error("Unimplemented effect: " + effect); + } - self._isInLoop = false; - } - }; + self._isInLoop = false; + } + }; }
--- a/trivial-zui.js Thu May 15 03:05:32 2008 -0700 +++ b/trivial-zui.js Thu May 15 23:42:23 2008 -0700 @@ -2,194 +2,194 @@ var RETURN_KEYCODE = 13; function TrivialZui() { - var contentLeft = $("#content").get(0).offsetLeft + "px"; - $("#top-window").get(0).style.left = contentLeft; + var contentLeft = $("#content").get(0).offsetLeft + "px"; + $("#top-window").get(0).style.left = contentLeft; - this._size = [80, 255]; - this._console = null; - this._activeWindow = 0; - this._inputString = ""; - this._currentCallback = null; - var self = this; + this._size = [80, 255]; + this._console = null; + this._activeWindow = 0; + this._inputString = ""; + this._currentCallback = null; + var self = this; - this.__proto__ = { - _windowKeypress: function(event) { - if ($("#current-input").length == 0) { - // We're not waiting for a line of input, but we may - // be waiting for a character of input. + this.__proto__ = { + _windowKeypress: function(event) { + if ($("#current-input").length == 0) { + // We're not waiting for a line of input, but we may + // be waiting for a character of input. - // Note that we have to return a ZSCII keycode here. - // - // For more information, see: - // - // http://www.gnelson.demon.co.uk/zspec/sect03.html - if (self._currentCallback) { - var keyCode = 0; - if (event.charCode) { - keyCode = event.charCode; - } else { - // TODO: Deal w/ arrow keys, etc. - switch (event.keyCode) { - case RETURN_KEYCODE: - keyCode = event.keyCode; - break; - } - } - if (keyCode != 0) { - var callback = self._currentCallback; + // Note that we have to return a ZSCII keycode here. + // + // For more information, see: + // + // http://www.gnelson.demon.co.uk/zspec/sect03.html + if (self._currentCallback) { + var keyCode = 0; + if (event.charCode) { + keyCode = event.charCode; + } else { + // TODO: Deal w/ arrow keys, etc. + switch (event.keyCode) { + case RETURN_KEYCODE: + keyCode = event.keyCode; + break; + } + } + if (keyCode != 0) { + var callback = self._currentCallback; - self._currentCallback = null; - callback(keyCode); - } - } - return false; - } + self._currentCallback = null; + callback(keyCode); + } + } + return false; + } - var oldInputString = self._inputString; + var oldInputString = self._inputString; - if (event.charCode) { - var newChar = String.fromCharCode(event.charCode); - var lastChar = self._inputString.charAt( - self._inputString.length - 1 - ); - if (!(newChar == " " && lastChar == " ")) { - self._inputString += newChar; - } - } else { - switch (event.keyCode) { - case BACKSPACE_KEYCODE: - if (self._inputString) { - self._inputString = self._inputString.slice(0, -1); - } - break; - case RETURN_KEYCODE: - var finalInputString = self._inputString; - var callback = self._currentCallback; + if (event.charCode) { + var newChar = String.fromCharCode(event.charCode); + var lastChar = self._inputString.charAt( + self._inputString.length - 1 + ); + if (!(newChar == " " && lastChar == " ")) { + self._inputString += newChar; + } + } else { + switch (event.keyCode) { + case BACKSPACE_KEYCODE: + if (self._inputString) { + self._inputString = self._inputString.slice(0, -1); + } + break; + case RETURN_KEYCODE: + var finalInputString = self._inputString; + var callback = self._currentCallback; - self._inputString = ""; - self._currentCallback = null; - finalInputString = finalInputString.entityify(); - $("#current-input").replaceWith( - ('<span class="finished-input">' + finalInputString + - '</span><br/>') - ); - callback(finalInputString); - } - } - if ($("#current-input") && - oldInputString != self._inputString) { - $("#current-input").html( - self._inputString.entityify() + - '<span id="cursor">_</span>' - ); - } - }, + self._inputString = ""; + self._currentCallback = null; + finalInputString = finalInputString.entityify(); + $("#current-input").replaceWith( + ('<span class="finished-input">' + finalInputString + + '</span><br/>') + ); + callback(finalInputString); + } + } + if ($("#current-input") && + oldInputString != self._inputString) { + $("#current-input").html( + self._inputString.entityify() + + '<span id="cursor">_</span>' + ); + } + }, - _eraseBottomWindow: function() { - $("#content").html(""); - }, + _eraseBottomWindow: function() { + $("#content").html(""); + }, - getSize: function() { - return self._size; - }, + getSize: function() { + return self._size; + }, - onLineInput: function(callback) { - self._currentCallback = callback; - $("#content").append( - '<span id="current-input"><span id="cursor">_</span></span>' - ); - }, + onLineInput: function(callback) { + self._currentCallback = callback; + $("#content").append( + '<span id="current-input"><span id="cursor">_</span></span>' + ); + }, - onCharacterInput: function(callback) { - self._currentCallback = callback; - }, + onCharacterInput: function(callback) { + self._currentCallback = callback; + }, - onQuit: function() { - }, + onQuit: function() { + }, - onSetStyle: function(textStyle, foreground, background) { - }, + onSetStyle: function(textStyle, foreground, background) { + }, - onSetWindow: function(window) { - // From the Z-Spec, section 8.7.2. - if (window == 1) - self._console.moveTo(0, 0); + onSetWindow: function(window) { + // From the Z-Spec, section 8.7.2. + if (window == 1) + self._console.moveTo(0, 0); - self._activeWindow = window; - }, + self._activeWindow = window; + }, - onEraseWindow: function(window) { - if (window == -2) { - self._console.clear(); - self._eraseBottomWindow(); - } else if (window == -1) { - // From the Z-Spec, section 8.7.3.3. - self.onSplitWindow(0); + onEraseWindow: function(window) { + 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(); - } - }, + // 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); - }, + onSetCursor: function(x, y) { + self._console.moveTo(x - 1, y - 1); + }, - 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)); - }, + 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) { - if (self._activeWindow == 0) { - output = output.entityify(); - output = output.replace('\n', '<br/>', 'g'); - $("#content").append(output); - window.scroll(0, document.body.scrollHeight); - } else { - self._console.write(output); - } - } - }; + onPrint: function(output) { + if (self._activeWindow == 0) { + output = output.entityify(); + output = output.replace('\n', '<br/>', 'g'); + $("#content").append(output); + window.scroll(0, document.body.scrollHeight); + } else { + self._console.write(output); + } + } + }; - $(window).keypress(self._windowKeypress); + $(window).keypress(self._windowKeypress); - // 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. + // 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; }; + var suppressionFunc = function(evt) { return false; }; - $(window).keyup(suppressionFunc); - $(window).keydown(suppressionFunc); + $(window).keyup(suppressionFunc); + $(window).keydown(suppressionFunc); - self._eraseBottomWindow(); + self._eraseBottomWindow(); } -$(document).ready( function() { - var logfunc = undefined; +$(document).ready(function() { + var logfunc = undefined; - if (window.console) { - logfunc = function(msg) { console.log(msg); }; - } + if (window.console) { + logfunc = function(msg) { console.log(msg); }; + } - var engine = new GnustoEngine(); - var zui = new TrivialZui(); - var runner = new EngineRunner(engine, zui, logfunc); + var engine = new GnustoEngine(); + var zui = new TrivialZui(); + var runner = new EngineRunner(engine, zui, logfunc); - engine.loadStory(troll_z5); - runner.run(); + engine.loadStory(troll_z5); + runner.run(); });