mirror of
https://github.com/dataarts/dat.gui.git
synced 2024-12-12 04:08:27 +00:00
Version 0.1
This commit is contained in:
parent
fe260b5dca
commit
f62a04c8e9
@ -18,7 +18,7 @@ h1 {
|
||||
font-weight: 800;
|
||||
text-transform: lowercase;
|
||||
line-height: 80px;
|
||||
margin: 20px 0 0 0;
|
||||
margin: 20px 0 20px 0;
|
||||
}
|
||||
|
||||
h1 a:link, h1 a:visited, h1 a:hover, h1 a:active {
|
||||
@ -26,20 +26,27 @@ h1 a:link, h1 a:visited, h1 a:hover, h1 a:active {
|
||||
margin-right: 7px;
|
||||
}
|
||||
|
||||
h1 span {
|
||||
|
||||
}
|
||||
|
||||
h1 img {
|
||||
width: 45px;
|
||||
height: 45px;
|
||||
-moz-border-radius: 9px;
|
||||
border-radius: 9px;
|
||||
margin-bottom: 20px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin-top: 30px;
|
||||
font-size: 18px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
#helvetica-demo {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 800;
|
||||
height: 300;
|
||||
z-index: -1;
|
||||
}
|
||||
|
||||
pre {
|
||||
@ -50,39 +57,62 @@ pre {
|
||||
font: 10px Monaco, monospace;
|
||||
}
|
||||
|
||||
p { font-size: 125%; max-width: 530px; line-height: 18px; margin-bottom: 36px; }
|
||||
p, ul {
|
||||
font-size: 125%;
|
||||
max-width: 530px;
|
||||
line-height: 18px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
li {
|
||||
margin-left: 18px;
|
||||
}
|
||||
|
||||
ul#desc {
|
||||
list-style: circle;
|
||||
font-size: 100%;
|
||||
max-width: 380px;
|
||||
}
|
||||
|
||||
a:link {
|
||||
color: #00aeff;
|
||||
}
|
||||
|
||||
a:visited {
|
||||
color: #0fa954;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #e61d5f;
|
||||
}
|
||||
|
||||
a:active {
|
||||
color: #54396e;
|
||||
}
|
||||
|
||||
footer {
|
||||
background-color: #eee;
|
||||
width: 510px;
|
||||
padding: 10px;
|
||||
background-color: #eee;
|
||||
width: 510px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
#columns {
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 10px;
|
||||
pre a:link,
|
||||
pre a:visited,
|
||||
pre a:active {
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
#columns div {
|
||||
float: left;
|
||||
width : auto;
|
||||
height: 10px;
|
||||
pre a:hover {
|
||||
color: #e61d5f;
|
||||
}
|
||||
|
||||
code {
|
||||
font: 10px Monaco, monospace;
|
||||
}
|
||||
|
||||
code strong {
|
||||
font-weight: normal;
|
||||
color: #e61d5f;
|
||||
}
|
||||
|
||||
/* SPAN elements with the classes below are added by prettyprint. */
|
||||
|
121
demo/demo.js
121
demo/demo.js
@ -1,30 +1,27 @@
|
||||
function DynamicText(message, width, height, textAscent) {
|
||||
function FizzyText(message) {
|
||||
|
||||
var _this = this;
|
||||
|
||||
var width = 550;
|
||||
var height = 200;
|
||||
var textAscent = 82;
|
||||
var textLeft = 80;
|
||||
|
||||
|
||||
this.growthSpeed = 0.5;
|
||||
this.minSize = 0;
|
||||
this.maxSize = 3;
|
||||
this.maxSize = 3.2;
|
||||
|
||||
this.noiseScale = 300;
|
||||
this.noiseStrength = 10;
|
||||
this.stepSize = 1;
|
||||
this.speed = 0.4;
|
||||
|
||||
this.displayOutline = false;
|
||||
|
||||
var message = message;
|
||||
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
this.textAscent = textAscent;
|
||||
|
||||
var colors = [
|
||||
"#00aeff",
|
||||
"#0fa954",
|
||||
"#54396e",
|
||||
"#e61d5f"
|
||||
]
|
||||
|
||||
var colors = ["#00aeff", "#0fa954", "#54396e", "#e61d5f"];
|
||||
|
||||
var r = document.createElement('canvas');
|
||||
var s = r.getContext('2d');
|
||||
@ -42,19 +39,19 @@ function DynamicText(message, width, height, textAscent) {
|
||||
var pixels = [];
|
||||
var particles = [];
|
||||
|
||||
s.font = g.font = "800 "+textAscent+"px helvetica, arial, sans-serif";
|
||||
s.font = g.font = "800 " + textAscent + "px helvetica, arial, sans-serif";
|
||||
|
||||
// Set reference onto particles
|
||||
for (var i = 0; i < 1000; i++) {
|
||||
particles.push( new Particle(Math.random()*width, Math.random()*height));
|
||||
particles.push(new Particle(Math.random() * width, Math.random() * height));
|
||||
}
|
||||
|
||||
var createBitmap = function(m) {
|
||||
var createBitmap = function (m) {
|
||||
s.fillStyle = "#fff";
|
||||
s.fillRect(0, 0, width, height);
|
||||
|
||||
s.fillStyle = "#222";
|
||||
s.fillText(m, 0, textAscent);
|
||||
s.fillText(m, textLeft, textAscent);
|
||||
|
||||
// Pull reference
|
||||
var imageData = s.getImageData(0, 0, width, height);
|
||||
@ -62,78 +59,108 @@ function DynamicText(message, width, height, textAscent) {
|
||||
|
||||
};
|
||||
|
||||
var render = function() {
|
||||
var render = function () {
|
||||
|
||||
g.clearRect(0, 0, width, height);
|
||||
|
||||
if(_this.displayOutline) {
|
||||
if (_this.displayOutline) {
|
||||
g.globalCompositeOperation = "source-over";
|
||||
g.strokeStyle = "#000";
|
||||
g.lineWidth = .25;
|
||||
g.strokeText(message, 0, textAscent);
|
||||
g.lineWidth = .5;
|
||||
g.strokeText(message, textLeft, textAscent);
|
||||
}
|
||||
|
||||
g.globalCompositeOperation = "darker";
|
||||
|
||||
for (var i = 0; i < particles.length; i++) {
|
||||
g.fillStyle = colors[i%colors.length];
|
||||
g.fillStyle = colors[i % colors.length];
|
||||
particles[i].render();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var getPosition = function(i) {
|
||||
return { x: (i - (width * 4) * Math.floor(i/(width * 4))) / 4, y: Math.floor(i/(width * 4)) };
|
||||
var getPosition = function (i) {
|
||||
return {
|
||||
x: (i - (width * 4) * Math.floor(i / (width * 4))) / 4,
|
||||
y: Math.floor(i / (width * 4))
|
||||
};
|
||||
};
|
||||
|
||||
var getColor = function(x, y) {
|
||||
var getColor = function (x, y) {
|
||||
var base = (Math.floor(y) * width + Math.floor(x)) * 4;
|
||||
var c = { r: pixels[base + 0], g: pixels[base + 1], b: pixels[base + 2], a: pixels[base + 3]};
|
||||
|
||||
return "rgb("+c.r+","+c.g+","+c.b+")";
|
||||
var c = {
|
||||
r: pixels[base + 0],
|
||||
g: pixels[base + 1],
|
||||
b: pixels[base + 2],
|
||||
a: pixels[base + 3]
|
||||
};
|
||||
|
||||
this.__defineGetter__("message", function() {
|
||||
return "rgb(" + c.r + "," + c.g + "," + c.b + ")";
|
||||
};
|
||||
|
||||
this.__defineGetter__("message", function () {
|
||||
return message;
|
||||
});
|
||||
|
||||
this.__defineSetter__("message", function(m) {
|
||||
this.__defineSetter__("message", function (m) {
|
||||
message = m;
|
||||
createBitmap(message);
|
||||
});
|
||||
|
||||
createBitmap(message);
|
||||
this.explode = function() {
|
||||
var mag = Math.random()*30+30;
|
||||
for (var i in particles) {
|
||||
var angle= Math.random()*Math.PI*2;
|
||||
particles[i].vx = Math.cos(angle)*mag;
|
||||
particles[i].vy = Math.sin(angle)*mag;
|
||||
}
|
||||
};
|
||||
|
||||
this.message = message;
|
||||
|
||||
setInterval(render, 30);
|
||||
|
||||
function Particle(x, y, c) {
|
||||
this.p = { x: x, y: y };
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
this.vx = 0;
|
||||
this.vy = 0;
|
||||
this.r = 0;
|
||||
|
||||
this.render = function() {
|
||||
this.render = function () {
|
||||
var c = getColor(this.x, this.y);
|
||||
var angle = noise(this.x / _this.noiseScale, this.y / _this.noiseScale) * _this.noiseStrength;
|
||||
|
||||
var angle = noise(this.p.x/_this.noiseScale, this.p.y/_this.noiseScale) * _this.noiseStrength;
|
||||
var c = getColor(this.p.x, this.p.y);
|
||||
if (c == "rgb(255,255,255)") {
|
||||
this.r -= _this.growthSpeed;
|
||||
} else {
|
||||
var onScreen = this.x > 0 && this.x < width &&
|
||||
this.y > 0 && this.y < height;
|
||||
var isBlack = c != "rgb(255,255,255)" &&
|
||||
onScreen;
|
||||
|
||||
if (isBlack) {
|
||||
this.r += _this.growthSpeed;
|
||||
} else {
|
||||
this.r -= _this.growthSpeed;
|
||||
}
|
||||
this.p.x += Math.cos(angle) * _this.stepSize;
|
||||
this.p.y += -Math.sin(angle) * _this.stepSize;
|
||||
this.vx *= 0.5;
|
||||
this.vy *= 0.5;
|
||||
this.x += Math.cos(angle) * _this.speed + this.vx;
|
||||
this.y += -Math.sin(angle) * _this.speed + this.vy;
|
||||
this.r = constrain(this.r, _this.minSize, _this.maxSize);
|
||||
if(this.r <= _this.minSize) {
|
||||
this.p.x = Math.random() * width;
|
||||
this.p.y = Math.random() * height;
|
||||
if (this.r <= _this.minSize) {
|
||||
this.x = Math.random() * width;
|
||||
this.y = Math.random() * height;
|
||||
}
|
||||
if (this.r <= 0) {
|
||||
return;
|
||||
}
|
||||
g.beginPath();
|
||||
g.arc(this.p.x, this.p.y, this.r, 0, Math.PI*2, false);
|
||||
g.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
|
||||
g.fill();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var constrain = function(v, o1, o2) {
|
||||
var constrain = function (v, o1, o2) {
|
||||
if (v < o1) v = o1;
|
||||
else if (v > o2) v = o2;
|
||||
return v;
|
||||
|
489
gui-tobeminified.js
Normal file
489
gui-tobeminified.js
Normal file
@ -0,0 +1,489 @@
|
||||
// This version of gui.js appends the style definitions via javascript.
|
||||
var GUI = new function() {
|
||||
|
||||
var _this = this;
|
||||
|
||||
var controllers = [];
|
||||
|
||||
var style = '#guidat{color:#fff;position:fixed;width:280px;z-index:200;opacity:.97;top:0;left:100%;margin-left:-300px;background-color:#fff;-moz-transition:margin-top .2s ease-out;-webkit-transition:margin-top .2s ease-out;transition:margin-top .2s ease-out;-webkit-box-shadow:0 0 10px rgba(0,0,0,0.3);-moz-box-shadow:0 0 10px rgba(0,0,0,0.3);box-shadow:0 0 10px rgba(0,0,0,0.3)}#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)}#guidat-toggle{text-decoration:none;cursor:pointer;color:#fff;background-color:#222;text-align:center;display:block;padding:5px}#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 .15s linear;-webkit-transition:background-color .15s linear;transition:background-color .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 input:hover{background-color:#444}.guidat-controller input:focus{background-color:#555}.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:0 1px 3px rgba(0,0,0,0.5);-moz-box-shadow:0 1px 3px rgba(0,0,0,0.5);box-shadow:0 1px 3px rgba(0,0,0,0.5)}.guidat-propertyname{padding:5px;padding-top:7px;cursor:default;display:inline-block}.guidat-slider-bg:hover,.guidat-slider-bg.active{background-color:#444}.guidat-slider-bg:hover .guidat-slider-fg,.guidat-slider-bg.active .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{background-color:#00aeff;height:20px}'
|
||||
|
||||
|
||||
this.add = function() {
|
||||
|
||||
// We need to call GUI.start() before .add()
|
||||
if (!started) {
|
||||
error("Make sure to call GUI.start() in the window.onload function");
|
||||
return;
|
||||
}
|
||||
|
||||
var object = arguments[0];
|
||||
var propertyName = arguments[1];
|
||||
|
||||
// Have we already added this?
|
||||
if (alreadyControlled(object, propertyName)) {
|
||||
error("Controller for \"" + propertyName+"\" already added.");
|
||||
return;
|
||||
}
|
||||
|
||||
var value = object[propertyName];
|
||||
|
||||
// Does this value exist? Is it accessible?
|
||||
if (value == undefined) {
|
||||
error(object + " either has no property \""+propertyName+"\", or the property is inaccessible.");
|
||||
return;
|
||||
}
|
||||
|
||||
var type = typeof value;
|
||||
var handler = addHandlers[type];
|
||||
|
||||
// Do we know how to deal with this data type?
|
||||
if (handler == undefined) {
|
||||
error("Cannot create controller for data type \""+type+"\"");
|
||||
return;
|
||||
}
|
||||
|
||||
var controllerObject = handler.apply(this, arguments);
|
||||
|
||||
// Were we able to make the controller?
|
||||
if (!controllerObject) {
|
||||
error("Error creating controller for \""+propertyName+"\".");
|
||||
return;
|
||||
}
|
||||
|
||||
// Success.
|
||||
controllerContainer.appendChild(controllerObject.domElement);
|
||||
controllers.push(controllerObject);
|
||||
|
||||
return controllerObject;
|
||||
|
||||
}
|
||||
|
||||
var addHandlers = {
|
||||
|
||||
"number": function() {
|
||||
return construct(NumberController, arguments);
|
||||
},
|
||||
|
||||
"string": function() {
|
||||
return construct(StringController, arguments);
|
||||
},
|
||||
|
||||
"boolean": function() {
|
||||
return construct(BooleanController, arguments);
|
||||
},
|
||||
|
||||
"function": function() {
|
||||
return construct(FunctionController, arguments);
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
var alreadyControlled = function(object, propertyName) {
|
||||
for (var i in controllers) {
|
||||
if (controllers[i].object == object &&
|
||||
controllers[i].propertyName == propertyName) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
var error = function(str) {
|
||||
if (typeof console.log == 'function') {
|
||||
console.error("[GUI ERROR] " + str);
|
||||
}
|
||||
};
|
||||
|
||||
var construct = function(constructor, args) {
|
||||
function F() {
|
||||
return constructor.apply(this, args);
|
||||
}
|
||||
F.prototype = constructor.prototype;
|
||||
return new F();
|
||||
};
|
||||
|
||||
|
||||
|
||||
// GUI ... GUI
|
||||
|
||||
this.domElement = null;
|
||||
var controllerContainer;
|
||||
var started = false;
|
||||
var open = false;
|
||||
|
||||
// TODO: obtain this dynamically?
|
||||
var domElementMarginTop = 300;
|
||||
|
||||
this.start = function() {
|
||||
|
||||
var styleSheet = document.createElement('style');
|
||||
styleSheet.setAttribute('type', 'text/css');
|
||||
styleSheet.innerHTML = style;
|
||||
document.getElementsByTagName('head')[0].appendChild(styleSheet);
|
||||
|
||||
|
||||
this.domElement = document.createElement('div');
|
||||
this.domElement.setAttribute('id', 'guidat');
|
||||
|
||||
controllerContainer = document.createElement('div');
|
||||
controllerContainer.setAttribute('id', 'guidat-controllers');
|
||||
|
||||
toggleButton = document.createElement('a');
|
||||
toggleButton.setAttribute('id', 'guidat-toggle');
|
||||
toggleButton.setAttribute('href', '#');
|
||||
toggleButton.innerHTML = "Show Controls";
|
||||
toggleButton.addEventListener('click', function(e) {
|
||||
_this.toggle();
|
||||
e.preventDefault();
|
||||
}, false);
|
||||
|
||||
this.domElement.appendChild(controllerContainer);
|
||||
this.domElement.appendChild(toggleButton);
|
||||
|
||||
this.domElement.style.marginTop = -domElementMarginTop+"px";
|
||||
|
||||
document.body.appendChild(this.domElement);
|
||||
|
||||
started = true;
|
||||
|
||||
};
|
||||
|
||||
this.toggle = function() {
|
||||
|
||||
if (open) {
|
||||
this.hide();
|
||||
} else {
|
||||
this.show();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
this.show = function() {
|
||||
this.domElement.style.marginTop = 0+"px";
|
||||
toggleButton.innerHTML = "Hide Controls";
|
||||
open = true;
|
||||
}
|
||||
|
||||
this.hide = function() {
|
||||
this.domElement.style.marginTop = -domElementMarginTop+"px";
|
||||
toggleButton.innerHTML = "Show Controls";
|
||||
open = false;
|
||||
}
|
||||
|
||||
};
|
||||
// TODO: Leaving the window while dragging the slider and then removing the mouse
|
||||
// still leaves slider in focus.
|
||||
// TODO: Problem with multiple sliders.
|
||||
var Slider = function(numberController, min, max, step, initValue) {
|
||||
|
||||
var min = min;
|
||||
var max = max;
|
||||
var step = step;
|
||||
|
||||
var clicked = false;
|
||||
var _this = this;
|
||||
|
||||
var x, px;
|
||||
|
||||
this.domElement = document.createElement('div');
|
||||
this.fg = document.createElement('div');
|
||||
this.domElement.setAttribute('class', 'guidat-slider-bg');
|
||||
this.fg.setAttribute('class', 'guidat-slider-fg');
|
||||
|
||||
this.domElement.appendChild(this.fg);
|
||||
|
||||
var map = function(v, i1, i2, o1, o2) {
|
||||
var v = o1 + (o2 - o1) * ((v - i1) / (i2 - i1));
|
||||
if (v < o1) v = o1;
|
||||
else if (v > o2) v = o2;
|
||||
return v;
|
||||
}
|
||||
|
||||
var findPos = function(obj) {
|
||||
var curleft = curtop = 0;
|
||||
if (obj.offsetParent) {
|
||||
do {
|
||||
curleft += obj.offsetLeft;
|
||||
curtop += obj.offsetTop;
|
||||
} while (obj = obj.offsetParent);
|
||||
return [curleft,curtop];
|
||||
}
|
||||
}
|
||||
|
||||
this.__defineSetter__('value', function(e) {
|
||||
|
||||
var pct = map(e, min, max, 0, 100);
|
||||
this.fg.style.width = pct+"%";
|
||||
|
||||
});
|
||||
|
||||
var onDrag = function(e) {
|
||||
if (!clicked) return;
|
||||
var pos = findPos(_this.domElement);
|
||||
var val = map(e.pageX, pos[0], pos[0] + _this.domElement.offsetWidth, min, max);
|
||||
val = Math.round(val/step)*step;
|
||||
numberController.updateValue(val);
|
||||
}
|
||||
|
||||
this.domElement.addEventListener('mousedown', function(e) {
|
||||
clicked = true;
|
||||
x = px = e.pageX;
|
||||
_this.domElement.setAttribute('class', 'guidat-slider-bg active');
|
||||
_this.fg.setAttribute('class', 'guidat-slider-fg active');
|
||||
onDrag(e);
|
||||
}, false);
|
||||
|
||||
|
||||
document.addEventListener('mouseup', function(e) {
|
||||
_this.domElement.setAttribute('class', 'guidat-slider-bg');
|
||||
_this.fg.setAttribute('class', 'guidat-slider-fg');
|
||||
clicked = false;
|
||||
}, false);
|
||||
|
||||
document.addEventListener('mousemove', onDrag, false);
|
||||
|
||||
|
||||
|
||||
|
||||
this.value = initValue;
|
||||
|
||||
}
|
||||
var Controller = function() {
|
||||
|
||||
var onChange = null;
|
||||
|
||||
this.setName = function(n) {
|
||||
this.propertyNameElement.innerHTML = n;
|
||||
}
|
||||
|
||||
this.setValue = function(n) {
|
||||
this.object[this.propertyName] = n;
|
||||
if (onChange != null) {
|
||||
onChange.call(this, n);
|
||||
}
|
||||
}
|
||||
|
||||
this.getValue = function() {
|
||||
return this.object[this.propertyName];
|
||||
}
|
||||
|
||||
this.onChange = function(fnc) {
|
||||
onChange = fnc;
|
||||
}
|
||||
|
||||
this.makeUnselectable = function(elem) {
|
||||
elem.onselectstart = function() { return false; };
|
||||
elem.style.MozUserSelect = "none";
|
||||
elem.style.KhtmlUserSelect = "none";
|
||||
elem.unselectable = "on";
|
||||
}
|
||||
|
||||
this.makeSelectable = function(elem) {
|
||||
elem.onselectstart = function() { };
|
||||
elem.style.MozUserSelect = "auto";
|
||||
elem.style.KhtmlUserSelect = "auto";
|
||||
elem.unselectable = "off";
|
||||
}
|
||||
|
||||
this.domElement = document.createElement('div');
|
||||
this.domElement.setAttribute('class', 'guidat-controller ' + this.type);
|
||||
|
||||
this.object = arguments[0];
|
||||
this.propertyName = arguments[1];
|
||||
|
||||
this.propertyNameElement = document.createElement('span');
|
||||
this.propertyNameElement.setAttribute('class', 'guidat-propertyname');
|
||||
this.setName(this.propertyName);
|
||||
this.domElement.appendChild(this.propertyNameElement);
|
||||
|
||||
this.makeUnselectable(this.domElement);
|
||||
|
||||
};
|
||||
var BooleanController = function() {
|
||||
this.type = "boolean";
|
||||
Controller.apply(this, arguments);
|
||||
|
||||
var _this = this;
|
||||
var input = document.createElement('input');
|
||||
input.setAttribute('type', 'checkbox');
|
||||
|
||||
this.domElement.addEventListener('click', function(e) {
|
||||
input.checked = !input.checked;
|
||||
e.preventDefault();
|
||||
_this.setValue(input.checked);
|
||||
}, false);
|
||||
|
||||
input.addEventListener('mouseup', function(e) {
|
||||
input.checked = !input.checked; // counteracts default.
|
||||
}, false);
|
||||
|
||||
this.domElement.style.cursor = "pointer";
|
||||
this.propertyNameElement.style.cursor = "pointer";
|
||||
this.domElement.appendChild(input);
|
||||
|
||||
};
|
||||
BooleanController.prototype = new Controller();
|
||||
BooleanController.prototype.constructor = BooleanController;
|
||||
var FunctionController = function() {
|
||||
this.type = "function";
|
||||
var _this = this;
|
||||
Controller.apply(this, arguments);
|
||||
this.domElement.addEventListener('click', function() {
|
||||
_this.object[_this.propertyName].call(_this.object);
|
||||
}, false);
|
||||
this.domElement.style.cursor = "pointer";
|
||||
this.propertyNameElement.style.cursor = "pointer";
|
||||
};
|
||||
FunctionController.prototype = new Controller();
|
||||
FunctionController.prototype.constructor = FunctionController;
|
||||
// TODO: Provide alternate controllers for non-html5 browsers?
|
||||
var NumberController = function() {
|
||||
|
||||
this.type = "number";
|
||||
|
||||
Controller.apply(this, arguments);
|
||||
|
||||
var _this = this;
|
||||
|
||||
// If we simply click and release a number field, we want to highlight it.
|
||||
// This variable keeps track of whether or not we've dragged
|
||||
var draggedNumberField = false;
|
||||
|
||||
var clickedNumberField = false;
|
||||
|
||||
var y = py = 0;
|
||||
|
||||
var min = arguments[2];
|
||||
var max = arguments[3];
|
||||
var step = arguments[4];
|
||||
|
||||
if (!step) {
|
||||
if (min && max) {
|
||||
step = (max-min)*0.01;
|
||||
} else {
|
||||
step = 1;
|
||||
}
|
||||
}
|
||||
|
||||
var numberField = document.createElement('input');
|
||||
numberField.setAttribute('id', this.propertyName);
|
||||
|
||||
// Little up and down arrows are pissing me off.
|
||||
numberField.setAttribute('type', 'text');
|
||||
numberField.setAttribute('value', this.getValue());
|
||||
|
||||
if (step) numberField.setAttribute('step', step);
|
||||
|
||||
this.domElement.appendChild(numberField);
|
||||
|
||||
var slider;
|
||||
|
||||
if (min != undefined && max != undefined) {
|
||||
slider = new Slider(this, min, max, step, this.getValue());
|
||||
this.domElement.appendChild(slider.domElement);
|
||||
}
|
||||
|
||||
numberField.addEventListener('blur', function(e) {
|
||||
var val = parseFloat(this.value);
|
||||
if (!isNaN(val)) {
|
||||
_this.updateValue(val);
|
||||
} else {
|
||||
this.value = _this.getValue();
|
||||
}
|
||||
}, false);
|
||||
|
||||
numberField.addEventListener('mousewheel', function(e) {
|
||||
e.preventDefault();
|
||||
this.updateValue(_this.getValue() + Math.abs(e.wheelDeltaY)/e.wheelDeltaY*step);
|
||||
return false;
|
||||
}, false);
|
||||
|
||||
numberField.addEventListener('mousedown', function(e) {
|
||||
py = y = e.pageY;
|
||||
clickedNumberField = true;
|
||||
document.addEventListener('mousemove', dragNumberField, false);
|
||||
}, false);
|
||||
|
||||
document.addEventListener('mouseup', function(e) {
|
||||
document.removeEventListener('mousemove', dragNumberField, false);
|
||||
_this.makeSelectable(GUI.domElement);
|
||||
_this.makeSelectable(numberField);
|
||||
if (clickedNumberField && !draggedNumberField) {
|
||||
numberField.focus();
|
||||
numberField.select();
|
||||
}
|
||||
draggedNumberField = false;
|
||||
clickedNumberField = false;
|
||||
}, false);
|
||||
|
||||
// Kinda nast
|
||||
if (navigator.appVersion.indexOf('chrome') != -1) {
|
||||
document.addEventListener('mouseout', function(e) {
|
||||
document.removeEventListener('mousemove', dragNumberField, false);
|
||||
}, false);
|
||||
}
|
||||
|
||||
var dragNumberField = function(e) {
|
||||
draggedNumberField = true;
|
||||
e.preventDefault();
|
||||
|
||||
// We don't want to be highlighting this field as we scroll.
|
||||
// Or any other fields in this gui for that matter ...
|
||||
// TODO: Make makeUselectable go through each element and child element.
|
||||
_this.makeUnselectable(GUI.domElement);
|
||||
_this.makeUnselectable(numberField);
|
||||
|
||||
py = y;
|
||||
y = e.pageY;
|
||||
var dy = py - y;
|
||||
var newVal = _this.getValue() + dy*step;
|
||||
_this.updateValue(newVal);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.updateValue = function(val) {
|
||||
|
||||
val = parseFloat(val);
|
||||
|
||||
if (min != undefined && val <= min) {
|
||||
val = min;
|
||||
} else if (max != undefined && val >= max) {
|
||||
val = max;
|
||||
}
|
||||
_this.setValue(val);
|
||||
|
||||
numberField.value = _this.getValue();
|
||||
if (slider) slider.value = _this.getValue();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
NumberController.prototype = new Controller();
|
||||
NumberController.prototype.constructor = NumberController;
|
||||
var StringController = function() {
|
||||
|
||||
this.type = "string";
|
||||
|
||||
var _this = this;
|
||||
|
||||
Controller.apply(this, arguments);
|
||||
|
||||
var input = document.createElement('input');
|
||||
|
||||
var initialValue = this.getValue();
|
||||
|
||||
input.setAttribute('value', initialValue);
|
||||
input.setAttribute('spellcheck', 'false');
|
||||
this.domElement.addEventListener('mouseup', function() {
|
||||
input.focus();
|
||||
input.select();
|
||||
}, false);
|
||||
|
||||
input.addEventListener('keyup', function() {
|
||||
_this.setValue(input.value);
|
||||
}, false);
|
||||
|
||||
this.domElement.appendChild(input);
|
||||
};
|
||||
StringController.prototype = new Controller();
|
||||
StringController.prototype.constructor = StringController;
|
2
gui.css
2
gui.css
@ -94,7 +94,7 @@
|
||||
border: 0;
|
||||
color: #1ed36f;
|
||||
margin-right: 2px;
|
||||
width: 53%;
|
||||
width: 148px;
|
||||
}
|
||||
|
||||
.guidat-controller.boolean {
|
||||
|
1
gui.min.js
vendored
Normal file
1
gui.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
118
index.html
118
index.html
@ -1,66 +1,34 @@
|
||||
<!doctype html>
|
||||
<head>
|
||||
<title>GUI-DAT</title>
|
||||
<title>gui-dat</title>
|
||||
|
||||
<link rel="icon" type="image/gif" href="demo/assets/favicon.gif">
|
||||
|
||||
<link href="gui.css" media="screen" rel="stylesheet" type="text/css" />
|
||||
<link href="demo/demo.css" media="screen" rel="stylesheet" type="text/css" />
|
||||
<script type="text/javascript" src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
|
||||
|
||||
<script type="text/javascript" src="gui.js"></script>
|
||||
<script type="text/javascript" src="controllers/slider.js"></script>
|
||||
<script type="text/javascript" src="controllers/controller.js"></script>
|
||||
<script type="text/javascript" src="controllers/controller.string.js"></script>
|
||||
<script type="text/javascript" src="controllers/controller.number.js"></script>
|
||||
<script type="text/javascript" src="controllers/controller.boolean.js"></script>
|
||||
<script type="text/javascript" src="controllers/controller.function.js"></script>
|
||||
<script type="text/javascript" src="gui.min.js"></script>
|
||||
<script type="text/javascript" src="demo/improvedNoise.js"></script>
|
||||
<script type="text/javascript" src="demo/demo.js"></script>
|
||||
<script type="text/javascript">
|
||||
var controllableObject =
|
||||
{
|
||||
numberProperty: 20,
|
||||
constrainedNum: 0,
|
||||
notchedNum: 240,
|
||||
pageTitle: "gui-dat",
|
||||
anotherTextProperty: "another string",
|
||||
booleanProperty: false,
|
||||
anotherBooleanProperty: false,
|
||||
functionProperty: function() {
|
||||
alert("I am a function!");
|
||||
}
|
||||
};
|
||||
|
||||
window.onload = function() {
|
||||
|
||||
var testDisplay = new DynamicText("gui-dat", 600, 100, 75);
|
||||
|
||||
prettyPrint();
|
||||
var fizzyText = new FizzyText("gui-dat");
|
||||
|
||||
GUI.start();
|
||||
|
||||
// Creates a number box
|
||||
GUI.add(testDisplay, "maxSize", 1, 5);
|
||||
// Text field
|
||||
GUI.add(fizzyText, "message");
|
||||
|
||||
// Creates a slider (min, max)
|
||||
GUI.add(testDisplay, "growthSpeed", 0.01, 1);
|
||||
// Sliders with min and max
|
||||
GUI.add(fizzyText, "maxSize", 0.5, 7);
|
||||
GUI.add(fizzyText, "growthSpeed", 0.01, 1);
|
||||
GUI.add(fizzyText, "speed", 0.1, 2);
|
||||
|
||||
// Creates a slider (min, max)
|
||||
GUI.add(testDisplay, "noiseStrength", 10, 100);
|
||||
// Sliders with min, max and increment.
|
||||
GUI.add(fizzyText, "noiseStrength", 10, 100, 5);
|
||||
|
||||
// Creates a slider (min, max)
|
||||
GUI.add(testDisplay, "stepSize", 1, 4);
|
||||
// Boolean checkbox
|
||||
GUI.add(fizzyText, "displayOutline");
|
||||
|
||||
// Creates a text field
|
||||
GUI.add(testDisplay, "message");
|
||||
|
||||
// Creates a checkbox
|
||||
GUI.add(testDisplay, "displayOutline");
|
||||
|
||||
// Creates a button
|
||||
GUI.add(controllableObject, "functionProperty")
|
||||
.setName("Fire a Function");
|
||||
GUI.add(fizzyText, "explode")
|
||||
|
||||
};
|
||||
|
||||
@ -76,60 +44,24 @@
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1><a href = "http://twitter.com/guidat"><img src = "demo/assets/favicon.gif" border = "0" alt = "GUI-DAT flag" /></a><span id = "helvetica-demo"></span></h1>
|
||||
<div id = "helvetica-demo"></div>
|
||||
<h1><a href = "http://twitter.com/guidat"><img src = "demo/assets/favicon.gif" border = "0" alt = "GUI-DAT flag" /></a></h1>
|
||||
<p>
|
||||
<strong>gui-dat</strong> is a lightweight controller library for JavaScript. It allows you to easily manipulate variables and fire functions on the fly.
|
||||
</p>
|
||||
|
||||
<p>• <a href="#"><strong>Download the minified source</strong></a> <small>[2.3kb]</small><br/>
|
||||
• <a href="http://github.com/jonobr1/GUI-DAT">Contribute on GitHub!</a></p>
|
||||
<ul>
|
||||
<li><a href=""><strong>Download the minified source</strong></a> <small>[9.8kb]</small></li>
|
||||
<li><a href="http://github.com/jonobr1/GUI-DAT">Contribute on GitHub!</a></p></li>
|
||||
</ul>
|
||||
|
||||
<h2>Basic Usage</h2>
|
||||
<pre id="demo-pre" class="prettyprint"><span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="pln"> </span><span class="atn">src</span><span class="pun">=</span><span class="atv">"gui.min.js"</span><span class="tag">></script></span><span class="pln"><br></span><span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">></span><span class="pln"><br><br>window</span><span class="pun">.</span><span class="pln">onload </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br><br> </span><span class="kwd">var</span><span class="pln"> fizzyText </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ"><a href="demo/demo.js">FizzyText</a></span><span class="pun">(</span><span class="str">"gui-dat"</span><span class="pun">);</span><span class="pln"><br><br> GUI</span><span class="pun">.</span><span class="pln">start</span><span class="pun">();</span><span class="pln"><br> <br> </span><span class="com">// Adds a text field</span><span class="pln"><br> GUI</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">fizzyText</span><span class="pun">,</span><span class="pln"> </span><span class="str">"message"</span><span class="pun">);</span><span class="pln"><br> <br> </span><span class="com">// Adds sliders with min and max</span><span class="pln"><br> GUI</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">fizzyText</span><span class="pun">,</span><span class="pln"> </span><span class="str">"maxSize"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0.5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pun">);</span><span class="pln"><br> GUI</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">fizzyText</span><span class="pun">,</span><span class="pln"> </span><span class="str">"growthSpeed"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0.01</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">);</span><span class="pln"><br> GUI</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">fizzyText</span><span class="pun">,</span><span class="pln"> </span><span class="str">"speed"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0.1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span><span class="pln"><br> <br> </span><span class="com">// Adds sliders with min, max and increment.</span><span class="pln"><br> GUI</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">fizzyText</span><span class="pun">,</span><span class="pln"> </span><span class="str">"noiseStrength"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">100</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">);</span><span class="pln"><br> <br> </span><span class="com">// Adds a boolean checkbox</span><span class="pln"><br> GUI</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">fizzyText</span><span class="pun">,</span><span class="pln"> </span><span class="str">"displayOutline"</span><span class="pun">);</span><span class="pln"><br><br> </span><span class="com">// Adds a function button</span><span class="pln"><br> GUI</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">fizzyText</span><span class="pun">,</span><span class="pln"> </span><span class="str">"explode"</span><span class="pun">)</span><span class="pln"><br> <br></span><span class="pun">};</span><span class="pln"><br><br></span><span class="tag"></script></span></pre>
|
||||
|
||||
<pre id="demo-pre" class="prettyprint">
|
||||
<script type="text/javascript" src="gui.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
var controllableObject =
|
||||
{
|
||||
numberProperty: 20,
|
||||
constrainedNum: 0,
|
||||
notchedNum: 240,
|
||||
textProperty: "a string",
|
||||
anotherTextProperty: "another string",
|
||||
booleanProperty: false,
|
||||
anotherBooleanProperty: false,
|
||||
functionProperty: function() {
|
||||
alert("I am a function!");
|
||||
}
|
||||
};
|
||||
|
||||
window.onload = function() {
|
||||
|
||||
GUI.start();
|
||||
|
||||
// Creates a number box
|
||||
GUI.add(controllableObject, "numberProperty");
|
||||
|
||||
// Creates a slider (min, max)
|
||||
GUI.add(controllableObject, "constrainedNum", -100, 100)
|
||||
|
||||
// Creates a slider with notches
|
||||
GUI.add(controllableObject, "notchedNum", 0, 800, 100)
|
||||
|
||||
// Creates a text field
|
||||
GUI.add(controllableObject, "textProperty");
|
||||
|
||||
// Creates a checkbox
|
||||
GUI.add(controllableObject, "booleanProperty");
|
||||
|
||||
// Creates a button
|
||||
GUI.add(controllableObject, "functionProperty")
|
||||
.setName("Fire a Function");
|
||||
|
||||
};
|
||||
|
||||
</script></pre>
|
||||
<ul id="desc">
|
||||
<li><strong>gui-dat</strong> will infer the type of the property you're trying to add<br/>(based on its initial value) and create the corresponding control.</li>
|
||||
<li>The properties must be public, i.e. defined by <code><strong>this</strong>.prop = value</code>.</li>
|
||||
</ul>
|
||||
<footer>
|
||||
By <a href="http://georgemichaelbrower.com/">George Michael Brower</a>, <a href="http://jonobr1.com/">Jono Brandel</a>, and <a href="http://github.com/jonobr1/GUI-DAT">you</a>.
|
||||
</footer>
|
||||
|
Loading…
Reference in New Issue
Block a user