Architecture for Gui and EventReceiver

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Architecture for Gui and EventReceiver

Post by Memorial76 »

Greetings, i'm a beginner and autodidact in programming andIirrlicht and I've been working for a couple of weeks now on an architecture linking GUI and Events without having to write an giant Event Receiver Class nor a huge Gui Class.

I'm suming up there the way I did it hoping it may useful for someone

First I create a Class Called GuiGroup (maybe not so well named in deeds...):

this is the pattern:

Code: Select all

#pragma once
using namespace irr;

class CGuiGroup
{
public:
	CGuiGroup(CToolBox* pToolBox, IrrlichtDevice* pDevice); //CToolBox is my own Tool box Class
	~CGuiGroup(void);

	virtual void create();
	virtual void del();
	virtual void update();
	virtual bool OnEvent(const SEvent& event);

protected:
	CToolBox* m_pToolBox;
	IrrlichtDevice* m_pDevice;
	gui::IGUIEnvironment* m_pEnv;
	core::dimension2d<u32> m_desktopRes;

	bool m_bCreated;
};
From there i create all my classes inherited from CGuiGroup (for example CGuiFpsMonitor) according to that pattern:

create(): used to add Elements to the GuiEnvironment.
del(): to remove them
update(): to update them
onEvent(...) to catch events

Here is the example of CGuiFpsMonitor : public CGuiGroup

Code: Select all

#pragma once
#include "GuiGroup.h"
class CUserManager;

class CGuiFpsMonitor :
	public CGuiGroup
public:
	CGuiFpsMonitor(CToolBox* pToolBox, IrrlichtDevice* pDevice, CUserManager* pUserManager);
	~CGuiFpsMonitor(void);

	void create();
	void del();
	void update();
	bool OnEvent(const SEvent& event);

private:
	CUserManager* m_pUserManager;
};
Then I have another class called CGuiManager which can be seen as a container of all objects from Classes inherited from GuiClasses where I store pointers to them in a vector.
I also store all the IDs of the GuiElements in struture there
This vector is used to throw the events to each object of my Gui

This is a shortened version of the .h

Code: Select all

#pragma once
using namespace irr;

enum GUI_ID
{
	//FPS MONITOR
	GUI_STT_FPS,
	
};

class CToolBox;
class CConverter;
class CGuiGroup;


class CGuiManager
{
public:
	CGuiManager(CToolBox* pToolBox, IrrlichtDevice* pDevice, CUserManager* pUserManager);
	~CGuiManager();

	//MODES DU GUI
	void gotoMainPage();

	//ELEMENTS DU GUI
	const std::vector<CGuiGroup*> getGuiGroups();

	CGuiGroup* getGuiFpsMonitor();

private:
	CToolBox* m_pToolBox;

	IrrlichtDevice* m_pDevice;
	gui::IGUIEnvironment* m_pEnv;
	core::dimension2d<u32> m_desktopRes;

	
	//ELEMENTS DU GUI
	std::vector<CGuiElement*> m_vGuiElements;	//Conteneur des elements
	
	CGuiGroup* m_pGuiFpsMonitor;
	
};
Then in my EventReceiver i just throw the GUIEvents to every Group thanks to the my GuiManager

The main advantages I found are:
* I don't get an frightening giant event receiver.
* Each gui part owns it's event receiver
* Each gui part may react to other gui's events thanks to IDs.

Please do not hesitate to give me your opinion, advices or to ask an question.
Last edited by Memorial76 on Tue Mar 09, 2010 5:55 pm, edited 1 time in total.
CiRiuS2
Posts: 14
Joined: Mon Oct 26, 2009 12:14 am
Location: Spain

Post by CiRiuS2 »

Hi memorial76 i am interested in this architecture, can you post the full code (files) of a simple example plz?
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

Hi,

thanks for you interest. Unfortunately, my files are more complicated than what I posted and I would be might hard to get only what is necessary.

asically, you just have to create a CGuiGroup classe and the GuiManager.

Then create one class for each Gui group inheriting from CGuiGroup and instanciate them in the GuiManager.

Maybe you could try this and ask me question about particular points.

Thanks again for your interest
CiRiuS2
Posts: 14
Joined: Mon Oct 26, 2009 12:14 am
Location: Spain

Post by CiRiuS2 »

I dont understand why CGuiGroup isn't public of IEventReceiver, or how you pass the events from CGuiManager to each CGuiGrop, or where u instanciate the CGuiManager, etc etc...

Really you can't make a simply test code?
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

First I started with oinheriting CGuiGroup form IEventReceiver but it happened useless.
how you pass the events from CGuiManager to each CGuiGrop, or where u instanciate the CGuiManager
this is the point.
CGui Manager is instanciated in the main() function as MyEventReceiver.
CGuiFpsMonitor (and ll CGuiGroup inherited classes are instanciated in the CGuiManager object).

In the eventreceiver (basic IEventReceiver class) in the OnEvent function, I have a pointer to an CGuiManager object which calls the OnEvent function of each GuiGroup:

In the MyEventReceiver.cpp

Code: Select all

bool CEventReceiver::OnEvent(const SEvent& event)
{
	
	//EVENEMENTS DU GUI
	if (event.EventType == EET_GUI_EVENT)
	{
		for( unsigned int i = 0; i < m_pGuiManager->getGuiGroups().size(); i++ )
		{
			if( m_pGuiManager->getGuiGroups().at(i) )
				m_pGuiManager->getGuiGroups().at(i)->OnEvent(event);
		}
		
	}

	return false;
}
in CGuiManager.cpp, the getGuiGroups():

Code: Select all

const std::vector<CGuiElement*> CGuiManager::getGuiElements()
{
	return m_vGuiGroups;
}
where m_vGuiGroups is a std::vector<CGuiGroup*> filled in the constructor of CGuiManager:

Code: Select all

CGuiManager::CGuiManager(CToolBox* pToolBox,
						IrrlichtDevice* pDevice,
						CUserManager* pUserManager,
						C3dManager* p3dManager)
:m_pToolBox(pToolBox),
m_pDevice(pDevice),
m_pUserManager(pUserManager),
m_p3dManager(p3dManager)
{
	m_pToolBox->getExecRecord()->record("Creation de l'interface grahique");

	m_pEnv = pDevice->getGUIEnvironment();
	m_desktopRes = pDevice->getVideoModeList()->getDesktopResolution();

	m_vGuiElements.clear();

	//Creation des elements du GUI
	m_pGuiFpsMonitor = new CGuiFpsMonitor(m_pToolBox, m_pDevice, this, m_pUserManager);
	m_vGuiGroups.push_back(m_pGuiFpsMonitor);



	m_pToolBox->getExecRecord()->record("Interface graphique cree");

	updateSkinColor();

}
Really you can't make a simply test code?
I'm afraid not yet because my classes are more complicated than that and i had to change some names because they were not very understandable for someone else. Basically i can't just copy and paste my code.
If I could draw this it would be easer to understand.
I'm sorry
CiRiuS2
Posts: 14
Joined: Mon Oct 26, 2009 12:14 am
Location: Spain

Post by CiRiuS2 »

Thanks, I will try now.
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

Thanks to you for your interest :wink:
CiRiuS2
Posts: 14
Joined: Mon Oct 26, 2009 12:14 am
Location: Spain

Post by CiRiuS2 »

Here is a working test based on your classes that I did in 2 hours, credits to you :wink:
http://www.4shared.com/file/238706916/8 ... cture.html
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

The example project goes directly to the GUI Resources folder. A binary is always welcome. Will be explored when the time for the menus come. Thanks to both of you! :wink:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

Thanks to you too for your interest
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

Thanks to CiRius2 I've done a whole project to demonstrate my pattern based on his example.

I'm gonna publish it very soon if someone could tell me where it can be hosted.
Thanks
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

It is done!!!!
Thanks a lot CiRius2, I wouldn't have done this if you hadn't started.
Check this version, I changed a lot of stuf so that it's more what i wanted it to be.

http://irrgerap.sourceforge.net/
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
CiRiuS2
Posts: 14
Joined: Mon Oct 26, 2009 12:14 am
Location: Spain

Post by CiRiuS2 »

Good work, your solution is cleaner and is improved. I definitely will include this interface in my project. :wink:
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

Thanks a lot
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

Updated version is onLine, really minor changes.
Screen shot added

Link in my signature
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
Post Reply