PDA

View Full Version : UI Design Patterns? Common structure with UI.


Omita
11-29-2007, 09:07 PM
I am trying to write a function that returns a boolean if the operation was successful. Pretty basic at first, but part of the function requires a UI input from the user. How can I have the function know that the use has inputed a field correctly without writing a "while loop" that is watching a global variable?

Does anyone have a basic UI example?

Omita
11-29-2007, 09:14 PM
Pseudo code to show my delema better.

proc int myFunction()
{

// function needs to return 1 if success and 0 for failure
if (default path exists)
{
return 0;
}else{
return `startUI()`; // having trouble with a UI Building script to actually return any data from the "startUI()" function back to myFunction(), because all the UI callbacks.
}

}

proc int startUI()
{
//creating Window and buttons
//but has no way of knowing the return values of the button callbacks?
}

proc okButtonCallback()
{
//Forces UI to change, but can't talk to startUI() function.
}

Omita
11-29-2007, 09:16 PM
Double post.

goleafsgo
11-30-2007, 12:49 AM
You can use the layoutDialog command to create a modal window. A modal window is one that holds onto the focus until you close it. eg. the file open dialog is a modal window.

Here is an example. Call layoutDialogTest with nothing selected and it just returns false. Call it with something selected and it pops up a custom window and waits for you to make a choice. It is set up so when you make a choice it will return the label of the button which you can then use to decide what to do next.


global proc layoutDialogUI()
{
// Get the dialog's formLayout.
//
string $form = `setParent -q`;

// layoutDialog's are not resizable, so hard code a size here,
// to make sure all UI elements are visible.
//
formLayout -e -width 300 $form;

string $t = `text -l "What do you want to do?"`;

// Note the return strings specified with the -dismiss flag. That's what
// is returned from the call to layoutDialog and that's what you can use to
// decide what to do next.
string $b1 = `button -l "Abort" -c "layoutDialog -dismiss \"Abort\""`;
string $b2 = `button -l "Skip" -c "layoutDialog -dismiss \"Skip\""`;
string $b3 = `button -l "Continue" -c "layoutDialog -dismiss \"Continue\""`;

string $cb1 = `checkBox -label "Remember my choice"`;

int $spacer = 5;
int $top = 5;
int $edge = 5;

formLayout -edit
-attachForm $t "top" $top
-attachForm $t "left" $edge
-attachNone $t "bottom"
-attachForm $t "right" $edge

-attachControl $b1 "top" $spacer $t
-attachForm $b1 "left" $edge
-attachNone $b1 "bottom"
-attachPosition $b1 "right" $spacer 33

-attachControl $b2 "top" $spacer $t
-attachPosition $b2 "left" $spacer 33
-attachNone $b2 "bottom"
-attachPosition $b2 "right" $spacer 66

-attachControl $b3 "top" $spacer $t
-attachPosition $b3 "left" $spacer 66
-attachNone $b3 "bottom"
-attachForm $b3 "right" $edge

-attachControl $cb1 "top" $spacer $b1
-attachForm $cb1 "left" $edge
-attachForm $cb1 "bottom" $spacer
-attachNone $cb1 "right"
$form;
}

global proc int layoutDialogTest()
{
if (size(`ls -sl`) > 0)
{
string $returnValue = `layoutDialog -ui "layoutDialogUI"`;
if ($returnValue == "Abort")
{
print ("You hit the Abort button\n");
}
else if ($returnValue == "Skip")
{
print ("You hit the Skip button\n");
}
else if ($returnValue == "Continue")
{
print ("You hit the Continue button\n");
}
else if ($returnValue == "dismiss")
{
print ("You closed the window\n");
}
else
{
print ("You shouldn't be able to hit this case.\n");
}

return true;
}
else
{
print ("Nothing selected.\n");
return false;
}
}

Omita
11-30-2007, 01:11 AM
Awesome, I was just about to post another exaple showing how utilizing errors wasn't working if a funciton was called that created a UI WIndow. But your example shows why.

But for those that wanted to see my example here it is:


proc test()
{
makeWindow();
print ("Don't Print this... \n");

}

proc makeWindow(){

if (`window -exists sighWindow`) deleteUI sighWindow;
// The main window;
window -wh 400 50 -title "Sigh" sighWindow ;
columnLayout -adjustableColumn true;
button -label "Cancel" -command "buttonCallbackFunction";
showWindow sighWindow;
}

proc buttonCallbackFunction()
{
error "Break?";
}

Omita
11-30-2007, 01:45 AM
The example above only using the formLayout dialogue.


proc test()
{
layoutDialogTest();
print ("Don't Print this... \n");

}

global proc layoutDialogUI()
{
// Get the dialog's formLayout.
//
string $form = `setParent -q`;

// layoutDialog's are not resizable, so hard code a size here,
// to make sure all UI elements are visible.
//
formLayout -e -width 300 $form;

string $t = `text -l "What do you want to do?"`;

// Note the return strings specified with the -dismiss flag. That's what
// is returned from the call to layoutDialog and that's what you can use to
// decide what to do next.
string $b1 = `button -l "Abort" -c "layoutDialog -dismiss \"Abort\""`;
string $b2 = `button -l "Skip" -c "layoutDialog -dismiss \"Skip\""`;
string $b3 = `button -l "Continue" -c "layoutDialog -dismiss \"Continue\""`;

string $cb1 = `checkBox -label "Remember my choice"`;

int $spacer = 5;
int $top = 5;
int $edge = 5;

formLayout -edit
-attachForm $t "top" $top
-attachForm $t "left" $edge
-attachNone $t "bottom"
-attachForm $t "right" $edge

-attachControl $b1 "top" $spacer $t
-attachForm $b1 "left" $edge
-attachNone $b1 "bottom"
-attachPosition $b1 "right" $spacer 33

-attachControl $b2 "top" $spacer $t
-attachPosition $b2 "left" $spacer 33
-attachNone $b2 "bottom"
-attachPosition $b2 "right" $spacer 66

-attachControl $b3 "top" $spacer $t
-attachPosition $b3 "left" $spacer 66
-attachNone $b3 "bottom"
-attachForm $b3 "right" $edge

-attachControl $cb1 "top" $spacer $b1
-attachForm $cb1 "left" $edge
-attachForm $cb1 "bottom" $spacer
-attachNone $cb1 "right"
$form;
}

global proc int layoutDialogTest()
{
if (size(`ls -sl`) > 0)
{
string $returnValue = `layoutDialog -ui "layoutDialogUI"`;
if ($returnValue == "Abort")
{
print ("You hit the Abort button\n");
}
else if ($returnValue == "Skip")
{
print ("You hit the Skip button\n");
}
else if ($returnValue == "Continue")
{
print ("You hit the Continue button\n");
}
else if ($returnValue == "dismiss")
{
print ("You closed the window\n");
}
else
{
print ("You shouldn't be able to hit this case.\n");
}

return true;
}
else
{
print ("Nothing selected.\n");
return false;
}
}

Omita
11-30-2007, 03:59 AM
Just finished my tool. That is some very good information to know. I've never had to hold up the script window with a UI before. So this is some very helpful information. I did notice that fileDialog had a different behavior the drawing a Window and I was pretty stumped there.

Thanks you so much.

Norb
11-30-2007, 07:03 PM
wow, that is handy. Wish I had know about this about 2 weeks ago =P

CGTalk Moderation
11-30-2007, 07:03 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.