List of all members | Public Types | Public Member Functions
thor::ActionMap< ActionId > Class Template Reference

Class template that associates identifiers with dynamic actions. More...

Inherits NonCopyable.

Public Types

typedef EventSystem< ActionContext< ActionId >, ActionId > CallbackSystem
 EventSystem to connect ActionMap with event listeners.
 

Public Member Functions

 ActionMap ()
 Default constructor.
 
 ActionMap (ActionMap &&source)
 Move constructor.
 
ActionMapoperator= (ActionMap &&source)
 Move assignment operator.
 
void update (sf::Window &window)
 Clears old events and polls the window for new ones. More...
 
void pushEvent (const sf::Event &event)
 Feeds the action map with a SFML event, that can be used during the current frame. More...
 
void clearEvents ()
 Removes the events that have been temporarily stored. More...
 
Actionoperator[] (const ActionId &id)
 Returns the action associated with a given action identifier. More...
 
void removeAction (const ActionId &id)
 Remove the action associated with the specified id.
 
void clearActions ()
 Removes all actions associated with this map.
 
bool isActive (const ActionId &id) const
 Returns whether the action associated with the specified identifier is currently in effect. More...
 
void invokeCallbacks (CallbackSystem &system, sf::Window *window) const
 Forwards active actions to a callback system. More...
 

Detailed Description

template<typename ActionId>
class thor::ActionMap< ActionId >

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().

Template Parameters
ActionIdThe type of ID you want to map to actions. This can be a string, an enum, or anything with a < operator and value semantics.

Member Function Documentation

template<typename ActionId >
void thor::ActionMap< ActionId >::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().

template<typename ActionId >
void thor::ActionMap< ActionId >::invokeCallbacks ( CallbackSystem system,
sf::Window *  window 
) const

Forwards active actions to a callback system.

Parameters
systemCallback system of type EventSystem< ActionContext<ActionId>, ActionId >
windowWindow pointer which is stored in the ActionContext passed to the callbacks. Can be nullptr.

For every action that is currently active, the action ID is passed to system, where all listener functions associated with the ID are invoked. For a given action registered in the action map, the callback system is invoked at most once, even if the action has been combined with logical operators and multiple sub-actions are active. That is, an action Shift&&X won't lead to an invocation for both Shift and X. This is the expected behavior.

// Listener function for "run" actions
void callback(thor::ActionContext<std::string> context);
// Map to register "run" action
map["run"] = thor::Action(sf::Keyboard::R);
// Create EventSystem object, connect "run" action to callback
system.connect("run", &callback);
// In the main loop: Forward actions to the callback system
map.invokeCallbacks(system, &window);
Warning
While this function is being called, any modifications to system result in undefined behavior. That is, do not remove or insert callbacks during the invocation of another callback. If you need to modify the system, delay the modifications until invokeCallbacks() returns.
See also
thor::ActionContext
template<typename ActionId>
bool thor::ActionMap< ActionId >::isActive ( const ActionId &  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);
template<typename ActionId>
Action& thor::ActionMap< ActionId >::operator[] ( const ActionId &  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:

map["run"] = thor::Action(sf::Keyboard::R);
template<typename ActionId>
void thor::ActionMap< ActionId >::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().

template<typename ActionId>
void thor::ActionMap< ActionId >::update ( sf::Window &  window)

Clears old events and polls the window for new ones.

Parameters
windowThe SFML window from which events are polled.

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(window);

is equivalent to

sf::Event event;
while (window.pollEvent(event))
map.pushEvent(event);

The documentation for this class was generated from the following file: