Nebula
Loading...
Searching...
No Matches
keyboardbase.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "input/inputhandler.h"
13#include "input/key.h"
14#include "input/modifier.h"
15
16//------------------------------------------------------------------------------
17namespace Base
18{
20{
22public:
26 virtual ~KeyboardBase();
27
29 virtual void BeginCapture();
31 virtual void EndCapture();
32
34 void SetKeyDown(Input::Key::Code keyCode);
36 void SetKeyUp(Input::Key::Code keyCode);
37
39 bool KeyPressed(Input::Key::Code keyCode) const;
41 bool KeyDown(Input::Key::Code keyCode) const;
43 bool KeyUp(Input::Key::Code keyCode) const;
45 const Util::String& GetCharInput() const;
49 bool ModifierDown(Input::Modifier::Code mods) const;
50
51protected:
53 virtual void OnAttach();
55 virtual void OnBeginFrame();
57 virtual bool OnEvent(const Input::InputEvent& inputEvent);
59 virtual void OnObtainCapture();
61 virtual void OnReleaseCapture();
63 virtual void OnReset();
64
65
66private:
68 {
69 public:
71 KeyState() : pressed(false), down(false), up(false) {};
72
73 bool pressed; // currently pressed
74 bool down; // was down this frame
75 bool up; // was up this frame
76 };
80};
81
82//------------------------------------------------------------------------------
85inline void
87{
88 KeyState& keyState = this->keyStates[keyCode];
89 if (!keyState.pressed)
90 {
91 keyState.down = true;
92 keyState.pressed = true;
93 }
94}
95
96//------------------------------------------------------------------------------
99inline void
101{
102 this->keyStates[keyCode].up = true;
103}
104
105//------------------------------------------------------------------------------
108inline bool
110{
111 return this->keyStates[keyCode].pressed;
112}
113
114//------------------------------------------------------------------------------
117inline bool
119{
120 return this->keyStates[keyCode].down;
121}
122
123//------------------------------------------------------------------------------
126inline bool
128{
129 return this->keyStates[keyCode].up;
130}
131
132//------------------------------------------------------------------------------
135inline const Util::String&
137{
138 return this->charInput;
139}
140
141//------------------------------------------------------------------------------
149{
151 for (IndexT i = 0; i < Input::Modifier::NumKeys; i++)
152 {
154 if (this->keyStates[key].pressed)
155 mods |= Input::Modifier::FromKey(key);
156 }
157 return mods;
158}
159
160//------------------------------------------------------------------------------
163inline bool
168
169} // namespace Input
170//------------------------------------------------------------------------------
171
Definition keyboardbase.h:68
bool pressed
Definition keyboardbase.h:73
KeyState()
constructor
Definition keyboardbase.h:71
bool down
Definition keyboardbase.h:74
bool up
Definition keyboardbase.h:75
void SetKeyDown(Input::Key::Code keyCode)
sets a key to be down
Definition keyboardbase.h:86
virtual void OnAttach()
called when the handler is attached to the input server
Definition keyboardbase.cc:58
virtual void OnReset()
reset the input handler
Definition keyboardbase.cc:189
virtual void OnObtainCapture()
called when input handler obtains capture
Definition keyboardbase.cc:161
__DeclareClass(KeyboardBase)
Util::FixedArray< KeyState > nextKeyStates
Definition keyboardbase.h:78
bool KeyUp(Input::Key::Code keyCode) const
return true if key was released at least once in current frame
Definition keyboardbase.h:127
virtual void EndCapture()
end input capturing to this event handler
Definition keyboardbase.cc:48
bool KeyDown(Input::Key::Code keyCode) const
return true if key was pushed down at least once in current frame
Definition keyboardbase.h:118
virtual void BeginCapture()
capture input to this event handler
Definition keyboardbase.cc:38
void SetKeyUp(Input::Key::Code keyCode)
sets a key to be up
Definition keyboardbase.h:100
KeyboardBase()
constructor
Definition keyboardbase.cc:19
Util::FixedArray< KeyState > keyStates
Definition keyboardbase.h:77
Util::String charInput
Definition keyboardbase.h:79
const Util::String & GetCharInput() const
get character input in current frame
Definition keyboardbase.h:136
bool KeyPressed(Input::Key::Code keyCode) const
return true if a key is currently pressed
Definition keyboardbase.h:109
virtual void OnReleaseCapture()
called when input handler looses capture
Definition keyboardbase.cc:175
Input::Modifier::Code GetMods() const
the modifiers held right now
Definition keyboardbase.h:148
bool ModifierDown(Input::Modifier::Code mods) const
return true when the held state satisfies the modifiers
Definition keyboardbase.h:164
virtual bool OnEvent(const Input::InputEvent &inputEvent)
called when an input event should be processed
Definition keyboardbase.cc:104
virtual void OnBeginFrame()
called on InputServer::BeginFrame()
Definition keyboardbase.cc:71
The input events of the Input subsystems.
Definition inputevent.h:25
Input handlers receive and process input events.
Definition inputhandler.h:29
Code
key codes
Definition key.h:25
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
Nebula's universal string class.
Definition string.h:50
Definition gamecontentserverbase.cc:10
constexpr Key::Code Keys[]
Definition modifier.h:43
uint32_t Code
Definition modifier.h:21
constexpr IndexT NumKeys
Definition modifier.h:50
Code FromKey(Key::Code key)
The bit a key code has, or NoModifier when the key is not a modifier key.
Definition modifier.h:57
constexpr Code NoModifier
Definition modifier.h:23
bool Matches(Code required, Code held)
Return true when the held bits satisfy the requirement.
Definition modifier.h:109
int IndexT
Definition types.h:49