Gainput  v1.0.0
Classes | Public Types | Public Member Functions | List of all members
gainput::InputManager Class Reference

Manages all input devices and some other helpful stuff. More...

#include <GainputInputManager.h>

Classes

struct  DeviceInput
 

Public Types

typedef HashMap< DeviceId, InputDevice * > DeviceMap
 
typedef DeviceMap::iterator iterator
 Iterator over all registered devices.
 
typedef DeviceMap::const_iterator const_iterator
 Const iterator over all registered devices.
 

Public Member Functions

 InputManager (bool useSystemTime=true, Allocator &allocator=GetDefaultAllocator())
 Initializes the manager. More...
 
 ~InputManager ()
 Destructs the manager.
 
void SetDisplaySize (int width, int height)
 Sets the window resolution. More...
 
void HandleEvent (XEvent &event)
 [LINUX ONLY] Lets the InputManager handle the given X event. More...
 
void HandleMessage (const MSG &msg)
 [WINDOWS ONLY] Lets the InputManager handle the given Windows message. More...
 
int32_t HandleInput (AInputEvent *event)
 [ANDROID ONLY] Lets the InputManager handle the given input event.
 
void HandleDeviceInput (DeviceInput const &input)
 
void Update ()
 Updates the input state, call this every frame. More...
 
void Update (uint64_t deltaTime)
 Updates the input state and the manager's time, call this every frame. More...
 
AllocatorGetAllocator () const
 Returns the allocator to be used for memory allocations.
 
uint64_t GetTime () const
 Returns a monotonic time in milliseconds.
 
template<class T >
DeviceId CreateDevice (unsigned index=InputDevice::AutoIndex, InputDevice::DeviceVariant variant=InputDevice::DV_STANDARD)
 Creates an input device and registers it with the manager. More...
 
template<class T >
T * CreateAndGetDevice (unsigned index=InputDevice::AutoIndex, InputDevice::DeviceVariant variant=InputDevice::DV_STANDARD)
 Creates an input device, registers it with the manager and returns it. More...
 
InputDeviceGetDevice (DeviceId deviceId)
 Returns the input device with the given ID. More...
 
const InputDeviceGetDevice (DeviceId deviceId) const
 Returns the input device with the given ID. More...
 
DeviceId FindDeviceId (const char *typeName, unsigned index) const
 Returns the ID of the device with the given type and index. More...
 
DeviceId FindDeviceId (InputDevice::DeviceType type, unsigned index) const
 Returns the ID of the device with the given type and index. More...
 
iterator begin ()
 Returns the begin iterator over all registered devices.
 
iterator end ()
 Returns the end iterator over all registered devices.
 
const_iterator begin () const
 Returns the begin iterator over all registered devices.
 
const_iterator end () const
 Returns the end iterator over all registered devices.
 
ListenerId AddListener (InputListener *listener)
 Registers a listener to be notified when a button state changes. More...
 
void RemoveListener (ListenerId listenerId)
 De-registers the given listener.
 
void ReorderListeners ()
 Sorts the list of listeners which controls the order in which listeners are called. More...
 
size_t GetAnyButtonDown (DeviceButtonSpec *outButtons, size_t maxButtonCount) const
 Checks if any button on any device is down. More...
 
unsigned GetDeviceCountByType (InputDevice::DeviceType type) const
 Returns the number of devices with the given type.
 
int GetDisplayWidth () const
 Returns the graphical display's width in pixels.
 
int GetDisplayHeight () const
 Returns the graphical display's height in pixels.
 
ModifierId AddDeviceStateModifier (DeviceStateModifier *modifier)
 Registers a modifier that will be called after devices have been updated.
 
void RemoveDeviceStateModifier (ModifierId modifierId)
 De-registers the given modifier.
 
void EnqueueConcurrentChange (InputDevice &device, InputState &state, InputDeltaState *delta, DeviceButtonId buttonId, bool value)
 
void EnqueueConcurrentChange (InputDevice &device, InputState &state, InputDeltaState *delta, DeviceButtonId buttonId, float value)
 
void ConnectForStateSync (const char *ip, unsigned port)
 [IN dev BUILDS ONLY] Connect to a remote host to send device state changes to.
 
void StartDeviceStateSync (DeviceId deviceId)
 [IN dev BUILDS ONLY] Initiate sending of device state changes to the given device.
 
void SetDebugRenderingEnabled (bool enabled)
 Enable/disable debug rendering of input devices.
 
bool IsDebugRenderingEnabled () const
 Returns true if debug rendering is enabled, false otherwise.
 
void SetDebugRenderer (DebugRenderer *debugRenderer)
 Sets the debug renderer to be used if debug rendering is enabled.
 
DebugRendererGetDebugRenderer () const
 Returns the previously set debug renderer.
 

Detailed Description

Manages all input devices and some other helpful stuff.

This manager takes care of all device-related things. Normally, you should only need one that contains all your input devices.

After instantiating an InputManager, you have to call SetDisplaySize(). You should also create some input devices using the template function CreateDevice() which returns the device ID that is needed to do anything further with the device (for example, see InputMap).

The manager has to be updated every frame by calling Update(). Depending on the platform, you may have to call another function as part of your message handling code (see HandleMessage(), HandleInput()).

Note that destruction of an InputManager invalidates all input maps based on it and all devices created through it.

Examples:
/samples/basic/basicsample_android.cpp, /samples/basic/basicsample_linux.cpp, /samples/basic/basicsample_win.cpp, /samples/dynamic/dynamicsample.cpp, /samples/gesture/gesturesample.cpp, /samples/listener/listenersample.cpp, /samples/recording/recordingsample.cpp, and /samples/sync/syncsample.cpp.

Constructor & Destructor Documentation

◆ InputManager()

gainput::InputManager::InputManager ( bool  useSystemTime = true,
Allocator allocator = GetDefaultAllocator() 
)

Initializes the manager.

Further initialization is typically necessary.

Parameters
useSystemTimeSpecifies if the GetTime() function uses system time or the time supplied to Update(uint64_t).
allocatorThe memory allocator to be used for all allocations.
See also
SetDisplaySize
GetTime

Member Function Documentation

◆ AddListener()

ListenerId gainput::InputManager::AddListener ( InputListener listener)

Registers a listener to be notified when a button state changes.

If there are listeners registered, all input devices will have to record their state changes. This incurs extra runtime costs.

Examples:
/samples/listener/listenersample.cpp, /samples/recording/recordingsample.cpp, and /samples/sync/syncsample.cpp.

◆ CreateAndGetDevice()

template<class T >
T * gainput::InputManager::CreateAndGetDevice ( unsigned  index = InputDevice::AutoIndex,
InputDevice::DeviceVariant  variant = InputDevice::DV_STANDARD 
)
inline

Creates an input device, registers it with the manager and returns it.

Template Parameters
TThe input device class, muste be derived from InputDevice.
Parameters
variantRequests the specified device variant. Note that this is only a request. Another implementation variant of the device may silently be instantiated instead. To determine what variant was instantiated, call InputDevice::GetVariant().
Returns
The newly created input device.
Examples:
/samples/gesture/gesturesample.cpp.

◆ CreateDevice()

template<class T >
DeviceId gainput::InputManager::CreateDevice ( unsigned  index = InputDevice::AutoIndex,
InputDevice::DeviceVariant  variant = InputDevice::DV_STANDARD 
)
inline

Creates an input device and registers it with the manager.

Template Parameters
TThe input device class, muste be derived from InputDevice.
Parameters
variantRequests the specified device variant. Note that this is only a request. Another implementation variant of the device may silently be instantiated instead. To determine what variant was instantiated, call InputDevice::GetVariant().
Returns
The ID of the newly created input device.
Examples:
/samples/basic/basicsample_android.cpp, /samples/basic/basicsample_linux.cpp, /samples/basic/basicsample_win.cpp, /samples/dynamic/dynamicsample.cpp, /samples/gesture/gesturesample.cpp, /samples/listener/listenersample.cpp, /samples/recording/recordingsample.cpp, and /samples/sync/syncsample.cpp.

◆ FindDeviceId() [1/2]

DeviceId gainput::InputManager::FindDeviceId ( const char *  typeName,
unsigned  index 
) const

Returns the ID of the device with the given type and index.

Parameters
typeNameThe name of the device type. Should come from InputDevice::GetTypeName().
indexThe index of the device. Should come from InputDevice::GetIndex().
Returns
The device's ID or InvalidDeviceId if no matching device exists.
Examples:
/samples/dynamic/dynamicsample.cpp.

◆ FindDeviceId() [2/2]

DeviceId gainput::InputManager::FindDeviceId ( InputDevice::DeviceType  type,
unsigned  index 
) const

Returns the ID of the device with the given type and index.

Parameters
typeThe device type. Should come from InputDevice::GetType().
indexThe index of the device. Should come from InputDevice::GetIndex().
Returns
The device's ID or InvalidDeviceId if no matching device exists.

◆ GetAnyButtonDown()

size_t gainput::InputManager::GetAnyButtonDown ( DeviceButtonSpec outButtons,
size_t  maxButtonCount 
) const

Checks if any button on any device is down.

Parameters
[out]outButtonsAn array with maxButtonCount fields to receive the device buttons that are down.
maxButtonCountThe number of fields in outButtons.
Returns
The number of device buttons written to outButtons.
Examples:
/samples/dynamic/dynamicsample.cpp.

◆ GetDevice() [1/2]

InputDevice * gainput::InputManager::GetDevice ( DeviceId  deviceId)
inline

Returns the input device with the given ID.

Returns
The input device or 0 if it doesn't exist.
Examples:
/samples/basic/basicsample_win.cpp, /samples/dynamic/dynamicsample.cpp, /samples/recording/recordingsample.cpp, and /samples/sync/syncsample.cpp.

◆ GetDevice() [2/2]

const InputDevice * gainput::InputManager::GetDevice ( DeviceId  deviceId) const
inline

Returns the input device with the given ID.

Returns
The input device or 0 if it doesn't exist.

◆ HandleEvent()

void gainput::InputManager::HandleEvent ( XEvent &  event)

[LINUX ONLY] Lets the InputManager handle the given X event.

Call this function for event types MotionNotify, ButtonPress, ButtonRelease, KeyPress, KeyRelease.

Examples:
/samples/basic/basicsample_linux.cpp, /samples/dynamic/dynamicsample.cpp, /samples/gesture/gesturesample.cpp, /samples/listener/listenersample.cpp, /samples/recording/recordingsample.cpp, and /samples/sync/syncsample.cpp.

◆ HandleMessage()

void gainput::InputManager::HandleMessage ( const MSG &  msg)

[WINDOWS ONLY] Lets the InputManager handle the given Windows message.

Call this function for message types WM_CHAR, WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP, WM_?BUTTON*, WM_MOUSEMOVE, WM_MOUSEWHEEL.

Examples:
/samples/basic/basicsample_win.cpp, /samples/dynamic/dynamicsample.cpp, /samples/gesture/gesturesample.cpp, /samples/listener/listenersample.cpp, /samples/recording/recordingsample.cpp, and /samples/sync/syncsample.cpp.

◆ ReorderListeners()

void gainput::InputManager::ReorderListeners ( )

Sorts the list of listeners which controls the order in which listeners are called.

The order of listeners may be important as the functions being called to notify a listener of a state change can control if the state change will be passed to any consequent listeners. Call this function whenever listener priorites have changed. It is automatically called by AddListener() and RemoveListener().

◆ SetDisplaySize()

void gainput::InputManager::SetDisplaySize ( int  width,
int  height 
)
inline

Sets the window resolution.

Informs the InputManager and its devices of the size of the window that is used for receiving inputs. For example, the size is used to to normalize mouse inputs.

Examples:
/samples/basic/basicsample_android.cpp, /samples/basic/basicsample_linux.cpp, /samples/basic/basicsample_win.cpp, /samples/dynamic/dynamicsample.cpp, /samples/gesture/gesturesample.cpp, /samples/listener/listenersample.cpp, /samples/recording/recordingsample.cpp, and /samples/sync/syncsample.cpp.

◆ Update() [1/2]

void gainput::InputManager::Update ( )

◆ Update() [2/2]

void gainput::InputManager::Update ( uint64_t  deltaTime)

Updates the input state and the manager's time, call this every frame.

Updates the time returned by GetTime() and then calls the regular Update(). This function should only be called if the InputManager was initialized with useSystemTime set to false.

Parameters
deltaTimeThe provided must be in milliseconds.
See also
Update
GetTime

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