Game Development Community

dev|Pro Game Development Curriculum

Get Top Control from Canvas

by Jaimi McEntire · 01/13/2009 (1:29 pm) · 3 comments

in GuiCanvas.h, add the following declaration after the declaration for getContentControl:

// Gets the dialog at the top of the stack.
   virtual GuiControl *getTopControl();

in GuiCanvas.cpp, add the following two functions:
GuiControl *GuiCanvas::getTopControl()
{
   if (size() < 1)
      return NULL;

   GuiControl *ctrl = NULL;

   ctrl = static_cast<GuiControl*>(last());

   return ctrl;
}

ConsoleMethod( GuiCanvas, getTopControl, S32, 2, 2, "Get the GuiControl (dialog) which is on top.")
{
   GuiControl *ctrl = object->getTopControl();
   if(ctrl)
      return ctrl->getId();
   return -1;
}

Here is an example of use. When you press "escape" while playing a mission, a dialog is displayed asking if you wish to exit. But pressing escape again does not dismiss it. while there are many ways to change this behavior, it is quite easy by modifying the EscapeFromGame to use the new function as follows. In defaults.bind.cs, change the EscapeFromGame function as follows:

function escapeFromGame()
{
   if (Canvas.getContent() == Canvas.getTopControl())
   {
      if ( $Server::ServerType $= "SinglePlayer" )
         MessageBoxYesNo( "Quit Mission", "Exit from this Mission?", "disconnect();", "");
      else
         MessageBoxYesNo( "Disconnect", "Disconnect from the server?", "disconnect();", "");
   }
   else
   {
      Canvas.popDialog( Canvas.getTopControl());
   }
}

Now, any dialog that you pop up that is not the playgui can be dismissed with escape.

#1
01/14/2009 (1:32 am)
nice one! ty!
#2
01/17/2009 (9:41 pm)
I love any and every add-on or improvement to the GUI system. Nice work! Very useful =)
#3
01/18/2009 (7:25 am)
@Michael - Great! I have a couple more changes that I've been meaning to post: An updated GUIGameListMenuControl that gives you more control over where things are placed, and exposes some additional functions to scripting, and A new container that uses a single bitmap with margins (as opposed to a Bitmap Array) and can tile and stretch.