Game Development Community

How to Create a Context Menu

by Charlie Sibbach · in Torque Game Engine · 02/08/2009 (11:01 am) · 2 replies

I want to bring up a context menu when the user right-clicks on certain gui elements or objects in the game, and I'm wondering how to do it. I'm thinking I make a GuiPopupMenuCtrl and hide it in the main gui. Then, when I get a right-click event (using AFX object selection or a mouse event ctrl), I set the position of my menu and unhide it; the problem I think I'll run into is expanding the menu without a second click.

Is there an easier way, or correct way to do this? I didn't find anything on the forums, and I don't remember seeing a GuiContextMenu ctrl (or anything similar). I'd rather avoid having to roll my own on this one if possible; maybe there's a resource out there I missed?

#1
02/09/2009 (2:23 pm)
I think that's what the GuiPopUpMenuCtrl is for... but since it's typically a context-sensitive thing (i.e. menu contents depend on where the user is clicking), you need to make your own callback for the right-mouse button event that will open the menu and have it call a method you make on the pop-up menu that populates it with the "stuff" you need.

An alternative would be to create a bunch of different GuiPopUpMenuCtrl menus that are pre-populated for each possible context, and have the mouse event code just choose which one to activate (deactivating any others that might be open if necessary).

Which is better for you depends on whether you want to edit script code (for the callback) and make calls to update the one menu programmatically... or whether you want to edit GUI code (for the various menus and their functions) with a simple "choose which menu to pop-up" callback routine.
#2
02/12/2009 (11:23 pm)
I did find some example code for this in the TorqueIDE project, which does use GuiPopupMenuCtrls created dynamically. The key part I was missing is the %popupMenu.forceOnAction() function, which will open the menu for you. Combined with setSelected() and a proper GuiProfile, I should be in the money.