Handling Gui events with CIrrEventReceiver class

A forum to store posts deemed exceptionally wise and useful
Post Reply
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

Handling Gui events with CIrrEventReceiver class

Post by Yustme »

Hi,

I have updated the CIrrEventReceiver class to handle all gui events which you can find on this page (scroll down to my last post):

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=24508

CIrrEventReceiver class already handles all keyboard and mouse inputs.

I'll be explaining how to use the class to handle gui events.

First we get some useful pointers:

Code: Select all

CIrrEventReceiver rv;
IrrlichtDevice* device = createDevice(EDT_OPENGL,dimension2d<s32>(800,600), 32, false, true, true, &rv);
IVideoDriver* driver = device->getVideoDriver();
IGUIEnvironment* guienv = device->getGUIEnvironment();

Then we create a window where we put our buttons and checkboxes etc on:

Code: Select all

// create the window where we want to put our controls on.
IGUIWindow* window = guienv->addWindow(rect<s32>(position2di(80, 30),dimension2di(600, 550)),false,L"Menu",0,101);

Then we add a few gui elements to the window:

Code: Select all

        //
	// create a horizontal scrollbar
	//
	IGUIScrollBar* scrollbar1 = guienv->addScrollBar(true, rect<s32>(position2di(50,75),dimension2di(200,20)), window, 102);
	
	// set the maximum value of the scrollbar to 255.
	scrollbar1->setMax(255);

	// set scrollbar position to alpha value of an arbitrary element
	scrollbar1->setPos(guienv->getSkin()->getColor(EGDC_WINDOW).getAlpha());

	// add statictext above scrollbar.
	guienv->addStaticText(L"Transparent Control:", rect<s32>(position2di(300,50),dimension2di(200,20)), true, false, window);
	
	//
	// create another horizontal scrollbar
	//
	IGUIScrollBar* scrollbar2 = guienv->addScrollBar(true, rect<s32>(position2di(300,75),dimension2di(200,20)), window, 103);
	
	// set the maximum value of the scrollbar to 255.
	scrollbar2->setMax(255);

	// set scrollbar position to alpha value of an arbitrary element
	scrollbar2->setPos(guienv->getSkin()->getColor(EGDC_WINDOW).getAlpha());
	
	//
	// create a few buttons.
	//
	IGUIButton* button1 = guienv->addButton(rect<s32>(position2di(50,105),dimension2di(200,30)), 
			window,104, L"Button 1",L"Button 1");

	IGUIButton* button2 = guienv->addButton(rect<s32>(position2di(50,145),dimension2di(200,30)), 
			window,105, L"Button 2",L"Button 2");

	IGUIButton* button3 = guienv->addButton(rect<s32>(position2di(50,185),dimension2di(200,30)), 
			window,106, L"Button 3",L"Button 3");
	
	//
	// create a few editableboxes:
	//
	IGUIEditBox* editbox1 = guienv->addEditBox(L"Editable Box 1", rect<s32>(position2di(300,105),dimension2di(200,20)), true, window, 107);
	IGUIEditBox* editbox2 = guienv->addEditBox(L"Editable Box 2", rect<s32>(position2di(300,135),dimension2di(200,20)), true, window, 108);
	IGUIEditBox* editbox3 = guienv->addEditBox(L"Editable Box 3", rect<s32>(position2di(300,165),dimension2di(200,20)), true, window, 109);
	
	//
	// create a few checkboxex.
	//
	IGUICheckBox* checkbox1 = guienv->addCheckBox(false, rect<s32>(position2di(265,50),dimension2di(20,20)), window, 110);
	IGUICheckBox* checkbox2 = guienv->addCheckBox(false, rect<s32>(position2di(265,75),dimension2di(20,20)), window, 111);
	IGUICheckBox* checkbox3 = guienv->addCheckBox(false, rect<s32>(position2di(265,100),dimension2di(20,20)), window, 112);
	IGUICheckBox* checkbox4 = guienv->addCheckBox(false, rect<s32>(position2di(265,125),dimension2di(20,20)), window, 113);
	IGUICheckBox* checkbox5 = guienv->addCheckBox(false, rect<s32>(position2di(265,150),dimension2di(20,20)), window, 114);

In our main loop we check which ID caused the event and which gui element that was.

For example, we want to check if "button1" has been pressed. This is how you would do that:

Code: Select all

if(rv.getEventCallerByID() == 104 && rv.getEventCallerByElement(EGET_BUTTON_CLICKED))
   cout << "button 1 has been pressed" << endl;

The button1 has ID 104, so we check if "rv.getEventCallerByID()" returns 104. And we want to know if the button was clicked and NOT whether the mouse cursor hovered over it or if it lost it's focus.

So we check if "rv.getEventCallerByElement(EGET_BUTTON_CLICKED)" returns true.

A gui element can produce multiple events. So it's up to you which one you want to handle.

If you know which gui event you want to handle, you use this function:

Code: Select all

rv.getEventCallerByElement(EGUI_EVENT_TYPE)
But you can have multiple buttons on your window. So checking which ID caused the event would be wise.

Using those 2 functions in combination makes it possible to handle any event without troubles. It wont conflict with any other gui element.

Suppose we want to see if the mouse cursor hovered over our button1, then this is how we would check it:

Code: Select all

if(rv.getEventCallerByID() == 104 && rv.getEventCallerByElement(EGET_ELEMENT_HOVERED))
   cout << "button 1 has been hovered over by the cursor" << endl;

ID 104 is button1, so all we need to change is this functions argument:

Code: Select all

rv.getEventCallerByElement(EGET_ELEMENT_HOVERED)

Ok that's it guys. I'll post a link soon from where you can download my project and play with the (gui) events.

[EDIT 07-02-2006]

Hi,

Here is the link:

http://irrlichtirc.g0dsoft.com/Yustme/

Download the file: "HandlingGuiEvents.zip".

[/EDIT]
Post Reply