Game Development Community

GuiTextEditCtrl validate and finding controls in a GUI

by Demolishun · in Torque 3D Professional · 08/11/2012 (5:10 pm) · 6 replies

First I wanted to share how to find a control even if the name of the control conflicts in the global namespace:
function findSubGUI(%this,%name){
   for(%i = 0; %i < %this.getcount(); %i++){  
      %gui = %this.getObject(%i);  
      if(isObject(%gui))
         if(%gui.name $= %name)  
            return %gui;
   } 
   
   return 0;
}
Just provide the top gui control name for %this and the name of the sub control for %name.


Now I wanted to share how to use validate on a GuiTextEditCtrl:
function GenTerrainGui::validateInputs(%this){
   // check for valid filename
   %filenameobj = findSubGUI(%this,"TerrainFilename");
   if(%filenameobj){
      %filename = trim(%filenameobj.getText());      
      %len = strlen(%filename);
      %end = strlwr(getSubStr(%filename,%len-4,4));      
      if(%end !$= ".ter"){
         %filenameobj.setText(%filename @ ".ter");
      }else{
         %filenameobj.setText(%filename);
      }
   }
}
I had tried using .text to get the value of the text, and to set the value of the text. This does not work. To get the value of the text in the field of the widget you must use getText() otherwise you get the old value. SetText seems to work to set the actual value. Not sure if it differs from .text = <value>. However, it seems to work.

I am posting this as a thread rather than a resource to solicit any better ideas for how to do the above. I took some trial and error to get this working and I am hoping there might be better ways to do this.

About the author

I love programming, I love programming things that go click, whirr, boom. For organized T3D Links visit: http://demolishun.com/?page_id=67


#1
08/11/2012 (8:04 pm)
Okay, now I found a better way for finding gui elements:
function findSubGUI(%this,%name){
   // search for gui, even subchildren
   %gui = %this.findObjectByInternalName(%name,true);
   
   return %gui;   
}
This uses the internalName of the objects.
#2
08/12/2012 (12:58 am)
frank,
which one faster?
using a name for each gui component
or
finding using their internal name?

i think last one is faster as it makes searching scope smaller.

and
anyone know on which situation performance is a a matter for gui?
#3
08/12/2012 (8:56 am)
There's another a way to retrieve child GUI controls by internal name using the following operators:
-> // get immediate child control of parent by internal name.
--> // get a child control that resides within parent tree, does a recursive search for child, by internal name.

Examples:
%parent->childInternalName; // get control with internal name of 'childInternalName' that is a child of %parent control.
%root-->childInternalName; // get control with internal name of 'childInternalName' that is a child somewhere within %root control's hierarchy.

The Torque built-in editors make extensive use of these operators.
#4
08/12/2012 (1:19 pm)
@ashan,
I think the second one is faster using the interalName. The reason is not because it is a smaller set as the script version is actually only searching the first level of widgets within the gui itself. The second one is using a C++ function designed to search within the same gui and if you set the second parameter to true it searches recursively through every sub gui of every sub gui. Even using the recursive option I bet it is faster because it is not pure script.

@Nathan,
Thanks for the heads up on those. I just tested the finding of anything inside the root of the gui and it completely replaces the findSubGUI function. What a nice find!
#5
08/12/2012 (1:33 pm)
@Nathan,
I tested this out on SimGroups and it works for those as well. You just set the internalName and it works for that. This is a very versatile way to access sub objects in a SimGroup!
#6
08/12/2012 (4:08 pm)
Yep, the -> and --> syntax sugar was added just for this sort of thing.