![]() |
CUGL 4.0
Cornell University Game Library
|
#include <CUGamepad.h>
Public Types | |
| typedef std::function< void(const GamepadInputEvent 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< Gamepad > | open (const std::string uid) |
| std::shared_ptr< Gamepad > | 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 |
Public Member Functions inherited from cugl::InputDevice | |
| Uint32 | acquireKey () |
| Uint32 | currentFocus () const |
| virtual bool | requestFocus (Uint32 key) |
| void | releaseFocus () |
Friends | |
| class | Input |
Additional Inherited Members | |
Static Public Attributes inherited from cugl::InputDevice | |
| static const Uint32 | RESERVED_KEY = UINT32_MAX |
Protected Member Functions inherited from cugl::InputDevice | |
| 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 |
Protected Attributes inherited from cugl::InputDevice | |
| std::string | _name |
| Uint32 | _focus |
| Uint32 | _nextKey |
This class is an input manager for a collection of gamepad
While it is possible to have more than one gamepad attached at any time, SDL broadcasts all gamepad state changes. Therefore, it is useful to have a central hub that manages gamepads and dispatches events to the appropriate gamepad. In addition, this particular input device monitors when gamepads are added and removed.
Gamepads only receive events when they are activated. See the methods GamepadInput#open and GamepadInput#close for how to activate and deactivate gamepads.
This type represents a listener for the GamepadInput 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 gamepad 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 gamepad 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 Gamepad input device.
The function type is equivalent to
std::function<void(const GamepadInputEvent event, bool focus)>
| event | The gamepad input event |
| focus | Whether the listener currently has focus |
| bool cugl::GamepadInput::addListener | ( | Uint32 | key, |
| Listener | listener | ||
| ) |
Adds an gamepad 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::GamepadInput::close | ( | const std::string | uid | ) |
Closes the gamepad for the given UID
This invalidates all references to the gamepad, making them no longer usable. The only way to access the gamepad again is to call open.
| uid | The device UID |
| std::vector< std::string > cugl::GamepadInput::devices | ( | ) | const |
Returns the list of connected devices.
The list is a vector of unique identifiers (UID) used to identify each connected gamepad. 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 gamepad manager filters its devices
Our gamepad 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 gamepads will be listed.
| void cugl::GamepadInput::filter | ( | bool | value | ) |
Sets whether this gamepad manager filters its devices
Our gamepad 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 gamepads will be listed.
| value | whether this gamepad manager filters its devices |
| std::shared_ptr< Gamepad > cugl::GamepadInput::get | ( | const std::string | uid | ) |
Returns a reference to the given gamepad.
This method assumes the gamepad 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::GamepadInput::getListener | ( | Uint32 | key | ) | const |
Returns the gamepad 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::GamepadInput::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::GamepadInput::isListener | ( | Uint32 | key | ) | const |
Returns true if key represents a listener object
| key | The identifier for the listener |
| std::shared_ptr< Gamepad > cugl::GamepadInput::open | ( | const std::string | uid | ) |
Returns a reference to a newly actived gamepad.
This method activates the gamepad with the given UID. If the gamepad is already active, it simply returns a reference to the existing gamepad. 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 gamepads when you are done with them. However, deactivating this input automatically disposes of any active gamepads.
| 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::GamepadInput::removeListener | ( | Uint32 | key | ) |
Removes the gamepad 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::GamepadInput::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.