KeyBehavior.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, 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_KEYBEHAVIOR_H
17 #define SURGSIM_BLOCKS_KEYBEHAVIOR_H
18 
19 #include <memory>
20 #include <string>
21 
23 
24 namespace SurgSim
25 {
26 namespace Input
27 {
28 class InputComponent;
29 }
30 
31 namespace Blocks
32 {
33 
37 {
38 public:
40  explicit KeyBehavior(const std::string& name);
41 
43  ~KeyBehavior();
44 
45  void update(double dt) override;
46  bool doInitialize() override;
47  bool doWakeUp() override;
48 
51  void setInputComponent(std::shared_ptr<Framework::Component> inputComponent);
52 
55  std::shared_ptr<Input::InputComponent> getInputComponent() const;
56 
61  static bool registerKey(int keycode, const std::string& description);
62 
66  static bool unregisterKey(int keycode);
67 
69  static void logMap();
70 
71 protected:
74  virtual void onKeyDown(int key) = 0;
75 
78  virtual void onKeyUp(int key) = 0;
79 
81  std::shared_ptr<Input::InputComponent> m_inputComponent;
82 
84  int m_lastKey;
85 
88  static boost::mutex m_keyMapMutex;
89  static std::unordered_map<int, std::string> m_keyMap;
91 };
92 
93 }; // namespace Blocks
94 }; // namespace SurgSim
95 
96 #endif
SurgSim::Blocks::KeyBehavior::m_lastKey
int m_lastKey
Keep track if the key was pressed the last time around.
Definition: KeyBehavior.h:84
SurgSim::Blocks::KeyBehavior::setInputComponent
void setInputComponent(std::shared_ptr< Framework::Component > inputComponent)
Set the input component from which pressed key comes.
Definition: KeyBehavior.cpp:43
SurgSim::Blocks::KeyBehavior::unregisterKey
static bool unregisterKey(int keycode)
Remove a key from the registry.
Definition: KeyBehavior.cpp:117
SurgSim::Blocks::KeyBehavior::onKeyDown
virtual void onKeyDown(int key)=0
Implement to execute functionality on key press.
SurgSim::Framework::Behavior
Behaviors perform actions.
Definition: Behavior.h:41
SurgSim::Blocks::KeyBehavior::update
void update(double dt) override
Update the behavior.
Definition: KeyBehavior.cpp:68
SurgSim
Definition: CompoundShapeToGraphics.cpp:30
SurgSim::Blocks::KeyBehavior
Behavior to abstract the functionality of keyboard driven behaviors, can be programmed to react to a ...
Definition: KeyBehavior.h:37
SurgSim::Blocks::KeyBehavior::registerKey
static bool registerKey(int keycode, const std::string &description)
Register the key, this let's the system keep track of keys used in the application.
Definition: KeyBehavior.cpp:95
SurgSim::Blocks::KeyBehavior::doInitialize
bool doInitialize() override
Interface to be implemented by derived classes.
Definition: KeyBehavior.cpp:54
SurgSim::Blocks::KeyBehavior::m_keyMapMutex
static boost::mutex m_keyMapMutex
Definition: KeyBehavior.h:88
SurgSim::Blocks::KeyBehavior::~KeyBehavior
~KeyBehavior()
Destructor.
Definition: KeyBehavior.cpp:39
SurgSim::Blocks::KeyBehavior::m_keyMap
static std::unordered_map< int, std::string > m_keyMap
Definition: KeyBehavior.h:89
SurgSim::Blocks::KeyBehavior::KeyBehavior
KeyBehavior(const std::string &name)
Constructor.
Definition: KeyBehavior.cpp:30
SurgSim::Blocks::KeyBehavior::getInputComponent
std::shared_ptr< Input::InputComponent > getInputComponent() const
Get the input component of this behavior, from which the pressed key comes.
Definition: KeyBehavior.cpp:49
SurgSim::Blocks::KeyBehavior::logMap
static void logMap()
Write the keymap out to the logger.
Definition: KeyBehavior.cpp:129
SurgSim::Blocks::KeyBehavior::doWakeUp
bool doWakeUp() override
Interface to be implemented by derived classes.
Definition: KeyBehavior.cpp:59
SurgSim::Blocks::KeyBehavior::onKeyUp
virtual void onKeyUp(int key)=0
Implement to execute functionality on key release.
SurgSim::Blocks::KeyBehavior::m_inputComponent
std::shared_ptr< Input::InputComponent > m_inputComponent
Input component needs to provide key.
Definition: KeyBehavior.h:81
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
Behavior.h