Game Development Community

Push/pop a guiControl within a guiControl

by Teromous · in Torque Game Builder · 04/12/2008 (12:30 pm) · 5 replies

Hey guys, I'll summarize what I'm trying to accomplish. I want the player to be able to push a button to call a function, but I don't want this button to always be visible. When they push "b" the button will toggle on and off, allowing it to show on the screen or disappear.

Now I accomplished this, but when the button was pushed and the function was called, it would wait for mousedown feedback. The player can not click anything on the screen because the gui was pushed on TOP of the PlayScreen gui. The player COULD hit the "b" button to pop the gui, then click on the screen (which ended up working) but I wanted the player to be able to click someplace on the screen (not just the button) whether the button was visible or not.

I figured that by adding the button's guiControl code inside the PlayScreen.gui's guiControl that the player could click the button and click on the screen without problem. There was a problem though...which is that now I can't figure out how to popDialog the button now!

Here's what I was using to push/pop:

moveMap.bindCmd(keyboard, "b", TheButton @ ".ToggleTheButton();", "");

// toggle TheButton to open or close
function TheButton::ToggleTheButton(%this)
{
if(!%this.isAwake())
{
Canvas.pushDialog(%this);
}
else
{
Canvas.popDialog(%this);
}
}



Here's the gui condensed (because there's unneeded stuff in there):


%guiContent = new GuiChunkedBitmapCtrl(PlayScreenGui) {
canSaveDynamicFields = "0";
isContainer = "1";
Profile = "GuiContentProfile";
HorizSizing = "width";
VertSizing = "height";
Position = "0 0";
Extent = "640 480";
MinExtent = "8 8";
canSave = "1";
Visible = "1";
hovertime = "1000";
bitmap = "~/data/images/logoblack";
useVariable = "0";
tile = "0";

new t2dSceneWindow(sceneWindow2D) {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiContentProfile";
HorizSizing = "width";
VertSizing = "height";
Position = "0 0";
Extent = "640 480";
MinExtent = "8 8";
canSave = "1";
Visible = "1";
hovertime = "1000";
lockMouse = "0";
useWindowMouseEvents = "1";
useObjectMouseEvents = "1";
};


new GuiControl(TheButton) {
canSaveDynamicFields = "0";
isContainer = "1";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "0 416";
Extent = "640 64";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
useWindowMouseEvents = "0";


new GuiBitmapButtonCtrl(ButtonOne) {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "330 0";
Extent = "64 64";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
command = "ButtonOne();";
tooltipprofile = "GuiDefaultProfile";
ToolTip = "Button One";
hovertime = "1000";
groupNum = "-1";
buttonType = "PushButton";
useMouseEvents = "1";
bitmap = "~/data/images/ButtonPics/ButtonOne.png";
useWindowMouseEvents = "0";
};
};


};




I was thinking about using .setVisible but just not knowing if there is a way to push or pop a guiControl within another guiControl is unnerving.

#1
04/12/2008 (1:38 pm)
You cannot push/pop a ctrl -within- another control. Push/pop operates on the canvas level only. I think your best bet is setvisible.
#2
04/12/2008 (2:02 pm)
James is on the money.
For controlling the visibility of individual buttons (or other guiControls), setVisible() is probably what you want.
#3
04/12/2008 (2:20 pm)
Alternatively, you can add/remove objects within that control.
#4
04/12/2008 (2:44 pm)
I have tried add/remove(ing) controls dynamically before with unusual results, I also remember seeing on the forums before that it is not "reccomended". But not sure where/who said that.
#5
04/15/2008 (10:10 pm)
I've got a little system that I use... Maybe it will help... If you need more code to make sense of it, let me know. I had to mix push/remove to get my menus to build and implode properly.

/---------------------------------------------------------------------------------------------
// GUI WINDOW BUILDER
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// Get windows to load when the scene is created.
//    -scenegraph: the scene that has been loaded.
//    -this: the GUID assigned to the scene.
//---------------------------------------------------------------------------------------------
function menu::onLevelLoaded(%this, %scenegraph)
{   
   $menuID = %this;
   
   %this.mainMenu();
}

//---------------------------------------------------------------------------------------------
// Open a window.
//---------------------------------------------------------------------------------------------
function menu::createMenu(%this)
{
   %name = "base" @ $menuDepth @ "Gui";   
   Canvas.pushDialog(%name);   
}

//---------------------------------------------------------------------------------------------
// Close a window.
//---------------------------------------------------------------------------------------------
function menu::closeMenu(%this)
{
   %name = "base" @ $menuDepth @ "Gui";      
   Canvas.remove(%name);
}


//---------------------------------------------------------------------------------------------
// Add a window GUI.
//---------------------------------------------------------------------------------------------
function menu::addBase(%this)
{  
   // create the window base
  if ($menuDepth == 0) 
      %this.GuiBase0();
   else if ($menuDepth == 1)
      %this.GuiBase1();       
   else if ($menuDepth == 2)
      %this.GuiBase2();   
}

//---------------------------------------------------------------------------------------------
// Add a button to a GUI.
//   -position: the position of the button in the window. Ex: "0 0"
//   -extent: the size of the button. Ex: "10 10"
//   -command: button event. Ex: "menu::exit(menuGui);" @ "menu::optionsMM(menuGui);"
//   -bitmap: the graphic to use. EX: "button"
//   -text: the text to appear on the button. EX: "Start Game" or $strButton[0]
//   -profile: the style of the text. EX: $profile[2]
//---------------------------------------------------------------------------------------------
function menu::addButton(%this, %text, %bitmap, %profile, %position, %extent, %command)
{   
   // adjust input values
   %bitmap = "~/data/images/" @ %bitmap @ ".png";

   // create the button
   %this.GuiButton();  
   
   // customize the button   
   buttonGui.text = %text;    
   buttonGui.bitmap = %bitmap; 
   buttonGui.profile = %profile;
   buttonGui.position = %position; 
   buttonGui.extent = %extent;          
   buttonGui.command = %command; 
   
   // add the button to the window
   %name = "base" @ $menuDepth @ "Gui";  
   %name.addGuiControl(buttonGui);   
}

//---------------------------------------------------------------------------------------------
// Change index assigned to menu to indicate depth.
//  -EX: 0=Menu, 1=Sub-menu, 2=Sub-sub-menu 
//---------------------------------------------------------------------------------------------
function menu::changeMenuDepth(%this)
{   
   $menuDepth = %this; 
}


//---------------------------------------------------------------------------------------------
// GUIs
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// GUI Base 1
//---------------------------------------------------------------------------------------------
function menu::GuiBase0(%this)
{
    new GuiControl(base0Gui) 
    {
       Profile = "GuiDefaultProfile";
       HorizSizing = "relative";
       VertSizing = "relative";
       Extent = "800 600";
       Visible = "1";
       opaque = true; 
    };
}

//---------------------------------------------------------------------------------------------
// GUI Base 2
//---------------------------------------------------------------------------------------------
function menu::GuiBase1(%this)
{
    new GuiControl(base1Gui) 
    {
       Profile = "GuiDefaultProfile";
       HorizSizing = "relative";
       VertSizing = "relative";
       Extent = "800 600";
       Visible = "1";
    };
}

//---------------------------------------------------------------------------------------------
// GUI Base 3
//---------------------------------------------------------------------------------------------
function menu::GuiBase2(%this)
{
    new GuiControl(base2Gui) 
    {
       Profile = "GuiDefaultProfile"
       HorizSizing = "relative";
       VertSizing = "relative";
       Extent = "800 600";
       Visible = "1";
    };
}

//---------------------------------------------------------------------------------------------
// Button GUI
//---------------------------------------------------------------------------------------------
function menu::GuiButton(%this)
{
   new GuiBitmapButtonTextCtrl(buttonGui) 
   {  
      Profile = "";    
      HorizSizing = "relative";
      VertSizing = "relative";
      Visible = "1";
   };
}

//---------------------------------------------------------------------------------------------
// GUI WINDOWS
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// Main Menu window.
//---------------------------------------------------------------------------------------------
function menu::mainMenu(%this)
{   
    // ID Assigned To The Window
    $menuID = %this;
   
    // Build Window
    %this.addBase();
    
    // Start Game - Difficulty Level Button
    %text = %this.loadButtonLang(1);
    %this.addButton(%text, empty, $profile[2], "510 295", "220 30", "menu::changeMenuDepth(1); menu::closeMenu($menuID); menu::difficultyMenu($menuID);");
        
    // Quit Game Button
    %text = %this.loadButtonLang(4);    
    %this.addButton(%text, empty, $profile[2], "510 400", "220 30", "menu::quitGame();");
    
    // Create Menu            
    %this.createMenu();
}

//---------------------------------------------------------------------------------------------
// Difficulty Mode window
//---------------------------------------------------------------------------------------------
function menu::difficultyMenu(%this)
{   
    // ID Assigned To The Window
    $menuID = %this;
       
    // Build Window
    %this.addBase();
    
    // Easy Mode Button
    %text = %this.loadButtonLang(5);
    %this.addButton(%text, button1, $profile[10], "125 210", "140 30", "menu::closeMenu($menuID); menu::setDifficulty(1); menu::changeMenuDepth(0); menu::closeMenu($menuID);");
    
    // Normal Mode Button
    %text = %this.loadButtonLang(6);
    %this.addButton(%text, button1, $profile[10], "325 210", "140 30", "menu::closeMenu($menuID); menu::setDifficulty(2); menu::changeMenuDepth(0); menu::closeMenu($menuID);");
    
    // Difficult Mode Button
    %text = %this.loadButtonLang(7);
    %this.addButton(%text, button1, $profile[10], "525 210", "140 30", "menu::closeMenu($menuID); menu::setDifficulty(3); menu::changeMenuDepth(0); menu::closeMenu($menuID);");
    
    // Back Button
    %this.addBack("370 260", "menu::closeMenu($menuID); menu::changeMenuDepth(0);");
    
    // Create Menu            
    %this.createMenu();
}