EventHandler - JayhawkZombie/EECS581Project GitHub Wiki
EventHandler
EventHandler is the class in the Engine directly responsible for handling events (hence the name).
The class contains a set of member methods, which the engine can call directly, and a set of function pointers, which can be rebound to invoke custom behavior.
Methods
void BindCallback(Events type, function_pointer)
There are 4 of these methods, each of which takes a function pointer of a different signature
These are called to bind the function pointers stored in the event handler
The possible event types are:
- MouseDown
- MouseRelease
- MouseOver
- MouseExit
- MouseMoved
- KeyPressed
- KeyReleased
- TextEntered
- WindowResized
- LostFocus
- GainedFocus
- WindowClosed
- MouseScrolled
With most object, no callback needs to be bound for the WindowClosed, LostFocus, and GainedFocus callbacks, since the engine core will handle those directly. If you bind them, your methods will never be called anyway.
example:
void MyClass::KeyWasPressed(const sf::Keyboard::Key &key)...
//somewhere else in code, like on constructor
std::function<void(const sf::Keyboard::key &key)> callbackPointer = std::bind(&MyClass::KeyWasPressed, this, std::placeholders::_1);
MyHandler.BindCallback(Events::KeyPressed, callbackPointer);
Or you can use Lambas, which I think look a lot cleaner
void MyClass::KeyWasPressed(const sf::Keyboard::Key &key)...
//Somewhere else in code
MyHandler.BindCallback(Events::KeyPressed,
[this](const sf::Keyboard::Key &key)
{
this->KeyWasPressed(key);
}
);
In any case, use BindCallback to bind a function pointer to be called when a specific event occurs (and an engine level above yours hasn't already consumed that event)
Callbacks that can be rebound:
- ftnCallback_MouseMovement
- Signature:
void SomeName(const sf::Vector2f &position)
- Signature:
- ftnCallback_MouseOver
- Signature:void SomeName(const sf::Vector2f &position)
- ftnCallback_MouseExit
- Signature:
void SomeName(const sf::Vector2f &position)
- Signature:
- ftnCallback_MousePress
- Signature:
void SomeName(const sf::Vector2f &position, const sf::Mouse::Button &which)
- Signature:
- ftnCallback_MouseRelease
- Signature:
void SomeName(const sf::Vector2f &position, const sf::Mouse::Button &which)
- Signature:
- ftnCallback_MouseScroll
- Signature:
void SomeName(const sf::Vector2f &position)
- Signature:
- ftnCallback_KeyPress
- Signature:
void SomeName(const sf::Keyboard::Key &which)
- Signature:
- ftnCallback_KeyRelease
- Signature:
void SomeName(const sf::Keyboard::Key &which)
- Signature:
- ftnCallback_TextEntered
- Signature:
void SomeName(const sf::Keyboard::Key &which)
- Signature:
- ftnCallback_WindowClosed
- Signature:
void SomeName()
- Signature:
- ftnCallback_WindowResized
- Signature:
void SomeName()
- Signature:
In addition, there are member methods in the event handler class that are called by the engine itself, which cannot be rebound. These methods are a way for the event handler to do something it needs to in addition to allowing custom behavior. For game objects in a level, none will ever need to use those methods. By default, those methods will simply invoke the callbacks that were bound for custom behavior and do nothing else. But those methods have the same signature as the callbacks listed above. The only difference is that the ones above are function objects, and can thus be rebound to refer to any function you wish that matches its signature, whereas the others are member functions of the class.
If you rebind one of those, the function you bind it to MUST match the signature above exactly, including const
and &
.