Torque 2D/Getting Started/GuiControlListPopUp

From TDN

Introduction


I needed to add a dropdown box , with some custom values for my player. I hacked around on it a bit and came up with this simple Step by Step for those needing to accomplish the same thing.

Creating the GUI


First I started by Creating a new T2D Project as a sandbox.
I then proceeded to the GUI Editor
Click on Project -> GUI Editor....

Image:t2d-example-GuiControlListPopUp-1.png

The GUI Editor Screen
Image:t2d-example-GuiControlListPopUp-2.png

Once open click on File->New GUI....

Image:t2d-example-GuiControlListPopUp-3.png

Give your new GUI a name I've Chosen Example1 for mine.
Click on Create.

Image:t2d-example-GuiControlListPopUp-4.png

At this point you should have a blank GUI Canvas.
Go Ahead and click on "New Control"

Browse down the list until you get to GuiControlListPopUp.
Select that and a new GuiControlListPopUp control will be placed on your form.

Image:t2d-example-GuiControlListPopUp-5.png

Place the control where you would like, and slide the Screen Splitter over a bit so we can see our settings better

Image:t2d-example-GuiControlListPopUp-6.png

Lets Go Ahead and Give the control a Name. I used "selectionChooser"
fill in the text , I used "Select an Option"
Dont Forget to press "APPLY", or the settings wont be saved with your GUI
Image:t2d-example-GuiControlListPopUp-8.png

Go ahead and save your GUI under whatever name you like. I used "exampleScreen.gui"

Image:t2d-example-GuiControlListPopUp-10.png

Now that the Gui has been saved, open up "exampleScreen.gui" in the editor of your choice.
You should have something similar to the following.

//--- OBJECT WRITE BEGIN ---
new GuiControl(Example1) {
   canSaveDynamicFields = "0";
   Profile = "GuiDefaultProfile";
   HorizSizing = "right";
   VertSizing = "bottom";
   position = "0 0";
   Extent = "800 600";
   MinExtent = "8 2";
   canSave = "1";
   Visible = "1";
   hovertime = "1000";

   new   GuiControlListPopUp(selectionChooser) {
      canSaveDynamicFields = "0";
      Profile = "GuiControlListPopupProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      position = "37 162";
      Extent = "270 18";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      text = " Select an Option";
      maxLength = "1024";
      maxPopupHeight = "200";
      sbUsesNAColor = "0";
      reverseTextList = "0";
      bitmapBounds = "16 16";
   };
};
//--- OBJECT WRITE END ---


Like most torque Objects, since we named the control in the GUI editor (selectionChooser), we can now use that to configure the control.
somewhere after you have "exec" the gui ....

// Clear our Drop Down. 
selectionChooser.clear();


If we had not cleared this, it would have the default list of GUI objects like it does in the GUI editor.
Now we can setup some options to pick from

//add a few options to pick from
selectionChooser.add("Apple",0,0);
selectionChooser.add("Orange",1,0);
selectionChooser.add("Cherry",2,0);


You'll notice how we manually set the index using parameter 2 of the add call.
Just to make sure the list gets sorted by index. we can make a call to sort

selectionChooser.sort();


We can now use our new GUI by making the appropriate calls when needed.

$playerFavoriteFruitId = selectionChooser.getSelected();
$playerFavoriteFruitText = selectionChooser.getTextById($playerFavoriteFruitId);


Modify our project to load the gui

exec("~/gui/exampleScreen.gui");


Get our project to launch the proper content.

Canvas.setContent(Example1);



Launch Your Project. And Select some fruit.
Image:t2d-example-GuiControlListPopUp-11.png
Enjoy!

--Rodney Rindels 08:08, 13 May 2006 (PST)