mirror of
https://github.com/dataarts/dat.gui.git
synced 2024-12-12 04:08:27 +00:00
39 lines
19 KiB
JavaScript
39 lines
19 KiB
JavaScript
|
var DAT=DAT||{};
|
||
|
DAT.GUI=function(a){a==void 0&&(a={});if(a.height==void 0)a.height=300;var b=[],c=[],d=!0,g,i,k=this,h=!0,e=280,n=!1,f,o,l=0,q;this.domElement=document.createElement("div");this.domElement.setAttribute("class","guidat");this.domElement.style.width=e+"px";var j=a.height,m=document.createElement("div");m.setAttribute("class","guidat-controllers");m.style.height=j+"px";m.addEventListener("DOMMouseScroll",function(a){var b=this.scrollTop;a.wheelDelta?b+=a.wheelDelta:a.detail&&(b+=a.detail);a.preventDefault&&
|
||
|
a.preventDefault();a.returnValue=!1;m.scrollTop=b},!1);var p=document.createElement("a");p.setAttribute("class","guidat-toggle");p.setAttribute("href","#");p.innerHTML=h?"Close Controls":"Open Controls";var s=!1,A=0,t=!1,u,w,v,x,B=function(a){w=u;x=v;u=a.pageY;v=a.pageX;a=u-w;if(!h)if(a>0)h=!0,j=f=1,p.innerHTML=o||"Close Controls";else return;var b=x-v;if(a>0&&j>i){var c=DAT.GUI.map(j,i,i+100,1,0);a*=c}s=!0;A+=a;dragDisplacementX+=b;f+=a;e+=b;j+=a;m.style.height=f+"px";e=DAT.GUI.constrain(e,240,500);
|
||
|
k.domElement.style.width=e+"px";y()};p.addEventListener("mousedown",function(a){w=u=a.pageY;x=v=a.pageX;t=!0;a.preventDefault();dragDisplacementX=A=0;document.addEventListener("mousemove",B,!1);return!1},!1);p.addEventListener("click",function(a){a.preventDefault();return!1},!1);document.addEventListener("mouseup",function(a){t&&!s&&k.toggle();if(t&&s)if(dragDisplacementX==0&&z(),f>i)clearTimeout(q),f=l=i,r();else if(m.children.length>=1){var b=m.children[0].offsetHeight;clearTimeout(q);l=Math.round(j/
|
||
|
b)*b-1;l<=0?(k.close(),f=b*2):(f=l,r())}document.removeEventListener("mousemove",B,!1);a.preventDefault();return t=s=!1},!1);this.domElement.appendChild(m);this.domElement.appendChild(p);if(a.domElement)a.domElement.appendChild(this.domElement);else if(DAT.GUI.autoPlace){if(DAT.GUI.autoPlaceContainer==null)DAT.GUI.autoPlaceContainer=document.createElement("div"),DAT.GUI.autoPlaceContainer.setAttribute("id","guidat"),document.body.appendChild(DAT.GUI.autoPlaceContainer);DAT.GUI.autoPlaceContainer.appendChild(this.domElement)}this.autoListenIntervalTime=
|
||
|
1E3/60;var C=function(){g=setInterval(function(){k.listen()},this.autoListenIntervalTime)};this.__defineSetter__("autoListen",function(a){(d=a)?c.length>0&&C():clearInterval(g)});this.__defineGetter__("autoListen",function(){return d});this.listenTo=function(a){c.length==0&&C();c.push(a)};this.unlistenTo=function(a){for(var b=0;b<c.length;b++)c[b]==a&&c.splice(b,1);c.length<=0&&clearInterval(g)};this.listen=function(a){var a=a||c,b;for(b in a)a[b].updateDisplay()};this.listenAll=function(){this.listen(b)};
|
||
|
this.autoListen=!0;var D=function(a,b){function c(){return a.apply(this,b)}c.prototype=a.prototype;return new c};this.add=function(){if(arguments.length==1){var a=[],c;for(c in arguments[0])a.push(k.add(arguments[0],c));return a}a=arguments[0];c=arguments[1];a:for(var d in b)if(b[d].object==a&&b[d].propertyName==c)break a;d=a[c];if(d==void 0)DAT.GUI.error(a+" either has no property '"+c+"', or the property is inaccessible.");else if(a=typeof d,d=E[a],d==void 0)DAT.GUI.error("Cannot create controller for data type '"+
|
||
|
a+"'");else{for(var e=[this],g=0;g<arguments.length;g++)e.push(arguments[g]);if(d=D(d,e))return m.appendChild(d.domElement),b.push(d),DAT.GUI.allControllers.push(d),a!="function"&&DAT.GUI.saveIndex<DAT.GUI.savedValues.length&&(d.setValue(DAT.GUI.savedValues[DAT.GUI.saveIndex]),DAT.GUI.saveIndex++),y(),n||(f=i),d;else DAT.GUI.error("Error creating controller for '"+c+"'.")}};var y=function(){i=0;for(var a in b)i+=b[a].domElement.offsetHeight;m.style.overflowY=i-1>f?"auto":"hidden"},E={number:DAT.GUI.ControllerNumber,
|
||
|
string:DAT.GUI.ControllerString,"boolean":DAT.GUI.ControllerBoolean,"function":DAT.GUI.ControllerFunction};this.reset=function(){};this.toggle=function(){h?this.close():this.open()};this.open=function(){p.innerHTML=o||"Close Controls";l=f;clearTimeout(q);r();z();h=!0};this.close=function(){p.innerHTML=o||"Open Controls";l=0;clearTimeout(q);r();z();h=!1};this.name=function(a){o=a;p.innerHTML=a};this.appearanceVars=function(){return[h,e,f,m.scrollTop]};var r=function(){j=m.offsetHeight;j+=(l-j)*0.6;
|
||
|
Math.abs(j-l)<1?j=l:q=setTimeout(r,1E3/30);m.style.height=Math.round(j)+"px";y()},z=function(){k.domElement.style.width=e-1+"px";setTimeout(function(){k.domElement.style.width=e+"px"},1)};if(DAT.GUI.guiIndex<DAT.GUI.savedAppearanceVars.length){e=parseInt(DAT.GUI.savedAppearanceVars[DAT.GUI.guiIndex][1]);k.domElement.style.width=e+"px";f=parseInt(DAT.GUI.savedAppearanceVars[DAT.GUI.guiIndex][2]);n=!0;if(eval(DAT.GUI.savedAppearanceVars[DAT.GUI.guiIndex][0])==!0){var j=f,F=DAT.GUI.savedAppearanceVars[DAT.GUI.guiIndex][3];
|
||
|
setTimeout(function(){m.scrollTop=F},0);if(DAT.GUI.scrollTop>-1)document.body.scrollTop=DAT.GUI.scrollTop;l=f;this.open()}DAT.GUI.guiIndex++}DAT.GUI.allGuis.push(this);if(DAT.GUI.allGuis.length==1&&(window.addEventListener("keyup",function(a){!DAT.GUI.supressHotKeys&&a.keyCode==72&&DAT.GUI.toggleHide()},!1),DAT.GUI.inlineCSS))a=document.createElement("style"),a.setAttribute("type","text/css"),a.innerHTML=DAT.GUI.inlineCSS,document.head.insertBefore(a,document.head.firstChild)};DAT.GUI.hidden=!1;
|
||
|
DAT.GUI.autoPlace=!0;DAT.GUI.autoPlaceContainer=null;DAT.GUI.allControllers=[];DAT.GUI.allGuis=[];DAT.GUI.supressHotKeys=!1;DAT.GUI.toggleHide=function(){DAT.GUI.hidden?DAT.GUI.open():DAT.GUI.close()};DAT.GUI.open=function(){DAT.GUI.hidden=!1;for(var a in DAT.GUI.allGuis)DAT.GUI.allGuis[a].domElement.style.display="block"};DAT.GUI.close=function(){DAT.GUI.hidden=!0;for(var a in DAT.GUI.allGuis)DAT.GUI.allGuis[a].domElement.style.display="none"};
|
||
|
DAT.GUI.saveURL=function(){var a=DAT.GUI.replaceGetVar("saveString",DAT.GUI.getSaveString());window.location=a};DAT.GUI.scrollTop=-1;DAT.GUI.load=function(a){var a=a.split(","),b=parseInt(a[0]);DAT.GUI.scrollTop=parseInt(a[1]);for(var c=0;c<b;c++){var d=a.splice(2,4);DAT.GUI.savedAppearanceVars.push(d)}DAT.GUI.savedValues=a.splice(2,a.length)};DAT.GUI.savedValues=[];DAT.GUI.savedAppearanceVars=[];
|
||
|
DAT.GUI.getSaveString=function(){var a=[],b;a.push(DAT.GUI.allGuis.length);a.push(document.body.scrollTop);for(b in DAT.GUI.allGuis)for(var c=DAT.GUI.allGuis[b].appearanceVars(),d=0;d<c.length;d++)a.push(c[d]);for(b in DAT.GUI.allControllers)DAT.GUI.allControllers[b].type!="function"&&(c=DAT.GUI.allControllers[b].getValue(),DAT.GUI.allControllers[b].type=="number"&&(c=DAT.GUI.roundToDecimal(c,4)),a.push(c));return a.join(",")};
|
||
|
DAT.GUI.getVarFromURL=function(a){for(var b,c=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),d=0;d<c.length;d++)if(b=c[d].split("="),b!=void 0&&b[0]==a)return b[1];return null};
|
||
|
DAT.GUI.replaceGetVar=function(a,b){for(var c,d=window.location.href,g=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),i=0;i<g.length;i++)if(c=g[i].split("="),c!=void 0&&c[0]==a)return d.replace(c[1],b);if(window.location.href.indexOf("?")!=-1)return d+"&"+a+"="+b;return d+"?"+a+"="+b};DAT.GUI.saveIndex=0;DAT.GUI.guiIndex=0;DAT.GUI.showSaveString=function(){alert(DAT.GUI.getSaveString())};
|
||
|
DAT.GUI.makeUnselectable=function(a){if(!(a==void 0||a.style==void 0)){a.onselectstart=function(){return!1};a.style.MozUserSelect="none";a.style.KhtmlUserSelect="none";a.unselectable="on";for(var a=a.childNodes,b=0;b<a.length;b++)DAT.GUI.makeUnselectable(a[b])}};DAT.GUI.makeSelectable=function(a){if(!(a==void 0||a.style==void 0)){a.onselectstart=function(){};a.style.MozUserSelect="auto";a.style.KhtmlUserSelect="auto";a.unselectable="off";for(var a=a.childNodes,b=0;b<a.length;b++)DAT.GUI.makeSelectable(a[b])}};
|
||
|
DAT.GUI.map=function(a,b,c,d,g){return d+(g-d)*((a-b)/(c-b))};DAT.GUI.constrain=function(a,b,c){a<b?a=b:a>c&&(a=c);return a};DAT.GUI.error=function(a){typeof console.error=="function"&&console.error("[DAT.GUI ERROR] "+a)};DAT.GUI.roundToDecimal=function(a,b){var c=Math.pow(10,b);return Math.round(a*c)/c};DAT.GUI.extendController=function(a){a.prototype=new DAT.GUI.Controller;a.prototype.constructor=a};DAT.GUI.addClass=function(a,b){DAT.GUI.hasClass(a,b)||(a.className+=" "+b)};
|
||
|
DAT.GUI.hasClass=function(a,b){return a.className.indexOf(b)!=-1};DAT.GUI.removeClass=function(a,b){a.className=a.className.replace(RegExp(" "+b,"g"),"")};DAT.GUI.getVarFromURL("saveString")!=null&&DAT.GUI.load(DAT.GUI.getVarFromURL("saveString"));
|
||
|
DAT.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);
|
||
|
DAT.GUI.makeUnselectable(this.domElement)};DAT.GUI.Controller.prototype.changeFunction=null;DAT.GUI.Controller.prototype.finishChangeFunction=null;DAT.GUI.Controller.prototype.name=function(a){this.propertyNameElement.innerHTML=a;return this};DAT.GUI.Controller.prototype.reset=function(){this.setValue(this.initialValue);return this};DAT.GUI.Controller.prototype.listen=function(){this.parent.listenTo(this);return this};DAT.GUI.Controller.prototype.unlisten=function(){this.parent.unlistenTo(this);return this};
|
||
|
DAT.GUI.Controller.prototype.setValue=function(a){this.object[this.propertyName]=a;this.changeFunction!=null&&this.changeFunction.call(this,a);this.updateDisplay();return this};DAT.GUI.Controller.prototype.getValue=function(){return this.object[this.propertyName]};DAT.GUI.Controller.prototype.updateDisplay=function(){};DAT.GUI.Controller.prototype.onChange=function(a){this.changeFunction=a;return this};DAT.GUI.Controller.prototype.onFinishChange=function(a){this.finishChangeFunction=a;return this};
|
||
|
DAT.GUI.Controller.prototype.options=function(){var a=this,b=document.createElement("select");if(arguments.length==1){var c=arguments[0],d;for(d in c){var g=document.createElement("option");g.innerHTML=d;g.setAttribute("value",c[d]);if(arguments[d]==this.getValue())g.selected=!0;b.appendChild(g)}}else for(d=0;d<arguments.length;d++){g=document.createElement("option");g.innerHTML=arguments[d];g.setAttribute("value",arguments[d]);if(arguments[d]==this.getValue())g.selected=!0;b.appendChild(g)}b.addEventListener("change",
|
||
|
function(){a.setValue(this.value);a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue())},!1);a.domElement.appendChild(b);return this};
|
||
|
DAT.GUI.ControllerBoolean=function(){this.type="boolean";DAT.GUI.Controller.apply(this,arguments);var a=this,b=document.createElement("input");b.setAttribute("type","checkbox");b.checked=this.getValue();this.setValue(this.getValue());this.domElement.addEventListener("click",function(c){b.checked=!b.checked;c.preventDefault();a.setValue(b.checked)},!1);b.addEventListener("mouseup",function(){b.checked=!b.checked},!1);this.domElement.style.cursor="pointer";this.propertyNameElement.style.cursor="pointer";
|
||
|
this.domElement.appendChild(b);this.updateDisplay=function(){b.checked=a.getValue()};this.setValue=function(a){if(typeof a!="boolean")try{a=eval(a)}catch(b){}return DAT.GUI.Controller.prototype.setValue.call(this,a)}};DAT.GUI.extendController(DAT.GUI.ControllerBoolean);
|
||
|
DAT.GUI.ControllerFunction=function(){this.type="function";var a=this;DAT.GUI.Controller.apply(this,arguments);this.domElement.addEventListener("click",function(){a.fire()},!1);this.domElement.style.cursor="pointer";this.propertyNameElement.style.cursor="pointer";var b=null;this.onFire=function(a){b=a;return this};this.fire=function(){b!=null&&b.call(this);a.object[a.propertyName].call(a.object)}};DAT.GUI.extendController(DAT.GUI.ControllerFunction);
|
||
|
DAT.GUI.ControllerNumber=function(){this.type="number";DAT.GUI.Controller.apply(this,arguments);var a=this,b=!1,c=!1,d=!1,g=!1,i=0,k=0,h=arguments[3],e=arguments[4],n=arguments[5];this.min=function(){var b=!1;h==void 0&&e!=void 0&&(b=!0);if(arguments.length==0)return h;else h=arguments[0];b&&(l(),n==void 0&&(n=(e-h)*0.01));return a};this.max=function(){var b=!1;h!=void 0&&e==void 0&&(b=!0);if(arguments.length==0)return e;else e=arguments[0];b&&(l(),n==void 0&&(n=(e-h)*0.01));return a};this.step=function(){if(arguments.length==
|
||
|
0)return n;else n=arguments[0];return a};this.getMin=function(){return h};this.getMax=function(){return e};this.getStep=function(){return n||1};var f=document.createElement("input");f.setAttribute("id",this.propertyName);f.setAttribute("type","text");f.setAttribute("value",this.getValue());n&&f.setAttribute("step",n);this.domElement.appendChild(f);var o,l=function(){o=new DAT.GUI.Slider(a,h,e,n,a.getValue());a.domElement.appendChild(o.domElement)};h!=void 0&&e!=void 0&&l();f.addEventListener("blur",
|
||
|
function(){var b=parseFloat(this.value);o&&DAT.GUI.removeClass(a.domElement,"active");isNaN(b)||a.setValue(b)},!1);f.addEventListener("mousewheel",function(b){b.preventDefault();a.setValue(a.getValue()+Math.abs(b.wheelDeltaY)/b.wheelDeltaY*a.getStep());return!1},!1);f.addEventListener("mousedown",function(b){k=i=b.pageY;c=!0;o&&(DAT.GUI.addClass(a.domElement,"active"),console.log(a.domElement.className));document.addEventListener("mousemove",j,!1);document.addEventListener("mouseup",q,!1)},!1);f.addEventListener("keydown",
|
||
|
function(b){switch(b.keyCode){case 13:b=parseFloat(this.value);a.setValue(b);break;case 38:b=a.getValue()+a.getStep();a.setValue(b);break;case 40:b=a.getValue()-a.getStep(),a.setValue(b)}},!1);var q=function(){document.removeEventListener("mousemove",j,!1);DAT.GUI.makeSelectable(f);c&&!b&&(f.focus(),f.select());c=b=!1;a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue());g=d=!1;document.removeEventListener("mouseup",q,!1)},j=function(c){k=i;i=c.pageY;var e=k-i;!d&&!g&&(e==0?
|
||
|
d=!0:g=!0);if(d)return!0;DAT.GUI.makeUnselectable(a.parent.domElement);DAT.GUI.makeUnselectable(f);b=!0;c.preventDefault();c=a.getValue()+e*a.getStep();a.setValue(c);return!1};this.options=function(){a.noSlider();a.domElement.removeChild(f);return DAT.GUI.Controller.prototype.options.apply(this,arguments)};this.noSlider=function(){o&&a.domElement.removeChild(o.domElement);return this};this.setValue=function(a){a=parseFloat(a);h!=void 0&&a<=h?a=h:e!=void 0&&a>=e&&(a=e);return DAT.GUI.Controller.prototype.setValue.call(this,
|
||
|
a)};this.updateDisplay=function(){f.value=DAT.GUI.roundToDecimal(a.getValue(),4);if(o)o.value=a.getValue()}};DAT.GUI.extendController(DAT.GUI.ControllerNumber);
|
||
|
DAT.GUI.ControllerString=function(){this.type="string";var a=this;DAT.GUI.Controller.apply(this,arguments);var b=document.createElement("input"),c=this.getValue();b.setAttribute("value",c);b.setAttribute("spellcheck","false");this.domElement.addEventListener("mouseup",function(){b.focus();b.select()},!1);b.addEventListener("keyup",function(c){c.keyCode==13&&a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue());a.setValue(b.value)},!1);b.addEventListener("blur",function(){DAT.GUI.supressHotKeys=
|
||
|
!1;a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue())},!1);b.addEventListener("focus",function(){DAT.GUI.supressHotKeys=!0},!1);this.updateDisplay=function(){b.value=a.getValue()};this.options=function(){a.domElement.removeChild(b);return DAT.GUI.Controller.prototype.options.apply(this,arguments)};this.domElement.appendChild(b)};DAT.GUI.extendController(DAT.GUI.ControllerString);
|
||
|
DAT.GUI.Slider=function(a,b,c,d,g){var i=!1,k=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 h=function(b){if(i){var c;c=k.domElement;var d=0,e=0;if(c.offsetParent){do d+=c.offsetLeft,e+=c.offsetTop;while(c=c.offsetParent);c=[d,e]}else c=void 0;b=DAT.GUI.map(b.pageX,c[0],c[0]+k.domElement.offsetWidth,a.getMin(),
|
||
|
a.getMax());b=Math.round(b/a.getStep())*a.getStep();a.setValue(b)}};this.domElement.addEventListener("mousedown",function(b){i=!0;DAT.GUI.addClass(a.domElement,"active");h(b);document.addEventListener("mouseup",e,!1)},!1);var e=function(){DAT.GUI.removeClass(a.domElement,"active");i=!1;a.finishChangeFunction!=null&&a.finishChangeFunction.call(this,a.getValue());document.removeEventListener("mouseup",e,!1)};this.__defineSetter__("value",function(b){this.fg.style.width=DAT.GUI.map(b,a.getMin(),a.getMax(),
|
||
|
0,100)+"%"});document.addEventListener("mousemove",h,!1);this.value=g};DAT.GUI.inlineCSS="#guidat { position: fixed; top: 0; right: 0; width: auto; z-index: 1001; text-align: right; } .guidat { color: #fff; opacity: 0.97; text-align: left; float: right; margin-right: 20px; margin-bottom: 20px; background-color: #fff; } .guidat, .guidat input { font: 9.5px Lucida Grande, sans-serif; } .guidat-controllers { height: 300px; overflow-y: auto; overflow-x: hidden; background-color: rgba(0, 0, 0, 0.1); } a.guidat-toggle { text-decoration: none; cursor: pointer; color: #fff; background-color: #222; text-align: center; display: block; padding: 5px; } a.guidat-toggle:hover { background-color: #000; } .guidat-controller { padding: 3px; height: 25px; clear: left; border-bottom: 1px solid #222; background-color: #111; } .guidat-controller, .guidat-controller input, .guidat-slider-bg, .guidat-slider-fg { -moz-transition: background-color 0.15s linear; -webkit-transition: background-color 0.15s linear; transition: background-color 0.15s linear; } .guidat-controller.boolean:hover, .guidat-controller.function:hover { background-color: #000; } .guidat-controller input { float: right; outline: none; border: 0; padding: 4px; margin-top: 2px; background-color: #222; } .guidat-controller select { margin-top: 4px; float: right; } .guidat-controller input:hover { background-color: #444; } .guidat-controller input:focus, .guidat-controller.active input { background-color: #555; color: #fff; } .guidat-controller.number { border-left: 5px solid #00aeff; } .guidat-controller.string { border-left: 5px solid #1ed36f; } .guidat-controller.string input { border: 0; color: #1ed36f; margin-right: 2px; width: 148px; } .guidat-controller.boolean { border-left: 5px solid #54396e; } .guidat-controller.function { border-left: 5px solid #e61d5f; } .guidat-controller.number input[type=text] { width: 35px; margin-left: 5px; margin-right: 2px; color: #00aeff; } .guidat .guidat-controller.boolean input { margin-top: 6px; margin-right: 2px; font-size: 20px; } .guidat-controller:last-child { border-bottom: none; -webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.5); box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.5); } .guidat-propertyname { padding: 5px; padding-top: 7px; cursor: default; display: inline-block; } .guidat-controller .guidat-slider-bg:hover, .guidat-controller.active .guidat-slider-bg { background-color: #444; } .guidat-controller .guidat-slider-bg .guidat-slider-fg:hover, .guidat-controller.active .guidat-slider-bg .guidat-slider-fg { background-color: #52c8ff; } .guidat-slider-bg { background-color: #222; cursor: ew-resize; width: 40%; margin-top: 2px; float: right; height: 21px; } .guidat-slider-fg { cursor: ew-resize; background-color: #00aeff; height: 21px; } ";
|