/* * Parchment Z-Machine UI and Runner * Built: 2010-10-18 * * Copyright (c) 2008-2010 The Parchment Contributors * Licenced under the GPL v2 * http://code.google.com/p/parchment */ (function(a){parchment.lib.ZUI=Object.subClass({init:function(d,e,f){var c=this,b=(gIsIphone&&a(document.body).width()<=480)?38:80;d.container.html('
');a.extend(c,{_size:[b,25],_console:null,_activeWindow:0,_currentCallback:null,_foreground:"default",_background:"default",_reverseVideo:false,_lastSeenY:0,_currStyles:["z-roman"],_expectedHash:window.location.hash,_isFixedWidth:false,_bufferMode:0,library:d,engine:e,hidden_load_indicator:0,bottom:a("#bottom"),current_input:a("#current-input"),text_input:new parchment.lib.TextInput("#parchment","#content"),_log:f||a.noop,_windowHashCheck:function(){if(window.location.hash!=c._expectedHash){c._restart()}}});c._setFixedPitchSizes();a("#top-window").css({width:c._pixelWidth+"px",lineHeight:c._pixelLineHeight+"px"});a("#content").css({width:c._pixelWidth+"px"});c._windowResize();c._bindEventHandlers();c._eraseBottomWindow()},onConsoleRender:function(){var b=a("#top-window").height();a("#content").css({padding:""+b+"px 0 0 0"})},_finalize:function(){var b=this;if(b._console){b._console.close();b._console=null}b.onPrint("\n[ The game has finished. ]");b._unbindEventHandlers()},_bindEventHandlers:function(){var b=this;a(window).resize(b._windowResize);b._intervalId=window.setInterval(b._windowHashCheck,1000)},_unbindEventHandlers:function(){var b=this;a(window).unbind("resize",b._windowResize);window.clearInterval(b._intervalId);this.text_input.die()},_windowResize:function(){var b=a("#content").offset().left+"px";a(".buffered-window").css({left:b})},_removeBufferedWindows:function(){var b=a("#buffered-windows > .buffered-window");b.fadeOut("slow",function(){b.remove()});if(!this.hidden_load_indicator){this.hidden_load_indicator=1;this.library.load_indicator.detach()}},_eraseBottomWindow:function(){a("#content").empty();this._lastSeenY=0},_restart:function(){this._finalize();location.reload()},setVersion:function(b){this._version=b},getSize:function(){return this._size},onLineInput:function(e){var b=this;if(b.engine.m_version<=3){var c=b._activeWindow;var d=b._reverseVideo;if(!b._console){b.onSplitWindow(1)}b._console.moveTo(0,0);b._activeWindow=1;b._reverseVideo=true;b.onPrint(b.engine.getStatusLine(b._console.width));b._reverseVideo=d;b._activeWindow=c}if(!b.hidden_load_indicator){b.hidden_load_indicator=1;b.library.load_indicator.detach()}b._currentCallback=e;b.text_input.getLine(e,b._calcFinalStyles())},onCharacterInput:function(c){var b=this;b._currentCallback=c;if(!b.hidden_load_indicator){b.hidden_load_indicator=1;b.library.load_indicator.detach()}b.text_input.getChar(c)},onSave:function(e){var b=this,d=this.library.url+"_saveData",c=file.base64_encode(e);if(window.globalStorage&&location.href.slice(0,5)!="file:"){window.globalStorage[location.hostname][d]=c}location=location.protocol+"//"+location.host+location.pathname+location.search+"#"+c;b._expectedHash=location.hash;b.onPrint("Your game has been saved to the URL. You may want to bookmark this page now; just reload it at any time to restore your game from this point.\n");return true},onRestore:function(){var c=null;if(location.hash){c=location.hash.slice(1)}if(!c&&window.globalStorage){var b=globalStorage[location.hostname][this.library.url+"_saveData"];if(b){c=b.value;location=location.protocol+"//"+location.host+location.pathname+location.search+"#"+c;this._expectedHash=location.hash}}if(c){return file.base64_decode(c)}else{return null}},onQuit:function(){this._finalize()},onRestart:function(){var b=this;b._finalize();window.location.hash="";b._restart()},onWimpOut:function(b){window.setTimeout(b,50)},onFlagsChanged:function(b,c){if(b){throw new FatalError("To transcript not yet implemented!")}this._isFixedWidth=c},onSetStyle:function(e,d,c){switch(e){case -1:break;case 0:this._currStyles=["z-roman"];this._reverseVideo=false;break;case 1:this._reverseVideo=true;break;case 2:this._currStyles.push("z-bold");break;case 4:this._currStyles.push("z-italic");break;case 8:this._currStyles.push("z-fixed-pitch");break;default:throw new FatalError("Unknown style: "+e)}var b={0:null,1:"default",2:"black",3:"red",4:"green",5:"yellow",6:"blue",7:"magenta",8:"cyan",9:"white"};if(b[d]){this._foreground=b[d]}if(b[c]){this._background=b[c]}},onSetWindow:function(c){var b=this;if(c==1){if(!b._console){b.onSplitWindow(1)}b._console.moveTo(0,0)}b._activeWindow=c},onEraseWindow:function(c){var b=this;document.body.className="bg-"+b._background;if(c==-2){b._console.clear();b._eraseBottomWindow()}else{if(c==-1){b.onSplitWindow(0);b._eraseBottomWindow()}else{if(c==0){b._eraseBottomWindow()}else{if(c==1&&b._console){b._console.clear()}}}}},onSetCursor:function(b,d){var c=this;if(c._console){c._console.moveTo(b-1,d-1)}},onSetBufferMode:function(b){this._bufferMode=b},onSplitWindow:function(d){var b=this;if(d==0){if(b._console){b._console.close();b._console=null}}else{if(!b._console||b._version==3||!b._bufferMode){b._console=new Console(b._size[0],d,a("#top-window").get(0),b)}else{if(b._console.height!=d){var c=document.createElement("div");c.className="buffered-window";c.innerHTML=b._console.renderHtml();a(c).css({width:b._pixelWidth+"px",lineHeight:b._pixelLineHeight+"px"});a("#buffered-windows").append(c);b._windowResize();b._console.resize(d)}}}b._bufferMode=0},_calcFinalStyles:function(){var d=this,c=d._foreground,e=d._background;if(d._reverseVideo){c=d._background;e=d._foreground;if(c=="default"){c="default-reversed"}if(e=="default"){e="default-reversed"}}var b=["fg-"+c,"bg-"+e];if(d._isFixedWidth){b.push("z-fixed-pitch")}return b.concat(d._currStyles).join(" ")},onPrint:function(c){var h=this,g=h._calcFinalStyles();h._log("print wind: "+h._activeWindow+" output: "+c.quote()+" style: "+g);if(h._activeWindow==0){var k=c.split("\n");for(var d=0;d