javascript - Method for disabling a button in dat.gui? -
i trying figure out way disable/enable buttons within dat.gui.
i have dat.gui set control animation. when animation reaches end, want "play" button become disabled. have tried adding "disabled" attribute dom elements of button, still seeing corresponding function fire when button clicked after attribute set.
my current method following:
- locate
lielement corresponds button in dat.gui interface - create new dom element semi-transparent , black, , add inside
lielement gray out contents of button. - in function bound button, check existence of "disabled" dom element within button, , if exists, refrain executing rest of function.
this hack, , love know if there method disabling button built right dat.gui, or better method not aware of.
in dat.gui functioncontroller class responsible buttons. if @ its source code, there no conditional logic in there. controller listen click events on button , call function on click. means won't library here - need check in handler whether button disabled. along these lines:
// find gui controller listening given property on given object function getcontroller(gui, object, property) { (var = 0; < gui.__controllers.length; i++) { var controller = gui.__controllers[i]; if (controller.object == object && controller.property == property) return controller; } return null; } ... object.button = function() { // don't if button disabled if (getcontroller(gui, this, "button").domelement.hasattribute("disabled")) return; alert("button clicked"); }; gui.add(object, "button"); ... // disable button getcontroller(gui, object, "button").domelement.setattribute("disabled", ""); note there no special styling disabled elements in dom.gui, have add own styles that. given see in case of button property label rather actual button isn't going quite trivial - think have place disabled attribute on controller.domelement.parentnode rather controller.domelement. should able use selector [disabled] > .property-name styles.
edit: can in more generic way extending functioncontroller:
function blockevent(event) { event.stoppropagation(); } object.defineproperty(dat.controllers.functioncontroller.prototype, "disabled", { get: function() { return this.domelement.hasattribute("disabled"); }, set: function(value) { if (value) { this.domelement.setattribute("disabled", "disabled"); this.domelement.addeventlistener("click", blockevent, true); } else { this.domelement.removeattribute("disabled"); this.domelement.removeeventlistener("click", blockevent, true); } }, enumerable: true }); this add property disabled controller catch click events button handler isn't triggered. disabling button gets simpler then:
getcontroller(gui, object, "button").disabled = true; and button handler can stay unchanged, won't triggered disabled buttons.
Comments
Post a Comment