I am trying to figure out a way to easily disable/enable buttons within dat.gui.
I have dat.gui set up to control an animation. When the animation reaches its end, I want the "Play" button to become disabled. I have tried adding a "disabled" attribute to the DOM elements of the button, but I am still seeing the corresponding function fire when the button is clicked after this attribute is set.
My current method is the following:
- Locate the
li
element that corresponds to the button in the dat.gui interface - Create a new DOM element that is semi-transparent and black, and add this inside the
li
element to gray out the contents of the button. - In the function bound to this button, check for the existence of this "disabled" DOM element within the button, and if it exists, refrain from executing the rest of the function.
This is a hack, and I would love to know if there was some method for disabling a button built right into dat.gui, or some better method that I am not aware of.
In dat.GUI the
FunctionController
class is responsible for buttons. If you look at its source code, there is no conditional logic in there. The controller will listen toclick
events on the button and it will always call the function on click. This means that you won't get any help from the library here - you need to check in the handler whether the button is disabled. Something along these lines:Note that there is no special styling for disabled elements in dom.GUI, you would have to add you own styles for that. Given that what you see in case of a button is the property label rather than the actual button this isn't going to be quite trivial - I think you will have to place the
disabled
attribute oncontroller.domElement.parentNode
rather thancontroller.domElement
. Then you should be able to use the selector[disabled] > .property-name
for your styles.Edit: You can actually do this in a more generic way by extending
FunctionController
:This will add a property
disabled
to the controller that will catchclick
events so that the button handler isn't triggered. Disabling the button gets simpler then:And the button handler can stay unchanged, it simply won't be triggered for disabled buttons.