Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Input::InputManager Class Reference

Action-based input dispatcher for polled input values and callbacks. More...

#include <InputManager.h>

Collaboration diagram for Input::InputManager:

Classes

struct  BoolAction
 Runtime state for one bool action name. More...
struct  BoolCallbackEntry
 Stored callback registration for a bool action. More...
struct  CallbackHandle
 Opaque handle for a registered input callback. More...
struct  FloatAction
 Runtime state for one float action name. More...
struct  FloatCallbackEntry
 Stored callback registration for a float action. More...
struct  ProviderHandle
 Opaque handle for a registered input provider. More...
struct  Vec2Action
 Runtime state for one vec2 action name. More...
struct  Vec2CallbackEntry
 Stored callback registration for a vec2 action. More...

Public Types

enum class  DispatchRule {
  EveryFrame ,
  Changed ,
  Pressed ,
  Released
}
 Rule that controls when a registered callback runs during InputManager::dispatchCallbacks(). More...
enum class  ActionValueType {
  Bool ,
  Float ,
  Vec2
}
 Runtime value type for an action provider. More...
using BoolProvider = std::function<bool()>
 Provider function for a bool action.
using FloatProvider = std::function<float()>
 Provider function for a float action.
using Vec2Provider = std::function<glm::vec2()>
 Provider function for a vec2 action.
using BoolCallback = std::function<void( bool value )>
 Callback function for a bool action. Receives the current sampled value.
using FloatCallback = std::function<void( float value )>
 Callback function for a float action. Receives the current sampled value.
using Vec2Callback = std::function<void( glm::vec2 value )>
 Callback function for a vec2 action. Receives the current sampled value.

Public Member Functions

 InputManager ()
 ~InputManager ()=default
void setupDefaultBindings ()
 Sets up the default controller mappings.
ProviderHandle bindBoolAction (std::string name, BoolProvider provider)
 Binds a provider for a named bool action.
ProviderHandle bindFloatAction (std::string name, FloatProvider provider)
 Binds a provider for a named float action.
ProviderHandle bindVec2Action (std::string name, Vec2Provider provider)
 Binds a provider for a named vec2 action.
CallbackHandle onBoolAction (const std::string &name, DispatchRule rule, BoolCallback callback)
 Registers a callback for a named bool action.
CallbackHandle onBoolPressed (const std::string &name, std::function< void()> callback)
 Registers a callback for a bool action press transition.
CallbackHandle onBoolReleased (const std::string &name, std::function< void()> callback)
 Registers a callback for a bool action release transition.
CallbackHandle onFloatAction (const std::string &name, DispatchRule rule, FloatCallback callback)
 Registers a callback for a named float action.
CallbackHandle onVec2Action (const std::string &name, DispatchRule rule, Vec2Callback callback)
 Registers a callback for a named vec2 action.
void removeCallback (CallbackHandle handle)
 Removes a callback registered with one of the on-action functions.
void removeProvider (ProviderHandle handle)
 Removes a provider registered with one of the bind-action functions.
void beginFrame ()
 Samples every bound provider and advances action state for a new frame.
void dispatchCallbacks ()
 Invokes callbacks that match their dispatch rule for the current frame.
bool getBoolAction (const std::string &name) const
 Returns the latest sampled bool value for a named action.
float getFloatAction (const std::string &name) const
 Returns the latest sampled float value for a named action.
glm::vec2 getVec2Action (const std::string &name) const
 Returns the latest sampled vec2 value for a named action.
const XrInputHandler & getXrInputHandler () const
XrInputHandler & getXrInputHandler ()
const XrRig & getXrRig () const
XrRig & getXrRig ()

Private Member Functions

CallbackHandle makeHandle ()
 Allocates the next callback handle id.

Private Attributes

std::unordered_map< std::string, BoolAction > boolActions_
 Bool actions keyed by action name.
std::unordered_map< std::string, FloatAction > floatActions_
 Float actions keyed by action name.
std::unordered_map< std::string, Vec2Action > vec2Actions_
 Vec2 actions keyed by action name.
uint64_t nextCallbackId_ = 1
 Next non-zero callback id assigned by makeHandle().
uint64_t nextProviderId_ = 1
 Next non-zero provider id assigned by a bind function.
bool callbacksDispatched_ = true
 Guards against dispatching callbacks twice for one sampled frame.
XrInputHandler xrInputHandler_
XrRig xrRig_

Detailed Description

Action-based input dispatcher for polled input values and callbacks.

InputManager stores named actions by value type: bool, float, and vec2. A provider function supplies the current value when InputManager::beginFrame() runs. Polling functions return the latest sampled value. Registered callbacks run when InputManager::dispatchCallbacks() is called.

Action names are type-local. The same string can be bound as a bool action and a float action without sharing state.

Definition at line 57 of file InputManager.h.

Member Typedef Documentation

◆ BoolCallback

using Input::InputManager::BoolCallback = std::function<void( bool value )>

Callback function for a bool action. Receives the current sampled value.

Definition at line 145 of file InputManager.h.

◆ BoolProvider

using Input::InputManager::BoolProvider = std::function<bool()>

Provider function for a bool action.

Definition at line 138 of file InputManager.h.

◆ FloatCallback

using Input::InputManager::FloatCallback = std::function<void( float value )>

Callback function for a float action. Receives the current sampled value.

Definition at line 147 of file InputManager.h.

◆ FloatProvider

using Input::InputManager::FloatProvider = std::function<float()>

Provider function for a float action.

Definition at line 140 of file InputManager.h.

◆ Vec2Callback

using Input::InputManager::Vec2Callback = std::function<void( glm::vec2 value )>

Callback function for a vec2 action. Receives the current sampled value.

Definition at line 149 of file InputManager.h.

◆ Vec2Provider

using Input::InputManager::Vec2Provider = std::function<glm::vec2()>

Provider function for a vec2 action.

Definition at line 142 of file InputManager.h.

Member Enumeration Documentation

◆ ActionValueType

Runtime value type for an action provider.

Enumerator
Bool 

Boolean action value.

Float 

Floating-point action value.

Vec2 

2D vector action value.

Definition at line 81 of file InputManager.h.

◆ DispatchRule

Rule that controls when a registered callback runs during InputManager::dispatchCallbacks().

InputManager::DispatchRule::Pressed and InputManager::DispatchRule::Released apply to bool actions only. Float and vec2 callbacks dispatch for InputManager::DispatchRule::EveryFrame and InputManager::DispatchRule::Changed.

Enumerator
EveryFrame 

Dispatch once for every sampled frame.

Changed 

Dispatch when the sampled value differs from the previous frame.

Pressed 

Dispatch when a bool action changes from false to true.

Released 

Dispatch when a bool action changes from true to false.

Definition at line 70 of file InputManager.h.

Constructor & Destructor Documentation

◆ InputManager()

Input::InputManager::InputManager ( )

◆ ~InputManager()

Input::InputManager::~InputManager ( )
default

Member Function Documentation

◆ beginFrame()

void Input::InputManager::beginFrame ( )

Samples every bound provider and advances action state for a new frame.

The previous sampled value is updated before the provider is called. Call this once per frame before polling actions or dispatching callbacks.

◆ bindBoolAction()

ProviderHandle Input::InputManager::bindBoolAction ( std::string name,
BoolProvider provider )

Binds a provider for a named bool action.

Parameters
nameAction name to bind.
providerFunction sampled by InputManager::beginFrame(). An empty provider samples as false.
Returns
Handle for removing the provider.

If a bool provider already exists for name, this call replaces it and resets the cached value to false. Existing bool callbacks for name remain registered.

◆ bindFloatAction()

ProviderHandle Input::InputManager::bindFloatAction ( std::string name,
FloatProvider provider )

Binds a provider for a named float action.

Parameters
nameAction name to bind.
providerFunction sampled by InputManager::beginFrame(). An empty provider samples as 0.0f.
Returns
Handle for removing the provider.

If a float provider already exists for name, this call replaces it and resets the cached value to 0.0f. Existing float callbacks for name remain registered.

◆ bindVec2Action()

ProviderHandle Input::InputManager::bindVec2Action ( std::string name,
Vec2Provider provider )

Binds a provider for a named vec2 action.

Parameters
nameAction name to bind.
providerFunction sampled by InputManager::beginFrame(). An empty provider samples as {0.0f, 0.0f}.
Returns
Handle for removing the provider.

If a vec2 provider already exists for name, this call replaces it and resets the cached value to zero. Existing vec2 callbacks for name remain registered.

◆ dispatchCallbacks()

void Input::InputManager::dispatchCallbacks ( )

Invokes callbacks that match their dispatch rule for the current frame.

A dispatch pass runs at most once per InputManager::beginFrame() call. Additional calls before the next frame do nothing.

◆ getBoolAction()

bool Input::InputManager::getBoolAction ( const std::string & name) const
nodiscard

Returns the latest sampled bool value for a named action.

Parameters
nameAction name to read.
Returns
Current sampled value, or false when no bool action exists for name.

◆ getFloatAction()

float Input::InputManager::getFloatAction ( const std::string & name) const
nodiscard

Returns the latest sampled float value for a named action.

Parameters
nameAction name to read.
Returns
Current sampled value, or 0.0f when no float action exists for name.

◆ getVec2Action()

glm::vec2 Input::InputManager::getVec2Action ( const std::string & name) const
nodiscard

Returns the latest sampled vec2 value for a named action.

Parameters
nameAction name to read.
Returns
Current sampled value, or {0.0f, 0.0f} when no vec2 action exists for name.

◆ getXrInputHandler() [1/2]

XrInputHandler & Input::InputManager::getXrInputHandler ( )

◆ getXrInputHandler() [2/2]

const XrInputHandler & Input::InputManager::getXrInputHandler ( ) const

◆ getXrRig() [1/2]

XrRig & Input::InputManager::getXrRig ( )

◆ getXrRig() [2/2]

const XrRig & Input::InputManager::getXrRig ( ) const

◆ makeHandle()

CallbackHandle Input::InputManager::makeHandle ( )
nodiscardprivate

Allocates the next callback handle id.

Returns
Valid callback handle with a non-zero id.

◆ onBoolAction()

CallbackHandle Input::InputManager::onBoolAction ( const std::string & name,
DispatchRule rule,
BoolCallback callback )

Registers a callback for a named bool action.

Parameters
nameAction name to observe.
ruleDispatch rule for the callback.
callbackFunction invoked by InputManager::dispatchCallbacks(). Empty callbacks are ignored.
Returns
Handle for removing the callback.

Callbacks can be registered before a provider is bound. Missing providers sample as false.

◆ onBoolPressed()

CallbackHandle Input::InputManager::onBoolPressed ( const std::string & name,
std::function< void()> callback )

Registers a callback for a bool action press transition.

Parameters
nameAction name to observe.
callbackFunction invoked when the action changes from false to true.
Returns
Handle for removing the callback.

◆ onBoolReleased()

CallbackHandle Input::InputManager::onBoolReleased ( const std::string & name,
std::function< void()> callback )

Registers a callback for a bool action release transition.

Parameters
nameAction name to observe.
callbackFunction invoked when the action changes from true to false.
Returns
Handle for removing the callback.

◆ onFloatAction()

CallbackHandle Input::InputManager::onFloatAction ( const std::string & name,
DispatchRule rule,
FloatCallback callback )

Registers a callback for a named float action.

Parameters
nameAction name to observe.
ruleDispatch rule for the callback. Only InputManager::DispatchRule::EveryFrame and InputManager::DispatchRule::Changed dispatch.
callbackFunction invoked by InputManager::dispatchCallbacks(). Empty callbacks are ignored.
Returns
Handle for removing the callback.

Changed callbacks dispatch when the absolute difference from the previous sampled value is greater than 0.0001f. Missing providers sample as 0.0f.

◆ onVec2Action()

CallbackHandle Input::InputManager::onVec2Action ( const std::string & name,
DispatchRule rule,
Vec2Callback callback )

Registers a callback for a named vec2 action.

Parameters
nameAction name to observe.
ruleDispatch rule for the callback. Only InputManager::DispatchRule::EveryFrame and InputManager::DispatchRule::Changed dispatch.
callbackFunction invoked by InputManager::dispatchCallbacks(). Empty callbacks are ignored.
Returns
Handle for removing the callback.

Changed callbacks dispatch when the squared distance from the previous sampled value is greater than 0.000001f. Missing providers sample as {0.0f, 0.0f}.

◆ removeCallback()

void Input::InputManager::removeCallback ( CallbackHandle handle)

Removes a callback registered with one of the on-action functions.

Parameters
handleCallback handle returned at registration time.

Invalid handles and already removed handles are ignored.

◆ removeProvider()

void Input::InputManager::removeProvider ( ProviderHandle handle)

Removes a provider registered with one of the bind-action functions.

Parameters
handleProvider handle returned at bind time.

Removing a provider clears the provider function and resets the cached value to that type's default. Registered callbacks for the action remain registered. Invalid handles, stale handles, and already removed handles are ignored.

◆ setupDefaultBindings()

void Input::InputManager::setupDefaultBindings ( )

Sets up the default controller mappings.

Member Data Documentation

◆ boolActions_

std::unordered_map<std::string, BoolAction> Input::InputManager::boolActions_
private

Bool actions keyed by action name.

Definition at line 387 of file InputManager.h.

◆ callbacksDispatched_

bool Input::InputManager::callbacksDispatched_ = true
private

Guards against dispatching callbacks twice for one sampled frame.

Definition at line 392 of file InputManager.h.

◆ floatActions_

std::unordered_map<std::string, FloatAction> Input::InputManager::floatActions_
private

Float actions keyed by action name.

Definition at line 388 of file InputManager.h.

◆ nextCallbackId_

uint64_t Input::InputManager::nextCallbackId_ = 1
private

Next non-zero callback id assigned by makeHandle().

Definition at line 390 of file InputManager.h.

◆ nextProviderId_

uint64_t Input::InputManager::nextProviderId_ = 1
private

Next non-zero provider id assigned by a bind function.

Definition at line 391 of file InputManager.h.

◆ vec2Actions_

std::unordered_map<std::string, Vec2Action> Input::InputManager::vec2Actions_
private

Vec2 actions keyed by action name.

Definition at line 389 of file InputManager.h.

◆ xrInputHandler_

XrInputHandler Input::InputManager::xrInputHandler_
private

Definition at line 394 of file InputManager.h.

◆ xrRig_

XrRig Input::InputManager::xrRig_
private

Definition at line 395 of file InputManager.h.


The documentation for this class was generated from the following file:
  • /workspace/magerbeton/gl3-vulkan/code/Engine/include/Engine/Input/InputManager.h