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 
84 
86  std::shared_ptr<Input::InputComponent> m_inputComponent;
87 };
88 
89 }; // namespace Blocks
90 }; // namespace SurgSim
91 
92 #endif //SURGSIM_BLOCKS_KEYBOARDCALLBACKBEHAVIOR_H
SurgSim::Blocks::KeyboardCallbackBehavior::KeyboardCallbackBehavior
KeyboardCallbackBehavior(const std::string &name)
Constructor.
Definition: KeyboardCallbackBehavior.cpp:30
SurgSim::Blocks::KeyboardCallbackBehavior::m_inputComponent
std::shared_ptr< Input::InputComponent > m_inputComponent
Input component from which pressed key comes.
Definition: KeyboardCallbackBehavior.h:86
SurgSim::Blocks::KeyboardCallbackBehavior::registerKey
void registerKey(int key)
Register a key, so that when such key is pressed, this behavior will call the callback.
Definition: KeyboardCallbackBehavior.cpp:51
SurgSim::Blocks::KeyboardCallbackBehavior::registerCallback
void registerCallback(CallbackType func)
Register a callback function.
Definition: KeyboardCallbackBehavior.cpp:95
SurgSim::Blocks::KeyboardCallbackBehavior::update
void update(double dt) override
Update the behavior.
Definition: KeyboardCallbackBehavior.cpp:56
SurgSim::Blocks::KeyboardCallbackBehavior::getInputComponent
std::shared_ptr< Input::InputComponent > getInputComponent() const
Get the input component of this behavior, from which the pressed key comes.
Definition: KeyboardCallbackBehavior.cpp:46
SurgSim::Framework::Behavior
Behaviors perform actions.
Definition: Behavior.h:40
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Blocks::KeyboardCallbackBehavior::m_actionKey
int m_actionKey
The registered key, when pressed, the registered callback will be called.
Definition: KeyboardCallbackBehavior.h:80
SurgSim::Blocks::KeyboardCallbackBehavior::doInitialize
bool doInitialize() override
Interface to be implemented by derived classes.
Definition: KeyboardCallbackBehavior.cpp:73
SurgSim::Blocks::KeyboardCallbackBehavior::CallbackType
std::function< void()> CallbackType
Definition: KeyboardCallbackBehavior.h:39
SurgSim::Blocks::KeyboardCallbackBehavior::m_callback
CallbackType m_callback
Callback function.
Definition: KeyboardCallbackBehavior.h:83
SurgSim::Blocks::KeyboardCallbackBehavior::m_keyPressedLastUpdate
bool m_keyPressedLastUpdate
Record if any key is pressed in last update() call.
Definition: KeyboardCallbackBehavior.h:77
KeyCode.h
SurgSim::Blocks::KeyboardCallbackBehavior
This behavior will call the registered callback function when the registered key is pressed.
Definition: KeyboardCallbackBehavior.h:36
SurgSim::Blocks::KeyboardCallbackBehavior::doWakeUp
bool doWakeUp() override
Interface to be implemented by derived classes.
Definition: KeyboardCallbackBehavior.cpp:78
SurgSim::Blocks::KeyboardCallbackBehavior::SURGSIM_CLASSNAME
SURGSIM_CLASSNAME(SurgSim::Blocks::KeyboardCallbackBehavior)
SurgSim::Blocks::KeyboardCallbackBehavior::getKey
int getKey() const
Definition: KeyboardCallbackBehavior.cpp:90
Behavior.h
SurgSim::Blocks::KeyboardCallbackBehavior::setInputComponent
void setInputComponent(std::shared_ptr< Framework::Component > inputComponent)
Set the input component from which pressed key comes.
Definition: KeyboardCallbackBehavior.cpp:40