Game Development Community

C++ Loop through all child GUI objects.

by Peterjohn Griffiths · in Torque Game Engine · 09/17/2006 (7:35 pm) · 5 replies

I am trying to make the GuiMLTextCtrl take into account any GUI object that are a child of it when resizing on the Y axix.

To do this I need to be able to loop though all the child objects of the current parent object in the function "GuiMLTextCtrl::reflow()" that can be found in the "guiMLTextCtrl.cc" file.

I think to achieve this I need to amend the value of mMaxY in the function GuiMLTextCtrl::reflow() just before the line
resize(mBounds.point, Point2I(mBounds.extent.x, mMaxY));

Does anyone have anyidea how I would find out what child objects a parent control has so I can loop through them.
If I was in script, I would use something like the following where mBounds points to the ID of the parent object.
%group = mBounds;
%count = %group.getCount();
for (%i=0; %i<%count; %i++){
	%tmpObj = %group.getObject(%i)
	if ((%tmpObj.position.y + %tmpObj.Extent.y) > %mMaxY){
		%mMaxY = ((%tmpObj.position.y + %tmpObj.Extent.y;
	}
}

Many thanks in advance.

#1
09/17/2006 (9:06 pm)
Well here is a snippet (that is no longer used) that I did to loop through all the GUI controls in some early versions of the TGB Trial (which is now much different)... this function was in GuiCanvas and did a recursive call to all child GUIs (which in turn did a recursive call to all their c hildren etc) to check if one was an "edit" scenegraph. This is no longer in the TGB source so I am not giving away anything about TGB (in fact nothing is really TGB specific here anyways).

Hope this helps :)

bool GuiCanvas::checkForT2DEdit(GuiControl *guiControl)
{
   t2dSceneWindow *t2dControl = dynamic_cast<t2dSceneWindow*>(guiControl);

   if(t2dControl)
   {
      if(t2dControl->getSceneGraph() && t2dControl->getSceneGraph()->getIsEditorScene())
		  return true;
   }

   iterator i;
   for(i = guiControl->begin(); i != guiControl->end(); i++)
   {
      GuiControl *ctrl = dynamic_cast<GuiControl*>(*i);
      if(ctrl && checkForT2DEdit(ctrl))
	     return true;
   }

   return false;
}
#2
09/17/2006 (10:34 pm)
After digesting your post and having a look around in the guiControl.cc 's "GuiControl::messageSiblings" function that looks like this.
void GuiControl::messageSiblings(S32 message)
{
   GuiControl *parent = getParent();
   if (! parent) return;
   GuiControl::iterator i;
   for(i = parent->begin(); i != parent->end(); i++)
   {
      GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
      if (ctrl != this)
         ctrl->onMessage(this, message);
   }
}

I noticed the use of the parent in the for loop's begin() and end().
I have come up with the following to find all child objects.
GuiControl::iterator i;
   for(i = this->begin(); i != this->end(); i++)
   {
      GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
         //ctrl is a child object of this.
   }

Does this look like i'm on the right track with the for loop?. At the line "//ctrl is a child object of this" would they all be child objects or would all GuiControls in existance be looped through in the for loop?.

Many thanks again in advance.
#3
09/18/2006 (12:08 am)
Yes, you are in the right track w/ the begin() and end() stuff,
but you need to test ctrl for NULL to see if it's actuall a GuiControl and not some other random simObject:

...
      GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
      if (ctrl != NULL)
         // ctrl is a child GuiControl of this control
      else
         // Could be anything; Skip it.
...
#4
09/18/2006 (12:39 am)
Thanks Orion Elenzil,
I will give it a go tonight and see if I can get it to work.
Thanks again.
#5
09/20/2006 (7:46 pm)
This code works great.
I have managed to implement it into several guiMLTextCtrl functions so that the control expands its extent to include any child gui controls.
This is great for when you want to add rich text to scroll bar's.
I am still working on a bug where the scroll bar scrolls to the top when ever the scrollbar resizes.
When this is fixed I will post the code.