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

#include <CUInput.h>

Public Member Functions

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

Static Public Member Functions

static bool start ()
 
static void stop ()
 
static Inputget ()
 
template<typename T >
static Tget ()
 
template<typename T >
static bool activate ()
 
template<typename T >
static bool deactivate ()
 

Protected Member Functions

 Input ()
 
 ~Input ()
 

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

◆ Input()

cugl::Input::Input ( )
inlineprotected

Creates an uninitialized instance of the Input dispatcher.

◆ ~Input()

cugl::Input::~Input ( )
inlineprotected

Destroys the Input dispatcher, releasing any remaining devices.

Member Function Documentation

◆ activate()

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.

◆ clear()

void cugl::Input::clear ( )

Clears the input state of all active input devices

All InputDevice objects have a method InputDevice#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.

◆ deactivate()

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.

◆ get() [1/2]

static Input* cugl::Input::get ( )
static

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.

◆ get() [2/2]

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.

◆ registerDevice()

bool cugl::Input::registerDevice ( std::type_index  key,
InputDevice input 
)

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 InputDevice#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

◆ shutdown()

void cugl::Input::shutdown ( )

Shutdown and deregister any active input devices.

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

◆ start()

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

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.

◆ stop()

static void cugl::Input::stop ( )
static

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.

◆ unregisterDevice()

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

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 InputDevice#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.

◆ update()

bool cugl::Input::update ( SDL_Event  event)

Processes an SDL_Event by all active input devices

All InputDevice objects have a method InputDevice#updateState() 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

◆ _devices

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

The active devices registered with this service

◆ _reference

Timestamp cugl::Input::_reference
protected

The reference timestamp to convert SDL time to CUGL time

◆ _roffset

Uint32 cugl::Input::_roffset
protected

The reference time offset to convert SDL time to CUGL time

◆ _singleton

Input* cugl::Input::_singleton
staticprotected

The singleton for this service

◆ _subscribers

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: