Game Development Community

GUI Popup Menu Callback?

by Justin Proffitt · in Technical Issues · 06/12/2009 (12:25 pm) · 15 replies

I have recently gotten into making GUIs for my project and though I have gotten past the general problems of the GUI builder, I simply don't have enough knowledge to know how to make some things work. One such thing which is pretty essential to everything I am doing is making the options in a popup menu actually do anything. I have been able to add 'entries' to the menu using the add(Text,ID) method but I don't know what it calls back when I select one of them, so it's fairly useless. How can I make it respond with say, an echo?

Note: I haven't really gotten around to figuring out the sim sets yet, so since this appears to be a sim set, that may be a part of why I don't know how to do this.

Thanks.

Edit: New question.

About the author

I am a college student majoring in physics. As it so happens I like programming, a hobby which extends my profession in web design.


#1
06/12/2009 (12:41 pm)
GUI objects are all simSets, basically. You can do a dump of any object to see if it has a function or method that you can use. In this case, it's the ::onSelect() callback you're looking for.
#2
06/12/2009 (12:47 pm)
That works, thank you :)
#3
06/13/2009 (3:52 pm)
Actually I have another question. Since there are so few tutorials on how to make GUI's in torque, it is very hard to figure some things out. What I cannot seem to do is make a texteditctrl field that only takes numbers (or otherwise knows when there are only numbers in the field), deselects when you click elsewhere, and calls a function when you hit enter. This is probably a hell of a lot harder than making a drop down menu that does something, but I'd greatly appreciate it if somebody could help me at least with the last two of those things.
#4
06/13/2009 (7:53 pm)
For the deselection, I did the following:

function PlayGui::onMouseDown(%this)
{
   AnsTypeBox.setActive(0);
}

function AnsTypeBox::onMouseDown()
{
   AnsTypeBox.setActive(1);  
}

Where AnsTypeBox is a GuiTextEditCtrl, what happens is that anytime you click somewhere in PlayGui, unless AnsTypeBox itself receives and processes the ::onMouseDown() event, the control is set inactive. When it is an event in AnsTypeBox, then the second function activates it.

For the other question... If you want to remove the alphanumeric characters from the text in the control, you can try this:

%result = stripChars( %sourceString , %chars );

Where %sourceString is the text in that GuiTextEditCtrl, %chars would look like "abcdefgh...xyzABCDEF...XYZ" (the docs don't say if it's case sensitive, and I haven't used it yet), and %result is the resulting string after those chars have been stripped out.

Where that comes in handy is that you can do the following to check if there are alpha characters in the string at all:

function checkForAlpha(%text)
{
   %chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   %result = stripChars(%text, %chars);

   if(%result $= %text)
      return false; //no alpha characters found
   else
      return true; //alpha characters found
}

Not sure how fast that is, so if you need to loop through a bunch of stuff, you might want to look for another way, but I found it in the docs and thought it would do the trick for you. Hope it helps.
#5
06/13/2009 (10:49 pm)
I know your select/deselect functions will work, but I cannot get the texteditctrl object to recognize mouse events. I must be doing something wrong.
#6
06/14/2009 (6:13 am)
Are you getting any console errors?
#7
06/14/2009 (2:45 pm)
No, I just dont think I know all of what I am doing :p

As far as I can tell there is no way to set the useMouseEvents to true for the GUItexteditctrl, it doesn't recognize that as a parameter. The background/parent GUI will take mouse events, so I can deactivate it, but the GUItexteditctrl won't take mouse events. Though when I use the dump method on it, "onMouseDown()" is listed in there.

So I guess it's a matter of not knowing how to get the GUItexteditctrl object to register mouse events.
#8
06/14/2009 (8:11 pm)
One thing I do is put an echo in the function to make sure it gets called when I'm trying to track something down. That said, it sounds like you may have the GuiTextEditCtrl "behind" some other control that takes mouse events.
#9
06/14/2009 (8:33 pm)
That is actually what I was thinking, I had been setting up other stuff before I decided to use this gui object. Actually I think I may have figured out the problem, I have this on the same level as the scenewindow itself. I have a feeling its better practice to have everything as a child of the scenewindow or equivalent. Is there a way I can define what 'layer' a GUI object is in?

Besides that, since the textedit box does not have a usemouseevents parameter, that would mean that it by default detects mouse events?

Thank you so far for your help :) I am learning a bit as I go through the process of fixing all this.

Edit: I started a new GUI that only has the t2dscenewindow gui and the textedit gui inside a normal guicontrol object (So it's guicontrol{t2dscenewindow{}guitexteditctrl{}}, in that order). It still does not work, it registers the scenewindow but not the text box. :/
#10
06/15/2009 (9:50 am)
Can you post that part of the .gui file?
#11
06/15/2009 (11:57 am)
//--- OBJECT WRITE BEGIN ---
%guiContent = new GuiControl(playGUI) {
   canSaveDynamicFields = "0";
   isContainer = "1";
   Profile = "GuiBlackContentProfile";
   HorizSizing = "width";
   VertSizing = "heigth";
   Position = "0 0";
   Extent = "640 480";
   MinExtent = "8 2";
   canSave = "1";
   Visible = "1";
   hovertime = "1000";
   new t2dSceneWindow(sceneWindow2D) {
      canSaveDynamicFields = "0";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "width";
      VertSizing = "height";
      Position = "0 0";
      Extent = "640 480";
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      lockMouse = "0";
      useWindowMouseEvents = "1";
      useObjectMouseEvents = "1";
   };
   new GuiTextEditCtrl(ansTypeBox) {
      canSaveDynamicFields = "0";
      isContainer = "0";
      Profile = "GuiTextEditProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "375 267";
      Extent = "64 18";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      text = "Test";
      maxLength = "1024";
      historySize = "0";
      tabComplete = "0";
      sinkAllKeyEvents = "0";
   };
};
//--- OBJECT WRITE END ---

function sceneWindow2D::onMouseDown(%this){
	echo("TestWindow"); //Works
}

function ansTypeBox::onMouseDown(){
	echo("TestTBox");
}

I made it as similar to yours as possible, as you can see the t2dscenewindow (obviously) is set up to recognize mouse events, but the guitexteditcontrol is not, and doesn't recognize it when I put any variation of 'useMouseEvents = "1";' in there.
#12
06/15/2009 (2:46 pm)
Hmmmm... I looked at the source code, and it seems that its onMouseDown doesn't have a script-based callback that it calls (there is the possibility that I added this without remembering- I do things like that occasionally). So that would explain it.
#13
06/15/2009 (2:52 pm)
:( Unfortunately, I only own the engine, not the source code for it. So I guess that's where the road ends for now.

Thank you for all the help though, I learned a few things while screwing around with what you gave me.
#14
06/15/2009 (3:01 pm)
Whoops... Forgot that there were two flavors of TGB. Well, sorry, but yeah, it would be a source code mod...
#15
06/15/2009 (8:45 pm)
Actually, how about a different take on this.. Is it possible to tell the gui to stop accepting input until clicked on again, but not to turn it off/make it inactive?