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 const char* XrLeftTrigger = "XR.LeftTrigger";
16 inline constexpr const char* XrRightTrigger = "XR.RightTrigger";
17 inline constexpr const char* XrLeftGrip = "XR.LeftGrip";
18 inline constexpr const char* XrRightGrip = "XR.RightGrip";
19 inline constexpr const char* XrLeftThumbstick = "XR.LeftThumbstick";
20 inline constexpr const char* XrRightThumbstick = "XR.RightThumbstick";
21 } // namespace Actions
22
24 {
25 public:
33
35 {
36 uint64_t id = 0;
37
38 [[nodiscard]] explicit operator bool() const
39 {
40 return id != 0;
41 }
42 };
43
44 using BoolProvider = std::function<bool()>;
45 using FloatProvider = std::function<float()>;
46 using Vec2Provider = std::function<glm::vec2()>;
47
48 using BoolCallback = std::function<void( bool value )>;
49 using FloatCallback = std::function<void( float value )>;
50 using Vec2Callback = std::function<void( glm::vec2 value )>;
51
52 void bindBoolAction( std::string name, BoolProvider provider );
53 void bindFloatAction( std::string name, FloatProvider provider );
54 void bindVec2Action( std::string name, Vec2Provider provider );
55
56 CallbackHandle onBoolAction( const std::string & name, DispatchRule rule, BoolCallback callback );
57 CallbackHandle onBoolPressed( const std::string & name, std::function<void()> callback );
58 CallbackHandle onBoolReleased( const std::string & name, std::function<void()> callback );
59 CallbackHandle onFloatAction( const std::string & name, DispatchRule rule, FloatCallback callback );
60 CallbackHandle onVec2Action( const std::string & name, DispatchRule rule, Vec2Callback callback );
61
63
64 void beginFrame();
66
67 [[nodiscard]] bool getBoolAction( const std::string & name ) const;
68 [[nodiscard]] float getFloatAction( const std::string & name ) const;
69 [[nodiscard]] glm::vec2 getVec2Action( const std::string & name ) const;
70
71 private:
78
85
92
94 {
96 bool current = false;
97 bool previous = false;
98 std::vector<BoolCallbackEntry> callbacks;
99 };
100
102 {
104 glm::vec2 current{ 0.0f };
105 glm::vec2 previous{ 0.0f };
106 std::vector<Vec2CallbackEntry> callbacks;
107 };
108
110 {
112 float current = 0.0f;
113 float previous = 0.0f;
114 std::vector<FloatCallbackEntry> callbacks;
115 };
116
117 [[nodiscard]] CallbackHandle makeHandle();
118
119 std::unordered_map<std::string, BoolAction> boolActions_;
120 std::unordered_map<std::string, FloatAction> floatActions_;
121 std::unordered_map<std::string, Vec2Action> vec2Actions_;
122 uint64_t nextCallbackId_ = 1;
124 };
125} // namespace Input
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 const char * XrRightTrigger
constexpr const char * XrLeftTrigger
constexpr const char * XrLeftThumbstick
constexpr const char * XrRightThumbstick
constexpr const char * XrLeftGrip
constexpr const char * XrRightGrip
std::vector< BoolCallbackEntry > callbacks
std::vector< FloatCallbackEntry > callbacks
std::vector< Vec2CallbackEntry > callbacks