CUGL 2.3
Cornell University Game Library
|
#include <CUInput.h>
Public Member Functions | |
void | clear () |
bool | update (SDL_Event event) |
bool | registerDevice (std::type_index key, InputDevice *input) |
InputDevice * | unregisterDevice (std::type_index key) |
void | shutdown () |
Static Public Member Functions | |
static bool | start () |
static void | stop () |
static Input * | get () |
template<typename T > | |
static T * | get () |
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 |
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.
|
inlineprotected |
Creates an uninitialized instance of the Input dispatcher.
|
inlineprotected |
Destroys the Input dispatcher, releasing any remaining devices.
|
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.
T | The input device type |
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.
|
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.
T | The input device type |
|
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 device singleton for the given class T.
If the input device is not active, it returns nullptr.
T | The input device type |
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;
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.
|
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.
|
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.
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.
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.
event | The input event to process |
|
protected |
The active devices registered with this service
|
protected |
The reference timestamp to convert SDL time to CUGL time
|
protected |
The reference time offset to convert SDL time to CUGL time
|
staticprotected |
The singleton for this service
|
protected |
For each SDL_EventType, the devices that listen to that event type