KeyboardCallbackBehavior.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2015, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_BLOCKS_KEYBOARDCALLBACKBEHAVIOR_H
17 #define SURGSIM_BLOCKS_KEYBOARDCALLBACKBEHAVIOR_H
18 
19 #include <functional>
20 
23 
24 namespace SurgSim
25 {
26 namespace Input
27 {
28 class InputComponent;
29 }
30 
31 namespace Blocks
32 {
33 SURGSIM_STATIC_REGISTRATION(KeyboardCallbackBehavior);
34 
37 {
38 public:
39  typedef std::function<void()> CallbackType;
40 
43  explicit KeyboardCallbackBehavior(const std::string& name);
44 
46 
49  void setInputComponent(std::shared_ptr<Framework::Component> inputComponent);
50 
53  std::shared_ptr<Input::InputComponent> getInputComponent() const;
54 
57  void registerKey(int key);
58 
62  void registerCallback(CallbackType func);
63 
64  void update(double dt) override;
65 
66 protected:
69  int getKey() const;
70 
71  bool doInitialize() override;
72 
73  bool doWakeUp() override;
74 
75 private:
78 
81 
83  CallbackType m_callback;
84 
86  std::shared_ptr<Input::InputComponent> m_inputComponent;
87 };
88 
89 }; // namespace Blocks
90 }; // namespace SurgSim
91 
92 #endif //SURGSIM_BLOCKS_KEYBOARDCALLBACKBEHAVIOR_H
Definition: CompoundShapeToGraphics.cpp:29
int m_actionKey
The registered key, when pressed, the registered callback will be called.
Definition: KeyboardCallbackBehavior.h:80
CallbackType m_callback
Callback function.
Definition: KeyboardCallbackBehavior.h:83
#define SURGSIM_CLASSNAME(ClassName)
Declare the class name of a class with the appropriate function header, do not use quotes...
Definition: Macros.h:21
std::shared_ptr< Input::InputComponent > m_inputComponent
Input component from which pressed key comes.
Definition: KeyboardCallbackBehavior.h:86
Behaviors perform actions.
Definition: Behavior.h:40
This behavior will call the registered callback function when the registered key is pressed...
Definition: KeyboardCallbackBehavior.h:36
std::function< void()> CallbackType
Definition: KeyboardCallbackBehavior.h:39
bool m_keyPressedLastUpdate
Record if any key is pressed in last update() call.
Definition: KeyboardCallbackBehavior.h:77