gui.min.js 15 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var GUI=function(){var a=this,b=[],d=[],f=true,m,k,h=0;a=this;var n=false,i=280,o=false,g,p,e=0,s;this.domElement=document.createElement("div");this.domElement.setAttribute("class","guidat");this.domElement.style.width=i+"px";var q=document.createElement("div");q.setAttribute("class","guidat-controllers");q.addEventListener("DOMMouseScroll",function(c){var j=this.scrollTop;if(c.wheelDelta)j+=c.wheelDelta;else if(c.detail)j+=c.detail;c.preventDefault&&c.preventDefault();c.returnValue=false;q.scrollTop=
  2. j},false);q.style.height="0px";var r=document.createElement("a");r.setAttribute("class","guidat-toggle");r.setAttribute("href","#");r.innerHTML="Show Controls";var u=false,C=0,v=false,w,y,x,z,D=function(c){y=w;z=x;w=c.pageY;x=c.pageX;c=w-y;if(!n)if(c>0){n=true;h=g=1;r.innerHTML=p||"Hide Controls"}else return;var j=z-x;if(c>0&&h>k){var l=GUI.map(h,k,k+100,1,0);c*=l}u=true;C+=c;dragDisplacementX+=j;g+=c;i+=j;h+=c;q.style.height=g+"px";i=GUI.constrain(i,240,500);a.domElement.style.width=i+"px";A()};
  3. r.addEventListener("mousedown",function(c){y=w=c.pageY;z=x=c.pageX;v=true;c.preventDefault();dragDisplacementX=C=0;document.addEventListener("mousemove",D,false);return false},false);r.addEventListener("click",function(c){c.preventDefault();return false},false);document.addEventListener("mouseup",function(c){v&&!u&&a.toggle();if(v&&u){dragDisplacementX==0&&E();if(g>k){clearTimeout(s);g=e=k;t()}else if(q.children.length>=1){var j=q.children[0].offsetHeight;clearTimeout(s);e=Math.round(h/j)*j-1;if(e<=
  4. 0){a.hide();g=j*2}else{g=e;t()}}}document.removeEventListener("mousemove",D,false);c.preventDefault();return v=u=false},false);this.domElement.appendChild(q);this.domElement.appendChild(r);if(GUI.autoPlace){if(GUI.autoPlaceContainer==null){GUI.autoPlaceContainer=document.createElement("div");GUI.autoPlaceContainer.setAttribute("id","guidat");document.body.appendChild(GUI.autoPlaceContainer)}GUI.autoPlaceContainer.appendChild(this.domElement)}this.autoListenIntervalTime=1E3/60;var F=function(){m=setInterval(function(){a.listen()},
  5. this.autoListenIntervalTime)};this.__defineSetter__("autoListen",function(c){if(f=c)d.length>0&&F();else clearInterval(m)});this.__defineGetter__("autoListen",function(){return f});this.listenTo=function(c){d.length==0&&F();d.push(c)};this.unlistenTo=function(c){for(var j=0;j<d.length;j++)d[j]==c&&d.splice(j,1);d.length<=0&&clearInterval(m)};this.listen=function(c){c=c||d;for(var j in c)c[j].updateDisplay()};this.listenAll=function(){this.listen(b)};this.autoListen=true;var G=function(c,j){for(var l in b)if(b[l].object==
  6. c&&b[l].propertyName==j)return true;return false},H=function(c,j){function l(){return c.apply(this,j)}l.prototype=c.prototype;return new l};this.add=function(){var c=arguments[0],j=arguments[1];G(c,j);var l=c[j];if(l==undefined)GUI.error(c+' either has no property "'+j+'", or the property is inaccessible.');else{c=typeof l;l=J[c];if(l==undefined)GUI.error('Cannot create controller for data type "'+c+'"');else{for(var I=[this],B=0;B<arguments.length;B++)I.push(arguments[B]);if(l=H(l,I)){q.appendChild(l.domElement);
  7. b.push(l);GUI.allControllers.push(l);if(c!="function"&&GUI.saveIndex<GUI.savedValues.length){l.setValue(GUI.savedValues[GUI.saveIndex]);GUI.saveIndex++}A();o||(g=k);return l}else GUI.error('Error creating controller for "'+j+'".')}}};var A=function(){k=0;for(var c in b)k+=b[c].domElement.offsetHeight;q.style.overflowY=k-1>g?"auto":"hidden"},J={number:GUI.NumberController,string:GUI.StringController,"boolean":GUI.BooleanController,"function":GUI.FunctionController};G=function(c,j){for(var l in b)if(b[l].object==
  8. c&&b[l].propertyName==j)return true;return false};H=function(c,j){function l(){return c.apply(this,j)}l.prototype=c.prototype;return new l};this.reset=function(){};this.toggle=function(){n?this.hide():this.show()};this.show=function(){r.innerHTML=p||"Hide Controls";e=g;clearTimeout(s);t();n=true};this.hide=function(){r.innerHTML=p||"Show Controls";e=0;clearTimeout(s);t();n=false};this.name=function(c){p=c;r.innerHTML=c};this.appearanceVars=function(){return[n,i,g,q.scrollTop]};var t=function(){h+=
  9. (e-h)*0.6;if(Math.abs(h-e)<1){h=e;E()}else s=setTimeout(t,1E3/30);q.style.height=Math.round(h)+"px";A()},E=function(){a.domElement.style.width=i+1+"px";setTimeout(function(){a.domElement.style.width=i+"px"},1)};if(GUI.guiIndex<GUI.savedAppearanceVars.length){i=parseInt(GUI.savedAppearanceVars[GUI.guiIndex][1]);a.domElement.style.width=i+"px";g=parseInt(GUI.savedAppearanceVars[GUI.guiIndex][2]);o=true;if(eval(GUI.savedAppearanceVars[GUI.guiIndex][0])==true){h=g;var K=GUI.savedAppearanceVars[GUI.guiIndex][3];
  10. setTimeout(function(){q.scrollTop=K},0);if(GUI.scrollTop>-1)document.body.scrollTop=GUI.scrollTop;e=g;this.show()}GUI.guiIndex++}GUI.allGuis.push(this);GUI.allGuis.length==1&&window.addEventListener("keyup",function(c){c.keyCode==72&&GUI.toggleHide()},false)};GUI.hidden=false;GUI.autoPlace=true;GUI.autoPlaceContainer=null;GUI.allControllers=[];GUI.allGuis=[];GUI.toggleHide=function(){GUI.hidden?GUI.show():GUI.hide()};
  11. GUI.show=function(){GUI.hidden=false;for(var a in GUI.allGuis)GUI.allGuis[a].domElement.style.display="block"};GUI.hide=function(){GUI.hidden=true;for(var a in GUI.allGuis)GUI.allGuis[a].domElement.style.display="none"};GUI.saveURL=function(){var a=GUI.replaceGetVar("saveString",GUI.getSaveString());window.location=a};GUI.scrollTop=-1;
  12. GUI.load=function(a){a=a.split(",");var b=parseInt(a[0]);GUI.scrollTop=parseInt(a[1]);for(var d=0;d<b;d++){var f=a.splice(2,4);GUI.savedAppearanceVars.push(f)}GUI.savedValues=a.splice(2,a.length)};GUI.savedValues=[];GUI.savedAppearanceVars=[];
  13. GUI.getSaveString=function(){var a=[],b;a.push(GUI.allGuis.length);a.push(document.body.scrollTop);for(b in GUI.allGuis)for(var d=GUI.allGuis[b].appearanceVars(),f=0;f<d.length;f++)a.push(d[f]);for(b in GUI.allControllers)if(GUI.allControllers[b].type!="function"){d=GUI.allControllers[b].getValue();if(GUI.allControllers[b].type=="number")d=GUI.roundToDecimal(d,4);a.push(d)}return a.join(",")};
  14. GUI.getVarFromURL=function(a){for(var b,d=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),f=0;f<d.length;f++){b=d[f].split("=");if(b!=undefined)if(b[0]==a)return b[1]}return null};
  15. GUI.replaceGetVar=function(a,b){for(var d,f=window.location.href,m=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),k=0;k<m.length;k++){d=m[k].split("=");if(d!=undefined)if(d[0]==a)return f.replace(d[1],b)}if(window.location.href.indexOf("?")!=-1)return f+"&"+a+"="+b;return f+"?"+a+"="+b};GUI.saveIndex=0;GUI.guiIndex=0;GUI.showSaveString=function(){alert(GUI.getSaveString())};
  16. GUI.makeUnselectable=function(a){a.onselectstart=function(){return false};a.style.MozUserSelect="none";a.style.KhtmlUserSelect="none";a.unselectable="on"};GUI.makeSelectable=function(a){a.onselectstart=function(){};a.style.MozUserSelect="auto";a.style.KhtmlUserSelect="auto";a.unselectable="off"};GUI.map=function(a,b,d,f,m){return f+(m-f)*((a-b)/(d-b))};GUI.constrain=function(a,b,d){if(a<b)a=b;else if(a>d)a=d;return a};
  17. GUI.error=function(a){typeof console.error=="function"&&console.error("[GUI ERROR] "+a)};GUI.roundToDecimal=function(a,b){var d=Math.pow(10,b);return Math.round(a*d)/d};GUI.extendController=function(a){a.prototype=new GUI.Controller;a.prototype.constructor=a};GUI.getVarFromURL("saveString")!=null&&GUI.load(GUI.getVarFromURL("saveString"));
  18. GUI.Slider=function(a,b,d,f,m){var k=false,h=this;this.domElement=document.createElement("div");this.domElement.setAttribute("class","guidat-slider-bg");this.fg=document.createElement("div");this.fg.setAttribute("class","guidat-slider-fg");this.domElement.appendChild(this.fg);var n=function(g){if(k){var p=o(h.domElement);g=GUI.map(g.pageX,p[0],p[0]+h.domElement.offsetWidth,b,d);g=Math.round(g/f)*f;a.setValue(g)}};this.domElement.addEventListener("mousedown",function(g){k=true;h.domElement.className+=
  19. " active";h.fg.className+=" active";a.domElement.className+=" active";n(g);document.addEventListener("mouseup",i,false)},false);var i=function(){h.domElement.className=h.domElement.className.replace(" active","");h.fg.className=h.fg.className.replace(" active","");a.domElement.className=a.domElement.className.replace(" active","");k=false;a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue());document.removeEventListener("mouseup",i,false)},o=function(g){var p=0,e=0;if(g.offsetParent){do{p+=
  20. g.offsetLeft;e+=g.offsetTop}while(g=g.offsetParent);return[p,e]}};this.__defineSetter__("value",function(g){this.fg.style.width=GUI.map(g,b,d,0,100)+"%"});n=function(g){if(k){var p=o(h.domElement);g=GUI.map(g.pageX,p[0],p[0]+h.domElement.offsetWidth,b,d);g=Math.round(g/f)*f;a.setValue(g)}};this.domElement.addEventListener("mousedown",function(g){k=true;h.domElement.setAttribute("class","guidat-slider-bg active");h.fg.setAttribute("class","guidat-slider-fg active");n(g);document.addEventListener("mouseup",
  21. i,false)},false);i=function(){h.domElement.setAttribute("class","guidat-slider-bg");h.fg.setAttribute("class","guidat-slider-fg");k=false;a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue());document.removeEventListener("mouseup",i,false)};document.addEventListener("mousemove",n,false);this.value=m};
  22. GUI.Controller=function(){this.parent=arguments[0];this.object=arguments[1];this.propertyName=arguments[2];if(arguments.length>0)this.initialValue=this.propertyName[this.object];this.domElement=document.createElement("div");this.domElement.setAttribute("class","guidat-controller "+this.type);this.propertyNameElement=document.createElement("span");this.propertyNameElement.setAttribute("class","guidat-propertyname");this.name(this.propertyName);this.domElement.appendChild(this.propertyNameElement);
  23. GUI.makeUnselectable(this.domElement)};GUI.Controller.prototype.changeFunction=null;GUI.Controller.prototype.finishChangeFunction=null;GUI.Controller.prototype.name=function(a){this.propertyNameElement.innerHTML=a;return this};GUI.Controller.prototype.reset=function(){this.setValue(this.initialValue);return this};GUI.Controller.prototype.listen=function(){this.parent.listenTo(this);return this};GUI.Controller.prototype.unlisten=function(){this.parent.unlistenTo(this);return this};
  24. GUI.Controller.prototype.setValue=function(a){this.object[this.propertyName]=a;this.changeFunction!=null&&this.changeFunction.call(this,a);this.updateDisplay();return this};GUI.Controller.prototype.getValue=function(){return this.object[this.propertyName]};GUI.Controller.prototype.updateDisplay=function(){};GUI.Controller.prototype.onChange=function(a){this.changeFunction=a;return this};GUI.Controller.prototype.onFinishChange=function(a){this.finishChangeFunction=a;return this};
  25. GUI.Controller.prototype.options=function(){var a=this,b=document.createElement("select");if(arguments.length==1){var d=arguments[0],f;for(f in d){var m=document.createElement("option");m.innerHTML=f;m.setAttribute("value",d[f]);b.appendChild(m)}}else for(f=0;f<arguments.length;f++){m=document.createElement("option");m.innerHTML=arguments[f];m.setAttribute("value",arguments[f]);b.appendChild(m)}b.addEventListener("change",function(){a.setValue(this.value);a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,
  26. a.getValue())},false);a.domElement.appendChild(b);return this};
  27. GUI.BooleanController=function(){this.type="boolean";GUI.Controller.apply(this,arguments);var a=this,b=document.createElement("input");b.setAttribute("type","checkbox");this.domElement.addEventListener("click",function(d){b.checked=!b.checked;d.preventDefault();a.setValue(b.checked)},false);b.addEventListener("mouseup",function(){b.checked=!b.checked},false);this.domElement.style.cursor="pointer";this.propertyNameElement.style.cursor="pointer";this.domElement.appendChild(b);this.updateDisplay=function(){b.checked=
  28. a.getValue()};this.setValue=function(d){if(typeof d!="boolean")try{d=eval(d)}catch(f){}return GUI.Controller.prototype.setValue.call(this,d)}};GUI.extendController(GUI.BooleanController);
  29. GUI.FunctionController=function(){this.type="function";var a=this;GUI.Controller.apply(this,arguments);this.domElement.addEventListener("click",function(){a.fire()},false);this.domElement.style.cursor="pointer";this.propertyNameElement.style.cursor="pointer";var b=null;this.onFire=function(d){b=d;return this};this.fire=function(){b!=null&&b.call(this);a.object[a.propertyName].call(a.object)}};GUI.extendController(GUI.FunctionController);
  30. GUI.NumberController=function(){this.type="number";GUI.Controller.apply(this,arguments);var a=this,b=false,d=false,f=0,m=0,k=arguments[3],h=arguments[4],n=arguments[5];n||(n=k!=undefined&&h!=undefined?(h-k)*0.01:1);var i=document.createElement("input");i.setAttribute("id",this.propertyName);i.setAttribute("type","text");i.setAttribute("value",this.getValue());n&&i.setAttribute("step",n);this.domElement.appendChild(i);var o;if(k!=undefined&&h!=undefined){o=new GUI.Slider(this,k,h,n,this.getValue());
  31. this.domElement.appendChild(o.domElement)}i.addEventListener("blur",function(){var e=parseFloat(this.value);isNaN(e)||a.setValue(e)},false);i.addEventListener("mousewheel",function(e){e.preventDefault();a.setValue(a.getValue()+Math.abs(e.wheelDeltaY)/e.wheelDeltaY*n);return false},false);i.addEventListener("mousedown",function(e){m=f=e.pageY;d=true;document.addEventListener("mousemove",p,false);document.addEventListener("mouseup",g,false)},false);i.addEventListener("keydown",function(e){switch(e.keyCode){case 13:e=
  32. parseFloat(this.value);a.setValue(e);break;case 38:e=a.getValue()+n;a.setValue(e);break;case 40:e=a.getValue()-n;a.setValue(e)}},false);var g=function(){document.removeEventListener("mousemove",p,false);GUI.makeSelectable(a.parent.domElement);GUI.makeSelectable(i);if(d&&!b){i.focus();i.select()}if(o)o.domElement.className=o.domElement.className.replace(" active","");d=b=false;a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue());document.removeEventListener("mouseup",g,false)},
  33. p=function(e){b=true;e.preventDefault();GUI.makeUnselectable(a.parent.domElement);GUI.makeUnselectable(i);if(o)o.domElement.className+=" active";m=f;f=e.pageY;e=m-f;e=a.getValue()+e*n;a.setValue(e);return false};this.options=function(){a.noSlider();a.domElement.removeChild(i);return GUI.Controller.prototype.options.apply(this,arguments)};this.noSlider=function(){o&&a.domElement.removeChild(o.domElement);return this};this.setValue=function(e){e=parseFloat(e);if(k!=undefined&&e<=k)e=k;else if(h!=undefined&&
  34. e>=h)e=h;return GUI.Controller.prototype.setValue.call(this,e)};this.updateDisplay=function(){i.value=GUI.roundToDecimal(a.getValue(),4);if(o)o.value=a.getValue()}};GUI.extendController(GUI.NumberController);
  35. GUI.StringController=function(){this.type="string";var a=this;GUI.Controller.apply(this,arguments);var b=document.createElement("input"),d=this.getValue();b.setAttribute("value",d);b.setAttribute("spellcheck","false");this.domElement.addEventListener("mouseup",function(){b.focus();b.select()},false);b.addEventListener("keyup",function(f){f.keyCode==13&&a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue());a.setValue(b.value)},false);b.addEventListener("blur",function(){a.finishChangeFunction!=
  36. null&&a.finishChangeFunction.call(this,a.getValue())},false);this.updateDisplay=function(){b.value=a.getValue()};this.options=function(){a.domElement.removeChild(b);return GUI.Controller.prototype.options.apply(this,arguments)};this.domElement.appendChild(b)};GUI.extendController(GUI.StringController);