adding a feature
adding a feature
With irrlicht 0.3 i added a image button by duplicating the button files, changing the draw method and placing a new add button routine into the environment files. Now that 0.4 is out i wanted to use these buttons but i don't want to edit the engine again. Is there anyway to derive the button from within my application?
No, not at all. No changes to the engine necessary. To add your own button to the environment, just instantiate it.
Your class will look like this:
And for adding it do this:
This works. 
Your class will look like this:
Code: Select all
class CGUIYourClass : public IGUIElement
{
public:
//! constructor
CGUIYourClass(IGUIEnvironment* environment,
IGUIElement* parent, s32 id, core::rect<s32> rectangle)
: IGUIElement(environment, parent, id, rectangle)
{
// do something here
}
// blablabla...
Code: Select all
CGUIYourClass* c = new CGUIYourClass(device->getGUIEnvironment()
0, -1, aRectangle);
c->drop();
okay i have a new problem now involving recieving events. I included in my class a standard OnEvent(SEvent event) to recieve mouse clicks etc for the button. However it seems the function is never called. The follwing is from a file i've named CGUIImageButton which implements the new class
//! called if an event happened.
bool CGUIImageButton::OnEvent(SEvent event)
{
setVisible(false);
if (!IsEnabled)
return Parent ? Parent->OnEvent(event) : false;
switch(event.EventType)
{
case EET_GUI_EVENT:
if (event.GUIEvent.EventType == EGET_ELEMENT_MOUSE_FOCUS_LOST)
{
Pressed = false;
return true;
}
break;
case EET_MOUSE_INPUT_EVENT:
if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
{
Pressed = true;
Environment->setMouseFocus(this);
return true;
}
else
if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
{
bool wasPressed = Pressed;
Environment->removeMouseFocus(this);
Pressed = false;
if (wasPressed && Parent)
{
SEvent event;
event.EventType = EET_GUI_EVENT;
event.GUIEvent.Caller = this;
event.GUIEvent.EventType = EGET_BUTTON_CLICKED;
Parent->OnEvent(event);
}
return true;
}
break;
}
return Parent ? Parent->OnEvent(event) : false;
}
at first i thought maybe the events weren't registered as GUIEvents, but it seems that no event is registered at all. Any ideas?
//! called if an event happened.
bool CGUIImageButton::OnEvent(SEvent event)
{
setVisible(false);
if (!IsEnabled)
return Parent ? Parent->OnEvent(event) : false;
switch(event.EventType)
{
case EET_GUI_EVENT:
if (event.GUIEvent.EventType == EGET_ELEMENT_MOUSE_FOCUS_LOST)
{
Pressed = false;
return true;
}
break;
case EET_MOUSE_INPUT_EVENT:
if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
{
Pressed = true;
Environment->setMouseFocus(this);
return true;
}
else
if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
{
bool wasPressed = Pressed;
Environment->removeMouseFocus(this);
Pressed = false;
if (wasPressed && Parent)
{
SEvent event;
event.EventType = EET_GUI_EVENT;
event.GUIEvent.Caller = this;
event.GUIEvent.EventType = EGET_BUTTON_CLICKED;
Parent->OnEvent(event);
}
return true;
}
break;
}
return Parent ? Parent->OnEvent(event) : false;
}
at first i thought maybe the events weren't registered as GUIEvents, but it seems that no event is registered at all. Any ideas?
I have had some success with this. I have used a standard guibutton for the parent. The problem now seems to be that i can only display the image button within the bounds of the parent, and the parent must be visible for the child to function. I moved the image button using IGUIElement->move. The image is not displayed when moved, however the region where the button should be does act like a button