Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Programming (https://simplymaya.com/forum/forumdisplay.php?f=32)
-   -   Toggling a button ON - OFF (https://simplymaya.com/forum/showthread.php?t=20548)

skywola 08-03-2006 09:43 AM

Toggling a button ON - OFF
 
1 Attachment(s)
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 . . .
:alien:

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. . . .

vladimirjp 09-03-2006 01:22 AM

cant u just copy and paste the commands of the original script?

or make your script call the joint orient proc

skywola 09-03-2006 06:09 AM

I guess I have been staying up to late and can no longer afford to pay attention . . . .

I don't see a need to pass parameters, as this toggleButton procedure is just calling another global procedure, so it works well as it stands.

Because of that, there is no need to make the script call the joint orient proc, it is good as it stands. My coding was good, but my analysis of it afterward was flawed . . . .

I usually go to bed around 12:00pm, but some nights I wake up at about 2:00AM, and then stay up coding or goofing around till about 4:00AM, then go back to bed and get up at about 8:30, go to work at 9:45 on the days I work and get home about 10:30PM . . . . goofy scedule but I like it.

I guess what is important is that you DO have to have particular commands for each click of the button if you are going to create a toggle button, and most likely you would want the commands to invoke another procedure. The code should be good enough as an example for anyone who wants to create one for their own purposes.


All times are GMT. The time now is 10:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018