Skip to content

Instantly share code, notes, and snippets.

@bennlich
Created December 15, 2013 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennlich/7970023 to your computer and use it in GitHub Desktop.
Save bennlich/7970023 to your computer and use it in GitHub Desktop.
agentscript-dat.gui.js
(function() {
ABM.DatGUI = (function() {
function DatGUI(fbui) {
var self = this;
this.gui = new dat.GUI();
this.model = {};
this.fbui = fbui;
fbui.fb.on('child_added', function(uiSnap) {
var uiEl = uiSnap.val(),
name = uiSnap.name();
if (uiEl.type != 'button') {
// init ui model
this.model[name] = uiEl.val;
// init fb listeners
uiSnap.child('val').ref().on('value', function(valSnap) {
this.model[name] = valSnap.val();
this.updateGui();
}, this);
}
// init ui ctrl
var ctrl = null;
switch(uiEl.type) {
case 'button':
this.model[name] = function() {
self.fbui.setUIValue(name, true);
};
this.gui.add(this.model, name);
break;
case 'choice':
ctrl = this.gui.add(this.model, name, uiEl.vals);
break;
case 'switch':
ctrl = this.gui.add(this.model, name);
break;
case 'slider':
ctrl = this.gui.add(this.model, name, uiEl.min, uiEl.max).step(uiEl.step);
break;
}
if (ctrl) {
ctrl.onChange(function(value) {
console.log(self);
self.fbui.setUIValue(name, value);
});
}
}, this);
}
DatGUI.prototype.updateGui = function() {
for (var i in this.gui.__controllers) {
this.gui.__controllers[i].updateDisplay();
}
}
return DatGUI;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment