![]() |
CUGL 3.0
Cornell University Game Library
|
#include <CUGameController.h>
Public Types | |
typedef std::function< void(const GameControllerInputEvent event, bool focus)> | Listener |
Public Member Functions | |
bool | doesFilter () const |
void | filter (bool value) |
size_t | size () const |
std::vector< std::string > | devices () const |
std::string | getName (const std::string uid) |
std::shared_ptr< GameController > | open (const std::string uid) |
std::shared_ptr< GameController > | get (const std::string uid) |
void | close (const std::string uid) |
virtual bool | requestFocus (Uint32 key) override |
bool | isListener (Uint32 key) const |
const Listener | getListener (Uint32 key) const |
bool | addListener (Uint32 key, Listener listener) |
bool | removeListener (Uint32 key) |
virtual void | clearState () override |
virtual bool | updateState (const SDL_Event &event, const Timestamp &stamp) override |
virtual void | queryEvents (std::vector< Uint32 > &eventset) override |
![]() | |
Uint32 | acquireKey () |
Uint32 | currentFocus () const |
virtual bool | requestFocus (Uint32 key) |
void | releaseFocus () |
Friends | |
class | Input |
Additional Inherited Members | |
![]() | |
static const Uint32 | RESERVED_KEY = UINT32_MAX |
![]() | |
InputDevice () | |
virtual | ~InputDevice () |
bool | initWithName (const std::string name) |
virtual void | dispose () |
virtual void | clearState ()=0 |
virtual bool | updateState (const SDL_Event &event, const Timestamp &stamp)=0 |
virtual void | queryEvents (std::vector< Uint32 > &eventset)=0 |
![]() | |
std::string | _name |
Uint32 | _focus |
Uint32 | _nextKey |
This class is an input manager for a collection of game controllers
While it is possible to have more than one game controller attached at any time, SDL broadcasts all controller state changes. Therefore, it is useful to have a central hub that manages controllers and dispatches events to the appropriate controller. In addition, this particular input device monitors when controllers are added and removed.
Game controllers only receive events when they are activated. See the methods GameControllerInput#open
and GameControllerInput#close
for how to activate and deactivate controllers.
This type represents a listener for the GameControllerInput
class.
In CUGL, listeners are implemented as a set of callback functions, not as objects. This allows each listener to implement as much or as little functionality as it wants. A listener is identified by a key which should be a globally unique unsigned int.
An event is delivered whenever a new game controller is either added to or removed from the list of devices. This can happen when a device loses power, or it connected during a play session.
Listeners are guaranteed to be called at the start of an animation frame, before the method Application#update(float)
.
While game controller input listeners do not traditionally require focus like a keyboard does, we have included that functionality. While only one listener can have focus at a time, all listeners will receive input from the Game Controller input device.
The function type is equivalent to
std::function<void(const GameControllerInputEvent event, bool focus)>
event | The game controller input event |
focus | Whether the listener currently has focus |
bool cugl::GameControllerInput::addListener | ( | Uint32 | key, |
Listener | listener | ||
) |
Adds an game controller manager listener for the given object key
There can only be one listener for a given key. If there is already a listener for the key, the method will fail and return false. You must remove a listener before adding a new one for the same key.
key | The identifier for the listener |
listener | The listener to add |
|
overridevirtual |
Clears the state of this input device, readying it for the next frame.
Many devices keep track of what happened "this" frame. This method is necessary to advance the frame.
Implements cugl::InputDevice.
void cugl::GameControllerInput::close | ( | const std::string | uid | ) |
Closes the game controller for the given UID
This invalidates all references to the game controller, making them no longer usable. The only way to access the game controller again is to call open
.
uid | The device UID |
std::vector< std::string > cugl::GameControllerInput::devices | ( | ) | const |
Returns the list of connected devices.
The list is a vector of unique identifiers (UID) used to identify each connected controller. These identifiers are not very descriptive, as they are designed to be compact and unique. For a description of each device, use getName
.
|
inline |
Returns true if this game controller manager filters its devices
Our game controller manager is an interface built on top of the SDL joystick functions. However, SDL has a very broad definition of joystick, and uses it to include things like an accelerometer. If this value is true (which is the default), then only devices which match traditional game controllers will be listed.
void cugl::GameControllerInput::filter | ( | bool | value | ) |
Sets whether this game controller manager filters its devices
Our game controller manager is an interface built on top of the SDL joystick functions. However, SDL has a very broad definition of joystick, and uses it to include things like an accelerometer. If this value is true (which is the default), then only devices which match traditional game controllers will be listed.
value | whether this game controller manager filters its devices |
std::shared_ptr< GameController > cugl::GameControllerInput::get | ( | const std::string | uid | ) |
Returns a reference to the given game controller.
This method assumes the game controller for this UID has already been activated. The UID for the device should be one listed in devices()
. If the device does not exist, or the device has not been activate, this method will return nullptr.
uid | The device UID |
const Listener cugl::GameControllerInput::getListener | ( | Uint32 | key | ) | const |
Returns the game controller manager listener for the given object key
If there is no listener for the given key, it returns nullptr.
key | The identifier for the listener |
std::string cugl::GameControllerInput::getName | ( | const std::string | uid | ) |
Returns a descriptive name for the given device.
The UID for the device should be one listed in devices()
. If the device does not exist, it will return the empty string.
uid | The device UID |
bool cugl::GameControllerInput::isListener | ( | Uint32 | key | ) | const |
Returns true if key represents a listener object
key | The identifier for the listener |
std::shared_ptr< GameController > cugl::GameControllerInput::open | ( | const std::string | uid | ) |
Returns a reference to a newly actived game controller.
This method activates the game controller with the given UID. If the game controller is already active, it simply returns a reference to the existing game controller. The UID for the device should be one listed in devices()
. If the device does not exist, this method will return nullptr.
It is generally a good idea to close all game controllers when you are done with them. However, deactivating this game controller automatically disposes of any active controllers.
uid | The device UID |
|
overridevirtual |
Determine the SDL events of relevance and store there types in eventset.
An SDL_EventType is really Uint32. This method stores the SDL event types for this input device into the vector eventset, appending them to the end. The Input dispatcher then uses this information to set up subscriptions.
eventset | The set to store the event types. |
Implements cugl::InputDevice.
bool cugl::GameControllerInput::removeListener | ( | Uint32 | key | ) |
Removes the game controller manager listener for the given object key
If there is no active listener for the given key, this method fails and returns false.
key | The identifier for the listener |
|
overridevirtual |
Requests focus for the given identifier
Only a listener can have focus. This method returns false if key does not refer to an active listener
key | The identifier for the focus object |
Reimplemented from cugl::InputDevice.
size_t cugl::GameControllerInput::size | ( | ) | const |
Returns the number of connected devices.
This value will be affected by doesFilter
.
|
overridevirtual |
Processes an SDL_Event
The dispatcher guarantees that an input device only receives events that it subscribes to.
event | The input event to process |
stamp | The event timestamp in CUGL time |
Implements cugl::InputDevice.