Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
InputManager.h
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <glm/glm.hpp>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10#include "XrInputHandler.h"
11#include "XrRig.h"
12
13namespace Input
14{
20 namespace Actions
21 {
23 inline constexpr auto XrLeftTrigger = "XR.LeftTrigger";
25 inline constexpr auto XrRightTrigger = "XR.RightTrigger";
27 inline constexpr auto XrLeftGrip = "XR.LeftGrip";
29 inline constexpr auto XrRightGrip = "XR.RightGrip";
31 inline constexpr auto XrLeftThumbstick = "XR.LeftThumbstick";
33 inline constexpr auto XrRightThumbstick = "XR.RightThumbstick";
35 inline constexpr auto XrRightAButton = "XR.RightAButton";
37 inline constexpr auto XrRightBButton = "XR.RightBButton";
39 inline constexpr auto XrLeftXButton = "XR.LeftXButton";
41 inline constexpr auto XrLeftYButton = "XR.LeftYButton";
43 inline constexpr auto XrMenu = "XR.Menu";
44 } // namespace Actions
45
58 {
59 public:
61 ~InputManager() = default;
77
81 enum class ActionValueType
82 {
86 };
87
95 {
96 uint64_t id = 0;
97
102 [[nodiscard]] explicit operator bool() const
103 {
104 return id != 0;
105 }
106 };
107
112
121 {
122 uint64_t id = 0;
123
125 std::string name;
126
131 [[nodiscard]] explicit operator bool() const
132 {
133 return id != 0;
134 }
135 };
136
138 using BoolProvider = std::function<bool()>;
140 using FloatProvider = std::function<float()>;
142 using Vec2Provider = std::function<glm::vec2()>;
143
145 using BoolCallback = std::function<void( bool value )>;
147 using FloatCallback = std::function<void( float value )>;
149 using Vec2Callback = std::function<void( glm::vec2 value )>;
150
161 ProviderHandle bindBoolAction( std::string name, BoolProvider provider );
162
173 ProviderHandle bindFloatAction( std::string name, FloatProvider provider );
174
186 ProviderHandle bindVec2Action( std::string name, Vec2Provider provider );
187
199 CallbackHandle onBoolAction( const std::string & name, DispatchRule rule, BoolCallback callback );
200
208 CallbackHandle onBoolPressed( const std::string & name, std::function<void()> callback );
209
217 CallbackHandle onBoolReleased( const std::string & name, std::function<void()> callback );
218
232 CallbackHandle onFloatAction( const std::string & name, DispatchRule rule, FloatCallback callback );
233
247 CallbackHandle onVec2Action( const std::string & name, DispatchRule rule, Vec2Callback callback );
248
257
268
276
284
291 [[nodiscard]] bool getBoolAction( const std::string & name ) const;
292
299 [[nodiscard]] float getFloatAction( const std::string & name ) const;
300
307 [[nodiscard]] glm::vec2 getVec2Action( const std::string & name ) const;
308
311 const XrRig& getXrRig() const;
313
314 private:
324
334
344
349 {
351 uint64_t providerId = 0;
352 bool current = false;
353 bool previous = false;
354 std::vector<BoolCallbackEntry> callbacks;
355 };
356
361 {
363 uint64_t providerId = 0;
364 glm::vec2 current{ 0.0f };
365 glm::vec2 previous{ 0.0f };
366 std::vector<Vec2CallbackEntry> callbacks;
367 };
368
373 {
375 uint64_t providerId = 0;
376 float current = 0.0f;
377 float previous = 0.0f;
378 std::vector<FloatCallbackEntry> callbacks;
379 };
380
385 [[nodiscard]] CallbackHandle makeHandle();
386
387 std::unordered_map<std::string, BoolAction> boolActions_;
388 std::unordered_map<std::string, FloatAction> floatActions_;
389 std::unordered_map<std::string, Vec2Action> vec2Actions_;
390 uint64_t nextCallbackId_ = 1;
391 uint64_t nextProviderId_ = 1;
393
396 };
397
408 {
409 public:
411 InputBindingScope() = default;
412
417 explicit InputBindingScope( InputManager & inputManager );
418
421
424
430
437
440
450 void add( InputManager & inputManager, InputManager::ProviderHandle handle );
451
461 void add( InputManager & inputManager, InputManager::CallbackHandle handle );
462
468 void reset();
469
470 private:
475 void bindTo( InputManager & inputManager );
476
478 std::vector<InputManager::ProviderHandle> providerHandles_;
479 std::vector<InputManager::CallbackHandle> callbackHandles_;
480 };
481} // namespace Input
InputManager * inputManager_
Manager that owns the tracked handles, or nullptr when unbound.
InputBindingScope()=default
Creates an unbound scope.
std::vector< InputManager::CallbackHandle > callbackHandles_
Callback handles removed by reset().
InputBindingScope & operator=(const InputBindingScope &)=delete
Input binding scopes own registrations and cannot be copied.
~InputBindingScope()
Removes tracked registrations.
void bindTo(InputManager &inputManager)
Binds this scope to a manager, resetting existing registrations when the manager changes.
InputBindingScope(InputManager &inputManager)
Creates a scope bound to an input manager.
void add(InputManager &inputManager, InputManager::CallbackHandle handle)
Tracks a callback registration for automatic removal.
InputBindingScope(const InputBindingScope &)=delete
Input binding scopes own registrations and cannot be copied.
std::vector< InputManager::ProviderHandle > providerHandles_
Provider handles removed by reset().
InputBindingScope(InputBindingScope &&other) noexcept
Moves ownership of tracked registrations from another scope.
void add(InputManager &inputManager, InputManager::ProviderHandle handle)
Tracks a provider registration for automatic removal.
InputBindingScope & operator=(InputBindingScope &&other) noexcept
Removes current registrations, then moves registrations from another scope.
void reset()
Removes all tracked registrations and unbinds the scope.
Action-based input dispatcher for polled input values and callbacks.
bool getBoolAction(const std::string &name) const
Returns the latest sampled bool value for a named action.
uint64_t nextProviderId_
Next non-zero provider id assigned by a bind function.
CallbackHandle onBoolAction(const std::string &name, DispatchRule rule, BoolCallback callback)
Registers a callback for a named bool action.
std::function< bool()> BoolProvider
Provider function for a bool action.
std::function< void(float value)> FloatCallback
Callback function for a float action. Receives the current sampled value.
void removeProvider(ProviderHandle handle)
Removes a provider registered with one of the bind-action functions.
const XrInputHandler & getXrInputHandler() const
ProviderHandle bindBoolAction(std::string name, BoolProvider provider)
Binds a provider for a named bool action.
ProviderHandle bindVec2Action(std::string name, Vec2Provider provider)
Binds a provider for a named vec2 action.
uint64_t nextCallbackId_
Next non-zero callback id assigned by makeHandle().
CallbackHandle onVec2Action(const std::string &name, DispatchRule rule, Vec2Callback callback)
Registers a callback for a named vec2 action.
CallbackHandle onFloatAction(const std::string &name, DispatchRule rule, FloatCallback callback)
Registers a callback for a named float action.
const XrRig & getXrRig() const
ActionValueType
Runtime value type for an action provider.
@ Float
Floating-point action value.
@ Vec2
2D vector action value.
CallbackHandle makeHandle()
Allocates the next callback handle id.
XrInputHandler xrInputHandler_
~InputManager()=default
float getFloatAction(const std::string &name) const
Returns the latest sampled float value for a named action.
std::unordered_map< std::string, Vec2Action > vec2Actions_
Vec2 actions keyed by action name.
std::unordered_map< std::string, BoolAction > boolActions_
Bool actions keyed by action name.
bool callbacksDispatched_
Guards against dispatching callbacks twice for one sampled frame.
std::unordered_map< std::string, FloatAction > floatActions_
Float actions keyed by action name.
CallbackHandle onBoolPressed(const std::string &name, std::function< void()> callback)
Registers a callback for a bool action press transition.
void setupDefaultBindings()
Sets up the default controller mappings.
glm::vec2 getVec2Action(const std::string &name) const
Returns the latest sampled vec2 value for a named action.
std::function< void(bool value)> BoolCallback
Callback function for a bool action. Receives the current sampled value.
CallbackHandle onBoolReleased(const std::string &name, std::function< void()> callback)
Registers a callback for a bool action release transition.
std::function< float()> FloatProvider
Provider function for a float action.
void beginFrame()
Samples every bound provider and advances action state for a new frame.
void removeCallback(CallbackHandle handle)
Removes a callback registered with one of the on-action functions.
XrInputHandler & getXrInputHandler()
ProviderHandle bindFloatAction(std::string name, FloatProvider provider)
Binds a provider for a named float action.
DispatchRule
Rule that controls when a registered callback runs during InputManager::dispatchCallbacks().
@ Changed
Dispatch when the sampled value differs from the previous frame.
@ EveryFrame
Dispatch once for every sampled frame.
@ Pressed
Dispatch when a bool action changes from false to true.
@ Released
Dispatch when a bool action changes from true to false.
std::function< void(glm::vec2 value)> Vec2Callback
Callback function for a vec2 action. Receives the current sampled value.
std::function< glm::vec2()> Vec2Provider
Provider function for a vec2 action.
void dispatchCallbacks()
Invokes callbacks that match their dispatch rule for the current frame.
Creates OpenXR controller actions from the profile registry and samples the input state.
Resolves raw XR play-space poses into world-space tracked hand poses.
Definition XrRig.h:44
Built-in action names published by the engine XR input setup.
constexpr auto XrRightAButton
Right controller A button state.
constexpr auto XrRightBButton
Right controller B button state.
constexpr auto XrRightThumbstick
Right controller thumbstick vector.
constexpr auto XrLeftGrip
Left controller grip axis, in the range supplied by XR runtime.
constexpr auto XrRightGrip
Right controller grip axis, in the range supplied by XR runtime.
constexpr auto XrLeftXButton
Left controller X button state.
constexpr auto XrLeftYButton
Left controller Y button state.
constexpr auto XrLeftThumbstick
Left controller thumbstick vector.
constexpr auto XrRightTrigger
Right controller trigger axis, in the range supplied by XR runtime.
constexpr auto XrLeftTrigger
Left controller trigger axis, in the range supplied by XR runtime.
constexpr auto XrMenu
Left controller menu button state (universal across most interaction profiles).
Runtime state for one bool action name.
BoolProvider provider
Function sampled by beginFrame().
uint64_t providerId
Active provider handle id, or zero when unbound.
bool previous
Value from the previous beginFrame() call.
bool current
Latest sampled value.
std::vector< BoolCallbackEntry > callbacks
Callback registrations for this action name.
Stored callback registration for a bool action.
BoolCallback callback
Function invoked when the rule matches.
DispatchRule rule
Dispatch condition for this callback.
CallbackHandle handle
Handle returned to the caller for removal.
Opaque handle for a registered input callback.
Runtime state for one float action name.
std::vector< FloatCallbackEntry > callbacks
Callback registrations for this action name.
float previous
Value from the previous beginFrame() call.
uint64_t providerId
Active provider handle id, or zero when unbound.
float current
Latest sampled value.
FloatProvider provider
Function sampled by beginFrame().
Stored callback registration for a float action.
CallbackHandle handle
Handle returned to the caller for removal.
DispatchRule rule
Dispatch condition for this callback.
FloatCallback callback
Function invoked when the rule matches.
Opaque handle for a registered input provider.
ActionValueType type
Value type of the registered provider.
std::string name
Action name passed to the bind function.
Runtime state for one vec2 action name.
std::vector< Vec2CallbackEntry > callbacks
Callback registrations for this action name.
Vec2Provider provider
Function sampled by beginFrame().
glm::vec2 previous
Value from the previous beginFrame() call.
glm::vec2 current
Latest sampled value.
uint64_t providerId
Active provider handle id, or zero when unbound.
Stored callback registration for a vec2 action.
Vec2Callback callback
Function invoked when the rule matches.
CallbackHandle handle
Handle returned to the caller for removal.
DispatchRule rule
Dispatch condition for this callback.