Creating a custom button

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
Post Reply
danielmccarthy
Posts: 51
Joined: Fri May 30, 2014 12:55 am

Creating a custom button

Post by danielmccarthy »

I need to learn more about the IGUIElement class. I am wondering how I can make my own custom IGUIElement and have it be able to throw events to the event handler when it is clicked

Thanks,
Dan
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating a custom button

Post by CuteAlien »

Easiest solution is usually to start by copying the sources of the Irrlicht element which is already closest to what you need. For examples CGUIButton.cpp./h. Then rename the class and add it to your project file. After that start modifying it any way you like.

Note that you can also do a lot of modifications without needing to create own gui-element. And in current svn-trunk the number of options to modify buttons even increased. So very often it's sufficient to just set other textures or for example work by changing the skin-colors or using a customs-skin implementation.
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
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: Creating a custom button

Post by chronologicaldot »

Use the regular buttons and create an event handler that inherits from IEventHandler and is set to the engine's event handler. This event handler will have a function, OnEvent(SEvent& event) in which you code your button responses by checking for the button id.

Here's an example in quick code:

Code: Select all

 
int buttonid = 1; // your button id. Could also be an enum value
 
class myhandler : public IEventHandler {
bool OnEvent( SEvent& event ) {
if ( event.eventType == EET_GUIEEVENT ) // I forget the actual enum value, but it is EET_G something...
{
if ( event.guielem.id == buttonid ) {
// do something
return true; // event absorbed
}
}
return false; // continue processing
}
};
 
void main()
{
IrrlichtDevice* device = createDevice();
IGUIButton* button = device->getGUIEnvironment()->addButton( buttonid,  /* other details go here */ );
 
myhandler handler;
device->setEventHandler( handler );
}
 
Or something to that effect. No need to create a specialized button. The buttons you can easily change the image for, plus you can even make them animated on hover using sprites.
MyGrandmaWheels
Posts: 20
Joined: Thu May 07, 2015 9:05 am

Re: Creating a custom button

Post by MyGrandmaWheels »

Hello; I am trying to follow these steps to create a custom button, because I need to show in my button the caption text and also an icon; Irrlicht allow it, but the text overlaps the icon.
So I'm trying to customize the

Code: Select all

void CGUIButton::draw() 
of the class CGUIButton.

so I created a class ECCGUIButton copied from CGUIButton and I modified the draw method.
After that, I add the button to my form:

Code: Select all

btnCancel = (ECGUIButton *)guiEnv->addButton(rect<s32>(290, 200, 390, 230), 0, -1, L"Cancel");
btnCancel->setUseAlphaChannel(true);
btnCancel->setImage(EGBIS_IMAGE_UP, texIcons, getBtnImage(3, 0));
btnCancel->setImage(EGBIS_IMAGE_DISABLED, texIcons, getBtnImage(3, 1));
addChild(btnCancel);
I was compelled to do the cast (ECGUIButton *), but I suppose this is the problem; in fact,my method is not called, but the parent one is still called.

what I'm failing?
many thanks!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating a custom button

Post by CuteAlien »

Casts just tell the compiler which type to expect. They never change the type of an object in memory. So yeah - you have a CGUIButton and not an ECGUIButton. The real type is always decided on object creation.

You can create your own button simply with "new". Gui-objects derived from IGUIElement (or on of it's derived classes) get added to the gui-environment in their constructor (which is bad engine design, but just accept it for the moment). So usually you can drop() it immediately after that (unless you want to keep a reference around). As parent in the constructor you can use IGUIEnvironment::getRootGUIElement ().
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
MyGrandmaWheels
Posts: 20
Joined: Thu May 07, 2015 9:05 am

Re: Creating a custom button

Post by MyGrandmaWheels »

Oh, yes! I resolved, in effect.. so guiEnv->addButton is only a way to place a button with some properties all in a shot, nothing magic.. you're right, this method is not so natural and can easily send you astray..
well, as a plus, I don't manage c++ so good (eh! eh!) so I make a good confusion with class, casting, virtual and so on..

Many thanks as always!
Post Reply