Class template that associates identifiers with dynamic actions. More...
Public Types | |
typedef EventSystem < ActionContext < ActionIdentifier > , ActionIdentifier > | CallbackSystem |
EventSystem to connect ActionMap with event listeners. | |
Public Member Functions | |
ActionMap (sf::Window &window) | |
Constructor. | |
void | Update () |
Clears old events and polls the window for new ones. | |
void | PushEvent (const sf::Event &event) |
Feeds the action map with a SFML event, that can be used during the current frame. | |
void | ClearEvents () |
Removes the events that have been temporarily stored. | |
Action & | operator[] (const ActionIdentifier &id) |
Returns the action associated with a given action identifier. | |
void | RemoveAction (const ActionIdentifier &id) |
Remove the action associated with the specified id. | |
void | ClearActions () |
Removes all actions associated with this map. | |
bool | IsActive (const ActionIdentifier &id) const |
Returns whether the action associated with the specified identifier is currently in effect. | |
void | InvokeCallbacks (CallbackSystem &system) const |
Forwards active actions to a callback system. |
Class template that associates identifiers with dynamic actions.
You can use this template to map identifiers like strings or enumerators to a specific combination of SFML events (sf::Event) and real time input states (sf::Mouse, sf::Keyboard, sf::Joystick). After the initialization, IsActive() provides an easy way to check whether a specified identifier is associated with an active action. Furthermore, it is possible to map the actions to callbacks, which can be achieved with InvokeCallbacks().
ActionIdentifier | The type of ID you want to map to actions. This can be a string, an enum, or anything with a < operator and value semantics. |
thor::ActionMap< ActionIdentifier >::ActionMap | ( | sf::Window & | window | ) | [explicit] |
Constructor.
window | The SFML window used for event generation and referenced in ActionContext objects. |
void thor::ActionMap< ActionIdentifier >::ClearEvents | ( | ) |
Removes the events that have been temporarily stored.
You only need this function in combination with PushEvent(), if you want to feed the action map manually with events. Otherwise, you can just call Update().
void thor::ActionMap< ActionIdentifier >::InvokeCallbacks | ( | CallbackSystem & | system | ) | const |
Forwards active actions to a callback system.
system | Callback system of type EventSystem< ActionContext<ActionIdentifier>, ActionIdentifier > |
For every action that is currently active, the action ID is passed to system, where all listener functions associated with the ID are invoked.
// Listener function for "run" actions void Callback(const thor::ActionContext<std::string>& context); // Map to register "run" action thor::ActionMap<std::string> map(...); map["run"] = thor::Action(sf::Keyboard::R); // Create EventSystem object, connect "run" action to callback thor::ActionMap<std::string>::CallbackSystem system; system.Connect("run", &Callback); // In the main loop: Forward actions to the callback system map.InvokeCallbacks(system);
bool thor::ActionMap< ActionIdentifier >::IsActive | ( | const ActionIdentifier & | id | ) | const |
Returns whether the action associated with the specified identifier is currently in effect.
To be in effect, the boolean operation of the assigned action must yield true. Note that in contrast to registered callbacks, IsActive() doesn't take into account the situation where multiple events of the same type occur in a single frame.
// If LCtrl+A or B is pressed, the action is active. thor::Action(sf::Keyboard::A) && thor::Action(sf::Keyboard::LCtrl) || thor::Action(sf::Keyboard::B);
Action& thor::ActionMap< ActionIdentifier >::operator[] | ( | const ActionIdentifier & | id | ) |
Returns the action associated with a given action identifier.
Use this function to create new associations. If the action id hasn't been stored yet, it is inserted and an empty action is returned. Assign an object of type thor::Action to it. Example:
thor::ActionMap<std::string> map(...); map["run"] = thor::Action(sf::Keyboard::R);
void thor::ActionMap< ActionIdentifier >::PushEvent | ( | const sf::Event & | event | ) |
Feeds the action map with a SFML event, that can be used during the current frame.
When you use the Update() method, you needn't invoke PushEvent(). This method exists for more flexibility: You can push user-defined events, and you can do something else with the polled events before calling PushEvent().
void thor::ActionMap< ActionIdentifier >::Update | ( | ) |
Clears old events and polls the window for new ones.
When you invoke this method, you should not call sf::Window::PollEvent() in the same frame, since Update() already does that. The following code
map.Update();
is equivalent to
map.ClearEvents();
sf::Event event;
while (window.PollEvent(event))
map.PushEvent(event);