GUI elements dynamic cast

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
elfuz
Posts: 14
Joined: Wed Oct 13, 2010 10:20 am

GUI elements dynamic cast

Post by elfuz »

hi all

i'm still going with the Irrlicht engine for porting my software. However i'm running into an issue here.

as i understand to make new gui objects, the best way is to put cguiimage.cpp and cguiimage.h in the project folder, rename them and name the constructors something else.

then for adding it you call new CGUIYourComponent and add it to the gui->GetRootSceneNode().

Now the entire GUI is pretty dynamic and the objects are found trough an internal name (backbutton/downarrow). So in the CGUIElement.h i've added a name string and a getter and setter. Now i would need a function in the eguienvironment that returns an element based on a name and a type:

Code: Select all


IGUIElement* FindElement(stringw _name, EGUI_ELEMENT_TYPE _type)

{
// iterate trough all elements
if (element(i).GetName() == _name) && (element(i).GetType() == _type)
return element(i)
}

}

the questions are:

- how would i return a different type? is it safe to dynamic_cast them automatically to the type they are (for example CGUIMyElement)?

- is this functionality somehow already built in to the gui or is there a better way?

greetings
elFuz
CuteAlien
Admin
Posts: 9926
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

That doesn't exist yet for gui elements. But as similar functionality exists for scenenodes I want to add that also to the gui elements. I can probably do that in the next days (in svn trunk).

Irrlicht is compiled without rtti, so dynamic casts won't work (this would probably be the place where they would be most useful...). That's why next version will have something like a hardcoded rtti replacement (hasType - it's already in svn) to allow for better type-checking.
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
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You can query the type of an element already now, there's hasType, isType, or something like that. After that, you can static_cast the element to its derived type.
For searching an element you'd use the ID right now. Instead of naming the element, you'd number it. You can find the elements by starting from the parent node (gui environment if you search everywhere) and ask for the specific ID.
CuteAlien
Admin
Posts: 9926
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Hybrid is certainly right, there are other ways already to do most stuff (I thought hasType was only added in 1.8, but it's already in 1.7 it seems).

And you can use ID's instead of names, although I will still add names soon as they are often more useful and I want the interface to be more similar to ISceneNode which has both.
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
elfuz
Posts: 14
Joined: Wed Oct 13, 2010 10:20 am

Post by elfuz »

Code: Select all

	virtual IGUIElement* getElementFromName(stringw _name, bool searchchildren=false) const
	{
		IGUIElement* e = 0;

		core::list<IGUIElement*>::ConstIterator it = Children.begin();
		for (; it != Children.end(); ++it)
		{
			if ((*it)->getName() == _name)
				return (*it);

			if (searchchildren)
				e = (*it)->getElementFromName(_name, true);

			if (e)
				return e;
		}

		return e;
	}

Code: Select all


	//! Sets the new name of this element.
	virtual void setName(const wchar_t* _name)
	{
		Name = _name;
	}


	//! Returns name of this element.
	virtual const core::stringw& getName() const
	{
		return Name;
	}

and almost at the bottom:

Code: Select all


	core::stringw Name;

Post Reply