ASGE  3.2.0
Simple Game Framework in GL
ASGE::Input Class Referenceabstract

#include <Input.hpp>

Public Types

using CallbackID = std::string
 

Public Member Functions

 Input ()
 
 Input (const Input &rhs)=delete
 
Inputoperator= (const Input &rhs)=delete
 
virtual ~Input ()
 
virtual bool init (Renderer *renderer)=0
 
virtual void update ()=0
 
virtual void updateGamePadMappings (const std::filesystem::path &mappings_file)=0
 
virtual void getCursorPos (double &xpos, double &ypos) const =0
 
virtual void setCursorMode (ASGE::MOUSE::CursorMode mode)=0
 
virtual void setCursorPos (double x, double y) const =0
 
virtual GamePadData getGamePad (int idx) const =0
 
virtual GamePadData getGamePad () const =0
 
virtual std::vector< GamePadDatagetGamePads () const =0
 
void sendEvent (EventType type, SharedEventData data)
 
template<typename T , typename T2 >
CallbackID addCallbackFnc (EventType type, T fncPtr, T2 *obj)
 
template<typename T >
CallbackID addCallbackFnc (EventType type, T fncPtr)
 
void unregisterCallback (CallbackID id)
 

Public Attributes

bool use_threads = false
 

Detailed Description

The input handler system.

The input class is designed to work alongside the renderer and is responsible for sending events to interested parties. It will also provide helper functions that can retrieve non-event driven data such as the cursor position or the gamepad status. Much of the underlying implementation will be platform specific, with input offering a compatible interface.

// register a key callback
key_callback_id = this->inputs->addCallbackFnc(ASGE::EventType::E_KEY, &SampleGame::input, this);
// input handler for keys
void SampleGame::input(const ASGE::SharedEventData data)
{
const auto *key_event = dynamic_cast<const ASGE::KeyEvent*>(data.get());
auto action = key_event->action;
auto key = key_event->key;
if (key == ASGE::KEYS::KEY_ESCAPE)
{
signalExit();
}
}
// unregister a key callback
this->inputs->unregisterCallback(key_callback_id);
a key event
Definition: InputEvents.hpp:62

Definition at line 62 of file Input.hpp.

Constructor & Destructor Documentation

◆ Input()

ASGE::Input::Input ( )

Default Constructor.

◆ ~Input()

virtual ASGE::Input::~Input ( )
virtual

Destructor. Clears all callback functions.

Member Function Documentation

◆ addCallbackFnc() [1/2]

template<typename T >
CallbackID ASGE::Input::addCallbackFnc ( EventType  type,
fncPtr 
)
inline

Adds a callback function. Many events are callback driven. This function allows the use of a function to be called when the event happens.

Parameters
[in]typeThe type of event being listened for.
[in]fncPtrThe function pointer.
See also
EventType
Returns
the handle for the registered callback.

Definition at line 226 of file Input.hpp.

◆ addCallbackFnc() [2/2]

template<typename T , typename T2 >
CallbackID ASGE::Input::addCallbackFnc ( EventType  type,
fncPtr,
T2 *  obj 
)
inline

Adds a callback function. Many events are callback driven. This function allows the use of a member function to be called when the event happens.

Parameters
[in]typeThe type of event being listened for.
[in]fncPtrThe function pointer.
[in]objThe object (this ptr) the function belongs to.
See also
EventType
Returns
The handle for the registered callback.

Definition at line 207 of file Input.hpp.

◆ getCursorPos()

virtual void ASGE::Input::getCursorPos ( double &  xpos,
double &  ypos 
) const
pure virtual

Gets the cursor's (mouse) position. Grabs the current position in screen space of the mouse cursor. This can be used to detect the location of clicks as well as its current position. The positions are stored directly in the two parameters passed in.

Parameters
[in]xposThe position in the X axis.
[in]yposThe position in the Y axis.

◆ getGamePad() [1/2]

virtual GamePadData ASGE::Input::getGamePad ( ) const
pure virtual

Obtains the first connected controllers data. Searches for the first connected controller and then returns any data relating to it, including naming, axis and button states. This is currently polled (demanded) rather than event driven and is normally handled by the renderer's window.

Returns
The first connected gamepad and its data.
See also
GamePadData

◆ getGamePad() [2/2]

virtual GamePadData ASGE::Input::getGamePad ( int  idx) const
pure virtual

Obtains the controllers data. Searches for a controller connected at a specific idx and returns any data relating to it, including naming, axis and button states. This is currently polled (demanded) rather than event driven and is normally handled by the renderer's window.

Parameters
[in]idxThe id of the connected controller.
Returns
The game controller's connected state and data.
See also
GamePadData

◆ getGamePads()

virtual std::vector<GamePadData> ASGE::Input::getGamePads ( ) const
pure virtual

Retrieves a list of all connected game pads. Searches for all possible connected gamepads and retrieves the data for each one. If no attached gamepads are present then the vector will be empty. This can be used to easily process all connected inputs in a single call, rather than individually checking for each index to see if a gamepad is connected.

Returns
The game controllers connected state and data.

Example:

auto gamepads = inputs->getGamePads();
for (const auto& gamepad : gamepads)
{
Logging::INFO(gamepad.name);
}
void INFO(const std::string &message)
Definition: Logger.hpp:302
See also
GamePadData

◆ init()

virtual bool ASGE::Input::init ( Renderer renderer)
pure virtual

Destructor. Initialises and sets up the input system.

Parameters
rendererA pointer to the linked renderer system.
Returns
Returns whether the input system initialised.

◆ sendEvent()

void ASGE::Input::sendEvent ( EventType  type,
SharedEventData  data 
)

Sends an event. When an event happens, the type of event and the data relating to it is forwarded to any interested parties via their function pointer. NOTE: the data is shared because the events can be sent using threads and this ensures their lifespan.

Parameters
[in]typeThe type of event registered.
[in]dataThe data relating to the event.
See also
EventType
SharedEventData

◆ setCursorMode()

virtual void ASGE::Input::setCursorMode ( ASGE::MOUSE::CursorMode  mode)
pure virtual

Sets the cursor's (mouse) mode. Allows control over the mouse cursors visibility and whether or not it should be locked to the window.

Parameters
[in]modeThe cursor mode to set.
See also
ASGE::CursorMode

◆ setCursorPos()

virtual void ASGE::Input::setCursorPos ( double  x,
double  y 
) const
pure virtual

Sets the cursor's current position

Parameters
[in]xThe x position to move the mouse to.
[in]yThe y position to move the mouse to.

◆ unregisterCallback()

void ASGE::Input::unregisterCallback ( CallbackID  id)

Removes a callback function.

This requires a valid handle to work correctly. The handle is returned when the function event is added. This prevents callbacks happening for classes or functions that no longer exist.

Parameters
[in]idThe handle for the registered callback.

◆ update()

virtual void ASGE::Input::update ( )
pure virtual

Updates the input handler.

◆ updateGamePadMappings()

virtual void ASGE::Input::updateGamePadMappings ( const std::filesystem::path &  mappings_file)
pure virtual

Updates the GamePad mappings used. The engine makes use of a third party solution to correctly map button presses to connected gamepads in a consistent way, regardless of manufacturer or type. The aims is to solve the problem with the "X" button differs depending on the model of gamepad used. The file format should follow the project SDL_GameControllerDB which is the mapping system uses.

Parameters
[in]mappings_fileThe updated GamePad mappings.
See also
https://github.com/gabomdq/SDL_GameControllerDB

Member Data Documentation

◆ use_threads

bool ASGE::Input::use_threads = false

Allows events to use threads. Rather than sending events and waiting for their responses, threads can be used to perform the callbacks asynchronously. When using threads, care must be taken to ensure any operations in the callbacks is thread safe.

Definition at line 252 of file Input.hpp.


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