Simbody  3.8
Visualizer_InputListener.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMBODY_VISUALIZER_INPUT_LISTENER_H_
2 #define SimTK_SIMBODY_VISUALIZER_INPUT_LISTENER_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm) *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2010-12 Stanford University and the Authors. *
13  * Authors: Peter Eastman, Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
33 
34 namespace SimTK {
35 
36 //==============================================================================
37 // INPUT LISTENER
38 //==============================================================================
48 public:
54 enum Modifier {
55  ShiftIsDown = 0x01,
56  ControlIsDown = 0x02,
57  AltIsDown = 0x04,
58  IsSpecialKey = 0xC0
59 };
60 
61 static const unsigned SpecialKeyOffset = 0x100; // Added to each code
62 
65 enum KeyCode {
66  KeyControlC = 3, // some notable ASCII codes
67  KeyBeep = 7,
68  KeyBackspace = 8,
69  KeyTab = 9,
70  KeyLF = 10,
71  KeyReturn = 13,
72  KeyEnter = KeyReturn,
73  KeyEsc = 27,
74  KeyDelete = 127,
75 
76  KeyF1 = SpecialKeyOffset + 1, // function keys
77  KeyF2 = SpecialKeyOffset + 2,
78  KeyF3 = SpecialKeyOffset + 3,
79  KeyF4 = SpecialKeyOffset + 4,
80  KeyF5 = SpecialKeyOffset + 5,
81  KeyF6 = SpecialKeyOffset + 6,
82  KeyF7 = SpecialKeyOffset + 7,
83  KeyF8 = SpecialKeyOffset + 8,
84  KeyF9 = SpecialKeyOffset + 9,
85  KeyF10 = SpecialKeyOffset + 10,
86  KeyF11 = SpecialKeyOffset + 11,
87  KeyF12 = SpecialKeyOffset + 12,
88 
89  KeyLeftArrow = SpecialKeyOffset + 100, // directional keys
90  KeyUpArrow = SpecialKeyOffset + 101,
91  KeyRightArrow = SpecialKeyOffset + 102,
92  KeyDownArrow = SpecialKeyOffset + 103,
93  KeyPageUp = SpecialKeyOffset + 104,
94  KeyPageDown = SpecialKeyOffset + 105,
95  KeyHome = SpecialKeyOffset + 106,
96  KeyEnd = SpecialKeyOffset + 107,
97  KeyInsert = SpecialKeyOffset + 108
98 };
99 
101 virtual ~InputListener() {}
102 
117 virtual bool keyPressed(unsigned key, unsigned modifiers) {return false;}
118 
125 virtual bool menuSelected(int menu, int item) {return false;}
126 
133 virtual bool sliderMoved(int slider, Real value) {return false;}
134 };
135 
136 
137 
138 //==============================================================================
139 // INPUT SILO
140 //==============================================================================
234 : public Visualizer::InputListener {
235 public:
240 
243 bool isAnyUserInput() const;
244 
248 void waitForAnyUserInput() const;
249 
262 bool takeKeyHit(unsigned& key, unsigned& modifiers);
263 
269 void waitForKeyHit(unsigned& key, unsigned& modifiers);
270 
283 bool takeMenuPick(int& menu, int& item);
284 
290 void waitForMenuPick(int& menu, int& item);
291 
305 bool takeSliderMove(int& slider, Real& value);
306 
312 void waitForSliderMove(int& slider, Real& value);
313 
315 void clear();
316 
317 //------------------------------------------------------------------------------
318  private:
319 // Each of these will return true to the Visualizer's listener thread, meaning
320 // that the input will be absorbed and subsequent listeners (if any) will not
321 // be called.
322 virtual bool keyPressed(unsigned key, unsigned modifiers) override;
323 virtual bool menuSelected(int menu, int item) override;
324 virtual bool sliderMoved(int slider, Real value) override;
325 
326 class Impl;
327 const Impl& getImpl() const {assert(m_impl); return *m_impl;}
328 Impl& updImpl() {assert(m_impl); return *m_impl;}
329 
330 Impl* m_impl; // the lone data member in this class
331 };
332 
333 } // namespace SimTK
334 
335 #endif // SimTK_SIMBODY_VISUALIZER_INPUT_LISTENER_H_
Every Simbody header and source file should include this header before any other Simbody header.
#define SimTK_SIMBODY_EXPORT
Definition: Simbody/include/simbody/internal/common.h:68
Declares the Visualizer class used for collecting Simbody simulation results for display and interact...
This abstract class defines methods to be called when the Visualizer reports user activity back to th...
Definition: Visualizer_InputListener.h:47
virtual bool sliderMoved(int slider, Real value)
The user has moved one of the sliders you defined; here is the integer value you specified when you d...
Definition: Visualizer_InputListener.h:133
virtual bool menuSelected(int menu, int item)
The user has clicked one of the menu items you defined; here is the integer value you specified when ...
Definition: Visualizer_InputListener.h:125
virtual ~InputListener()
Destructor is virtual; be sure to override it if you need to clean up.
Definition: Visualizer_InputListener.h:101
Modifier
These represent modifications to the character that is passed into the keyPressed() method,...
Definition: Visualizer_InputListener.h:54
virtual bool keyPressed(unsigned key, unsigned modifiers)
This method is called when a user hits a keyboard key in the Visualizer window, unless that key is be...
Definition: Visualizer_InputListener.h:117
KeyCode
These are the special keys that the Visualizer may report via the keyPressed() method.
Definition: Visualizer_InputListener.h:65
This pre-built InputListener is extremely useful for processing user input that is intended to affect...
Definition: Visualizer_InputListener.h:234
bool isAnyUserInput() const
This is a very fast test that does not require locking; you don't have to use this but it is a good i...
~InputSilo()
Throws away any unprocessed input.
bool takeSliderMove(int &slider, Real &value)
This will return user changes to slider positions until they have all been consumed,...
InputSilo()
Default construction is all that is needed; there are no options.
bool takeMenuPick(int &menu, int &item)
This will return user menu picks until they have all been consumed, in the same order they were recei...
void waitForKeyHit(unsigned &key, unsigned &modifiers)
Same as takeKeyHit() except that if there is no key hit input available it waits until there is,...
void clear()
Throw away any pending unprocessed input of all types.
void waitForMenuPick(int &menu, int &item)
Same as takeMenuPick() except that if there is no menu pick input available it waits until there is,...
bool takeKeyHit(unsigned &key, unsigned &modifiers)
This will return user key hits until they have all been consumed, in the same order they were receive...
void waitForAnyUserInput() const
This will wait quietly until the user has provided some input to the visualizer. Any kind of input wi...
void waitForSliderMove(int &slider, Real &value)
Same as takeSliderMove() except that if there is no slider move input available it waits until there ...
Provide simple visualization of and interaction with a Simbody simulation, with real time control of ...
Definition: Visualizer.h:147
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:607