CUGL
Cornell University Game Library
Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
cugl::Input Class Reference

#include <CUInput.h>

Static Public Member Functions

template<typename T >
static Tget ()
 
template<typename T >
static bool activate ()
 
template<typename T >
static bool deactivate ()
 

Protected Member Functions

 Input ()
 
 ~Input ()
 
void clear ()
 
bool update (SDL_Event event)
 
bool registerDevice (std::type_index key, InputDevice *input)
 
InputDeviceunregisterDevice (std::type_index key)
 
void shutdown ()
 

Static Protected Member Functions

static bool start ()
 
static void stop ()
 
static Inputget ()
 

Protected Attributes

Timestamp _reference
 
Uint32 _roffset
 
std::unordered_map< std::type_index, InputDevice * > _devices
 
std::unordered_map< Uint32, std::unordered_set< std::type_index > > _subscribers
 

Static Protected Attributes

static Input_singleton
 

Friends

class Application
 

Detailed Description

This class is a dispatcher that provides access to the active input devices.

No input devices are active by default. This is to cut down on the overhead of processing a large number of input events. To use an input device, you must first activate it. Once active, you can access the devices from anywhere in the code via this singleton.

Activation happens via a templated syntax. You take the class of any input device that you want, and provide it to the activate() method. For example, if you want to activate the Keyboard, you use the syntax

 bool success = Input::activate<Keyboard>();

You get and deactivate an input device in the same way.

This is the only way to access an input device. All input devices have protected constructors and cannot be allocated by the user.

Constructor & Destructor Documentation

cugl::Input::Input ( )
inlineprotected

Creates an uninitialized instance of the Input dispatcher.

cugl::Input::~Input ( )
inlineprotected

Destroys the Input dispatcher, releasing any remaining devices.

Member Function Documentation

template<typename T >
static bool cugl::Input::activate ( )
inlinestatic

Activates the input device singleton for the given class T.

This method immediately registers the device, making it available for use. If T is not a valid input device, it returns false.

Template Parameters
TThe input device type
Returns
true if the input device was successfully activated.
void cugl::Input::clear ( )
protected

Clears the input state of all active input devices

All InputDevices have a method clearState() that flushes any cached input from the previous animation frame. This method (which should only be called by the Application class) invokes this method for all active devices.

template<typename T >
static bool cugl::Input::deactivate ( )
inlinestatic

Activates the input device singleton for the given class T.

This method immediately unregisters the device, making it no longer safe for use. If T is not an active input device, it returns false.

Template Parameters
TThe input device type
Returns
true if the input device was successfully deactivated.
static Input* cugl::Input::get ( )
staticprotected

Returns the Input dispatcher singleton.

This method (which should only be called by the Application class) provides direct access to the singleton so that events may be communicated. The user should never use this method. They should use the templated get() instead.

This method returns nullptr if start() has not yet been called.

Returns
the Input dispatcher singleton.
template<typename T >
static T* cugl::Input::get ( )
inlinestatic

Returns the input device singleton for the given class T.

If the input device is not active, it returns nullptr.

Template Parameters
TThe input device type
Returns
the input device singleton for the given class T.
bool cugl::Input::registerDevice ( std::type_index  key,
InputDevice input 
)
protected

Registers the given input device with the key.

This method places input into the _devices table with the given key. It also queries the device (via the queryEvents(std::vector<Uint32>&) method) for the associated event types. It actives these event types as necessary and adds this device as a subscriber for each event type.

If input is nullptr, then this method will return false;

Returns
true if registration was successful
void cugl::Input::shutdown ( )
protected

Shutdown and deregister any active input devices.

This method is emergency clean-up in case the user forgot to manually stop any active devices.

static bool cugl::Input::start ( )
staticprotected

Attempts to start the Input dispatcher, returning true on success.

This method (which should only be called by the Application class) allocates the singleton object. It it returns true, then the untemplated get() should no longer return nullptr.

If the dispatcher is already started, this method will return false.

Returns
true if the dispatcher successfully initialized.
static void cugl::Input::stop ( )
staticprotected

Stops the Input dispatcher.

This method (which should only be called by the Application class) deallocates the singleton object. Once it is called, the untemplated get() will subsequently return nullptr.

If the dispatcher is already stopped, this method will do nothing.

InputDevice* cugl::Input::unregisterDevice ( std::type_index  key)
protected

Registers the input device for the given the key.

This method removes the device for the given key from the _devices table. It queries the device (via the queryEvents(std::vector<Uint32>&) method) for the associated event types. It deactives these event types as necessary and removes this device as a subscriber for each event type.

Returns
the input device, ready for deletion.
bool cugl::Input::update ( SDL_Event  event)
protected

Processes an SDL_Event by all active input devices

All InputDevices have a method updateState(SDL_Event&) that reacts to an SDL input event. This method (which should only be called by the Application class) invokes this method for all appropriate devices. It only sends the event to devices that subscribe to its event type.

Parameters
eventThe input event to process
Returns
false if the input indicates that the application should quit.

Member Data Documentation

std::unordered_map<std::type_index, InputDevice*> cugl::Input::_devices
protected

The active devices registered with this service

Timestamp cugl::Input::_reference
protected

The reference timestamp to convert SDL time to CUGL time

Uint32 cugl::Input::_roffset
protected

The reference time offset to convert SDL time to CUGL time

Input* cugl::Input::_singleton
staticprotected

The singleton for this service

std::unordered_map<Uint32,std::unordered_set<std::type_index> > cugl::Input::_subscribers
protected

For each SDL_EventType, the devices that listen to that event type


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