5 #include <osgViewer/Viewer>
7 #include "FGManipulator.hxx"
9 #if !defined(X_DISPLAY_MISSING)
10 #define X_DOUBLE_SCROLL_BUG 1
13 // The manipulator is responsible for updating a Viewer's camera. It's
14 // event handling method is also a convenient place to run the the FG
15 // idle and draw handlers.
17 FGManipulator::FGManipulator() :
18 idleHandler(0), drawHandler(0), windowResizeHandler(0), keyHandler(0),
19 mouseClickHandler(0), mouseMotionHandler(0), currentModifiers(0),
20 osgModifiers(0), resizable(true), mouseWarped(false),
21 scrollButtonPressed(false)
23 using namespace osgGA;
25 keyMaskMap[GUIEventAdapter::KEY_Shift_L]
26 = GUIEventAdapter::MODKEY_LEFT_SHIFT;
27 keyMaskMap[GUIEventAdapter::KEY_Shift_R]
28 = GUIEventAdapter::MODKEY_RIGHT_SHIFT;
29 keyMaskMap[GUIEventAdapter::KEY_Control_L]
30 = GUIEventAdapter::MODKEY_LEFT_CTRL;
31 keyMaskMap[GUIEventAdapter::KEY_Control_R]
32 = GUIEventAdapter::MODKEY_RIGHT_CTRL;
33 keyMaskMap[GUIEventAdapter::KEY_Alt_L] = GUIEventAdapter::MODKEY_LEFT_ALT;
34 keyMaskMap[GUIEventAdapter::KEY_Alt_R] = GUIEventAdapter::MODKEY_RIGHT_ALT;
35 // We have to implement numlock too.
36 numlockKeyMap[GUIEventAdapter::KEY_KP_Insert] = '0';
37 numlockKeyMap[GUIEventAdapter::KEY_KP_End] = '1';
38 numlockKeyMap[GUIEventAdapter::KEY_KP_Down] = '2';
39 numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = '3';
40 numlockKeyMap[GUIEventAdapter::KEY_KP_Left] = '4';
41 numlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
42 numlockKeyMap[GUIEventAdapter::KEY_KP_Right] = '6';
43 numlockKeyMap[GUIEventAdapter::KEY_KP_Home] = '7';
44 numlockKeyMap[GUIEventAdapter::KEY_KP_Up] = '8';
45 numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = '9';
47 for (int i = 0; i < 128; i++)
51 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
54 position = matrix.getTrans();
55 attitude = matrix.getRotate();
58 osg::Matrixd FGManipulator::getMatrix() const
60 return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
63 osg::Matrixd FGManipulator::getInverseMatrix() const
65 return (osg::Matrixd::translate(-position)
66 * osg::Matrixd::rotate(attitude.inverse())) ;
69 // Not used, but part of the interface.
70 void FGManipulator::setNode(osg::Node* node)
75 const osg::Node* FGManipulator::getNode() const
80 osg::Node* FGManipulator::getNode()
85 // All the usual translation from window system to FG / plib
86 static int osgToFGModifiers(int modifiers)
89 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT |
90 osgGA::GUIEventAdapter::MODKEY_RIGHT_SHIFT))
91 result |= KEYMOD_SHIFT;
92 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL |
93 osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
94 result |= KEYMOD_CTRL;
95 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_ALT |
96 osgGA::GUIEventAdapter::MODKEY_RIGHT_ALT))
101 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
102 osgGA::GUIActionAdapter& us)
104 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
105 (void)handle(ea, us);
109 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
115 const osgViewer::Viewer* viewer;
116 viewer = dynamic_cast<const osgViewer::Viewer*>(&us);
121 const osg::Camera* camera;
122 camera = viewer->getCameraContainingPosition(ea.getX(), ea.getY(), lx, ly);
124 if (!(camera && fgOSIsMainCamera(camera)))
128 y = int(camera->getViewport()->height() - ly);
133 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
134 osgGA::GUIActionAdapter& us)
138 switch (ea.getEventType()) {
139 case osgGA::GUIEventAdapter::FRAME:
146 case osgGA::GUIEventAdapter::KEYDOWN:
147 case osgGA::GUIEventAdapter::KEYUP:
150 handleKey(ea, key, modmask);
151 eventToViewport(ea, us, x, y);
153 (*keyHandler)(key, modmask, x, y);
156 case osgGA::GUIEventAdapter::PUSH:
157 case osgGA::GUIEventAdapter::RELEASE:
159 bool mainWindow = eventToViewport(ea, us, x, y);
161 switch (ea.getButton()) {
162 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
165 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
168 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
172 if (mouseClickHandler)
173 (*mouseClickHandler)(button,
175 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
178 case osgGA::GUIEventAdapter::SCROLL:
180 bool mainWindow = eventToViewport(ea, us, x, y);
181 #ifdef X_DOUBLE_SCROLL_BUG
182 scrollButtonPressed = !scrollButtonPressed;
183 if (!scrollButtonPressed) // Drop the button release event
187 if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
191 if (mouseClickHandler) {
192 (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
193 (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
197 case osgGA::GUIEventAdapter::MOVE:
198 case osgGA::GUIEventAdapter::DRAG:
199 // If we warped the mouse, then disregard all pointer motion
200 // events for this frame. We really want to flush the event
201 // queue of mouse events, but don't have the ability to do
202 // that with osgViewer.
205 if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
206 (*mouseMotionHandler)(x, y);
208 case osgGA::GUIEventAdapter::RESIZE:
209 if (resizable && windowResizeHandler)
210 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
212 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
213 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
221 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
225 // XXX Probably other translations are needed too.
227 case osgGA::GUIEventAdapter::KEY_Escape: key = 0x1b; break;
228 case osgGA::GUIEventAdapter::KEY_Return: key = '\n'; break;
229 case osgGA::GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
230 case osgGA::GUIEventAdapter::KEY_Delete: key = 0x7f; break;
231 case osgGA::GUIEventAdapter::KEY_Tab: key = '\t'; break;
232 case osgGA::GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
233 case osgGA::GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
234 case osgGA::GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
235 case osgGA::GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
236 case osgGA::GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
237 case osgGA::GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
238 case osgGA::GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
239 case osgGA::GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
240 case osgGA::GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
241 case osgGA::GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
242 case osgGA::GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
243 case osgGA::GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
244 case osgGA::GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
245 case osgGA::GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
246 case osgGA::GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
247 case osgGA::GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
248 case osgGA::GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
249 case osgGA::GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
250 case osgGA::GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
251 case osgGA::GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
252 case osgGA::GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
253 case osgGA::GUIEventAdapter::KEY_KP_Delete: key = '.'; break;
254 case osgGA::GUIEventAdapter::KEY_KP_Enter: key = '\r'; break;
255 case osgGA::GUIEventAdapter::KEY_KP_Add: key = '+'; break;
256 case osgGA::GUIEventAdapter::KEY_KP_Divide: key = '/'; break;
257 case osgGA::GUIEventAdapter::KEY_KP_Multiply: key = '*'; break;
258 case osgGA::GUIEventAdapter::KEY_KP_Subtract: key = '-'; break;
260 osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
261 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
263 if (numPadIter != numlockKeyMap.end()) {
264 if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
265 key = numPadIter->second;
268 // Track the modifiers because OSG is currently (2.0) broken
269 KeyMaskMap::iterator iter = keyMaskMap.find(key);
270 if (iter != keyMaskMap.end()) {
271 int mask = iter->second;
272 if (eventType == osgGA::GUIEventAdapter::KEYUP)
273 osgModifiers &= ~mask;
275 osgModifiers |= mask;
278 modifiers = osgToFGModifiers(osgModifiers);
279 currentModifiers = modifiers;
280 if (eventType == osgGA::GUIEventAdapter::KEYUP)
281 modifiers |= KEYMOD_RELEASED;
283 // Release the letter key, for which the keypress was reported
284 if (key >= 0 && key < 128) {
285 if (modifiers & KEYMOD_RELEASED) {
286 key = release_keys[key];
288 release_keys[key] = key;
289 if (key >= 1 && key <= 26) {
290 release_keys[key + '@'] = key;
291 release_keys[key + '`'] = key;
292 } else if (key >= 'A' && key <= 'Z') {
293 release_keys[key - '@'] = key;
294 release_keys[tolower(key)] = key;
295 } else if (key >= 'a' && key <= 'z') {
296 release_keys[key - '`'] = key;
297 release_keys[toupper(key)] = key;