Is it possible to force a menu event?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

Is it possible to force a menu event?

Post by dgrafix »

I have a menu called view which brings up tool dialogue windows. The menu items are checkable. And hide windows if unchecked and show if not.

Problem i am having is when the window's X is clicked, the menu option stays checked. I want a way of forcing a menu event to un-check it (or simply track it down and say setChecked(false) or whatever. Unfortunately the menu items dont seem to work the same as the other gui elements when i use getElementFromID().
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Check the docu for IGUIContextMenu.

The menu has functions like setItemChecked (u32 idx, bool enabled) to access it's items.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

Post by dgrafix »

I tried that but it wants a zeroindex which does not appear to be the same as the ID assigned to it (at least i assume thats why i get a segf) :/
I dont want to hard code its position in the menu as the menu might change later.
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Yes, that works by index. But I think you can find the index by searching for a command id, like:

Code: Select all

s32 SearchIndexForCommandId(IGUIContextMenu * menu, s32 cmdId)
{
  s32 idx = -1;
  for ( u32 i=0; i<menu->getItemCount (); ++i )
  {
    if ( menu->getItemCommandId(i) == cmdId )
      return (s32)i;
  }
  return -1;
}

// in code you can use it like...
s32 idx = SearchIndexForCommandId(menu, cmdId);
if ( idx >= 0 )
{
  menu->setItemChecked ((u32)idx, true);
}
Code not tested, but I guess it should work.
Also the need for the type-casts (or an additional bool variable) might be a sign that Irrlicht sometimes gets carried a little too far in trying to be type-correct. Negative indices can sometimes be nice ;-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

CuteAlien wrote: Also the need for the type-casts (or an additional bool variable) might be a sign that Irrlicht sometimes gets carried a little too far in trying to be type-correct.
Actually, I'd take that as a sign that your code could be written better. What about this...

Code: Select all

#ifndef IRR_NPOS
#  include <limits.h>
#  define IRR_NPOS UINT_MAX
#endif

u32 SearchIndexForCommandId(IGUIContextMenu * menu, s32 cmdId) 
{ 
  s32 idx = -1; 
  for (u32 i = 0; i < menu->getItemCount (); ++i) 
  { 
    if (menu->getItemCommandId(i) == cmdId) 
      return i; 
  } 
  return IRR_NPOS; 
} 

// in code you can use it like... 
const u32 idx = SearchIndexForCommandId(menu, cmdId); 
if (IRR_NPOS != idx) 
{ 
  menu->setItemChecked (idx, true); 
}
CuteAlien wrote:Negative indices can sometimes be nice ;-)
It is almost never useful to index to the elements before the beginning of an array. Especially in a case like this where the array starts at index 0 and is controlled by someone else.

Travis
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

vitek wrote: Actually, I'd take that as a sign that your code could be written better. What about this...
No, don't like. Custom defines are harder to remember than -1.

You could actually kick the idx variable also out from my code as it's not used (sorry, I just wrote that down... compiler would probably complain anway about unused variable). But well, now we have 2 versions - all the better ^^

edit: Btw. using unsigned here *is* certainly also fine - the point was rather that this is one of the interfaces that has changed since I started with Irrlicht in some minor way which may or may not have improved the interface. It actually was a s32 at some time.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Right. I'm just pointing out that having half of the representable numeric range reserved for error isn't really necessary or useful.

Many libraries, including the C++ Standard Library, use a numeric constant for things like this (e.g., std::string::npos). There are even many functions in the C library (mbrlen is an example), that use one or more negative values to indicate error, but the actual return type is unsigned.

Travis
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Yeah, having something like npos in Irrlicht would probably be the correct solution.

(*sigh* is giving up half the numerical range for nicer looking code really such a bad deal? I tend to have a lot more ugly std::string::npos stuff in my code than I ever have situations where I actually use the full range).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply