CUGL 2.5
Cornell University Game Library
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Friends | List of all members
cugl::GameControllerInput Class Reference

#include <CUGameController.h>

Inheritance diagram for cugl::GameControllerInput:
cugl::InputDevice

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< GameControlleropen (const std::string uid)
 
std::shared_ptr< GameControllerget (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
 

Detailed Description

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.

Member Typedef Documentation

◆ Listener

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)>
Parameters
eventThe game controller input event
focusWhether the listener currently has focus

Member Function Documentation

◆ addListener()

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.

Parameters
keyThe identifier for the listener
listenerThe listener to add
Returns
true if the listener was succesfully added

◆ clearState()

virtual void cugl::GameControllerInput::clearState ( )
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.

◆ close()

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.

Parameters
uidThe device UID

◆ devices()

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.

Returns
the list of connected devices.

◆ doesFilter()

bool cugl::GameControllerInput::doesFilter ( ) const
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.

Returns
true if this game controller manager filters its devices

◆ filter()

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.

Parameters
valuewhether this game controller manager filters its devices

◆ get()

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.

Parameters
uidThe device UID
Returns
a reference to the given game controller.

◆ getListener()

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.

Parameters
keyThe identifier for the listener
Returns
the game controller manager listener for the given object key

◆ getName()

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.

Parameters
uidThe device UID
Returns
a descriptive name for the given device.

◆ isListener()

bool cugl::GameControllerInput::isListener ( Uint32  key) const

Returns true if key represents a listener object

Parameters
keyThe identifier for the listener
Returns
true if key represents a listener object

◆ open()

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.

Parameters
uidThe device UID
Returns
a reference to a newly actived game controller.

◆ queryEvents()

virtual void cugl::GameControllerInput::queryEvents ( std::vector< Uint32 > &  eventset)
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.

Parameters
eventsetThe set to store the event types.

Implements cugl::InputDevice.

◆ removeListener()

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.

Parameters
keyThe identifier for the listener
Returns
true if the listener was succesfully removed

◆ requestFocus()

virtual bool cugl::GameControllerInput::requestFocus ( Uint32  key)
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

Parameters
keyThe identifier for the focus object
Returns
false if key does not refer to an active listener

Reimplemented from cugl::InputDevice.

◆ size()

size_t cugl::GameControllerInput::size ( ) const

Returns the number of connected devices.

This value will be affected by doesFilter.

Returns
the number of connected devices.

◆ updateState()

virtual bool cugl::GameControllerInput::updateState ( const SDL_Event &  event,
const Timestamp stamp 
)
overridevirtual

Processes an SDL_Event

The dispatcher guarantees that an input device only receives events that it subscribes to.

Parameters
eventThe input event to process
stampThe event timestamp in CUGL time
Returns
false if the input indicates that the application should quit.

Implements cugl::InputDevice.


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