View Single Post
# 1 08-03-2006 , 09:43 AM
skywola's Avatar
Registered User
Join Date: Jan 2004
Location: Tempe, Arizona, USA
Posts: 224

Toggling a button ON - OFF

In another thread, JDigital did me the favor of mentioning the jsOrientJointUI script. . . . I had been looking for a script that could use a toggle button function, because I wanted to write one. I had been contemplating writing one for the Walkerman program that I wrote, but found that it would be impractical for that particular application, mainly because I was considering toggling a key-unkey function, but once the frame changed, I would have had to update every function that was keyable, so there was no point in persueing that.

Anyway the joint orient function fits the bill, so here is the main modifications I added to it.

It had two buttons, and ON button and an OFF button. I removed anything in the script referring to the OFF button, and removed the line of code that created he ON button, then I added this code:

// Create button with the toggle command
string $button = `button -label "On" toggle`;
button -edit -command ("toggleButton " + $button) $button;

Note that the first line of code names the button "toggle"

Then I added this procedure:

global proc toggleButton (string $whichButton) {
string $labelA; string $labelB; string $currentLabel;
$labelA = "On";
$labelB = "Off";
$currentLabel = `button -query -label $whichButton`;
if ($currentLabel != $labelA){
button -edit -label $labelA $whichButton;
jsDisplayAxisOnOff 1; // command to toggle on
} else {
button -edit -label $labelB $whichButton;
jsDisplayAxisOnOff 0; // command to toggle off

The tricky part of this procedure is that it must contain a command for whatever you want to do in your procedure, so as the code stands, you could not just create a generic toggleButton procedure, as the code is written. You be able to do it by passing in the commands as parameters I suppose, but when I am coding, as a general rule, I keep it simple, then later on when I feel energetic, I look for ways to improve the code.

Anyways as it stands, the procedure is imbedded in the jsOrientJointUI script, so it can call the "jsDisplayAxisOnOff 0;"
and "jsDisplayAxisOnOff 1;" without bothering to pass the parameters into the procedure. Maybe I'll get energetic later and write a generic toggleButton procedure that can be outside of the script. . . .

The new script, after my hacking on it I named jsOrientJointUIR
and will provide it for anyone who wants to look at it or try it out. . . .

Maybe someone else will feel energitic before I do and write the toggleButton script so it includes the command parameters . . .
user added image

BTW, I tried contacting the original author of this function, as there was an email listed for him, but the email address is evidently out of date. . . .

Attached Files
File Type: mel jsorientjointuir.mel (14.4 KB, 342 views)

"The Sage as an Astronomer: If you still see the stars as something above you, you lack the eye of knowledge." Friedrich Nietzsche