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().
Is it possible to force a menu event?
Is it possible to force a menu event?
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com
Visit www.d-grafix.com
Check the docu for IGUIContextMenu.
The menu has functions like setItemChecked (u32 idx, bool enabled) to access it's items.
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
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.
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
Visit www.d-grafix.com
Yes, that works by index. But I think you can find the index by searching for a command id, like:
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 ;-)
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);
}
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Actually, I'd take that as a sign that your code could be written better. What about this...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.
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);
}
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.CuteAlien wrote:Negative indices can sometimes be nice ;-)
Travis
No, don't like. Custom defines are harder to remember than -1.vitek wrote: Actually, I'd take that as a sign that your code could be written better. What about this...
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
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
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
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).
(*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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm