Game Development Community

Any way to Autoclear a GuiTextEditCtrl on First Access?

by Harry Durnan · in Torque Game Builder · 01/22/2012 (10:41 am) · 9 replies

I am trying to find a way to remove default text in a Text entry field the first time you click on it. Right now I have a handful of text entry points that come up with inital values like "Character Name Here", and currently you have to delete that out before putting in something else.

I tried giving the GuiTextEditCtrl entries a defined class, and then defining an onMouseDown function to try to clear them, but echoing out the code it wasn't triggering when I clicked on the fields.

I -could- just make them blank fields and use t2dTextObjects next to them as description fields, but I think having them initially populated with a description looks a bit better. Anyone have any idea how to get this sort of functionality?

#1
05/04/2012 (6:31 pm)
Dredging up this thread again... I've been poking at this some more, and I think my issue is I can't seem to get any callbacks to work for GuiTextEditCtrl. I've tried onMouseUp, onMouseDown, onSelect, onGainFirstResponder and nothing seems to echo back to the log. Is this an actual bug, working as designed, or am I just going about it with the wrong commands?
#2
05/05/2012 (1:05 am)
I'm not sure what you want to do. But I want to help as much as I can.
I'm no pro at all, I'm an artist.
I made these functions for my scorpions to be killed by mouse clicks and update Text object so the number 15 ticks down on every scorpion click.(text object I dragged and dropped on the screen in the TGB, so I guees it is not guiText)
function Counter::onLevelLoaded(%this, %scenegraph)    
 {      
       $g_whackedCount = 15;    
       // Reset the GuiTextCtrls    
       GuiWhacked.text = "Whacked:" SPC $g_whackedCount;    
 }     

function Scorpion01:: onMouseDown (%this, %modifier, %worldPosition, %clicks)
 {  
         $g_whackedCount--;
         GuiWhacked.text = "Whacked:" SPC $g_whackedCount;

         %this.safedelete(); 
 
         return;
      
  }

Not sure if it helps, or has any relevance to your problem.
But if you could post your code other people might find out where the problem is.
#3
05/05/2012 (6:32 am)
I can get callbacks to work on t2d objects just fine, but not GUI elements.

%guiContent = new GuiControl(mainScreenGui) {
   canSaveDynamicFields = "0";
   isContainer = "1";
   Profile = "GuiBlackContentProfile";
   HorizSizing = "width";
   VertSizing = "height";
   Position = "0 0";
   Extent = "800 600";
   MinExtent = "8 8";
   canSave = "1";
   Visible = "1";
   hovertime = "1000";

   new t2dSceneWindow(sceneWindow2D) {
      canSaveDynamicFields = "0";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "width";
      VertSizing = "height";
      Position = "0 0";
      Extent = "800 600";
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      lockMouse = "0";
      useWindowMouseEvents = "1";
      useObjectMouseEvents = "1";
   };
   new GuiTextEditCtrl(nameEntryBoxCharacter) {
      canSaveDynamicFields = "1";
      isContainer = "0";
      Profile = "GuiTextEditProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "343 313";
      Extent = "116 18";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      text = "Character Name";
      maxLength = "40";
      historySize = "0";
      password = "0";
      tabComplete = "0";
      sinkAllKeyEvents = "0";
      password = "0";
      passwordMask = "*";
      first = "0";
   };
   new GuiTextEditCtrl(guildEntryBox) {
      canSaveDynamicFields = "1";
      isContainer = "0";
      Profile = "GuiTextEditProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "341 218";
      Extent = "116 18";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      text = "Guild Name";
      maxLength = "40";
      historySize = "0";
      password = "0";
      tabComplete = "0";
      sinkAllKeyEvents = "0";
      password = "0";
      passwordMask = "*";
      first = "0";
   };
   new GuiTextEditCtrl(nameEntryBoxPlayer) {
      canSaveDynamicFields = "1";
      isContainer = "0";
      Profile = "GuiTextEditProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "344 267";
      Extent = "116 18";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      text = "Your Name Here";
      maxLength = "40";
      historySize = "0";
      password = "0";
      tabComplete = "0";
      sinkAllKeyEvents = "0";
      password = "0";
      passwordMask = "*";
      first = "0";
   };
};

Then trying to do either:

function nameEntryBoxPlayer::onMouseUp(%this)
{
echo("hit");
}

nor-

function GuiTextEditCtrl::onMouseUp(%this)
{
echo("hit");
}

seems to register the echo.
#4
05/05/2012 (11:40 am)
Well, hmm. You could try calling "dump" on the object and checking the console. From there you can find out everything about the object, such as if it is using mouse events and what callbacks/methods it has exactly.

That's the first thing I would try at least, the GUI controls seemed to defy logic when I used them as well, but I didn't study them as extensively as I did the scene objects. I hope this helps!
#5
05/05/2012 (12:53 pm)
I think your best bet is somehow getting a callback on "focus" (whether you click the mouse in the field or tab to it). And then using that callback to empty the field.

I'm 75% sure you won't get a callback for just focusing on the text control. You'd have to add that in C++.

Your two options for callbacks, from my experience/memory, are that every control has the "command" and "altCommand" fields where you can put a function name or run some commands. If I recall, the "altCommand" is executed when the return key is pressed in an edit control. I don't remember if the "command" field did anything for text.

That's all I know. Sorry!
#6
05/06/2012 (8:39 pm)
Hmm, I was hoping to get away without having to modify the C++ code, but it looks like that might be the only way to get this to work. I think I saw a post about this when I was originally searching - though it was from 2005/2006. I'd probably say this is a bug, or a long past due feature, that you should be able to script the basic feedback calls to GUI elements. I'll see if I can figure out how to get it to work.

P.S. I'm not that great with the C++ side of things, so if anyone with a good understanding wants to jump in and point out how this works it would be appreciated :)
#7
05/07/2012 (7:56 pm)
Work was a little busy, but I did try to take a look at this. Here's what I found so far, not that I've figured anything out yet.

GuiTextEditCtrl.cc does have a function GuiTextEditCtrl::onMouseDown - however, from what little I can tell it's just used to highlight text on mouse clicks. It does have a call to setFirstResponder(), with a note '//let the parent get the event' above it.

The setFirstResponder macro does seem to just pass it up to the parent. The parent of GuiTextEditCtrl is GuiTextEdit - which has no setFirstResponder code in it. So, the only logical thing I can think of is that it passes up to the next level of parent, GuiControl.

Looking at GuiControl.cc I noticed a few things. It looks like it passes setFirstResponder even higher up, but it does have some code to deal with it. Additionally, there's a bunch of empty functions for mouse presses, like:

void GuiControl::onRightMouseDown(const GuiEvent &)
{
}

I tried fooling with it a little, but didn't find anything that got it to correctly let me add onMouseDown (or anything similar) to work in script for GuiTextEditCtrl's yet.
#8
05/08/2012 (1:27 pm)
@harry, if you see something about "focus" that is even better than mouse clicks, I can assume. For GUIs in general, focus is when the control takes charge and receives and keyboard clicks. So maybe you can tab between fields and change the focus (I think Torque does this).

So if you find focus, you can probably steal some code and copy it here. Specifically, find some code that says something like execute(this, "onMouseDown", blah) or something and change it to do some other script callback, like "onFocus." Then you can write your on script code that does an onFocus and does what you want. In your case, that would be to clear the field.
#9
05/08/2012 (8:05 pm)
Hmm, apparently all you need to do is add:

if( isMethod("onMouseDown") )
	Con::executef(this, 1, "onMouseDown");

to the GuiTextEditCtrl::onMouseDown and it works :/ Adding it to GuiControl.cc would probably help some other ones, that don't have their own definition... but I've been mostly staying away from GUI elements.

Thanks for the help!