Gui Widget Scripting

From TorqueWiki

Jump to: navigation, search

Contents

[edit] GuiControl Callbacks

[edit] onWake

Called when the widget or its parent is made content of the canvas.

For a dialog when pushed.

[edit] onSleep

Called when the widget or its parent is removed from the canvas.

For a dialog when poped.

[edit] onCreate

Called when created within Wold-Editor Creator

[edit] onRemove

Hmm?? When it is removed with the editor?

[edit] Showing and Hideing Widgets

To show or a specific control and all its children, set its visible attribute to true:

selectGui.setVisible(true)
selectGui.visible = true;
selectGui.visible = 1;

or

%obj.setVisible(true)
%obj.visible = true;
%obj.visible = 1;


[edit] The "select" Widget

The select widget has to be limited to a section of the screen, so that it does not steal all input (mouse and keyboard) from the calling GUI (in our case the play GUI):

new GuiControl(selectGui) {
  canSaveDynamicFields = "0";
  Profile = "selectGuiWindowProfile";
  HorizSizing = "relative";
  VertSizing = "relative";
  Position = "190 166";
  Extent = "334 190";
  MinExtent = "8 2";
  canSave = "1";
  Visible = "1";
  hovertime = "1000";

  new GuiTextCtrl(info) {
    canSaveDynamicFields = "0";
    Profile = "selectGuiTextProfile";
    HorizSizing = "center";
    VertSizing = "center";
    Position = "104 81";
    Extent = "125 28";
    MinExtent = "8 2";
    canSave = "1";
    Visible = "1";
    hovertime = "1000";
    text = "info and explain ....";
    maxLength = "1024";
  };

  new GuiBitmapButtonCtrl(close) {
    canSaveDynamicFields = "0";
    Profile = "GuiButtonProfile";
    HorizSizing = "relative";
    VertSizing = "relative";
    Position = "100 100";
    Extent = "61 27";
    MinExtent = "8 2";
    canSave = "1";
    Visible = "1";
    //Command = "Canvas.popDialog(selectGui); ";
    Command = "info.setText(\"new text\"); ";
    hovertime = "1000";
    text = "done ";
    groupNum = "-1";
    buttonType = "PushButton";
  };
};

The button is "misused" to change the text of the info label, in the similar way gui elements may change each others properties and profiles.


The GUI can be added to the playGui in its onWake() function:

function PlayGui::onWake(%this)
{
  %this.add( selectGui );
  ...
}

The selectGui is shown or hidden from the playGui via two buttons:

  new GuiBitmapButtonCtrl(showSelect) {
      canSaveDynamicFields = "0";
      Profile = "GuiButtonProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "92 180";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
     Command = "selectGui.setVisible( true );";
     //Command = "selectGui.Visible = 1;";
      hovertime = "1000";
      text = "showSelect";
      groupNum = "-1";
      buttonType = "PushButton";
   };

   new GuiBitmapButtonCtrl(hideSelect) {
      canSaveDynamicFields = "0";
      Profile = "GuiButtonProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "92 220";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
     Command = "selectGui.setVisible( false );";
     //Command = "selectGui.Visible = 0;";
      hovertime = "1000";
      text = "hideSelect";
      groupNum = "-1";
      buttonType = "PushButton";
   };

[edit] Toggle Visibility

The "visible" parmeter does not seem to work reliable.

Add a text control to the playGui.gui:

  new GuiTextCtrl(blendText) 
    {
      canSaveDynamicFields = "0";
      internalName = "blendText";
      Profile = "GuiTextProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "300 100";
      Extent = "41 40";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      text = "----------blend text------------";
      maxLength = "255";
      // Custom stuff.
      isSetVisible="1";  // <- custom variable
  };

Or add the text control to the playGui.cs (do not do both):

function PlayGui::onWake(%this)
{
...
  %this.add( new GuiTextCtrl(blendText) 
    {
      canSaveDynamicFields = "0";
      internalName = "blendText";
      Profile = "GuiTextProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "300 100";
      Extent = "41 40";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      text = "----------blend text------------";
      maxLength = "255";
      // Custom stuff.
      isSetVisible="1"; // <- custom variable
    });
}

Then you may toggle the visibility of the text via button in the playGui:

   new GuiBitmapButtonCtrl(toggleScoreCounter) {
      canSaveDynamicFields = "0";
      Profile = "GuiButtonProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "92 260";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      Command = "PlayGui::toggleScoreCounter();"; // <- Call the toggle function
      hovertime = "1000";
      text = "toggleScoreCounter";
      groupNum = "-1";
      buttonType = "PushButton";
   };

The button calls a toggle function of the playGui:

function PlayGui::toggleScoreCounter( %this )
{
  if( Blendtext.issetvisible == 0 )
  {
    Blendtext.setVisible( true );
    Blendtext.issetvisible = 1;
  } 
  else
  {
    Blendtext.setVisible( false );
    Blendtext.issetvisible = 0;
  }
}


[edit] Pass Parameter from Button

Task: Identify the button which was pressed in the callback function.
The are several options to do this.

Create button, e.g., in playGui.gui:

   new GuiButtonCtrl(parameterButton) {
      canSaveDynamicFields = "0";
      Profile = "GuiButtonProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "100 253";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";

     //Command = "getParameter( 27 );";  <-- pass some value
     Command = "getParameter( "parameterButton" );"; <-- pass the object name

      hovertime = "1000";
      text = "Paramter";
      groupNum = "-1";
      buttonType = "PushButton";

      variable = "23"; <-- Set variable field within the button
      myVar = "77";    <-- Set your own variable
   };

Set the command field when you dynamically create the button either to its name or id or some other values you want to pass back to the button callback function.

This does not work:

   new GuiButtonCtrl(parameterButton) {
    ...
     Command = "getParameter( %this );";
     Command = "getParameter( %variable );";
    ...
   };

In client/scripts/playGui.cs:

Set the variable when the button comes up:

function parameterButton::onWake( %this )
{
  %var = 21;
  %this.Command = "getParameter(" SPC %var SPC ");";

  // or set

  //%this.Command = "getParameter( parameterButton );";
  %this.Command = "getParameter(" SPC %this.getName() SPC ");";
}

Check what arrives in the callback function for the button:

function getParameter( %p )
{
  echo( "-------------p" SPC %p );
  echo( "-------------p" SPC %p.getId() );  
  echo( "-------------p" SPC %p.getName() );  
  echo( "-------------p" SPC %p.variable );
  echo( "-------------p" SPC %p.myVar ); 
  echo( "var" SPC parameterButton.variable);
}
Personal tools
Games
Game Development
Modeling for Torque
Torque Game Builder (2D)
Console