getId(), "NewDynam"> Is this a good idea ? a little OOPification of GuiControls | Torque Game Engine | Forums | Community | GarageGames.com

Game Development Community

Is this a good idea ? a little OOPification of GuiControls

by Orion Elenzil · in Torque Game Engine · 09/19/2008 (4:33 pm) · 6 replies

Anyone know how to add a dynamic field to a ConsoleObject from in C ?

or rather,
does anyone know a better way than using Con::evaluatef(),
as the GuiInspector does here:
Con::evaluatef( "%d.%s = \"Default Value\";", mTarget->getId(), "NewDynamicField" );

tia!

.. woops, never mind, here we go:
object->setDataField(StringTable->insert(argv[i + 1]), NULL, argv[i]);

not sure why GuiInspector does it that way..

ooo

#1
09/19/2008 (5:05 pm)
Okay, the reason i was doing this
is because i'm adding a new console method to SimSet: addWithField().

%controlA.addWithField(%controlB, <fieldName>);

is equivelant to doing this:

%controlA.add(%controlB);
%controlA.<fieldName> = %controlB;

i know that some folks recommend against building GUIs dynamically,
but i/we tend to do quite a lot of it.
i long ago got over my surprise that they are so un-object-oriented.
for example, if i add a child control to a parent in the usual fashion,
the parent does not have a member variable which is the child,
and if the child is something you'll want to manipulate later (in realtime),
it's generally given a name in the global namespace.

so i'm thinking about experimenting with making it a little more OOP-like.

here's an example usage from the command line:

==>canvas.getContent().addWithField(new GuiButtonCtrl() { text="la la la"; }, "mPanicButton" );
==>canvas.getContent().dumpFields();
7974-GuiChunkedBitmapCtrl-LoginGui
Member Fields:
  <snip>
Tagged Fields:
  mPanicButton = "8887" <----------------- ta da


my question now is: from a GuiControl architecture point of view, does this seem like a good idea ?
#2
09/19/2008 (5:07 pm)
Here's the new consolemethod if anyone's interested.
i'll submit it as a resource after kicking it around a bit.
// oxe 20080919
// takes a list of <object, fieldName> pairs,
// and for each pair, adds <object> to the SimSet as normal,
// but also sets a field in the simSet named <fieldName> who's value is <object>.
// so eg
// %myGuiControl.addWithField(%someOtherGuiControl, "mTitleBar")
// results in %someOtherGuiControl being a child guiControl like normal,
// but also %myGuiControl.mTitleBar will equal %someOtherGuiControl.
//
// if the number of parameters is odd, the last object in the set will still be added, but not given a field.
ConsoleMethod(SimSet, addWithField, void, 3, 0, "set.add(obj1, name1, obj2, name2,...)")
{
   for(S32 i = 2; i < argc; i += 2)
   {
      SimObject *obj = Sim::findObject(argv[i]);
      if(obj)
      {
         object->addObject(obj);
         if (i + 1 < argc)
         {
            // here we are..
            object->setDataField(StringTable->insert(argv[i + 1]), NULL, argv[i]);
         }
      }
      else
      {
         Con::printf("Set::add: Object \"%s\" doesn't exist. %s", argv[i], getConsoleTrace());
      }
   }
}
#3
09/19/2008 (5:17 pm)
GuiControl also derives from SimGroup, so its pretty easy to loop through its children controls the same way you would with a SimGroup. If you want to find a particular one you would want to give your control an internalName and do something like this..

// Looks in the Set and all sub-sets
%ctrl = ParentControl-->ChildsInternalName;

// Does not look in sub-sets
%ctrl = ParentControl->ChildsInternalName;

The -> operator is a fairly new thing to TorqueScript, its actually the same as calling findByInternalName("string"), which is a method of SimSet.
#4
09/19/2008 (5:22 pm)
Hm, that's an interesting alternative.
the syntax is certainly cleaner.

we're based on TGE 1.3,
but even without the "->" operator, using findByInternalName() may be cleaner.

does that check all descendants, or just immediate children ?

thanks for the ideas!
#5
09/19/2008 (5:28 pm)
Looks like just immediate children.
thanks for pointing this out, James!

i knew about findObjectByInternalName(), i just hadn't thought of using it in this situation.
#6
09/19/2008 (5:44 pm)
As I said, --> checks all children, -> checks just immediate children. FindObjectByInternalName takes an optional bool for specifying this.