Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

Unity Basic event handling

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After reading through this : Link I decided I should do something similar for the interface elements in my project. So I have come up with this :


#include <iostream>
#include <boost/shared_ptr.hpp>
#include <map>


//
// base gui event class
//
class IGUIBaseEvent
{
public :
	virtual void execute()=0;
};

//gui event class
template<class T>
class GUIEvent:public IGUIBaseEvent
{
public:
	GUIEvent(T& obj,void (T::*funcPtr)())
		: _obj(obj),func(funcPtr)
	{}
	
	virtual void execute(){(_obj.*func)();}
	
protected:
	T& _obj;
	void (T::*func)();
};

//
// base class for all interface items 
//
class GUIElement
{
public:
	GUIElement()
	{
		
	}
	boost::shared_ptr<IGUIBaseEvent> onClick;
	boost::shared_ptr<IGUIBaseEvent> onPressed;
	boost::shared_ptr<IGUIBaseEvent> onOver;
	
	//check input?
	virtual void update()
	{
		//if(clicked && onClick)
		 onClick->execute();

		//if(pressed && onPressed)
		// onClick->execute();

	    //if(over && onOver)
		// onOver->execute();
		
	}

};

//
class Interface
{
public:
	Interface()
	{
		m_EndTurnButton.reset(new GUIElement());
		m_EndTurnButton->onClick.reset(new GUIEvent<Interface>(*this,&Interface::onEndTurnClicked));
	}

	void update()
	{
		m_EndTurnButton->update();

		//iterate over all gui items and update them 
		//std::map<unsigned int,boost::shared_ptr<GUIElement>>::iterator it;
		//for(it = m_GUIElements.begin(); it!= m_GUIElements.end(); ++it){
		//	it->second->update();
		//}
	}
private:
	//called when the end turn button is clicked
	void onEndTurnClicked()
	{
		std::cout<<"end turn clicked event fired!\n";
	}
	
	//end turn button
	boost::shared_ptr<GUIElement> m_EndTurnButton;
	//store all interface items in a std::map later
	//std::map<unsigned int,boost::shared_ptr<GUIElement>> m_GUIElements;
};

int main()
{
	boost::shared_ptr<Interface> inter(new Interface());
	
	inter->update();

	return 0;
}

Eventually all gui elements will be store in a std::map and iterated over to check input and they will call the event handlers accordingly.Button states and cursor position will be sent as parameters to GUIElement::update but i left them out as this is just a test program.I haven't included anything into the actual program.Anyway here comes my questions : 1) On this line : virtual void execute(){(_obj.*func)();} _obj is used to call the pointer to member?So _obj.*func points to Interface::onEndTurnClicked?There is now way to call a pointer to member function without a object instance right? I mean _obj must be used to call func. 2) Would it be a problem to always check if the on* shared_ptrs are valid inside GUIElement::update? I guess not because if there isn't any handler assigned to them then even if the cursor is over the element the event will not be fired. Any other suggestions?

Share this post


Link to post
Share on other sites
Advertisement
Rewritten in boost::bind & boost::function
Neat!


#include <iostream>
#include <map>

#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>


//
// base class for all interface items
//
class GUIElement
{
public:
GUIElement()
{

}

boost::function<void()> onClick;
boost::function<void()> onPressed;
boost::function<void()> onOver;

//check input?
virtual void update()
{
if(onClick)
onClick();

if(onPressed)
onPressed();
if(onOver)
onOver();
}

};

//
class Interface
{
public:
Interface()
{
m_EndTurnButton.reset(new GUIElement());
m_EndTurnButton->onClick = boost::bind(&Interface::onEndTurnClicked,this);

}
void update()
{
m_EndTurnButton->update();

//iterate over all gui items and update them
//std::map<unsigned int,boost::shared_ptr<GUIElement>>::iterator it;
//for(it = m_GUIElements.begin(); it!= m_GUIElements.end(); ++it){
// it->second->update();
//}
}
private:
//called when the end turn button is clicked
void onEndTurnClicked()
{
std::cout<<"end turn clicked event fired!\n";
}

//end turn button
boost::shared_ptr<GUIElement> m_EndTurnButton;
//store all interface items in a std::map later
//std::map<unsigned int,boost::shared_ptr<GUIElement>> m_GUIElements;
};

int main()
{
boost::shared_ptr<Interface> inter(new Interface());

inter->update();

return 0;
}

Share this post


Link to post
Share on other sites
Signals and slots are for broadcasting notifications with minimal coupling. Function and bind are for treating functions like proper objects. You may find yourself using them in similar areas of the code but they do different things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!