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
11
namespace
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
26
class
InputManager
27
{
28
public
:
29
enum class
DispatchRule
30
{
31
EveryFrame
,
32
Changed
,
33
Pressed
,
34
Released
,
35
};
36
40
struct
CallbackHandle
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
71
void
removeCallback
(
CallbackHandle
handle );
72
73
void
beginFrame
();
74
void
dispatchCallbacks
();
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
:
81
struct
BoolCallbackEntry
82
{
83
CallbackHandle
handle
;
84
DispatchRule
rule
=
DispatchRule::EveryFrame
;
85
BoolCallback
callback
;
86
};
87
88
struct
Vec2CallbackEntry
89
{
90
CallbackHandle
handle
;
91
DispatchRule
rule
=
DispatchRule::EveryFrame
;
92
Vec2Callback
callback
;
93
};
94
95
struct
FloatCallbackEntry
96
{
97
CallbackHandle
handle
;
98
DispatchRule
rule
=
DispatchRule::EveryFrame
;
99
FloatCallback
callback
;
100
};
101
102
struct
BoolAction
103
{
104
BoolProvider
provider
;
105
bool
current
=
false
;
106
bool
previous
=
false
;
107
std::vector<BoolCallbackEntry>
callbacks
;
108
};
109
110
struct
Vec2Action
111
{
112
Vec2Provider
provider
;
113
glm::vec2
current
{ 0.0f };
114
glm::vec2
previous
{ 0.0f };
115
std::vector<Vec2CallbackEntry>
callbacks
;
116
};
117
118
struct
FloatAction
119
{
120
FloatProvider
provider
;
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;
132
bool
callbacksDispatched_
=
true
;
133
};
134
}
// namespace Input
Input::InputManager
Action-based input dispatcher for polling providers and invoking callbacks.
Definition
InputManager.h:27
Input::InputManager::getBoolAction
bool getBoolAction(const std::string &name) const
Input::InputManager::onBoolAction
CallbackHandle onBoolAction(const std::string &name, DispatchRule rule, BoolCallback callback)
Input::InputManager::BoolProvider
std::function< bool()> BoolProvider
Definition
InputManager.h:53
Input::InputManager::FloatCallback
std::function< void(float value)> FloatCallback
Definition
InputManager.h:58
Input::InputManager::nextCallbackId_
uint64_t nextCallbackId_
Definition
InputManager.h:131
Input::InputManager::onVec2Action
CallbackHandle onVec2Action(const std::string &name, DispatchRule rule, Vec2Callback callback)
Input::InputManager::bindBoolAction
void bindBoolAction(std::string name, BoolProvider provider)
Input::InputManager::onFloatAction
CallbackHandle onFloatAction(const std::string &name, DispatchRule rule, FloatCallback callback)
Input::InputManager::makeHandle
CallbackHandle makeHandle()
Input::InputManager::getFloatAction
float getFloatAction(const std::string &name) const
Input::InputManager::vec2Actions_
std::unordered_map< std::string, Vec2Action > vec2Actions_
Definition
InputManager.h:130
Input::InputManager::boolActions_
std::unordered_map< std::string, BoolAction > boolActions_
Definition
InputManager.h:128
Input::InputManager::callbacksDispatched_
bool callbacksDispatched_
Definition
InputManager.h:132
Input::InputManager::floatActions_
std::unordered_map< std::string, FloatAction > floatActions_
Definition
InputManager.h:129
Input::InputManager::onBoolPressed
CallbackHandle onBoolPressed(const std::string &name, std::function< void()> callback)
Input::InputManager::bindFloatAction
void bindFloatAction(std::string name, FloatProvider provider)
Input::InputManager::getVec2Action
glm::vec2 getVec2Action(const std::string &name) const
Input::InputManager::bindVec2Action
void bindVec2Action(std::string name, Vec2Provider provider)
Input::InputManager::BoolCallback
std::function< void(bool value)> BoolCallback
Definition
InputManager.h:57
Input::InputManager::onBoolReleased
CallbackHandle onBoolReleased(const std::string &name, std::function< void()> callback)
Input::InputManager::FloatProvider
std::function< float()> FloatProvider
Definition
InputManager.h:54
Input::InputManager::beginFrame
void beginFrame()
Input::InputManager::removeCallback
void removeCallback(CallbackHandle handle)
Input::InputManager::DispatchRule
DispatchRule
Definition
InputManager.h:30
Input::InputManager::DispatchRule::Changed
@ Changed
Definition
InputManager.h:32
Input::InputManager::DispatchRule::EveryFrame
@ EveryFrame
Definition
InputManager.h:31
Input::InputManager::DispatchRule::Pressed
@ Pressed
Definition
InputManager.h:33
Input::InputManager::DispatchRule::Released
@ Released
Definition
InputManager.h:34
Input::InputManager::Vec2Callback
std::function< void(glm::vec2 value)> Vec2Callback
Definition
InputManager.h:59
Input::InputManager::Vec2Provider
std::function< glm::vec2()> Vec2Provider
Definition
InputManager.h:55
Input::InputManager::dispatchCallbacks
void dispatchCallbacks()
Input::Actions
Definition
InputManager.h:14
Input::Actions::XrRightThumbstick
constexpr auto XrRightThumbstick
Definition
InputManager.h:20
Input::Actions::XrLeftGrip
constexpr auto XrLeftGrip
Definition
InputManager.h:17
Input::Actions::XrRightGrip
constexpr auto XrRightGrip
Definition
InputManager.h:18
Input::Actions::XrLeftThumbstick
constexpr auto XrLeftThumbstick
Definition
InputManager.h:19
Input::Actions::XrRightTrigger
constexpr auto XrRightTrigger
Definition
InputManager.h:16
Input::Actions::XrLeftTrigger
constexpr auto XrLeftTrigger
Definition
InputManager.h:15
Input
Definition
VrGliderComponent.h:15
Input::InputManager::BoolAction
Definition
InputManager.h:103
Input::InputManager::BoolAction::provider
BoolProvider provider
Definition
InputManager.h:104
Input::InputManager::BoolAction::previous
bool previous
Definition
InputManager.h:106
Input::InputManager::BoolAction::current
bool current
Definition
InputManager.h:105
Input::InputManager::BoolAction::callbacks
std::vector< BoolCallbackEntry > callbacks
Definition
InputManager.h:107
Input::InputManager::BoolCallbackEntry
Definition
InputManager.h:82
Input::InputManager::BoolCallbackEntry::callback
BoolCallback callback
Definition
InputManager.h:85
Input::InputManager::BoolCallbackEntry::rule
DispatchRule rule
Definition
InputManager.h:84
Input::InputManager::BoolCallbackEntry::handle
CallbackHandle handle
Definition
InputManager.h:83
Input::InputManager::CallbackHandle
Opaque handle for a registered input callback.
Definition
InputManager.h:41
Input::InputManager::FloatAction
Definition
InputManager.h:119
Input::InputManager::FloatAction::callbacks
std::vector< FloatCallbackEntry > callbacks
Definition
InputManager.h:123
Input::InputManager::FloatAction::previous
float previous
Definition
InputManager.h:122
Input::InputManager::FloatAction::current
float current
Definition
InputManager.h:121
Input::InputManager::FloatAction::provider
FloatProvider provider
Definition
InputManager.h:120
Input::InputManager::FloatCallbackEntry
Definition
InputManager.h:96
Input::InputManager::FloatCallbackEntry::handle
CallbackHandle handle
Definition
InputManager.h:97
Input::InputManager::FloatCallbackEntry::rule
DispatchRule rule
Definition
InputManager.h:98
Input::InputManager::FloatCallbackEntry::callback
FloatCallback callback
Definition
InputManager.h:99
Input::InputManager::Vec2Action
Definition
InputManager.h:111
Input::InputManager::Vec2Action::callbacks
std::vector< Vec2CallbackEntry > callbacks
Definition
InputManager.h:115
Input::InputManager::Vec2Action::provider
Vec2Provider provider
Definition
InputManager.h:112
Input::InputManager::Vec2Action::previous
glm::vec2 previous
Definition
InputManager.h:114
Input::InputManager::Vec2Action::current
glm::vec2 current
Definition
InputManager.h:113
Input::InputManager::Vec2CallbackEntry
Definition
InputManager.h:89
Input::InputManager::Vec2CallbackEntry::callback
Vec2Callback callback
Definition
InputManager.h:92
Input::InputManager::Vec2CallbackEntry::handle
CallbackHandle handle
Definition
InputManager.h:90
Input::InputManager::Vec2CallbackEntry::rule
DispatchRule rule
Definition
InputManager.h:91
Engine
include
Engine
Input
InputManager.h
Generated by
1.14.0