Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
InputManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <glm/glm.hpp>
4
5#include <cstdint>
6#include <functional>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11namespace Input
12{
13 namespace Actions
14 {
15 inline constexpr auto XrLeftTrigger = "XR.LeftTrigger";
16 inline constexpr auto XrRightTrigger = "XR.RightTrigger";
17 inline constexpr auto XrLeftGrip = "XR.LeftGrip";
18 inline constexpr auto XrRightGrip = "XR.RightGrip";
19 inline constexpr auto XrLeftThumbstick = "XR.LeftThumbstick";
20 inline constexpr auto XrRightThumbstick = "XR.RightThumbstick";
21 } // namespace Actions
22
27 {
28 public:
36
41 {
45 uint64_t id = 0;
46
47 [[nodiscard]] explicit operator bool() const
48 {
49 return id != 0;
50 }
51 };
52
53 using BoolProvider = std::function<bool()>;
54 using FloatProvider = std::function<float()>;
55 using Vec2Provider = std::function<glm::vec2()>;
56
57 using BoolCallback = std::function<void( bool value )>;
58 using FloatCallback = std::function<void( float value )>;
59 using Vec2Callback = std::function<void( glm::vec2 value )>;
60
61 void bindBoolAction( std::string name, BoolProvider provider );
62 void bindFloatAction( std::string name, FloatProvider provider );
63 void bindVec2Action( std::string name, Vec2Provider provider );
64
65 CallbackHandle onBoolAction( const std::string & name, DispatchRule rule, BoolCallback callback );
66 CallbackHandle onBoolPressed( const std::string & name, std::function<void()> callback );
67 CallbackHandle onBoolReleased( const std::string & name, std::function<void()> callback );
68 CallbackHandle onFloatAction( const std::string & name, DispatchRule rule, FloatCallback callback );
69 CallbackHandle onVec2Action( const std::string & name, DispatchRule rule, Vec2Callback callback );
70
72
73 void beginFrame();
75
76 [[nodiscard]] bool getBoolAction( const std::string & name ) const;
77 [[nodiscard]] float getFloatAction( const std::string & name ) const;
78 [[nodiscard]] glm::vec2 getVec2Action( const std::string & name ) const;
79
80 private:
87
94
101
103 {
105 bool current = false;
106 bool previous = false;
107 std::vector<BoolCallbackEntry> callbacks;
108 };
109
111 {
113 glm::vec2 current{ 0.0f };
114 glm::vec2 previous{ 0.0f };
115 std::vector<Vec2CallbackEntry> callbacks;
116 };
117
119 {
121 float current = 0.0f;
122 float previous = 0.0f;
123 std::vector<FloatCallbackEntry> callbacks;
124 };
125
126 [[nodiscard]] CallbackHandle makeHandle();
127
128 std::unordered_map<std::string, BoolAction> boolActions_;
129 std::unordered_map<std::string, FloatAction> floatActions_;
130 std::unordered_map<std::string, Vec2Action> vec2Actions_;
131 uint64_t nextCallbackId_ = 1;
133 };
134} // namespace Input
Action-based input dispatcher for polling providers and invoking callbacks.
bool getBoolAction(const std::string &name) const
CallbackHandle onBoolAction(const std::string &name, DispatchRule rule, BoolCallback callback)
std::function< bool()> BoolProvider
std::function< void(float value)> FloatCallback
CallbackHandle onVec2Action(const std::string &name, DispatchRule rule, Vec2Callback callback)
void bindBoolAction(std::string name, BoolProvider provider)
CallbackHandle onFloatAction(const std::string &name, DispatchRule rule, FloatCallback callback)
CallbackHandle makeHandle()
float getFloatAction(const std::string &name) const
std::unordered_map< std::string, Vec2Action > vec2Actions_
std::unordered_map< std::string, BoolAction > boolActions_
std::unordered_map< std::string, FloatAction > floatActions_
CallbackHandle onBoolPressed(const std::string &name, std::function< void()> callback)
void bindFloatAction(std::string name, FloatProvider provider)
glm::vec2 getVec2Action(const std::string &name) const
void bindVec2Action(std::string name, Vec2Provider provider)
std::function< void(bool value)> BoolCallback
CallbackHandle onBoolReleased(const std::string &name, std::function< void()> callback)
std::function< float()> FloatProvider
void removeCallback(CallbackHandle handle)
std::function< void(glm::vec2 value)> Vec2Callback
std::function< glm::vec2()> Vec2Provider
constexpr auto XrRightThumbstick
constexpr auto XrLeftGrip
constexpr auto XrRightGrip
constexpr auto XrLeftThumbstick
constexpr auto XrRightTrigger
constexpr auto XrLeftTrigger
std::vector< BoolCallbackEntry > callbacks
Opaque handle for a registered input callback.
std::vector< FloatCallbackEntry > callbacks
std::vector< Vec2CallbackEntry > callbacks